|
39 | 39 | import com.datastax.oss.driver.api.testinfra.session.SessionRule;
|
40 | 40 | import com.datastax.oss.driver.api.testinfra.session.SessionUtils;
|
41 | 41 | import com.datastax.oss.driver.categories.ParallelizableTests;
|
| 42 | +import com.datastax.oss.driver.internal.core.context.DefaultDriverContext; |
| 43 | +import com.datastax.oss.driver.internal.core.metadata.schema.events.TypeChangeEvent; |
42 | 44 | import com.datastax.oss.driver.internal.core.metadata.token.DefaultTokenMap;
|
43 | 45 | import com.datastax.oss.driver.internal.core.metadata.token.TokenFactory;
|
44 | 46 | import com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures;
|
| 47 | +import com.datastax.oss.driver.shaded.guava.common.util.concurrent.Uninterruptibles; |
45 | 48 | import com.datastax.oss.protocol.internal.util.Bytes;
|
46 | 49 | import com.google.common.collect.ImmutableList;
|
47 | 50 | import java.nio.ByteBuffer;
|
48 | 51 | import java.time.Duration;
|
49 | 52 | import java.util.concurrent.CompletionStage;
|
| 53 | +import java.util.concurrent.CountDownLatch; |
| 54 | +import java.util.concurrent.TimeUnit; |
| 55 | +import java.util.function.Consumer; |
50 | 56 | import junit.framework.TestCase;
|
51 | 57 | import org.junit.Before;
|
52 | 58 | import org.junit.Rule;
|
@@ -444,6 +450,142 @@ public void should_fail_fast_if_id_changes_on_reprepare() {
|
444 | 450 | }
|
445 | 451 | }
|
446 | 452 |
|
| 453 | + private void invalidationResultSetTest(Consumer<CqlSession> createFn) { |
| 454 | + |
| 455 | + try (CqlSession session = sessionWithCacheSizeMetric()) { |
| 456 | + |
| 457 | + assertThat(getPreparedCacheSize(session)).isEqualTo(0); |
| 458 | + createFn.accept(session); |
| 459 | + |
| 460 | + session.prepare("select f from test_table_1 where e = ?"); |
| 461 | + session.prepare("select h from test_table_2 where g = ?"); |
| 462 | + assertThat(getPreparedCacheSize(session)).isEqualTo(2); |
| 463 | + |
| 464 | + CountDownLatch latch = new CountDownLatch(1); |
| 465 | + DefaultDriverContext ctx = (DefaultDriverContext) session.getContext(); |
| 466 | + ctx.getEventBus() |
| 467 | + .register( |
| 468 | + TypeChangeEvent.class, |
| 469 | + (e) -> { |
| 470 | + assertThat(e.oldType.getName().toString()).isEqualTo("test_type_2"); |
| 471 | + latch.countDown(); |
| 472 | + }); |
| 473 | + |
| 474 | + session.execute("ALTER TYPE test_type_2 add i blob"); |
| 475 | + Uninterruptibles.awaitUninterruptibly(latch, 2, TimeUnit.SECONDS); |
| 476 | + |
| 477 | + assertThat(getPreparedCacheSize(session)).isEqualTo(1); |
| 478 | + } |
| 479 | + } |
| 480 | + |
| 481 | + private void invalidationVariableDefsTest(Consumer<CqlSession> createFn, boolean isCollection) { |
| 482 | + |
| 483 | + try (CqlSession session = sessionWithCacheSizeMetric()) { |
| 484 | + |
| 485 | + assertThat(getPreparedCacheSize(session)).isEqualTo(0); |
| 486 | + createFn.accept(session); |
| 487 | + |
| 488 | + String fStr = isCollection ? "f contains ?" : "f = ?"; |
| 489 | + session.prepare(String.format("select e from test_table_1 where %s allow filtering", fStr)); |
| 490 | + String hStr = isCollection ? "h contains ?" : "h = ?"; |
| 491 | + session.prepare(String.format("select g from test_table_2 where %s allow filtering", hStr)); |
| 492 | + assertThat(getPreparedCacheSize(session)).isEqualTo(2); |
| 493 | + |
| 494 | + CountDownLatch latch = new CountDownLatch(1); |
| 495 | + DefaultDriverContext ctx = (DefaultDriverContext) session.getContext(); |
| 496 | + ctx.getEventBus() |
| 497 | + .register( |
| 498 | + TypeChangeEvent.class, |
| 499 | + (e) -> { |
| 500 | + assertThat(e.oldType.getName().toString()).isEqualTo("test_type_2"); |
| 501 | + latch.countDown(); |
| 502 | + }); |
| 503 | + |
| 504 | + session.execute("ALTER TYPE test_type_2 add i blob"); |
| 505 | + Uninterruptibles.awaitUninterruptibly(latch, 2, TimeUnit.SECONDS); |
| 506 | + |
| 507 | + assertThat(getPreparedCacheSize(session)).isEqualTo(1); |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + Consumer<CqlSession> setupCacheEntryTestBasic = |
| 512 | + (session) -> { |
| 513 | + session.execute("CREATE TYPE test_type_1 (a text, b int)"); |
| 514 | + session.execute("CREATE TYPE test_type_2 (c int, d text)"); |
| 515 | + session.execute("CREATE TABLE test_table_1 (e int primary key, f frozen<test_type_1>)"); |
| 516 | + session.execute("CREATE TABLE test_table_2 (g int primary key, h frozen<test_type_2>)"); |
| 517 | + }; |
| 518 | + |
| 519 | + @Test |
| 520 | + public void should_invalidate_cache_entry_on_basic_udt_change_result_set() { |
| 521 | + invalidationResultSetTest(setupCacheEntryTestBasic); |
| 522 | + } |
| 523 | + |
| 524 | + @Test |
| 525 | + public void should_invalidate_cache_entry_on_basic_udt_change_variable_defs() { |
| 526 | + invalidationVariableDefsTest(setupCacheEntryTestBasic, false); |
| 527 | + } |
| 528 | + |
| 529 | + Consumer<CqlSession> setupCacheEntryTestCollection = |
| 530 | + (session) -> { |
| 531 | + session.execute("CREATE TYPE test_type_1 (a text, b int)"); |
| 532 | + session.execute("CREATE TYPE test_type_2 (c int, d text)"); |
| 533 | + session.execute( |
| 534 | + "CREATE TABLE test_table_1 (e int primary key, f list<frozen<test_type_1>>)"); |
| 535 | + session.execute( |
| 536 | + "CREATE TABLE test_table_2 (g int primary key, h list<frozen<test_type_2>>)"); |
| 537 | + }; |
| 538 | + |
| 539 | + @Test |
| 540 | + public void should_invalidate_cache_entry_on_collection_udt_change_result_set() { |
| 541 | + invalidationResultSetTest(setupCacheEntryTestCollection); |
| 542 | + } |
| 543 | + |
| 544 | + @Test |
| 545 | + public void should_invalidate_cache_entry_on_collection_udt_change_variable_defs() { |
| 546 | + invalidationVariableDefsTest(setupCacheEntryTestCollection, true); |
| 547 | + } |
| 548 | + |
| 549 | + Consumer<CqlSession> setupCacheEntryTestTuple = |
| 550 | + (session) -> { |
| 551 | + session.execute("CREATE TYPE test_type_1 (a text, b int)"); |
| 552 | + session.execute("CREATE TYPE test_type_2 (c int, d text)"); |
| 553 | + session.execute( |
| 554 | + "CREATE TABLE test_table_1 (e int primary key, f tuple<int, test_type_1, text>)"); |
| 555 | + session.execute( |
| 556 | + "CREATE TABLE test_table_2 (g int primary key, h tuple<text, test_type_2, int>)"); |
| 557 | + }; |
| 558 | + |
| 559 | + @Test |
| 560 | + public void should_invalidate_cache_entry_on_tuple_udt_change_result_set() { |
| 561 | + invalidationResultSetTest(setupCacheEntryTestTuple); |
| 562 | + } |
| 563 | + |
| 564 | + @Test |
| 565 | + public void should_invalidate_cache_entry_on_tuple_udt_change_variable_defs() { |
| 566 | + invalidationVariableDefsTest(setupCacheEntryTestTuple, false); |
| 567 | + } |
| 568 | + |
| 569 | + Consumer<CqlSession> setupCacheEntryTestNested = |
| 570 | + (session) -> { |
| 571 | + session.execute("CREATE TYPE test_type_1 (a text, b int)"); |
| 572 | + session.execute("CREATE TYPE test_type_2 (c int, d text)"); |
| 573 | + session.execute("CREATE TYPE test_type_3 (e frozen<test_type_1>, f int)"); |
| 574 | + session.execute("CREATE TYPE test_type_4 (g int, h frozen<test_type_2>)"); |
| 575 | + session.execute("CREATE TABLE test_table_1 (e int primary key, f frozen<test_type_3>)"); |
| 576 | + session.execute("CREATE TABLE test_table_2 (g int primary key, h frozen<test_type_4>)"); |
| 577 | + }; |
| 578 | + |
| 579 | + @Test |
| 580 | + public void should_invalidate_cache_entry_on_nested_udt_change_result_set() { |
| 581 | + invalidationResultSetTest(setupCacheEntryTestNested); |
| 582 | + } |
| 583 | + |
| 584 | + @Test |
| 585 | + public void should_invalidate_cache_entry_on_nested_udt_change_variable_defs() { |
| 586 | + invalidationVariableDefsTest(setupCacheEntryTestNested, false); |
| 587 | + } |
| 588 | + |
447 | 589 | @Test
|
448 | 590 | public void should_infer_routing_information_when_partition_key_is_bound() {
|
449 | 591 | should_infer_routing_information_when_partition_key_is_bound(
|
|
0 commit comments