Skip to content

Commit e19fc8f

Browse files
KodaiDbrfrn169
authored andcommitted
Add more waits for cache expiry (#3046)
1 parent 4af9131 commit e19fc8f

File tree

4 files changed

+183
-73
lines changed

4 files changed

+183
-73
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: 87 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -391,35 +391,45 @@ public void dropTable_IfExists_ForNonExistingTable_ShouldNotThrowAnyException()
391391
@Test
392392
public void truncateTable_ShouldTruncateProperly()
393393
throws ExecutionException, TransactionException {
394-
DistributedTransactionManager manager = null;
394+
// Use a separate table name to avoid hitting the stale cache, which can cause test failure when
395+
// executing DMLs
396+
String table = "table_for_truncate";
397+
395398
try {
396399
// Arrange
397-
Key partitionKey = new Key(COL_NAME2, "aaa", COL_NAME1, 1);
398-
Key clusteringKey = new Key(COL_NAME4, 2, COL_NAME3, "bbb");
399-
manager = transactionFactory.getTransactionManager();
400-
manager.put(
401-
new Put(partitionKey, clusteringKey)
402-
.withValue(COL_NAME5, 3)
403-
.withValue(COL_NAME6, "ccc")
404-
.withValue(COL_NAME7, 4L)
405-
.withValue(COL_NAME8, 1.0f)
406-
.withValue(COL_NAME9, 1.0d)
407-
.withValue(COL_NAME10, true)
408-
.withValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8))
409-
.forNamespace(namespace1)
410-
.forTable(TABLE1));
400+
Map<String, String> options = getCreationOptions();
401+
admin.createTable(namespace1, table, TABLE_METADATA, true, options);
402+
Key partitionKey = Key.of(COL_NAME2, "aaa", COL_NAME1, 1);
403+
Key clusteringKey = Key.of(COL_NAME4, 2, COL_NAME3, "bbb");
404+
transactionalInsert(
405+
Insert.newBuilder()
406+
.namespace(namespace1)
407+
.table(table)
408+
.partitionKey(partitionKey)
409+
.clusteringKey(clusteringKey)
410+
.intValue(COL_NAME5, 3)
411+
.textValue(COL_NAME6, "ccc")
412+
.bigIntValue(COL_NAME7, 4L)
413+
.floatValue(COL_NAME8, 1.0f)
414+
.doubleValue(COL_NAME9, 1.0d)
415+
.booleanValue(COL_NAME10, true)
416+
.blobValue(COL_NAME11, "ddd".getBytes(StandardCharsets.UTF_8))
417+
.build());
411418

412419
// Act
413-
admin.truncateTable(namespace1, TABLE1);
420+
admin.truncateTable(namespace1, table);
414421

415422
// Assert
416423
List<Result> results =
417-
manager.scan(new Scan(partitionKey).forNamespace(namespace1).forTable(TABLE1));
424+
transactionalScan(
425+
Scan.newBuilder()
426+
.namespace(namespace1)
427+
.table(table)
428+
.partitionKey(partitionKey)
429+
.build());
418430
assertThat(results).isEmpty();
419431
} finally {
420-
if (manager != null) {
421-
manager.close();
422-
}
432+
admin.dropTable(namespace1, table, true);
423433
}
424434
}
425435

@@ -467,7 +477,10 @@ public void tableExists_ShouldReturnCorrectResults() throws ExecutionException {
467477
@Test
468478
public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorrectly()
469479
throws Exception {
470-
DistributedTransactionManager transactionManager = null;
480+
// Use a separate table name to avoid hitting the stale cache, which can cause test failure when
481+
// executing DMLs
482+
String table = "table_for_create_index";
483+
471484
try {
472485
// Arrange
473486
Map<String, String> options = getCreationOptions();
@@ -485,12 +498,11 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
485498
.addPartitionKey(COL_NAME1)
486499
.addSecondaryIndex(COL_NAME9)
487500
.build();
488-
admin.createTable(namespace1, TABLE4, metadata, options);
489-
transactionManager = transactionFactory.getTransactionManager();
490-
transactionManager.put(
491-
Put.newBuilder()
501+
admin.createTable(namespace1, table, metadata, options);
502+
Insert insert =
503+
Insert.newBuilder()
492504
.namespace(namespace1)
493-
.table(TABLE4)
505+
.table(table)
494506
.partitionKey(Key.ofInt(COL_NAME1, 1))
495507
.intValue(COL_NAME2, 2)
496508
.textValue(COL_NAME3, "3")
@@ -500,45 +512,43 @@ public void createIndex_ForAllDataTypesWithExistingData_ShouldCreateIndexesCorre
500512
.booleanValue(COL_NAME7, true)
501513
.blobValue(COL_NAME8, "8".getBytes(StandardCharsets.UTF_8))
502514
.textValue(COL_NAME9, "9")
503-
.build());
515+
.build();
516+
transactionalInsert(insert);
504517

505518
// Act
506-
admin.createIndex(namespace1, TABLE4, COL_NAME2, options);
507-
admin.createIndex(namespace1, TABLE4, COL_NAME3, options);
508-
admin.createIndex(namespace1, TABLE4, COL_NAME4, options);
509-
admin.createIndex(namespace1, TABLE4, COL_NAME5, options);
510-
admin.createIndex(namespace1, TABLE4, COL_NAME6, options);
519+
admin.createIndex(namespace1, table, COL_NAME2, options);
520+
admin.createIndex(namespace1, table, COL_NAME3, options);
521+
admin.createIndex(namespace1, table, COL_NAME4, options);
522+
admin.createIndex(namespace1, table, COL_NAME5, options);
523+
admin.createIndex(namespace1, table, COL_NAME6, options);
511524
if (isIndexOnBooleanColumnSupported()) {
512-
admin.createIndex(namespace1, TABLE4, COL_NAME7, options);
525+
admin.createIndex(namespace1, table, COL_NAME7, options);
513526
}
514-
admin.createIndex(namespace1, TABLE4, COL_NAME8, options);
527+
admin.createIndex(namespace1, table, COL_NAME8, options);
515528

516529
// Assert
517-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME2)).isTrue();
518-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME3)).isTrue();
519-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME4)).isTrue();
520-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME5)).isTrue();
521-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME6)).isTrue();
530+
assertThat(admin.indexExists(namespace1, table, COL_NAME2)).isTrue();
531+
assertThat(admin.indexExists(namespace1, table, COL_NAME3)).isTrue();
532+
assertThat(admin.indexExists(namespace1, table, COL_NAME4)).isTrue();
533+
assertThat(admin.indexExists(namespace1, table, COL_NAME5)).isTrue();
534+
assertThat(admin.indexExists(namespace1, table, COL_NAME6)).isTrue();
522535
if (isIndexOnBooleanColumnSupported()) {
523-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME7)).isTrue();
536+
assertThat(admin.indexExists(namespace1, table, COL_NAME7)).isTrue();
524537
}
525-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME8)).isTrue();
538+
assertThat(admin.indexExists(namespace1, table, COL_NAME8)).isTrue();
526539
if (isIndexOnBooleanColumnSupported()) {
527-
assertThat(admin.getTableMetadata(namespace1, TABLE4).getSecondaryIndexNames())
540+
assertThat(admin.getTableMetadata(namespace1, table).getSecondaryIndexNames())
528541
.containsOnly(
529542
COL_NAME2, COL_NAME3, COL_NAME4, COL_NAME5, COL_NAME6, COL_NAME7, COL_NAME8,
530543
COL_NAME9);
531544
} else {
532-
assertThat(admin.getTableMetadata(namespace1, TABLE4).getSecondaryIndexNames())
545+
assertThat(admin.getTableMetadata(namespace1, table).getSecondaryIndexNames())
533546
.containsOnly(
534547
COL_NAME2, COL_NAME3, COL_NAME4, COL_NAME5, COL_NAME6, COL_NAME8, COL_NAME9);
535548
}
536549

537550
} finally {
538-
admin.dropTable(namespace1, TABLE4, true);
539-
if (transactionManager != null) {
540-
transactionManager.close();
541-
}
551+
admin.dropTable(namespace1, table, true);
542552
}
543553
}
544554

