forked from scalar-labs/scalardb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Cosmos DB consistency level configurable (scalar-labs#1470)
- Loading branch information
Showing
7 changed files
with
125 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/src/main/java/com/scalar/db/storage/cosmos/CosmosUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,43 @@ | ||
package com.scalar.db.storage.cosmos; | ||
|
||
import com.azure.cosmos.ConsistencyLevel; | ||
import com.azure.cosmos.CosmosClient; | ||
import com.azure.cosmos.CosmosClientBuilder; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import java.util.Locale; | ||
|
||
public final class CosmosUtils { | ||
|
||
private CosmosUtils() {} | ||
|
||
public static String quoteKeyword(String keyword) { | ||
return "[\"" + keyword + "\"]"; | ||
} | ||
|
||
public static CosmosClient buildCosmosClient(CosmosConfig config) { | ||
return new CosmosClientBuilder() | ||
.endpoint(config.getEndpoint()) | ||
.key(config.getKey()) | ||
.directMode() | ||
.consistencyLevel(getConsistencyLevel(config)) | ||
.buildClient(); | ||
} | ||
|
||
@VisibleForTesting | ||
static ConsistencyLevel getConsistencyLevel(CosmosConfig config) { | ||
ConsistencyLevel consistencyLevel = | ||
config | ||
.getConsistencyLevel() | ||
.map(c -> ConsistencyLevel.valueOf(c.toUpperCase(Locale.ROOT))) | ||
.orElse(ConsistencyLevel.STRONG); | ||
|
||
// Only STRONG and BOUNDED_STALENESS are supported | ||
if (consistencyLevel != ConsistencyLevel.STRONG | ||
&& consistencyLevel != ConsistencyLevel.BOUNDED_STALENESS) { | ||
throw new IllegalArgumentException( | ||
"The specified consistency level is not supported:" + consistencyLevel); | ||
} | ||
|
||
return consistencyLevel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
core/src/test/java/com/scalar/db/storage/cosmos/CosmosUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.scalar.db.storage.cosmos; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.azure.cosmos.ConsistencyLevel; | ||
import java.util.Optional; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class CosmosUtilsTest { | ||
|
||
@Mock private CosmosConfig cosmosConfig; | ||
|
||
@BeforeEach | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.openMocks(this).close(); | ||
} | ||
|
||
@Test | ||
public void getConsistencyLevel_ShouldReturnStrongConsistency() { | ||
// Arrange | ||
|
||
// Act | ||
ConsistencyLevel actual = CosmosUtils.getConsistencyLevel(cosmosConfig); | ||
|
||
// Assert | ||
assertThat(actual).isEqualTo(ConsistencyLevel.STRONG); | ||
} | ||
|
||
@Test | ||
public void getConsistencyLevel_StrongGiven_ShouldReturnStrongConsistency() { | ||
// Arrange | ||
when(cosmosConfig.getConsistencyLevel()).thenReturn(Optional.of("STRONG")); | ||
|
||
// Act | ||
ConsistencyLevel actual = CosmosUtils.getConsistencyLevel(cosmosConfig); | ||
|
||
// Assert | ||
assertThat(actual).isEqualTo(ConsistencyLevel.STRONG); | ||
} | ||
|
||
@Test | ||
public void getConsistencyLevel_BoundedStalenessGiven_ShouldReturnBoundedStalenessConsistency() { | ||
// Arrange | ||
when(cosmosConfig.getConsistencyLevel()).thenReturn(Optional.of("bounded_staleness")); | ||
|
||
// Act | ||
ConsistencyLevel actual = CosmosUtils.getConsistencyLevel(cosmosConfig); | ||
|
||
// Assert | ||
assertThat(actual).isEqualTo(ConsistencyLevel.BOUNDED_STALENESS); | ||
} | ||
|
||
@Test | ||
public void getConsistencyLevel_InvalidConsistencyGiven_ShouldThrowIllegalArgumentException() { | ||
// Arrange | ||
when(cosmosConfig.getConsistencyLevel()).thenReturn(Optional.of("any")); | ||
|
||
// Act Assert | ||
Assertions.assertThatThrownBy(() -> CosmosUtils.getConsistencyLevel(cosmosConfig)) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters