Docker中的DocumentDB - "授权令牌在当前时间无效。"

时间:2016-08-04 15:39:43

标签: python azure docker tornado azure-cosmosdb

我在Docker中运行Python Tornado应用程序,而部分API涉及连接到DocumentDB以进行存储:

client = document_client.DocumentClient(config.uri, {'masterKey': config.key})
db = next((data for data in client.ReadDatabases() if data['id'] == config.db))
coll = next((docs for docs in client.ReadCollections(db['_self']) if docs['id'] == config.collection))

授权工作正常,我通过添加和删除文档完成了对数据库的多次调用。当我离开Docker容器运行几个小时(没有准确计算需要多长时间)或者当我将容器放置过夜并在早上检查它时,问题出现了,我得到了这个错误:

Traceback (most recent call last):
tornado1_1  |   File "api_app.py", line 76, in <module>
tornado1_1  |     class UserHandler(BaseHandler):
tornado1_1  |   File "api_app.py", line 82, in UserHandler
tornado1_1  |     db = next((data for data in client.ReadDatabases() if data['id'] == config.db))
tornado1_1  |   File "api_app.py", line 82, in <genexpr>
tornado1_1  |     db = next((data for data in client.ReadDatabases() if data['id'] == config.db))
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/query_iterable.py", line 123, in next
tornado1_1  |     retry_utility._Execute(self._iterable._client, self._iterable._client._global_endpoint_manager, callback)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/retry_utility.py", line 48, in _Execute
tornado1_1  |     result = _ExecuteFunction(function, *args, **kwargs)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/retry_utility.py", line 81, in _ExecuteFunction
tornado1_1  |     return function(*args, **kwargs)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/query_iterable.py", line 114, in callback
tornado1_1  |     if not self._iterable.fetch_next_block():
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/query_iterable.py", line 144, in fetch_next_block
tornado1_1  |     fetched_items = self.fetch_items()
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/query_iterable.py", line 184, in fetch_items
tornado1_1  |     (fetched_items, response_headers) = self._fetch_function(self._options)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/document_client.py", line 225, in fetch_fn
tornado1_1  |     options), self.last_response_headers
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/document_client.py", line 2349, in __QueryFeed
tornado1_1  |     headers)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/document_client.py", line 2206, in __Get
tornado1_1  |     headers)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/synchronized_request.py", line 168, in SynchronizedRequest
tornado1_1  |     return retry_utility._Execute(client, global_endpoint_manager, _InternalRequest, connection_policy, request_options, request_body)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/retry_utility.py", line 48, in _Execute
tornado1_1  |     result = _ExecuteFunction(function, *args, **kwargs)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/retry_utility.py", line 81, in _ExecuteFunction
tornado1_1  |     return function(*args, **kwargs)
tornado1_1  |   File "/usr/local/lib/python2.7/site-packages/pydocumentdb/synchronized_request.py", line 100, in _InternalRequest
tornado1_1  |     raise errors.HTTPFailure(response.status, data, headers)
tornado1_1  | pydocumentdb.errors.HTTPFailure: Status code: 403
tornado1_1  | {"code":"Forbidden","message":"The authorization token is not valid at the current time. Please create another token and retry (token start time: Thu, 04 Aug 2016 04:30:53 GMT, token expiry time: Thu, 04 Aug 2016 04:45:53 GMT, current server time: Thu, 04 Aug 2016 15:11:11 GMT).\r\nActivityId: af4c602a-9413-4eb3-b270-b8a57fa2d973"}

正如您所看到的,它可以与客户端建立连接,但它在db = next((data for data in client.ReadDatabases() if data['id'] == config.db))行失败,并引发一些关于服务器与令牌开始时间之间不匹配的奇怪错误。一旦我重新启动计算机(不仅仅是容器),它将再次运行一段不确定的时间。我在Azure Documentation上阅读了以下提示:

  

提示:   资源令牌的默认有效时间跨度为1小时。但是,令牌生存期可以明确指定,最长可达5小时。

不确定是否与此有关。

3 个答案:

答案 0 :(得分:0)

这可能是由于您的机器时间漂移(与服务器相比)持续增加,直到差异为一小时。

在异常消息中,您可以看到令牌开始/结束时间与当前服务器时间之间的延迟。

答案 1 :(得分:0)

这听起来像授权令牌到期。因此,您需要生成另一个。错误消息显示“请创建另一个令牌并重试”。

也许你在创建容器时创建了令牌?您可以尝试删除容器以强制它创建一个新容器。

答案 2 :(得分:0)

根据HTTP Status Codes for DocumentDB和您的说明,异常信息的状态代码403表示 The authorization token expired.

所以解决方案就是通过catch&amp; amp;创建一个新的客户端连接,而不是旧的客户端连接。处理异常。

作为参考,文章末尾有一个提示。

  

提示

     

资源令牌的默认有效时间跨度为1小时。但是,令牌生存期可以明确指定,最长可达5小时。

您可以通过指定标头x-ms-documentdb-expiry-seconds的值来引用REST API Create a PermissionReplace a Permission来修改令牌生存期。