首页 > 解决方案 > CosmosDatabase.createContainerIfNotExists() -> “具有指定 ID、名称或唯一索引的资源已存在。”

问题描述

如果我调用该方法:CosmosDatabase.createContainerIfNotExists(x)我收到错误消息:Resource with specified id, name, or unique index already exists.

我必须如何解释这个错误?

完整的异常堆栈跟踪:

{"ClassName":"CosmosException","userAgent":"azsdk-java-cosmos/4.13.0 Linux/4.15.0-124-generic JRE/11.0.11","statusCode":409,"resourceAddress":"https://10.10.10.16:8081/dbs/jenkins21/colls","error":"{\"code\":\"Conflict\",\"message\":\"Message: {\\\"Errors\\\":[\\\"Resource with specified id, name, or unique index already exists.\\\"]}\\r\\nActivityId: f42970e5-c07b-45f9-896e-62a0aa00122a, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: \\r\\nRequestStartTime: 2021-07-20T02:05:58.8219453Z, RequestEndTime: 2021-07-20T02:05:58.8239187Z,  Number of regions attempted:1\\r\\nResponseTime: 2021-07-20T02:05:58.8239187Z, StoreResult: StorePhysicalAddress: rntbd://10.10.10.16:10251/apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, LSN: 32164, GlobalCommittedLsn: -1, PartitionKeyRangeId: , IsValid: True, StatusCode: 409, SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: -1#32164, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: BatchApply\\r\\n, SDK: Microsoft.Azure.Documents.Common/2.11.0, StatusCode: Conflict\",\"additionalErrorInfo\":null}","innerErrorMessage":"Message: {\"Errors\":[\"Resource with specified id, name, or unique index already exists.\"]}\r\nActivityId: f42970e5-c07b-45f9-896e-62a0aa00122a, Request URI: /apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: \r\nRequestStartTime: 2021-07-20T02:05:58.8219453Z, RequestEndTime: 2021-07-20T02:05:58.8239187Z,  Number of regions attempted:1\r\nResponseTime: 2021-07-20T02:05:58.8239187Z, StoreResult: StorePhysicalAddress: rntbd://10.10.10.16:10251/apps/DocDbApp/services/DocDbMaster0/partitions/780e44f4-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, LSN: 32164, GlobalCommittedLsn: -1, PartitionKeyRangeId: , IsValid: True, StatusCode: 409, SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: -1#32164, UsingLocalLSN: False, TransportException: null, ResourceType: Collection, OperationType: BatchApply\r\n, SDK: Microsoft.Azure.Documents.Common/2.11.0, StatusCode: Conflict","causeInfo":null,"responseHeaders":"{Transfer-Encoding=chunked, x-ms-last-state-change-utc=Wed, 14 Jul 2021 20:52:01.947 GMT, x-ms-current-replica-set-size=1, Server=Microsoft-HTTPAPI/2.0, x-ms-request-duration-ms=1.132, x-ms-session-token=0:-1#32164, Access-Control-Allow-Origin=, lsn=32164, x-ms-schemaversion=1.11, x-ms-transport-request-id=41722, x-ms-number-of-read-regions=0, x-ms-current-write-quorum=1, x-ms-cosmos-quorum-acked-llsn=32164, x-ms-quorum-acked-lsn=32164, Date=Tue, 20 Jul 2021 02:05:58 GMT, x-ms-activity-id=f42970e5-c07b-45f9-896e-62a0aa00122a, x-ms-xp-role=0, x-ms-global-Committed-lsn=-1, Access-Control-Allow-Credentials=true, x-ms-cosmos-llsn=32164, x-ms-gatewayversion=version=2.11.0, x-ms-serviceversion=version=2.11.0.0, Content-Type=application/json}","requestHeaders":"[Accept=application/json, x-ms-date=Tue, 20 Jul 2021 02:05:58 GMT, Content-Type=application/json]","cosmosDiagnostics":{"userAgent":"azsdk-java-cosmos/4.13.0 Linux/4.15.0-124-generic JRE/11.0.11","requestLatencyInMs":368,"requestStartTimeUTC":"2021-07-20T02:05:58.681410Z","requestEndTimeUTC":"2021-07-20T02:05:59.050232Z","connectionMode":"GATEWAY","responseStatisticsList":[],"supplementalResponseStatisticsList":[],"addressResolutionStatistics":{},"regionsContacted":["https://10.10.10.16:8081/"],"retryContext":{"retryCount":0,"statusAndSubStatusCodes":null,"retryLatency":0},"metadataDiagnosticsContext":{"metadataDiagnosticList":null},"serializationDiagnosticsContext":{"serializationDiagnosticsList":[{"serializationType":"CONTAINER_SERIALIZATION","startTimeUTC":"2021-07-20T02:05:58.681516Z","endTimeUTC":"2021-07-20T02:05:58.681552Z","durationInMicroSec":36}]},"gatewayStatistics":{"sessionToken":null,"operationType":"Create","statusCode":409,"subStatusCode":0,"requestCharge":null,"requestTimeline":[{"eventName":"connectionCreated","startTimeUTC":"2021-07-20T02:05:58.681681Z","durationInMicroSec":117},{"eventName":"connectionConfigured","startTimeUTC":"2021-07-20T02:05:58.681798Z","durationInMicroSec":22},{"eventName":"requestSent","startTimeUTC":"2021-07-20T02:05:58.681820Z","durationInMicroSec":170},{"eventName":"transitTime","startTimeUTC":"2021-07-20T02:05:58.681990Z","durationInMicroSec":367906},{"eventName":"received","startTimeUTC":"2021-07-20T02:05:59.049896Z","durationInMicroSec":150}]},"systemInformation":{"usedMemory":"71893 KB","availableMemory":"452395 KB","systemCpuLoad":"(2021-07-20T02:05:31.477916Z 48.1%), (2021-07-20T02:05:36.477915Z 34.6%), (2021-07-20T02:05:41.477923Z 37.8%), (2021-07-20T02:05:46.477917Z 42.1%), (2021-07-20T02:05:51.477942Z 61.4%), (2021-07-20T02:05:56.477908Z 57.0%)"},"clientCfgs":{"id":0,"numberOfClients":1,"connCfg":{"rntbd":"(cto:PT5S, rto:PT5S, icto:PT0S, ieto:PT1H, mcpe:130, mrpc:30, cer:false)","gw":"(cps:1000, rto:PT5S, icto:null, p:false)","other":"(ed: true, cs: false)"},"consistencyCfg":"(consistency: null, mm: true, prgns: [])"}}}
    at com.azure.cosmos.BridgeInternal.createCosmosException(BridgeInternal.java:463)
    at com.azure.cosmos.implementation.RxGatewayStoreModel.validateOrThrow(RxGatewayStoreModel.java:365)
    at com.azure.cosmos.implementation.RxGatewayStoreModel.lambda$toDocumentServiceResponse$0(RxGatewayStoreModel.java:294)
    at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
    at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:73)
    at reactor.core.publisher.FluxPeek$PeekSubscriber.onNext(FluxPeek.java:199)
    at reactor.core.publisher.FluxHandle$HandleSubscriber.onNext(FluxHandle.java:118)
    at reactor.core.publisher.FluxMap$MapConditionalSubscriber.onNext(FluxMap.java:220)
    at reactor.core.publisher.FluxDoFinally$DoFinallySubscriber.onNext(FluxDoFinally.java:130)
    at reactor.core.publisher.FluxHandleFuseable$HandleFuseableSubscriber.onNext(FluxHandleFuseable.java:184)
    at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107)
    at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1815)
    at reactor.core.publisher.MonoCollectList$MonoCollectListSubscriber.onComplete(MonoCollectList.java:128)
    at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:259)
    at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
    at reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:389)
    at reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:396)
    at reactor.netty.channel.ChannelOperations.terminate(ChannelOperations.java:452)
    at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:684)
    at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:94)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.handler.ssl.SslHandler.unwrap(SslHandler.java:1533)
    at io.netty.handler.ssl.SslHandler.decodeNonJdkCompatible(SslHandler.java:1294)
    at io.netty.handler.ssl.SslHandler.decode(SslHandler.java:1331)
    at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:508)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:447)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
    at io.netty.channel.epoll.AbstractEpollStreamChannel$EpollStreamUnsafe.epollInReady(AbstractEpollStreamChannel.java:795)
    at io.netty.channel.epoll.AbstractEpollChannel$AbstractEpollUnsafe$1.run(AbstractEpollChannel.java:388)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.epoll.EpollEventLoop.run(EpollEventLoop.java:384)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)

标签: javaazureazure-cosmosdb

解决方案


如果出现此错误,我会使用 try catch 块解决问题并重复操作。调试代码显示异常发生后容器存在。

我们的应用程序是具有延迟初始化的多线程。可以是2个线程同时尝试创建需要的容器。该错误很难重现。

听起来这个方法不像建议的方法名称那样在数据库上是原子的,也不在驱动程序中处理它。非常糟糕的 API 文档没有说明任何过线程行为。有了这样的 API 文档,就不可能编写出稳定的软件。它需要尝试和错误。


推荐阅读