|
| 1 | +/* |
| 2 | + * Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.engine.timeseries; |
| 20 | + |
| 21 | +import com.arcadedb.TestHelper; |
| 22 | +import com.arcadedb.database.DatabaseInternal; |
| 23 | +import com.arcadedb.schema.Type; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.util.List; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.atomic.AtomicReference; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * Regression test for https://github.com/ArcadeData/arcadedb/discussions/3860 . |
| 34 | + * <p> |
| 35 | + * Verifies that concurrent writes to the same shard do NOT produce MVCC |
| 36 | + * {@code ConcurrentModificationException} errors and do NOT cause data duplication |
| 37 | + * (which would occur if the HTTP client retried after receiving a 503 for a partially |
| 38 | + * committed batch). |
| 39 | + * <p> |
| 40 | + * Prior to the fix, the per-shard {@code appendLock} was absent and two threads racing |
| 41 | + * to commit page 0 of the same shard would get a version mismatch at commit time: |
| 42 | + * "current v.X <> database v.Y. Please retry the operation". |
| 43 | + * |
| 44 | + * @author Luca Garulli (l.garulli@arcadedata.com) |
| 45 | + */ |
| 46 | +class TimeSeriesConcurrentWriteNoDuplicatesTest extends TestHelper { |
| 47 | + |
| 48 | + /** |
| 49 | + * Launches multiple threads that all write to a single-shard engine simultaneously. |
| 50 | + * With one shard every append competes for the same mutable-bucket page. |
| 51 | + * The test asserts that: |
| 52 | + * <ul> |
| 53 | + * <li>no exception is thrown by any writer thread;</li> |
| 54 | + * <li>the total sample count equals exactly threadCount × samplesPerThread |
| 55 | + * (i.e. no duplicates and no missing writes).</li> |
| 56 | + * </ul> |
| 57 | + */ |
| 58 | + @Test |
| 59 | + void concurrentWritesToSameShardNoDuplicatesAndNoMvccErrors() throws Exception { |
| 60 | + final int THREAD_COUNT = 8; |
| 61 | + final int SAMPLES_PER_THREAD = 200; |
| 62 | + |
| 63 | + final List<ColumnDefinition> columns = List.of( |
| 64 | + new ColumnDefinition("ts", Type.LONG, ColumnDefinition.ColumnRole.TIMESTAMP), |
| 65 | + new ColumnDefinition("value", Type.DOUBLE, ColumnDefinition.ColumnRole.FIELD) |
| 66 | + ); |
| 67 | + |
| 68 | + // Use shardCount=1 to force maximum contention: every append goes to the same shard. |
| 69 | + final TimeSeriesEngine engine = new TimeSeriesEngine((DatabaseInternal) database, "test_concurrent_no_dup", columns, 1); |
| 70 | + |
| 71 | + final CountDownLatch startLatch = new CountDownLatch(1); |
| 72 | + final CountDownLatch doneLatch = new CountDownLatch(THREAD_COUNT); |
| 73 | + final AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 74 | + |
| 75 | + for (int t = 0; t < THREAD_COUNT; t++) { |
| 76 | + final int threadIdx = t; |
| 77 | + final Thread thread = new Thread(() -> { |
| 78 | + try { |
| 79 | + startLatch.await(); |
| 80 | + for (int i = 0; i < SAMPLES_PER_THREAD; i++) { |
| 81 | + // Assign unique timestamps per thread so there are no intentional duplicates. |
| 82 | + final long ts = (long) threadIdx * SAMPLES_PER_THREAD + i; |
| 83 | + engine.appendSamples(new long[] { ts }, new Object[][] { { (double) ts } }); |
| 84 | + } |
| 85 | + } catch (final Throwable e) { |
| 86 | + failure.compareAndSet(null, e); |
| 87 | + } finally { |
| 88 | + doneLatch.countDown(); |
| 89 | + } |
| 90 | + }, "writer-" + t); |
| 91 | + thread.setDaemon(true); |
| 92 | + thread.start(); |
| 93 | + } |
| 94 | + |
| 95 | + startLatch.countDown(); |
| 96 | + assertThat(doneLatch.await(30, java.util.concurrent.TimeUnit.SECONDS)) |
| 97 | + .as("All writer threads should finish within 30 seconds") |
| 98 | + .isTrue(); |
| 99 | + |
| 100 | + assertThat(failure.get()) |
| 101 | + .as("No writer thread should throw an exception (MVCC or otherwise)") |
| 102 | + .isNull(); |
| 103 | + |
| 104 | + // Total samples must equal exactly threadCount × samplesPerThread. |
| 105 | + // Any MVCC exception that slipped through without the appendLock would have caused |
| 106 | + // the client (HTTP handler) to return 503; the client would then retry the whole |
| 107 | + // batch — duplicating every sample that was already committed before the failure. |
| 108 | + final long totalSamples = engine.countSamples(); |
| 109 | + assertThat(totalSamples) |
| 110 | + .as("Sample count must be exactly %d (no duplicates, no lost writes)", THREAD_COUNT * SAMPLES_PER_THREAD) |
| 111 | + .isEqualTo((long) THREAD_COUNT * SAMPLES_PER_THREAD); |
| 112 | + |
| 113 | + engine.close(); |
| 114 | + } |
| 115 | +} |
0 commit comments