diff --git a/sdk/cosmos/azure-cosmos-test/README.md b/sdk/cosmos/azure-cosmos-test/README.md index 2142864ad1739..891261165d997 100644 --- a/sdk/cosmos/azure-cosmos-test/README.md +++ b/sdk/cosmos/azure-cosmos-test/README.md @@ -40,88 +40,90 @@ The following section provides several code snippets covering how to create some ### High Channel Acquisition Scenario ```java readme-sample-highChannelAcquisitionScenario - FaultInjectionRule serverConnectionDelayRule = - new FaultInjectionRuleBuilder("ServerError-ConnectionTimeout") - .condition( - new FaultInjectionConditionBuilder() - .operationType(FaultInjectionOperationType.CREATE) - .build() - ) - .result( - FaultInjectionResultBuilders - .getResultBuilder(FaultInjectionServerErrorType.SERVER_CONNECTION_DELAY) - .delay(Duration.ofSeconds(6)) // default connection timeout is 5s - .times(1) - .build() - ) - .duration(Duration.ofMinutes(5)) - .build(); +FaultInjectionRule serverConnectionDelayRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.CREATE_ITEM) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionServerErrorType.CONNECTION_DELAY) + .delay(Duration.ofSeconds(6)) // default connection timeout is 5s + .times(1) + .build() + ) + .duration(Duration.ofMinutes(5)) + .build(); + +CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(serverConnectionDelayRule)).block(); ``` ### Broken Connection Scenario ```java readme-sample-brokenConnectionScenario - FaultInjectionRule timeoutRule = - new FaultInjectionRuleBuilder(timeoutRuleId) - .condition( - new FaultInjectionConditionBuilder() - .operationType(FaultInjectionOperationType.READ) - .build() - ) - .result( - FaultInjectionResultBuilders - .getResultBuilder(FaultInjectionServerErrorType.SERVER_RESPONSE_DELAY) - .times(1) - .delay(Duration.ofSeconds(6)) // the default time out is 5s - .build() - ) - .duration(Duration.ofMinutes(5)) - .build(); - - CosmosFaultInjectionHelper.configFaultInjectionRules(container, Arrays.asList(timeoutRule)).block(); +FaultInjectionRule timeoutRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.READ_ITEM) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionServerErrorType.RESPONSE_DELAY) + .times(1) + .delay(Duration.ofSeconds(6)) // the default time out is 5s + .build() + ) + .duration(Duration.ofMinutes(5)) + .build(); + +CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(timeoutRule)).block(); ``` ### Server Return Gone Scenario ```java readme-sample-serverReturnGoneScenario - FaultInjectionRule serverErrorRule = - new FaultInjectionRuleBuilder(ruleId) - .condition( - new FaultInjectionConditionBuilder() - .operationType(FaultInjectionOperationType.READ) - .build() - ) - .result( - FaultInjectionResultBuilders - .getResultBuilder(FaultInjectionServerErrorType.SERVER_GONE) - .times(1) - .build() - ) - .duration(Duration.ofMinutes(5)) - .build(); - - CosmosFaultInjectionHelper.configFaultInjectionRules(container, Arrays.asList(serverErrorRule)).block(); +FaultInjectionRule serverErrorRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.READ_ITEM) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionServerErrorType.GONE) + .times(1) + .build() + ) + .duration(Duration.ofMinutes(5)) + .build(); + +CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(serverErrorRule)).block(); ``` ### Random Connection Close Scenario ```java readme-sample-randomConnectionCloseScenario - FaultInjectionRule connectionErrorRule = - new FaultInjectionRuleBuilder(ruleId) - .condition( - new FaultInjectionConditionBuilder() - .operationType(FaultInjectionOperationType.CREATE) - .endpoints(new FaultInjectionEndpointBuilder(FeedRange.forLogicalPartition(new PartitionKey(createdItem.getMypk()))).build()) - .build() - ) - .result( - FaultInjectionResultBuilders - .getResultBuilder(errorType) - .interval(Duration.ofSeconds(1)) - .threshold(1.0) - .build() - ) - .duration(Duration.ofSeconds(2)) - .build(); - - CosmosFaultInjectionHelper.configFaultInjectionRules(container, Arrays.asList(connectionErrorRule)).block(); +FaultInjectionRule connectionErrorRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.CREATE_ITEM) + .endpoints(new FaultInjectionEndpointBuilder(FeedRange.forLogicalPartition(new PartitionKey(""))).build()) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionConnectionErrorType.CONNECTION_CLOSE) + .interval(Duration.ofSeconds(1)) + .threshold(1.0) + .build() + ) + .duration(Duration.ofSeconds(2)) + .build(); + +CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(connectionErrorRule)).block(); ``` ## Troubleshooting diff --git a/sdk/cosmos/azure-cosmos-test/src/samples/java/com/azure/cosmos/test/ReadmeSamples.java b/sdk/cosmos/azure-cosmos-test/src/samples/java/com/azure/cosmos/test/ReadmeSamples.java new file mode 100644 index 0000000000000..fb5e319fe4ec6 --- /dev/null +++ b/sdk/cosmos/azure-cosmos-test/src/samples/java/com/azure/cosmos/test/ReadmeSamples.java @@ -0,0 +1,125 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package java.com.azure.cosmos.test; + +import com.azure.cosmos.CosmosAsyncClient; +import com.azure.cosmos.CosmosAsyncContainer; +import com.azure.cosmos.CosmosClientBuilder; +import com.azure.cosmos.models.FeedRange; +import com.azure.cosmos.models.PartitionKey; +import com.azure.cosmos.test.faultinjection.CosmosFaultInjectionHelper; +import com.azure.cosmos.test.faultinjection.FaultInjectionConditionBuilder; +import com.azure.cosmos.test.faultinjection.FaultInjectionConnectionErrorType; +import com.azure.cosmos.test.faultinjection.FaultInjectionEndpointBuilder; +import com.azure.cosmos.test.faultinjection.FaultInjectionOperationType; +import com.azure.cosmos.test.faultinjection.FaultInjectionResultBuilders; +import com.azure.cosmos.test.faultinjection.FaultInjectionRule; +import com.azure.cosmos.test.faultinjection.FaultInjectionRuleBuilder; +import com.azure.cosmos.test.faultinjection.FaultInjectionServerErrorType; + +import java.time.Duration; +import java.util.Arrays; + +public class ReadmeSamples { + private final CosmosAsyncClient cosmosAsyncClient = new CosmosClientBuilder() + .endpoint("") + .key("") + .buildAsyncClient(); + private final CosmosAsyncContainer container = + cosmosAsyncClient + .getDatabase("") + .getContainer(""); + + public void highChannelAcquisitionScenario() { + // BEGIN: readme-sample-highChannelAcquisitionScenario + FaultInjectionRule serverConnectionDelayRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.CREATE_ITEM) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionServerErrorType.CONNECTION_DELAY) + .delay(Duration.ofSeconds(6)) // default connection timeout is 5s + .times(1) + .build() + ) + .duration(Duration.ofMinutes(5)) + .build(); + + CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(serverConnectionDelayRule)).block(); + // END: readme-sample-highChannelAcquisitionScenario + } + + public void brokenConnectionScenario() { + // BEGIN: readme-sample-brokenConnectionScenario + FaultInjectionRule timeoutRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.READ_ITEM) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionServerErrorType.RESPONSE_DELAY) + .times(1) + .delay(Duration.ofSeconds(6)) // the default time out is 5s + .build() + ) + .duration(Duration.ofMinutes(5)) + .build(); + + CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(timeoutRule)).block(); + // END: readme-sample-brokenConnectionScenario + } + + public void serverReturnGoneScenario() { + // BEGIN: readme-sample-serverReturnGoneScenario + FaultInjectionRule serverErrorRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.READ_ITEM) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionServerErrorType.GONE) + .times(1) + .build() + ) + .duration(Duration.ofMinutes(5)) + .build(); + + CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(serverErrorRule)).block(); + // END: readme-sample-serverReturnGoneScenario + } + + public void randomConnectionCloseScenario() { + // BEGIN: readme-sample-randomConnectionCloseScenario + FaultInjectionRule connectionErrorRule = + new FaultInjectionRuleBuilder("") + .condition( + new FaultInjectionConditionBuilder() + .operationType(FaultInjectionOperationType.CREATE_ITEM) + .endpoints(new FaultInjectionEndpointBuilder(FeedRange.forLogicalPartition(new PartitionKey(""))).build()) + .build() + ) + .result( + FaultInjectionResultBuilders + .getResultBuilder(FaultInjectionConnectionErrorType.CONNECTION_CLOSE) + .interval(Duration.ofSeconds(1)) + .threshold(1.0) + .build() + ) + .duration(Duration.ofSeconds(2)) + .build(); + + CosmosFaultInjectionHelper.configureFaultInjectionRules(container, Arrays.asList(connectionErrorRule)).block(); + // END: readme-sample-randomConnectionCloseScenario + } +} diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/ClientTelemetry.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/ClientTelemetry.java index cd62c7ede9ab5..c1729c766a24e 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/ClientTelemetry.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/ClientTelemetry.java @@ -305,7 +305,7 @@ private Mono sendClientTelemetry() { Mono httpResponseMono = this.httpClient.send(httpRequest, Duration.ofSeconds(Configs.getHttpResponseTimeoutInSeconds())); return httpResponseMono.flatMap(response -> { - if (response.statusCode() != HttpConstants.StatusCodes.OK) { + if (response.statusCode() != HttpConstants.StatusCodes.NO_CONTENT) { logger.error("Client telemetry request did not succeeded, status code {}", response.statusCode()); } diff --git a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/ClientTelemetryTest.java b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/ClientTelemetryTest.java index e1b6f33b7ba09..3ce3b6817b82f 100644 --- a/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/ClientTelemetryTest.java +++ b/sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/ClientTelemetryTest.java @@ -331,7 +331,7 @@ public void clientTelemetryWithStageJunoEndpoint(boolean useProxy) throws Interr Thread.sleep(5000); StepVerifier.create(httpResponseMono.get()).expectNextMatches(httpResponse -> { logger.info("clientTelemetryWithStageJunoEndpoint statusCode from juno {}", httpResponse.statusCode()); - return httpResponse.statusCode() == HttpConstants.StatusCodes.OK; + return httpResponse.statusCode() == HttpConstants.StatusCodes.NO_CONTENT; }).verifyComplete(); } finally { if (cosmosClient != null) {