Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Request Options #40061

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

tvaron3
Copy link
Contributor

@tvaron3 tvaron3 commented May 7, 2024

Dynamic Request Options

This is a feature for configuring some of the properties in the request options dynamically. The motivation behind this pr is for customers to be able to change the request options of their application without restarting it. This is limited to only document operations for this pr. The following classes were introduced

  • CosmosOperationPolicy class
    • Functional interface that is essentially a consumer with passed in CosmosOperationDetails.
    • Did not use consumer to give us the flexibility of adding default methods in the future.
  • ComosOperationDetails class
    • Encapsulates internal request options and diagnostic context
    • Gives method to override internal request options
  • IComosCommonRequestOptions interface
    • Getters for the properties that we want to make visible to the customer from our internal request options
  • OverridableRequestOptions interface
    • enforces override method for the setting the CosmosCommonRequestOptions
  • CosmosCommonRequestOptions class
    • new set of request options created by customer to override the internal request options
  • CosmosBulkRequestOptionsImpl, CosmosChangeFeedRequestOptionsImpl classes
    • created theses classes to avoid making breaking changes because our methods across request options are not always the same. Sometimes for a method we return a wrapper vs a primitive or the method names are slightly different. To ensure they all implement ICosmosCommonRequestOptions I added these classes.

The following properties are the ones being exposed as dynamically configurable

  • consistencyLevel
  • sessionToken
  • contentResponseOnWriteEnabled
  • nonIdempotentWriteRetryPolicy
  • dedicatedGatewayRequestOptions
  • cosmosEndToEndOperationLactencyPolicyConfig
  • excludedRegions
  • throughputControlGroupName
  • diagnositcThresholds
  • scanInQueryEnabled
  • maxDegreeOfParallelism
  • maxBufferedItemCount
  • responseContinuationTokenLimitInKb
  • maxItemCount
  • queryMetricsEnabled
  • indexMetricsEnabled
  • maxPrefetchPageCount
  • queryName

Below is a code snippet of how the dynamic request options could be used.

CosmosAsyncClient cosmosAsyncClient = new CosmosClientBuilder()
            .endpoint("something")
            .key("secret")
            .addPolicy((cosmosOperationDetails) -> {


                // Read from config file
                Properties prop = new Properties();
                String fileName = "app.config";
                try (FileInputStream fis = new FileInputStream(fileName)) {
                    prop.load(fis);
                } catch (FileNotFoundException ex) {
                    // Handle file not found exception
                } catch (IOException ex) {
                    // Handle other I/O exceptions
                }
                // ----------------------------------------------------------------------------


                // Figure out Operation
                CosmosDiagnosticsContext cosmosDiagnosticsContext = cosmosOperationDetails.getDiagnosticsContext();
                String operationType = cosmosDiagnosticsContext.getOperationType();


                if (operationType.equals("Create") ) {
                    // change request options for createItem --------------------------------------------------------------
                    CosmosCommonRequestOptions cosmosCommonRequestOptions = new CosmosCommonRequestOptions();
                    cosmosCommonRequestOptions.setCosmosEndToEndLatencyPolicyConfig(new CosmosEndToEndOperationLatencyPolicyConfig(true,
                        Duration.ofSeconds(Long.parseLong(prop.getProperty("timeout.seconds"))),
                        new ThresholdBasedAvailabilityStrategy()));
                    cosmosOperationDetails.setCommonOptions(cosmosCommonRequestOptions);
                }
            })
            .buildAsyncClient();

@github-actions github-actions bot added the Cosmos label May 7, 2024
@tvaron3 tvaron3 changed the title Poc for dynamic request options Dynamic Request Options May 7, 2024
@kushagraThapar
Copy link
Member

@tvaron3 / @FabianMeiswinkel / @xinlian12 / @jeet1995 / @TheovanKraay / @trande4884 we need to think about spring-data-cosmos SDK use case as well here. Because we are not going to expose requestOptions to spring-data-cosmos or kafka connector for that matter. So we should just make sure this extension approach works for all the connectors.

@trande4884
Copy link
Member

@tvaron3 / @FabianMeiswinkel / @xinlian12 / @jeet1995 / @TheovanKraay / @trande4884 we need to think about spring-data-cosmos SDK use case as well here. Because we are not going to expose requestOptions to spring-data-cosmos or kafka connector for that matter. So we should just make sure this extension approach works for all the connectors.

For spring I can check functionality once this is released!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants