我正在使用 Docker 容器化 CosmosDB 模拟器 和 dotnet 核心测试项目。
我遇到了一个异常,因为代码要求“强”一致性,这对于我们真正的天蓝色环境来说很好,但模拟器拒绝了它,并显示以下内容
System.ArgumentException
ConsistencyLevel Strong specified in the request is invalid when service is
configured with consistency level Session.
Ensure the request consistency level is not stronger than the service
consistency level.
at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.ValidateAndSetConsistencyLevelAsync(RequestMessage requestMessage)
at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.SendAsync(RequestMessage request, CancellationToken cancellationToken)
at Microsoft.Azure.Cosmos.Handlers.RequestInvokerHandler.SendAsync(String resourceUriString, ResourceType resourceType, OperationType operationType, RequestOptions requestOptions, ContainerInternal cosmosContainerCore, FeedRange feedRange, Stream streamPayload, Action`1 requestEnricher, ITrace trace, CancellationToken cancellationToken)
at Microsoft.Azure.Cosmos.ContainerCore.ProcessItemStreamAsync(Nullable`1 partitionKey, String itemId, Stream streamPayload, OperationType operationType, ItemRequestOptions requestOptions, ITrace trace, CancellationToken cancellationToken)
at Microsoft.Azure.Cosmos.ContainerCore.ExtractPartitionKeyAndProcessItemStreamAsync[T](Nullable`1 partitionKey, String itemId, T item, OperationType operationType, ItemRequestOptions requestOptions, ITrace trace, CancellationToken cancellationToken)
at Microsoft.Azure.Cosmos.ContainerCore.CreateItemAsync[T](T item, ITrace trace, Nullable`1 partitionKey, ItemRequestOptions requestOptions, CancellationToken cancellationToken)
at Microsoft.Azure.Cosmos.ClientContextCore.RunWithDiagnosticsHelperAsync[TResult](String containerName, String databaseName, OperationType operationType, ITrace trace, Func`2 task, Func`2 openTelemetry, String operationName, RequestOptions requestOptions)
at Microsoft.Azure.Cosmos.ClientContextCore.<>c__DisplayClass31_0`1.<<OperationHelperWithRootTraceWithSynchronizationContextAsync>b__0>d.MoveNext()
...
请求正在使用创建项目时的选项:
await container.CreateItemAsync
(
item: item,
partitionKeyValue,
new ItemRequestOptions { ConsistencyLevel = ConsistencyLevel.Strong },
cancellationToken
);
如何配置模拟器假装使用强一致性,以便它不会拒绝请求?
我已经阅读了一致性模型,所以我明白为什么当 Cosmos 配置为较弱的“会话”一致性时,你不能要求强一致性。但我找不到任何关于如何使模拟器表现得像“强”模式的参考。我不想更改代码,因为它被编写为使用“强”大概是有充分理由的,而削弱请求可能会破坏我正在查看的系统中微妙但重要的东西。
通过附加参数添加它:
cosmosdb:
image: mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator
environment:
AZURE_COSMOS_EMULATOR_ARGS: "/Consistency=Strong"
这是由模拟器中的
start.sh
文件传递的:
docker exec -it devcontainer-cosmosdb-1 /bin/bash
root@a3ea5276c3a3:/usr/local/bin/cosmos/bin#
grep --line-number OTHER_ARGS start.sh
11:EMULATOR_OTHER_ARGS="${AZURE_COSMOS_EMULATOR_ARGS:-/enablepreview}"
14: EMULATOR_OTHER_ARGS="$EMULATOR_OTHER_ARGS /EnableMongoDBEndpoint=${AZURE_COSMOS_EMULATOR_ENABLE_MONGODB_ENDPOINT}"
16: EMULATOR_OTHER_ARGS="$EMULATOR_OTHER_ARGS /nocompute"
70:./cosmosdb-emulator -w $COSMOS_APP_HOME/appdata -- /Microsoft.Azure.Cosmos.Emulator.exe /enablepreview /disableRIO /minimal $EMULATOR_PARTITION_SETTINGS /disablethrottling $EMULATOR_CERTIFICATE_OPTION /alternativenames=$EMULATOR_IP_ADDRESS,$EMULATOR_OTHER_IP_ADDRESSES /alternativeips=$EMULATOR_IP_ADDRESS,$EMULATOR_OTHER_IP_ADDRESSES /publicipaddressoverride=$EMULATOR_IP_ADDRESS /AllowNetworkAccess /Key=$EMULATOR_KEY $EMULATOR_OTHER_ARGS