Skip to content

Commit fb652ce

Browse files
feeblefakieKodaiD
andauthored
Backport to branch(3.15) : Add more waits for cache expiry (#3067)
Co-authored-by: Kodai Doki <[email protected]>
1 parent d0ddded commit fb652ce

File tree

4 files changed

+222
-83
lines changed

4 files changed

+222
-83
lines changed

core/src/integration-test/java/com/scalar/db/transaction/jdbc/JdbcTransactionAdminIntegrationTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.scalar.db.transaction.jdbc;
22

3+
import com.google.common.util.concurrent.Uninterruptibles;
4+
import com.scalar.db.api.DistributedTransaction;
35
import com.scalar.db.api.DistributedTransactionAdminIntegrationTestBase;
6+
import com.scalar.db.api.DistributedTransactionManager;
7+
import com.scalar.db.api.Insert;
8+
import com.scalar.db.api.Result;
9+
import com.scalar.db.api.Scan;
410
import com.scalar.db.config.DatabaseConfig;
511
import com.scalar.db.exception.storage.ExecutionException;
12+
import com.scalar.db.exception.transaction.TransactionException;
613
import com.scalar.db.storage.jdbc.JdbcConfig;
714
import com.scalar.db.storage.jdbc.JdbcEnv;
15+
import java.util.List;
816
import java.util.Properties;
17+
import java.util.concurrent.TimeUnit;
918
import org.junit.jupiter.api.Disabled;
1019
import org.junit.jupiter.api.Test;
1120
import org.junit.jupiter.api.condition.DisabledIf;
@@ -160,4 +169,29 @@ public void dropCoordinatorTables_IfExist_CoordinatorTablesDoNotExist_ShouldNotT
160169
throws ExecutionException {
161170
super.dropCoordinatorTables_IfExist_CoordinatorTablesDoNotExist_ShouldNotThrowAnyException();
162171
}
172+
173+
@Override
174+
protected void transactionalInsert(Insert insert) throws TransactionException {
175+
// Wait for cache expiry
176+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
177+
178+
try (DistributedTransactionManager manager = transactionFactory.getTransactionManager()) {
179+
DistributedTransaction transaction = manager.start();
180+
transaction.insert(insert);
181+
transaction.commit();
182+
}
183+
}
184+
185+
@Override
186+
protected List<Result> transactionalScan(Scan scan) throws TransactionException {
187+
// Wait for cache expiry
188+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
189+
190+
try (DistributedTransactionManager manager = transactionFactory.getTransactionManager()) {
191+
DistributedTransaction transaction = manager.start();
192+
List<Result> results = transaction.scan(scan);
193+
transaction.commit();
194+
return results;
195+
}
196+
}
163197
}

integration-test/src/main/java/com/scalar/db/api/DistributedTransactionAdminIntegrationTestBase.java

