Commit cc072fa5 authored by Hema Vishnu Pola [Microsoft]'s avatar Hema Vishnu Pola [Microsoft]
Browse files

Merge branch 'users/rosakhuj/cosmosRetry' into 'master'

Added Configurable retries to Cosmos

See merge request !98
parents 9c273dcc e6b8b9cd
Pipeline #46608 passed with stages
in 9 minutes and 30 seconds
......@@ -2,11 +2,14 @@ package org.opengroup.osdu.azure.cosmosdb;
import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PostConstruct;
import com.azure.cosmos.ThrottlingRetryOptions;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.opengroup.osdu.azure.di.CosmosRetryConfiguration;
import org.opengroup.osdu.azure.partition.PartitionInfoAzure;
import org.opengroup.osdu.azure.partition.PartitionServiceClient;
import org.opengroup.osdu.common.Validators;
......@@ -27,6 +30,9 @@ public class CosmosClientFactoryImpl implements ICosmosClientFactory {
private Map<String, CosmosClient> cosmosClientMap;
@Autowired
private CosmosRetryConfiguration cosmosRetryConfiguration;
/**
* Initializes the private variables as required.
*/
......@@ -58,9 +64,13 @@ public class CosmosClientFactoryImpl implements ICosmosClientFactory {
*/
private CosmosClient createCosmosClient(final String dataPartitionId) {
PartitionInfoAzure pi = this.partitionService.getPartition(dataPartitionId);
ThrottlingRetryOptions throttlingRetryOptions = cosmosRetryConfiguration.getThrottlingRetryOptions();
CosmosClient cosmosClient = new CosmosClientBuilder()
.endpoint(pi.getCosmosEndpoint())
.key(pi.getCosmosPrimaryKey())
.throttlingRetryOptions(throttlingRetryOptions)
.buildClient();
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME)
.info("Created CosmosClient for dataPartition {}.", dataPartitionId);
......
/** Copyright © Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
package org.opengroup.osdu.azure.di;
import com.azure.cosmos.ThrottlingRetryOptions;
import lombok.Getter;
import lombok.Setter;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.time.Duration;
/**
* CosmosRetryConfiguration settings.
*/
@Configuration
@ConfigurationProperties("azure.cosmos")
@Getter
@Setter
public class CosmosRetryConfiguration {
public static final String LOGGER_NAME = CosmosRetryConfiguration.class.getName();
private ThrottlingRetryOptions defaultThrottlingRetryOptions = new ThrottlingRetryOptions();
/**
* Value for max Retry Count on Throttled Requests for Cosmos.
*/
private int maxRetryCount = defaultThrottlingRetryOptions.getMaxRetryAttemptsOnThrottledRequests(); // Sets the default value for axRetryAttemptsOnThrottledRequests
/**
* Value for max retry wait time for Cosmos (Value in seconds).
*/
private long retryWaitTimeout = defaultThrottlingRetryOptions.getMaxRetryWaitTime().getSeconds(); // Sets the default value for MaxRetryWaitTime in seconds
/**
* Set's the Throttling retry options based on application.properties configuration.
* @return Throttling retry options
*/
public ThrottlingRetryOptions getThrottlingRetryOptions() {
ThrottlingRetryOptions throttlingRetryOptions = new ThrottlingRetryOptions();
throttlingRetryOptions.setMaxRetryAttemptsOnThrottledRequests(this.getMaxRetryCount());
throttlingRetryOptions.setMaxRetryWaitTime(Duration.ofSeconds(this.getRetryWaitTimeout()));
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("Retry Options on CosmosClient with maxRetryAttempts = {} , MaxRetryWaitTime = {}.", throttlingRetryOptions.getMaxRetryAttemptsOnThrottledRequests(), throttlingRetryOptions.getMaxRetryWaitTime());
return throttlingRetryOptions;
}
}
/** Copyright © Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**/
package org.opengroup.osdu.azure.di;
import com.azure.cosmos.ThrottlingRetryOptions;
import io.jsonwebtoken.lang.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.logging.CoreLogger;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import java.lang.reflect.Field;
import java.time.Duration;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;
@ExtendWith(MockitoExtension.class)
public class CosmosRetryConfigurationTest {
@InjectMocks
CosmosRetryConfiguration cosmosRetryConfiguration;
@Mock
private CoreLoggerFactory coreLoggerFactory;
@Mock
private CoreLogger coreLogger;
ThrottlingRetryOptions defaultRetryOptions = new ThrottlingRetryOptions();
/**
* Workaround for inability to mock static methods like getInstance().
*
* @param mock CoreLoggerFactory mock instance
*/
private void mockSingleton(CoreLoggerFactory mock) {
try {
Field instance = CoreLoggerFactory.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, mock);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Reset workaround for inability to mock static methods like getInstance().
*/
private void resetSingleton() {
try {
Field instance = CoreLoggerFactory.class.getDeclaredField("instance");
instance.setAccessible(true);
instance.set(null, null);
instance.setAccessible(false);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@BeforeEach
void setUp() {
openMocks(this);
mockSingleton(coreLoggerFactory);
when(coreLoggerFactory.getLogger(anyString())).thenReturn(coreLogger);
}
@AfterEach
public void takeDown() {
resetSingleton();
}
@Test
public void should_set_maxRetryCount_ThrottlingOptions() {
int maxRetryValue = 1;
cosmosRetryConfiguration.setMaxRetryCount(maxRetryValue);
ThrottlingRetryOptions throttlingRetryOptions = cosmosRetryConfiguration.getThrottlingRetryOptions();
Assert.isTrue(throttlingRetryOptions.getMaxRetryAttemptsOnThrottledRequests() == maxRetryValue);
Assert.isTrue(throttlingRetryOptions.getMaxRetryWaitTime().equals(defaultRetryOptions.getMaxRetryWaitTime()));
}
@Test
public void should_set_RetryWaitTimeout_ThrottlingOptions() {
int retryWaitTimeoutValue = 20;
cosmosRetryConfiguration.setRetryWaitTimeout(retryWaitTimeoutValue);
ThrottlingRetryOptions throttlingRetryOptions = cosmosRetryConfiguration.getThrottlingRetryOptions();
Assert.isTrue(throttlingRetryOptions.getMaxRetryWaitTime().equals(Duration.ofSeconds(retryWaitTimeoutValue)));
Assert.isTrue(throttlingRetryOptions.getMaxRetryAttemptsOnThrottledRequests() == defaultRetryOptions.getMaxRetryAttemptsOnThrottledRequests());
}
@Test
public void should_set_RetryWaitTimeout_MaxRetry_ThrottlingOptions() {
int retryWaitTimeoutValue = 20;
int maxRetryValue = 1;
cosmosRetryConfiguration.setRetryWaitTimeout(retryWaitTimeoutValue);
cosmosRetryConfiguration.setMaxRetryCount(maxRetryValue);
ThrottlingRetryOptions throttlingRetryOptions = cosmosRetryConfiguration.getThrottlingRetryOptions();
Assert.isTrue(throttlingRetryOptions.getMaxRetryAttemptsOnThrottledRequests() == maxRetryValue);
Assert.isTrue(throttlingRetryOptions.getMaxRetryWaitTime().equals(Duration.ofSeconds(retryWaitTimeoutValue)));
}
@Test
public void should_not_set_RetryWaitTimeout_MaxRetry_ThrottlingOptions() {
ThrottlingRetryOptions throttlingRetryOptions = cosmosRetryConfiguration.getThrottlingRetryOptions();
Assert.isTrue(throttlingRetryOptions.getMaxRetryAttemptsOnThrottledRequests() == defaultRetryOptions.getMaxRetryAttemptsOnThrottledRequests());
Assert.isTrue(throttlingRetryOptions.getMaxRetryWaitTime().equals(defaultRetryOptions.getMaxRetryWaitTime()));
}
}
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