大集合的 Firestore DeadlineExceeded 异常
来源:Golang技术栈
时间:2023-04-12 14:59:35 343浏览 收藏
在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《大集合的 Firestore DeadlineExceeded 异常》,聊聊golang,希望可以帮助到正在努力赚钱的你。
问题内容
我正在尝试从 Google Firestore 中读取更大的集合以进行测试和归档。当我尝试从包含超过 6k 个文档的集合中获取所有文档时,我遇到了一些有趣的错误。
朴素的 Python 解决方案
我的第一次尝试是使用 Python google-cloud-firestore
(版本 0.30.0)库。
source_client = firestore.Client() source = source_client.collection(collection) source_data = source.get() counter = 0 for f in source_data: app.logger.info(f.id) counter += 1 if counter % 100 == 0: app.logger.info('%s %d', datetime.now(), counter) app.logger.info('%s Finally read all %d documents', datetime.now(), counter)
这给出了以下输出:
INFO:flask.app:2018-11-08 09:49:03.923795 6400 INFO:flask.app:2018-11-08 09:49:04.115410 6500 ... INFO:flask.app:2018-11-08 09:49:03.923795 6400 INFO:flask.app:2018-11-08 09:49:04.115410 6500 WARNING:flask.app:2018-11-08 09:49:04.128478 copy brocken by exception Traceback (most recent call last): File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise raise value File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise raise value File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/carsten/projects/transfertool/firestore/transfertool/main.py", line 142, in transfer count_collection(source_collection) File "/home/carsten/projects/transfertool/firestore/transfertool/main.py", line 94, in count_collection for f in source_collection.offset(1000).get(): File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/query.py", line 588, in get for index, response_pb in enumerate(response_iterator): File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 83, in next six.raise_from(exceptions.from_grpc_error(exc), exc) File "", line 3, in raise_from # Permission is hereby granted, free of charge, to any person obtaining a copy google.api_core.exceptions.DeadlineExceeded: 504 Deadline Exceeded
这似乎是由配额引起的。即使我在这里看不到。它似乎是基于时间的,因为当我在元素之间以小睡眠运行时,我的吞吐量会降低,并且在大约 50 秒后会出现异常。
使用 Python 进行分页
对于这个问题,这个库中有一个分页部分。由于我的应用程序不应该关心我尝试传输什么样的数据,我无法使用该start_after
接口,但仍然有一个偏移接口,我至少可以使用它分批读取。
for f in source_collection.offset(last_read_offset).get():
只要last_read_offset
低于 1001,它就会给我正确的结果。如果我从 1000
的偏移量开始,我可以得到结果,直到我google.api_core.exceptions.DeadlineExceeded exception
从上面得到。但是当我从更大的事情开始时,我得到:
Traceback (most recent call last): File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__ return self.wsgi_app(environ, start_response) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app response = self.handle_exception(e) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception reraise(exc_type, exc_value, tb) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise raise value File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app response = self.full_dispatch_request() File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request rv = self.handle_user_exception(e) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception reraise(exc_type, exc_value, tb) File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise raise value File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request rv = self.dispatch_request() File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/home/carsten/projects/transfertool/firestore/transfertool/main.py", line 144, in transfer count_collection(source_collection) File "/home/carsten/projects/transfertool/firestore/transfertool/main.py", line 94, in count_collection for f in source_collection.offset(1001).get(): File "/home/carsten/projects/transfertool/venv/lib/python3.6/site-packages/google/cloud/firestore_v1beta1/query.py", line 599, in get raise ValueError(msg) ValueError: Unexpected server response. All responses other than the first must contain a document. The response at index 1 was read_time { seconds: 1541668338 nanos: 420813000 } skipped_results: 1
查看库代码,后端似乎正在发送一条被解释为无效的消息。
通过 node.js 重试
好吧,也许我的代码或 Python 客户端库有问题。让我们尝试使用节点。
const admin = require('firebase-admin'); admin.initializeApp({ credential: admin.credential.applicationDefault() }); var db = admin.firestore(); admin.firestore().settings( { timestampsInSnapshots: true }) var counter = 0 console.log('Read collection') db.collection(collection).get() .then(querySnapshot => { querySnapshot.forEach(documentSnapshot => { counter++; }); console.log(counter) }) .catch( error => { console.log(error) });
即使超时更明显是 60 秒,它与 python 库的作用相同。
[2018-11-09T08:36:30.992Z] App listening on port 8080 [2018-11-09T08:36:30.993Z] Press Ctrl+C to quit. [2018-11-09T08:36:37.390Z] Read collection [2018-11-09T08:37:37.406Z] { Error: 4 DEADLINE_EXCEEDED: Deadline Exceeded at Object.exports.createStatusError (/home/carsten/projects/node_modules/grpc/src/common.js:87:15) at ClientReadableStream._emitStatusIfDone (/home/carsten/projects/node_modules/grpc/src/client.js:235:26) at ClientReadableStream._readsDone (/home/carsten/projects/node_modules/grpc/src/client.js:201:8) at /home/carsten/projects/node_modules/grpc/src/client_interceptors.js:679:15 code: 4, metadata: Metadata { _internal_repr: {} }, details: 'Deadline Exceeded' }
有没有人有类似的经历或很好的提示如何继续?
PS:exportDocument
/importDocument
接口是不够的,有时我们需要在读取后调整数据。而且我不知道 Firestore
将哪种格式导出到 Google Cloud Storage 或如何转换它。
编辑:golang
并且为了它,我尝试了golang api。
log.Println("Collecting data") snapshotIter := client.Collection(collection.(string)).Documents(ctx) defer snapshotIter.Stop() if err != nil { log.Fatalln(err) } i := 0 for { _, err := snapshotIter.Next() if err == iterator.Done { break } if err != nil { log.Fatalln(err) } if i % 100 == 0{ log.Println(i) } i++ } log.Println("Done")
与预期的超时相同。
2018/11/12 15:01:20 Collecting data 2018/11/12 15:01:21 0 2018/11/12 15:01:21 100 2018/11/12 15:01:21 200 2018/11/12 15:01:21 300 2018/11/12 15:01:21 400 2018/11/12 15:01:22 500 2018/11/12 15:01:22 600 2018/11/12 15:01:22 700 .... 2018/11/12 15:02:22 29800 2018/11/12 15:02:23 29900 2018/11/12 15:02:23 rpc error: code = DeadlineExceeded desc = The datastore operation timed out, or the data was temporarily unavailable.
但此外,偏移量也可以正常工作:
snapshotIter := client.Collection(collection.(string)).Offset(30000).Documents(ctx)
正确答案
在 firebase 支持团队的帮助下,我们发现 python 客户端 api 确实存在错误。在下一个版本中会有一个错误修复。很可能它将使 python
库能够按 documentid 排序,因此使用start_after()
.
在那之前,您有两种可能的解决方案:
-
使用另一个字段进行排序和使用
start_after()
-
使用带有分页的 node.js 库,例如:
var db = admin.firestore(); admin.firestore().settings({ timestampsInSnapshots: true }); function readNextPage(lastReadDoc) { let query = db .collection(collection) .orderBy(admin.firestore.FieldPath.documentId()) .limit(100); }
终于介绍完啦!小伙伴们,这篇关于《大集合的 Firestore DeadlineExceeded 异常》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
-
439 收藏
-
262 收藏
-
193 收藏
-
188 收藏
-
500 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习