@@ -613,7 +623,10 @@ public void createIndex_IfNotExists_ForAlreadyExistingIndex_ShouldNotThrowAnyExc
613623
@Test
614624
public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
615625
throws Exception {
616-
DistributedTransactionManager transactionManager = null;
626+
// Use a separate table name to avoid hitting the stale cache, which can cause test failure when
627+
// executing DMLs
628+
String table = "table_for_drop_index";
629+
617630
try {
618631
// Arrange
619632
Map<String, String> options = getCreationOptions();
@@ -641,12 +654,11 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
641654
if (isIndexOnBooleanColumnSupported()) {
642655
metadata = TableMetadata.newBuilder(metadata).addSecondaryIndex(COL_NAME7).build();
643656
}
644-
admin.createTable(namespace1, TABLE4, metadata, options);
645-
transactionManager = transactionFactory.getTransactionManager();
646-
transactionManager.put(
647-
Put.newBuilder()
657+
admin.createTable(namespace1, table, metadata, options);
658+
Insert insert =
659+
Insert.newBuilder()
648660
.namespace(namespace1)
649-
.table(TABLE4)
661+
.table(table)
650662
.partitionKey(Key.ofInt(COL_NAME1, 1))
651663
.intValue(COL_NAME2, 2)
652664
.textValue(COL_NAME3, "3")
@@ -656,34 +668,32 @@ public void dropIndex_ForAllDataTypesWithExistingData_ShouldDropIndexCorrectly()
656668
.booleanValue(COL_NAME7, true)
657669
.blobValue(COL_NAME8, "8".getBytes(StandardCharsets.UTF_8))
658670
.textValue(COL_NAME9, "9")
659-
.build());
671+
.build();
672+
transactionalInsert(insert);
660673

661674
// Act
662-
admin.dropIndex(namespace1, TABLE4, COL_NAME2);
663-
admin.dropIndex(namespace1, TABLE4, COL_NAME3);
664-
admin.dropIndex(namespace1, TABLE4, COL_NAME4);
665-
admin.dropIndex(namespace1, TABLE4, COL_NAME5);
666-
admin.dropIndex(namespace1, TABLE4, COL_NAME6);
675+
admin.dropIndex(namespace1, table, COL_NAME2);
676+
admin.dropIndex(namespace1, table, COL_NAME3);
677+
admin.dropIndex(namespace1, table, COL_NAME4);
678+
admin.dropIndex(namespace1, table, COL_NAME5);
679+
admin.dropIndex(namespace1, table, COL_NAME6);
667680
if (isIndexOnBooleanColumnSupported()) {
668-
admin.dropIndex(namespace1, TABLE4, COL_NAME7);
681+
admin.dropIndex(namespace1, table, COL_NAME7);
669682
}
670-
admin.dropIndex(namespace1, TABLE4, COL_NAME8);
683+
admin.dropIndex(namespace1, table, COL_NAME8);
671684

672685
// Assert
673-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME2)).isFalse();
674-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME3)).isFalse();
675-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME4)).isFalse();
676-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME5)).isFalse();
677-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME6)).isFalse();
678-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME7)).isFalse();
679-
assertThat(admin.indexExists(namespace1, TABLE4, COL_NAME8)).isFalse();
680-
assertThat(admin.getTableMetadata(namespace1, TABLE4).getSecondaryIndexNames())
686+
assertThat(admin.indexExists(namespace1, table, COL_NAME2)).isFalse();
687+
assertThat(admin.indexExists(namespace1, table, COL_NAME3)).isFalse();
688+
assertThat(admin.indexExists(namespace1, table, COL_NAME4)).isFalse();
689+
assertThat(admin.indexExists(namespace1, table, COL_NAME5)).isFalse();
690+
assertThat(admin.indexExists(namespace1, table, COL_NAME6)).isFalse();
691+
assertThat(admin.indexExists(namespace1, table, COL_NAME7)).isFalse();
692+
assertThat(admin.indexExists(namespace1, table, COL_NAME8)).isFalse();
693+
assertThat(admin.getTableMetadata(namespace1, table).getSecondaryIndexNames())
681694
.containsOnly(COL_NAME9);
682695
} finally {
683-
admin.dropTable(namespace1, TABLE4, true);
684-
if (transactionManager != null) {
685-
transactionManager.close();
686-
}
696+
admin.dropTable(namespace1, table, true);
687697
}
688698
}
689699