Lines changed: 126 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -425,35 +425,45 @@ public void dropTable_IfExists_ForNonExistingTable_ShouldNotThrowAnyException()
425425
@Test
426426
public void truncateTable_ShouldTruncateProperly()
427427
throws ExecutionException, TransactionException {
428-
DistributedTransactionManager manager = null;
428+
// Use a separate table name to avoid hitting the stale cache, which can cause test failure when
429+
// executing DMLs
430+
String table = "table_for_truncate";
431+
429432
try {
430433
// Arrange
431-
Key partitionKey = new Key(COL_NAME2, "aaa", COL_NAME1, 1);
432-
Key clusteringKey = new Key(COL_NAME4, 2, COL_NAME3, "bbb");
433-
manager = transactionFactory.getTransactionManager();
434-
manager.put(
435-
new Put(partitionKey, clusteringKey)
436-
.withValue(COL_NAME5, 3)
437-
.withValue(COL_NAME6, "ccc")
438-
.withValue(COL_NAME7, 4L)
439-
.withValue(COL_NAME8, 1.0f)
440-
.withValue(COL_NAME9, 1.0d)
441-
.withValue(COL_NAME10, true)
442-
.withValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8))
443-
.forNamespace(namespace1)
444-
.forTable(TABLE1));
434+
Map<String, String> options = getCreationOptions();
435+
admin.createTable(namespace1, table, TABLE_METADATA, true, options);
436+
Key partitionKey = Key.of(COL_NAME2, "aaa", COL_NAME1, 1);
437+
Key clusteringKey = Key.of(COL_NAME4, 2, COL_NAME3, "bbb");
438+
transactionalInsert(
439+
Insert.newBuilder()
440+
.namespace(namespace1)
441+
.table(table)
442+
.partitionKey(partitionKey)
443+
.clusteringKey(clusteringKey)
444+
.intValue(COL_NAME5, 3)
445+
.textValue(COL_NAME6, "ccc")
446+
.bigIntValue(COL_NAME7, 4L)
447+
.floatValue(COL_NAME8, 1.0f)
448+
.doubleValue(COL_NAME9, 1.0d)
449+
.booleanValue(COL_NAME10, true)
450+
.blobValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8))
451+
.build());
445452

446453
// Act
447-
admin.truncateTable(namespace1, TABLE1);
454+
admin.truncateTable(namespace1, table);
448455

449456
// Assert
450457
List<Result> results =
451-
manager.scan(new Scan(partitionKey).forNamespace(namespace1).forTable(TABLE1));
458+
transactionalScan(
459+
Scan.newBuilder()
460+
.namespace(namespace1)
461+
.table(table)
462+
.partitionKey(partitionKey)
463+
.build());
452464
assertThat(results).isEmpty();
453465
} finally {
454-
if (manager != null) {
455-
manager.close();
456-
}
466+
admin.dropTable(namespace1, table, true);
457467
}
458468
}
459469

@@ -501,7 +511,10 @@ public void tableExists_ShouldReturnCorrectResults() throws ExecutionException {
501511
@Test
502512
public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorrectly()
503513
throws Exception {
504-
DistributedTransactionManager transactionManager = null;
514+
// Use a separate table name to avoid hitting the stale cache, which can cause test failure when
515+
// executing DMLs
516+
String table = "table_for_create_index";
517+
505518
try {
506519
// Arrange
507520
Map<String, String> options = getCreationOptions();
@@ -525,12 +538,11 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
525538
metadataBuilder = metadataBuilder.addColumn(COL_NAME13, DataType.TIMESTAMP);
526539
}
527540
TableMetadata metadata = metadataBuilder.build();
528-
admin.createTable(namespace1, TABLE4, metadata, options);
529-
transactionManager = transactionFactory.getTransactionManager();
541+
admin.createTable(namespace1, table, metadata, options);
530542
InsertBuilder.Buildable insert =
531543
Insert.newBuilder()
532544
.namespace(namespace1)
533-
.table(TABLE4)
545+
.table(table)
534546
.partitionKey(Key.ofInt(COL_NAME1, 1))
535547
.intValue(COL_NAME2, 2)
536548
.textValue(COL_NAME3, "3")
@@ -551,45 +563,45 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
551563
COL_NAME13,
552564
LocalDateTime.of(LocalDate.of(2020, 6, 2), LocalTime.of(12, 2, 6, 123_000_000)));
553565
}
554-
transactionManager.insert(insert.build());
566+
transactionalInsert(insert.build());
555567

556568
// Act
557-
admin.createIndex(namespace1, TABLE4, COL_NAME2, options);
558-
admin.createIndex(namespace1, TABLE4, COL_NAME3, options);
559-
admin.createIndex(namespace1, TABLE4, COL_NAME4, options);
560-
admin.createIndex(namespace1, TABLE4, COL_NAME5, options);
561-
admin.createIndex(namespace1, TABLE4, COL_NAME6, options);
569+
admin.createIndex(namespace1, table, COL_NAME2, options);
570+
admin.createIndex(namespace1, table, COL_NAME3, options);
571+
admin.createIndex(namespace1, table, COL_NAME4, options);
572+
admin.createIndex(namespace1, table, COL_NAME5, options);
573+
admin.createIndex(namespace1, table, COL_NAME6, options);
562574
if (isIndexOnBooleanColumnSupported()) {
563-
admin.createIndex(namespace1, TABLE4, COL_NAME7, options);
575+
admin.createIndex(namespace1, table, COL_NAME7, options);
564576
}
565-
admin.createIndex(namespace1, TABLE4, COL_NAME8, options);
566-
admin.createIndex(namespace1, TABLE4, COL_NAME10, options);
567-
admin.createIndex(namespace1, TABLE4, COL_NAME11, options);
568-
admin.createIndex(namespace1, TABLE4, COL_NAME12, options);
577+
admin.createIndex(namespace1, table, COL_NAME8, options);
578+
admin.createIndex(namespace1, table, COL_NAME10, options);
579+
admin.createIndex(namespace1, table, COL_NAME11, options);
580+
admin.createIndex(namespace1, table, COL_NAME12, options);
569581
if (isTimestampTypeSupported()) {
570-
admin.createIndex(namespace1, TABLE4, COL_NAME13, options);
582+
admin.createIndex(namespace1, table, COL_NAME13, options);
571583
}
572584

573585
// Assert
574-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME2)).isTrue();
575-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME3)).isTrue();
576-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME4)).isTrue();
577-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME5)).isTrue();
578-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME6)).isTrue();
586+
assertThat(admin.indexExists(namespace1, table, COL_NAME2)).isTrue();
587+
assertThat(admin.indexExists(namespace1, table, COL_NAME3)).isTrue();
588+
assertThat(admin.indexExists(namespace1, table, COL_NAME4)).isTrue();
589+
assertThat(admin.indexExists(namespace1, table, COL_NAME5)).isTrue();
590+
assertThat(admin.indexExists(namespace1, table, COL_NAME6)).isTrue();
579591
if (isIndexOnBooleanColumnSupported()) {
580-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME7)).isTrue();
592+
assertThat(admin.indexExists(namespace1, table, COL_NAME7)).isTrue();
581593
}
582-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME8)).isTrue();
583-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME9)).isTrue();
584-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME10)).isTrue();
585-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME11)).isTrue();
586-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME12)).isTrue();
594+
assertThat(admin.indexExists(namespace1, table, COL_NAME8)).isTrue();
595+
assertThat(admin.indexExists(namespace1, table, COL_NAME9)).isTrue();
596+
assertThat(admin.indexExists(namespace1, table, COL_NAME10)).isTrue();
597+
assertThat(admin.indexExists(namespace1, table, COL_NAME11)).isTrue();
598+
assertThat(admin.indexExists(namespace1, table, COL_NAME12)).isTrue();
587599
if (isTimestampTypeSupported()) {
588-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME13)).isTrue();
600+
assertThat(admin.indexExists(namespace1, table, COL_NAME13)).isTrue();
589601
}
590602

591603
Set<String> actualSecondaryIndexNames =
592-
admin.getTableMetadata(namespace1, TABLE4).getSecondaryIndexNames();
604+
admin.getTableMetadata(namespace1, table).getSecondaryIndexNames();
593605
assertThat(actualSecondaryIndexNames)
594606
.contains(
595607
COL_NAME2,
@@ -612,12 +624,8 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
612624
indexCount++;
613625
}
614626
assertThat(actualSecondaryIndexNames).hasSize(indexCount);
615-
616627
} finally {
617-
admin.dropTable(namespace1, TABLE4, true);
618-
if (transactionManager != null) {
619-
transactionManager.close();
620-
}
628+
admin.dropTable(namespace1, table, true);
621629
}
622630
}
623631

@@ -692,11 +700,14 @@ public void createIndex_IfNotExists_ForAlreadyExistingIndex_ShouldNotThrowAnyExc
692700
@Test
693701
public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
694702
throws Exception {
695-
DistributedTransactionManager transactionManager = null;
703+
// Use a separate table name to avoid hitting the stale cache, which can cause test failure when
704+
// executing DMLs
705+
String table = "table_for_drop_index";
706+
696707
try {
697708
// Arrange
698709
Map<String, String> options = getCreationOptions();
699-
TableMetadata metadata =
710+
TableMetadata.Builder metadataBuilder =
700711
TableMetadata.newBuilder()
701712
.addColumn(COL_NAME1, DataType.INT)
702713
.addColumn(COL_NAME2, DataType.INT)
@@ -707,6 +718,9 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
707718
.addColumn(COL_NAME7, DataType.BOOLEAN)
708719
.addColumn(COL_NAME8, DataType.BLOB)
709720
.addColumn(COL_NAME9, DataType.TEXT)
721+
.addColumn(COL_NAME10, DataType.DATE)
722+
.addColumn(COL_NAME11, DataType.TIME)
723+
.addColumn(COL_NAME12, DataType.TIMESTAMPTZ)
710724
.addPartitionKey(COL_NAME1)
711725
.addSecondaryIndex(COL_NAME2)
712726
.addSecondaryIndex(COL_NAME3)
@@ -716,16 +730,22 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
716730
.addSecondaryIndex(COL_NAME8)
717731
.addSecondaryIndex(COL_NAME9)
718732
.addSecondaryIndex(COL_NAME9)
719-
.build();
733+
.addSecondaryIndex(COL_NAME10)
734+
.addSecondaryIndex(COL_NAME11)
735+
.addSecondaryIndex(COL_NAME12);
720736
if (isIndexOnBooleanColumnSupported()) {
721-
metadata = TableMetadata.newBuilder(metadata).addSecondaryIndex(COL_NAME7).build();
737+
metadataBuilder = metadataBuilder.addSecondaryIndex(COL_NAME7);
722738
}
723-
admin.createTable(namespace1, TABLE4, metadata, options);
724-
transactionManager = transactionFactory.getTransactionManager();
725-
transactionManager.put(
726-
Put.newBuilder()
739+
if (isTimestampTypeSupported()) {
740+
metadataBuilder.addColumn(COL_NAME13, DataType.TIMESTAMP);
741+
metadataBuilder.addSecondaryIndex(COL_NAME13);
742+
}
743+
admin.createTable(namespace1, table, metadataBuilder.build(), options);
744+
745+
InsertBuilder.Buildable insert =
746+
Insert.newBuilder()
727747
.namespace(namespace1)
728-
.table(TABLE4)
748+
.table(table)
729749
.partitionKey(Key.ofInt(COL_NAME1, 1))
730750
.intValue(COL_NAME2, 2)
731751
.textValue(COL_NAME3, "3")
@@ -735,34 +755,53 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
735755
.booleanValue(COL_NAME7, true)
736756
.blobValue(COL_NAME8, "8".getBytes(StandardCharsets.UTF_8))
737757
.textValue(COL_NAME9, "9")
738-
.build());
758+
.timeValue(COL_NAME11, LocalTime.of(12, 2, 6, 123_456_000))
759+
.timestampTZValue(
760+
COL_NAME12,
761+
LocalDateTime.of(LocalDate.of(2020, 6, 2), LocalTime.of(12, 2, 6, 123_000_000))
762+
.toInstant(ZoneOffset.UTC));
763+
if (isTimestampTypeSupported()) {
764+
insert.timestampValue(
765+
COL_NAME13,
766+
LocalDateTime.of(LocalDate.of(2020, 6, 2), LocalTime.of(12, 2, 6, 123_000_000)));
767+
}
768+
transactionalInsert(insert.build());
739769

740770
// Act
741-
admin.dropIndex(namespace1, TABLE4, COL_NAME2);
742-
admin.dropIndex(namespace1, TABLE4, COL_NAME3);
743-
admin.dropIndex(namespace1, TABLE4, COL_NAME4);
744-
admin.dropIndex(namespace1, TABLE4, COL_NAME5);
745-
admin.dropIndex(namespace1, TABLE4, COL_NAME6);
771+
admin.dropIndex(namespace1, table, COL_NAME2);
772+
admin.dropIndex(namespace1, table, COL_NAME3);
773+
admin.dropIndex(namespace1, table, COL_NAME4);
774+
admin.dropIndex(namespace1, table, COL_NAME5);
775+
admin.dropIndex(namespace1, table, COL_NAME6);
746776
if (isIndexOnBooleanColumnSupported()) {
747-
admin.dropIndex(namespace1, TABLE4, COL_NAME7);
777+
admin.dropIndex(namespace1, table, COL_NAME7);
778+
}
779+
admin.dropIndex(namespace1, table, COL_NAME8);
780+
admin.dropIndex(namespace1, table, COL_NAME10);
781+
admin.dropIndex(namespace1, table, COL_NAME11);
782+
admin.dropIndex(namespace1, table, COL_NAME12);
783+
if (isTimestampTypeSupported()) {
784+
admin.dropIndex(namespace1, table, COL_NAME13);
748785
}
749-
admin.dropIndex(namespace1, TABLE4, COL_NAME8);
750786

751787
// Assert
752-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME2)).isFalse();
753-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME3)).isFalse();
754-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME4)).isFalse();
755-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME5)).isFalse();
756-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME6)).isFalse();
757-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME7)).isFalse();
758-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME8)).isFalse();
759-
assertThat(admin.getTableMetadata(namespace1, TABLE4).getSecondaryIndexNames())
788+
assertThat(admin.indexExists(namespace1, table, COL_NAME2)).isFalse();
789+
assertThat(admin.indexExists(namespace1, table, COL_NAME3)).isFalse();
790+
assertThat(admin.indexExists(namespace1, table, COL_NAME4)).isFalse();
791+
assertThat(admin.indexExists(namespace1, table, COL_NAME5)).isFalse();
792+
assertThat(admin.indexExists(namespace1, table, COL_NAME6)).isFalse();
793+
assertThat(admin.indexExists(namespace1, table, COL_NAME7)).isFalse();
794+
assertThat(admin.indexExists(namespace1, table, COL_NAME8)).isFalse();
795+
assertThat(admin.getTableMetadata(namespace1, table).getSecondaryIndexNames())
760796
.containsOnly(COL_NAME9);
761-
} finally {
762-
admin.dropTable(namespace1, TABLE4, true);
763-
if (transactionManager != null) {
764-
transactionManager.close();
797+
assertThat(admin.indexExists(namespace1, table, COL_NAME10)).isFalse();
798+
assertThat(admin.indexExists(namespace1, table, COL_NAME11)).isFalse();
799+
assertThat(admin.indexExists(namespace1, table, COL_NAME12)).isFalse();
800+
if (isTimestampTypeSupported()) {
801+
assertThat(admin.indexExists(namespace1, table, COL_NAME13)).isFalse();
765802
}
803+
} finally {
804+
admin.dropTable(namespace1, table, true);
766805
}
767806
}
768807

@@ -969,4 +1008,8 @@ protected boolean isIndexOnBooleanColumnSupported() {
9691008
protected boolean isTimestampTypeSupported() {
9701009
return true;
9711010
}
1011+
1012+
protected abstract void transactionalInsert(Insert insert) throws TransactionException;
1013+
1014+
protected abstract List<Result> transactionalScan(Scan scan) throws TransactionException;
9721015
}

0 commit comments

Comments
 (0)