|
22 | 22 | import com.arcadedb.database.DatabaseInternal; |
23 | 23 | import com.arcadedb.database.Document; |
24 | 24 | import com.arcadedb.database.MutableDocument; |
| 25 | +import com.arcadedb.database.RID; |
25 | 26 | import com.arcadedb.database.bucketselectionstrategy.ThreadBucketSelectionStrategy; |
26 | 27 | import com.arcadedb.engine.WALException; |
27 | 28 | import com.arcadedb.engine.WALFile; |
| 29 | +import com.arcadedb.exception.DuplicatedKeyException; |
28 | 30 | import com.arcadedb.exception.NeedRetryException; |
29 | 31 | import com.arcadedb.exception.TransactionException; |
30 | 32 | import com.arcadedb.graph.MutableVertex; |
|
37 | 39 | import com.arcadedb.schema.VertexType; |
38 | 40 | import org.junit.jupiter.api.Test; |
39 | 41 |
|
40 | | -import java.io.File; |
41 | | -import java.io.IOException; |
42 | | -import java.util.ArrayList; |
43 | | -import java.util.Calendar; |
44 | | -import java.util.Iterator; |
45 | | -import java.util.List; |
46 | | -import java.util.concurrent.Callable; |
47 | | -import java.util.concurrent.atomic.AtomicBoolean; |
48 | | -import java.util.concurrent.atomic.AtomicInteger; |
49 | | -import java.util.logging.Level; |
| 42 | +import java.io.*; |
| 43 | +import java.util.*; |
| 44 | +import java.util.concurrent.*; |
| 45 | +import java.util.concurrent.atomic.*; |
| 46 | +import java.util.logging.*; |
50 | 47 |
|
51 | 48 | import static org.assertj.core.api.Assertions.assertThat; |
52 | 49 | import static org.assertj.core.api.Assertions.fail; |
@@ -457,6 +454,83 @@ public void testAsyncEdges() { |
457 | 454 | database.transaction(() -> assertThat(database.countType("Arc", true)).isEqualTo(TOT - 1)); |
458 | 455 | } |
459 | 456 |
|
| 457 | + @Test |
| 458 | + public void testExceptionInsideTransaction() { |
| 459 | + final Database db = database; |
| 460 | + |
| 461 | + final int TOT = 100; |
| 462 | + |
| 463 | + final VertexType type = database.getSchema().getOrCreateVertexType("Node"); |
| 464 | + //type.getOrCreateProperty("id", Type.STRING).getOrCreateIndex(Schema.INDEX_TYPE.LSM_TREE, true); |
| 465 | + |
| 466 | + final AtomicInteger thrownExceptions = new AtomicInteger(0); |
| 467 | + final AtomicInteger caughtExceptions = new AtomicInteger(0); |
| 468 | + final AtomicInteger committed = new AtomicInteger(0); |
| 469 | + |
| 470 | + final RID[] rid = new RID[1]; |
| 471 | + |
| 472 | + database.transaction(() -> { |
| 473 | + final MutableVertex v = db.newVertex("Node"); |
| 474 | + v.set("id", 0); |
| 475 | + v.set("name", "Exception(al)"); |
| 476 | + v.set("surname", "Test"); |
| 477 | + v.save(); |
| 478 | + rid[0] = v.getIdentity(); |
| 479 | + }); |
| 480 | + |
| 481 | + final int CONCURRENT_THREADS = 4; |
| 482 | + |
| 483 | + // SPAWN ALL THE THREADS |
| 484 | + final Thread[] threads = new Thread[CONCURRENT_THREADS]; |
| 485 | + for (int i = 0; i < CONCURRENT_THREADS; i++) { |
| 486 | + threads[i] = new Thread(() -> { |
| 487 | + for (int k = 0; k < TOT; ++k) { |
| 488 | + final int id = k; |
| 489 | + |
| 490 | + try { |
| 491 | + database.transaction(() -> { |
| 492 | + MutableVertex v = rid[0].asVertex().modify(); |
| 493 | + v.set("id", id); |
| 494 | + v.save(); |
| 495 | + |
| 496 | + if ((id + 1) % 100 == 0) { |
| 497 | + thrownExceptions.incrementAndGet(); |
| 498 | +// System.out.println("Simulating exception at id=" + id + " (thread=" + Thread.currentThread().getName() + ")"); |
| 499 | + //throw new DuplicatedKeyException("indexName", "key", rid[0]); |
| 500 | + throw new RuntimeException("Test exception at " + id); |
| 501 | + } |
| 502 | + }); |
| 503 | + committed.incrementAndGet(); |
| 504 | + |
| 505 | + } catch (Exception e) { |
| 506 | + caughtExceptions.incrementAndGet(); |
| 507 | +// System.out.println("Caught: " + e.getMessage() + " (id=" + id + ", thread=" + Thread.currentThread().getName() + ")"); |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + }); |
| 512 | + threads[i].start(); |
| 513 | + } |
| 514 | + |
| 515 | + // WAIT FOR ALL THE THREADS |
| 516 | + for (int i = 0; i < CONCURRENT_THREADS; i++) |
| 517 | + try { |
| 518 | + threads[i].join(); |
| 519 | + } catch (InterruptedException e) { |
| 520 | + // IGNORE IT |
| 521 | + } |
| 522 | + |
| 523 | + assertThat(database.countType("Node", true)).isEqualTo(1); |
| 524 | + |
| 525 | + assertThat(committed.get()).isGreaterThan(0); |
| 526 | + assertThat(thrownExceptions.get()).isGreaterThan(0); |
| 527 | + assertThat(caughtExceptions.get()).isGreaterThan(0); |
| 528 | + assertThat(committed.get() + caughtExceptions.get()).isEqualTo(TOT * CONCURRENT_THREADS); |
| 529 | + |
| 530 | +// System.out.println( |
| 531 | +// "Committed: " + committed.get() + ", Thrown: " + thrownExceptions.get() + ", Caught: " + caughtExceptions.get()); |
| 532 | + } |
| 533 | + |
460 | 534 | @Test |
461 | 535 | public void testDeleteOverwriteCompositeKeyInTx() { |
462 | 536 | database.transaction(() -> |
|
0 commit comments