Commit a1d00d14 authored by unknown's avatar unknown
Browse files

Added option for user to select gateway mode or direct mode, and option to set cache staleness

parent 0cc7e8a4
Pipeline #49743 failed with stage
in 12 seconds
......@@ -20,7 +20,7 @@
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.11.0-SNAPSHOT-AB-26.6.21</version>
<version>0.11.0-SNAPSHOT-AB-1.7.21</version>
<name>core-lib-azure</name>
<properties>
......
......@@ -18,6 +18,9 @@ import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.common.Validators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
......@@ -25,6 +28,8 @@ import org.springframework.stereotype.Component;
* Implementation for ICosmosClientFactory.
*/
@Component
@Configuration
@ConfigurationProperties("azure.cosmos")
@Lazy
public class CosmosClientFactoryImpl implements ICosmosClientFactory {
private static final String LOGGER_NAME = CosmosClientFactoryImpl.class.getName();
......@@ -45,6 +50,9 @@ public class CosmosClientFactoryImpl implements ICosmosClientFactory {
@Autowired
private CosmosRetryConfiguration cosmosRetryConfiguration;
@Value("${azure.cosmos.enableGatewayMode}")
private boolean enableGatewayMode;
/**
* Initializes the private variables as required.
*/
......@@ -93,13 +101,35 @@ public class CosmosClientFactoryImpl implements ICosmosClientFactory {
ThrottlingRetryOptions throttlingRetryOptions = cosmosRetryConfiguration.getThrottlingRetryOptions();
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(pi.getCosmosGatewayEndpoint())
.key(pi.getCosmosPrimaryKey())
.gatewayMode()
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.throttlingRetryOptions(throttlingRetryOptions)
.buildClient();
CosmosClient cosmosClient;
if (enableGatewayMode) {
cosmosClient = new CosmosClientBuilder()
.endpoint(pi.getCosmosEndpoint())
.key(pi.getCosmosPrimaryKey())
.throttlingRetryOptions(throttlingRetryOptions)
.gatewayMode()
.consistencyLevel(ConsistencyLevel.EVENTUAL)
.buildClient();
System.out.println("------------------Running the service on Gateway Mode ------------------");
} else {
cosmosClient = new CosmosClientBuilder()
.endpoint(pi.getCosmosEndpoint())
.key(pi.getCosmosPrimaryKey())
.throttlingRetryOptions(throttlingRetryOptions)
.buildClient();
System.out.println("------------------Running the service on Direct Mode ------------------");
}
System.out.println(enableGatewayMode);
// CosmosClient cosmosClient = new CosmosClientBuilder()
// .endpoint(pi.getCosmosGatewayEndpoint())
// .key(pi.getCosmosPrimaryKey())
// .gatewayMode()
// .consistencyLevel(ConsistencyLevel.EVENTUAL)
// .throttlingRetryOptions(throttlingRetryOptions)
// .buildClient();
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME)
.info("Created CosmosClient for dataPartition {}.", dataPartitionId);
return cosmosClient;
......
......@@ -31,6 +31,8 @@ import org.opengroup.osdu.azure.logging.DependencyPayload;
import org.opengroup.osdu.azure.query.CosmosStorePageRequest;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
......@@ -79,6 +81,7 @@ import java.util.Optional;
*/
@Component
@ConfigurationProperties("azure.cosmos")
@Lazy
public class CosmosStore {
......@@ -88,6 +91,9 @@ public class CosmosStore {
@Autowired
private ICosmosClientFactory cosmosClientFactory;
@Value("${azure.cosmos.maxIntegratedCacheStaleness}")
private int maxIntegratedCacheStaleness;
/**
* @param duration set MaxIntegratedCacheStaleness
* @return CosmosItemRequestOptions
......@@ -243,7 +249,7 @@ public class CosmosStore {
try {
CosmosContainer cosmosContainer = getCosmosContainer(dataPartitionId, cosmosDBName, collection);
PartitionKey key = new PartitionKey(partitionKey);
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(60));
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(maxIntegratedCacheStaleness));
cosmosContainer.replaceItem(item, id, key, options);
} catch (NotFoundException e) {
statusCode = e.getStatusCode();
......@@ -312,7 +318,7 @@ public class CosmosStore {
final String cosmosDBName,
final String collection,
final Class<T> clazz) {
CosmosQueryRequestOptions options = queryRequestOptions(Duration.ofMinutes(60));
CosmosQueryRequestOptions options = queryRequestOptions(Duration.ofMinutes(maxIntegratedCacheStaleness));
return queryItems(dataPartitionId, cosmosDBName, collection, new SqlQuerySpec("SELECT * FROM c"), options, clazz);
}
......@@ -464,7 +470,7 @@ public class CosmosStore {
final long start = System.currentTimeMillis();
int statusCode = HttpStatus.SC_OK;
try {
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(60));
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(maxIntegratedCacheStaleness));
PartitionKey key = new PartitionKey(partitionKey);
T item = container.readItem(id, key, options, clazz).getItem();
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).debug(String.format("READ_ITEM with id=%s and partition_key=%s", id, partitionKey));
......@@ -504,7 +510,7 @@ public class CosmosStore {
int statusCode = HttpStatus.SC_OK;
try {
PartitionKey key = new PartitionKey(partitionKey);
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(60));
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(maxIntegratedCacheStaleness));
container.deleteItem(id, key, options);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).debug(String.format("DELETE_ITEM with id=%s and partition_key=%s", id, partitionKey));
} catch (NotFoundException e) {
......@@ -543,7 +549,7 @@ public class CosmosStore {
int statusCode = HttpStatus.SC_OK;
try {
PartitionKey key = new PartitionKey(partitionKey);
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(60));
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(maxIntegratedCacheStaleness));
container.upsertItem(item, key, options);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).debug(String.format("UPSERT_ITEM with partition_key=%s", partitionKey));
} catch (CosmosException e) {
......@@ -577,7 +583,7 @@ public class CosmosStore {
int statusCode = HttpStatus.SC_OK;
try {
PartitionKey key = new PartitionKey(partitionKey);
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(60));
CosmosItemRequestOptions options = itemRequestOptions(Duration.ofMinutes(maxIntegratedCacheStaleness));
container.createItem(item, key, options);
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).debug(String.format("CREATE_ITEM with partition_key=%s", partitionKey));
} catch (ConflictException e) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment