|
28 | 28 | import io.questdb.cairo.ColumnType; |
29 | 29 | import io.questdb.cairo.PartitionBy; |
30 | 30 | import io.questdb.cairo.TableReader; |
| 31 | +import io.questdb.cairo.TableToken; |
31 | 32 | import io.questdb.cairo.TableWriter; |
32 | 33 | import io.questdb.cairo.TableWriterAPI; |
33 | 34 | import io.questdb.cairo.pool.PoolListener; |
@@ -260,6 +261,104 @@ public void testColumnTypeStaysTheSameWhileColumnAdded() throws Exception { |
260 | 261 | }, false, 250); |
261 | 262 | } |
262 | 263 |
|
| 264 | + @Test |
| 265 | + public void testConcurrentWriteAndTruncate() throws Exception { |
| 266 | + Assume.assumeTrue(walEnabled); |
| 267 | + runInContext((receiver) -> { |
| 268 | + String tableName = "concurrent_test"; |
| 269 | + final int iterations = 500; |
| 270 | + final SOCountDownLatch startLatch = new SOCountDownLatch(2); |
| 271 | + final SOCountDownLatch finishLatch = new SOCountDownLatch(2); |
| 272 | + final AtomicInteger errorCount = new AtomicInteger(0); |
| 273 | + String initialData = tableName + ",location=init_location,symbol=test temperature=20.0 1465839830100000000\n"; |
| 274 | + send(initialData, tableName); |
| 275 | + mayDrainWalQueue(); |
| 276 | + |
| 277 | + Thread ilpWriteThread = new Thread(() -> { |
| 278 | + try { |
| 279 | + startLatch.countDown(); |
| 280 | + startLatch.await(); |
| 281 | + |
| 282 | + try (Socket socket = getSocket()) { |
| 283 | + for (int i = 0; i < iterations; i++) { |
| 284 | + try { |
| 285 | + // Use duplicate timestamp and symbols to trigger metadata operations |
| 286 | + String lineData = tableName + ",location=test_location,symbol=sym" + (i % 3) + |
| 287 | + " temperature=" + (20.0 + i) + " " + |
| 288 | + (1465839830102000000L + (i % 5)) + "\n"; |
| 289 | + sendToSocket(socket, lineData); |
| 290 | + |
| 291 | + if (i % 50 == 0) { |
| 292 | + mayDrainWalQueue(); |
| 293 | + } |
| 294 | + |
| 295 | + if (i % 20 == 0) { |
| 296 | + Os.sleep(1); |
| 297 | + } |
| 298 | + } catch (Throwable e) { |
| 299 | + errorCount.incrementAndGet(); |
| 300 | + } |
| 301 | + } |
| 302 | + } |
| 303 | + } catch (Throwable e) { |
| 304 | + errorCount.incrementAndGet(); |
| 305 | + } finally { |
| 306 | + Path.clearThreadLocals(); |
| 307 | + finishLatch.countDown(); |
| 308 | + } |
| 309 | + }); |
| 310 | + |
| 311 | + Thread truncateThread = new Thread(() -> { |
| 312 | + try { |
| 313 | + startLatch.countDown(); |
| 314 | + startLatch.await(); |
| 315 | + boolean drop = false; |
| 316 | + |
| 317 | + for (int i = 0; i < iterations; i++) { |
| 318 | + try { |
| 319 | + // Alternate between truncate and drop operations |
| 320 | + if (i % 15 == 0) { |
| 321 | + TableToken tt = engine.getTableTokenIfExists(tableName); |
| 322 | + if (tt != null) { |
| 323 | + try (TableWriterAPI writer = getTableWriterAPI(tableName)) { |
| 324 | + writer.truncateSoft(); |
| 325 | + } |
| 326 | + mayDrainWalQueue(); |
| 327 | + drop = true; |
| 328 | + } |
| 329 | + } else if (i % 25 == 0 && drop) { |
| 330 | + TableToken tt = engine.getTableTokenIfExists(tableName); |
| 331 | + if (tt != null) { |
| 332 | + engine.dropTableOrMatView(path, tt); |
| 333 | + drop = false; |
| 334 | + } |
| 335 | + } |
| 336 | + Os.sleep(2); |
| 337 | + } catch (Throwable e) { |
| 338 | + errorCount.incrementAndGet(); |
| 339 | + } |
| 340 | + } |
| 341 | + } catch (Throwable e) { |
| 342 | + errorCount.incrementAndGet(); |
| 343 | + } finally { |
| 344 | + Path.clearThreadLocals(); |
| 345 | + finishLatch.countDown(); |
| 346 | + } |
| 347 | + }); |
| 348 | + |
| 349 | + ilpWriteThread.start(); |
| 350 | + truncateThread.start(); |
| 351 | + |
| 352 | + try { |
| 353 | + finishLatch.await(); |
| 354 | + Assert.assertEquals(0, errorCount.get()); |
| 355 | + } finally { |
| 356 | + ilpWriteThread.interrupt(); |
| 357 | + truncateThread.interrupt(); |
| 358 | + } |
| 359 | + }); |
| 360 | + } |
| 361 | + |
263 | 362 | @Test |
264 | 363 | public void testCreationAttemptNonPartitionedTableWithWal() throws Exception { |
265 | 364 | Assume.assumeTrue(walEnabled); |
|
0 commit comments