@@ -886,4 +896,8 @@ public void getNamespaceNames_ShouldReturnCreatedNamespaces() throws ExecutionEx
886896
protected boolean isIndexOnBooleanColumnSupported() {
887897
return true;
888898
}
899+
900+
protected abstract void transactionalInsert(Insert insert) throws TransactionException;
901+
902+
protected abstract List<Result> transactionalScan(Scan scan) throws TransactionException;
889903
}

integration-test/src/main/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdminIntegrationTestBase.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package com.scalar.db.transaction.consensuscommit;
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;
10+
import com.scalar.db.exception.transaction.TransactionException;
11+
import java.util.List;
412
import java.util.Properties;
13+
import java.util.concurrent.TimeUnit;
514

615
public abstract class ConsensusCommitAdminIntegrationTestBase
716
extends DistributedTransactionAdminIntegrationTestBase {
@@ -23,4 +32,29 @@ protected final Properties getProperties(String testName) {
2332
}
2433

2534
protected abstract Properties getProps(String testName);
35+
36+
@Override
37+
protected void transactionalInsert(Insert insert) throws TransactionException {
38+
// Wait for cache expiry
39+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
40+
41+
try (DistributedTransactionManager manager = transactionFactory.getTransactionManager()) {
42+
DistributedTransaction transaction = manager.start();
43+
transaction.insert(insert);
44+
transaction.commit();
45+
}
46+
}
47+
48+
@Override
49+
protected List<Result> transactionalScan(Scan scan) throws TransactionException {
50+
// Wait for cache expiry
51+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
52+
53+
try (DistributedTransactionManager manager = transactionFactory.getTransactionManager()) {
54+
DistributedTransaction transaction = manager.start();
55+
List<Result> results = transaction.scan(scan);
56+
transaction.commit();
57+
return results;
58+
}
59+
}
2660
}

integration-test/src/main/java/com/scalar/db/transaction/singlecrudoperation/SingleCrudOperationTransactionAdminIntegrationTestBase.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.scalar.db.transaction.singlecrudoperation;
22

3+
import com.google.common.util.concurrent.Uninterruptibles;
34
import com.scalar.db.api.DistributedTransactionAdminIntegrationTestBase;
5+
import com.scalar.db.api.DistributedTransactionManager;
6+
import com.scalar.db.api.Insert;
7+
import com.scalar.db.api.Result;
8+
import com.scalar.db.api.Scan;
49
import com.scalar.db.config.DatabaseConfig;
510
import com.scalar.db.exception.storage.ExecutionException;
11+
import com.scalar.db.exception.transaction.TransactionException;
12+
import java.util.List;
613
import java.util.Properties;
14+
import java.util.concurrent.TimeUnit;
715
import org.junit.jupiter.api.Disabled;
816
import org.junit.jupiter.api.Test;
917

@@ -79,4 +87,24 @@ public void dropCoordinatorTables_IfExist_CoordinatorTablesDoNotExist_ShouldNotT
7987
throws ExecutionException {
8088
super.dropCoordinatorTables_IfExist_CoordinatorTablesDoNotExist_ShouldNotThrowAnyException();
8189
}
90+
91+
@Override
92+
protected void transactionalInsert(Insert insert) throws TransactionException {
93+
// Wait for cache expiry
94+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
95+
96+
try (DistributedTransactionManager manager = transactionFactory.getTransactionManager()) {
97+
manager.insert(insert);
98+
}
99+
}
100+
101+
@Override
102+
protected List<Result> transactionalScan(Scan scan) throws TransactionException {
103+
// Wait for cache expiry
104+
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
105+
106+
try (DistributedTransactionManager manager = transactionFactory.getTransactionManager()) {
107+
return manager.scan(scan);
108+
}
109+
}
82110
}

0 commit comments

Comments
 (0)