检索大量数据时,Heroku 中的 React App 运行 失败

React App running in Heroku fails when retrieving large amounts of data

我在 Heroku(免费选项)上有一个反应应用程序(Node 后端)运行 连接到 Atlas(也是免费选项)上的 MongoDB 运行。当我将应用程序从我的本地机器连接到 Atlas DB 时,一切都很好,并且在大约 10 秒内检索到数据(所有 108 K 条记录),更少量的数据(4-500 条记录)在更短的时间内检索到。从 Heroku 上的应用程序 运行 到 Atlas DB 的相同请求失败。 Heroku 上的应用程序 运行 可以在不到一秒的时间内从同一集合(108 K 条记录)中检索少量记录(1-10 条)。一旦我尝试检索几百条记录,系统就会失败。以下是日志。我包括了显示成功检索 1 条记录然后在请求大约 450 条记录时失败的日志部分。

我有三个问题:

  1. 问题的原因是什么?
  2. Heroku 的免费选项是否有解决方法?
  3. 如果免费选项没有变通方法,我需要达到什么 Heroku 付费级别?我需要采取哪些步骤才能使其正常运行?我将来可能会升级,但想在朝那个方向前进之前证明一切正常。

日志:

2021-06-14T17:26:17.488675+00:00 heroku[router]: at=info method=GET path="/api/constructs/definitions/byMaxUpdateDate/5ed591a0570a1f29585b5cf6" host=protected-cliffs-99136.herokuapp.com request_id=3c01815a-23a3-46fa-ab75-4e36e76da739 fwd="73.119.93.241" dyno=web.1 connect=0ms service=321ms status=304 bytes=376 protocol=https
2021-06-14T17:26:17.573725+00:00 app[web.1]: ConstructDefinition.controller - listByCriteria - Start
2021-06-14T17:26:17.573762+00:00 app[web.1]: createQueryObj - iteration : 1  owner_id:  5ed591a0570a1f29585b5cf6
2021-06-14T17:26:17.573797+00:00 app[web.1]: createQueryObj - iteration : 2  myClass:  Academic
2021-06-14T17:26:17.573832+00:00 app[web.1]: createQueryObj - iteration : 3  category:  Vocabulary Expansion
2021-06-14T17:26:17.573861+00:00 app[web.1]: createQueryObj - iteration : 4  subject:  Vocabulary
2021-06-14T17:26:17.573896+00:00 app[web.1]: createQueryObj - iteration : 5  difficultyLevel:  6
2021-06-14T17:26:17.573923+00:00 app[web.1]: createQueryObj - iteration : 6  ageRange:  2
2021-06-14T17:26:17.573954+00:00 app[web.1]: createQueryObj - iteration : 7  topic:  zythum
2021-06-14T17:26:17.573999+00:00 app[web.1]: createQueryObject - queryString =  {"owner_id": "5ed591a0570a1f29585b5cf6","myClass": "Academic","category": "Vocabulary Expansion","subject": "Vocabulary","difficultyLevel": "6","ageRange": "2","topic": "zythum"}
2021-06-14T17:26:17.899908+00:00 heroku[router]: at=info method=GET path="/api/constructs/definitions/byCriteria/5ed591a0570a1f29585b5cf6?owner_id=5ed591a0570a1f29585b5cf6&myClass=Academic&category=Vocabulary%20Expansion&subject=Vocabulary&difficultyLevel=6&ageRange=2&topic=zythum" host=protected-cliffs-99136.herokuapp.com request_id=5af21335-c10d-4ee6-981f-7232affcbfc2 fwd="73.119.93.241" dyno=web.1 connect=0ms service=360ms status=304 bytes=375 protocol=https


2021-06-14T17:26:36.487043+00:00 app[web.1]: ConstructDefinition.controller - listByCriteria - Start
2021-06-14T17:26:36.487603+00:00 app[web.1]: createQueryObj - iteration : 1  owner_id:  5ed591a0570a1f29585b5cf6
2021-06-14T17:26:36.487680+00:00 app[web.1]: createQueryObj - iteration : 2  myClass:  Academic
2021-06-14T17:26:36.487758+00:00 app[web.1]: createQueryObj - iteration : 3  difficultyLevel:  6
2021-06-14T17:26:36.487836+00:00 app[web.1]: createQueryObject - queryString =  {"owner_id": 

"5ed591a0570a1f29585b5cf6","myClass": "Academic","difficultyLevel": "6"}
    2021-06-14T17:27:04.964250+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/api/constructs/definitions/byCriteria/5ed591a0570a1f29585b5cf6?owner_id=5ed591a0570a1f29585b5cf6&myClass=Academic&false&false&difficultyLevel=6&false&false" host=protected-cliffs-99136.herokuapp.com request_id=79ec14d7-9e6c-41d7-9682-fd967bebbee4 fwd="73.119.93.241" dyno=web.1 connect=0ms service=28516ms status=503 bytes=0 protocol=https
    2021-06-14T17:27:04.896082+00:00 app[web.1]:

2021-06-14T17:27:04.896093+00:00 app[web.1]: <--- Last few GCs --->
2021-06-14T17:27:04.896093+00:00 app[web.1]:
2021-06-14T17:27:04.896095+00:00 app[web.1]: [21:0x5cc8230]   453328 ms: Mark-sweep 255.3 (258.1) -> 254.5 (258.1) MB, 146.3 / 0.0 ms  (average mu = 0.133, current mu = 0.036) allocation failure scavenge might not succeed
2021-06-14T17:27:04.896095+00:00 app[web.1]: [21:0x5cc8230]   453635 ms: Mark-sweep 255.4 (258.1) -> 254.6 (258.3) MB, 302.1 / 0.0 ms  (average mu = 0.068, current mu = 0.016) allocation failure scavenge might not succeed
2021-06-14T17:27:04.896096+00:00 app[web.1]:
2021-06-14T17:27:04.896096+00:00 app[web.1]:
2021-06-14T17:27:04.896096+00:00 app[web.1]: <--- JS stacktrace --->
2021-06-14T17:27:04.896096+00:00 app[web.1]:
2021-06-14T17:27:04.896098+00:00 app[web.1]: ==== JS stack trace =========================================
2021-06-14T17:27:04.896098+00:00 app[web.1]:
2021-06-14T17:27:04.896099+00:00 app[web.1]: 0: ExitFrame [pc: 0x145ce19]
2021-06-14T17:27:04.896100+00:00 app[web.1]: Security context: 0x05bfaa040921 <JSObject>
2021-06-14T17:27:04.896101+00:00 app[web.1]: 1: completeMany(aka completeMany) [0x199c8a762a21] [/app/node_modules/mongoose/lib/helpers/query/completeMany.js:~21] [pc=0x1de09459122b](this=0x1096cc9004b9 <undefined>,0x0a6a9ed68769 <JSFunction model (sfi = 0x26feb614f221)>,0x23be33099751 <JSArray[100072]>,0x23be330a1591 <Object map = 0x2cc683e3d161>,0x23be330a1601 <Object map = 0x2cc683e3d161>,0x23be330a1...
2021-06-14T17:27:04.896101+00:00 app[web.1]:
2021-06-14T17:27:04.896117+00:00 app[web.1]: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
2021-06-14T17:27:04.896224+00:00 app[web.1]:
2021-06-14T17:27:04.910524+00:00 app[web.1]: Writing Node.js report to file: report.20210614.172704.21.0.001.json
2021-06-14T17:27:04.910608+00:00 app[web.1]: Node.js report completed
2021-06-14T17:27:04.911501+00:00 app[web.1]: 1: 0xa9d570 node::Abort() [node]
2021-06-14T17:27:04.912203+00:00 app[web.1]: 2: 0xa9f832 node::OnFatalError(char const*, char const*) [node]
2021-06-14T17:27:04.913000+00:00 app[web.1]: 3: 0xc0772e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
2021-06-14T17:27:04.913707+00:00 app[web.1]: 4: 0xc07aa9 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
2021-06-14T17:27:04.914476+00:00 app[web.1]: 5: 0xdb5fb5  [node]
2021-06-14T17:27:04.915251+00:00 app[web.1]: 6: 0xdb6646 v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [node]
2021-06-14T17:27:04.916020+00:00 app[web.1]: 7: 0xdc4eb9 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
2021-06-14T17:27:04.916830+00:00 app[web.1]: 8: 0xdc5cf5 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
2021-06-14T17:27:04.917647+00:00 app[web.1]: 9: 0xdc87cc v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [node]
2021-06-14T17:27:04.918404+00:00 app[web.1]: 10: 0xd8f3a4 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [node]
2021-06-14T17:27:04.919225+00:00 app[web.1]: 11: 0x10dc6ce v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [node]
2021-06-14T17:27:04.920443+00:00 app[web.1]: 12: 0x145ce19  [node]
2021-06-14T17:27:04.964341+00:00 app[web.1]: Aborted
2021-06-14T17:27:04.968680+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-06-14T17:27:04.969190+00:00 app[web.1]: npm ERR! errno 134
2021-06-14T17:27:04.970722+00:00 app[web.1]: npm ERR! teachlearngame@2.0.0 start: `NODE_ENV=production node ./dist/server.generated.js`
2021-06-14T17:27:04.970948+00:00 app[web.1]: npm ERR! Exit status 134
2021-06-14T17:27:04.971205+00:00 app[web.1]: npm ERR!
2021-06-14T17:27:04.971416+00:00 app[web.1]: npm ERR! Failed at the teachlearngame@2.0.0 start script.
2021-06-14T17:27:04.971618+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-06-14T17:27:04.978470+00:00 app[web.1]:
2021-06-14T17:27:04.978794+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-06-14T17:27:04.978986+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-06-14T17_27_04_972Z-debug.log
2021-06-14T17:27:05.035373+00:00 heroku[web.1]: Process exited with status 134
2021-06-14T17:27:05.174452+00:00 heroku[web.1]: State changed from up to crashed
2021-06-14T17:27:05.179240+00:00 heroku[web.1]: State changed from crashed to starting
2021-06-14T17:27:13.491905+00:00 heroku[web.1]: Starting process with command `npm start`
2021-06-14T17:27:16.042157+00:00 app[web.1]:
2021-06-14T17:27:16.042234+00:00 app[web.1]: > teachlearngame@2.0.0 start /app
2021-06-14T17:27:16.042240+00:00 app[web.1]: > NODE_ENV=production node ./dist/server.generated.js
2021-06-14T17:27:16.042245+00:00 app[web.1]:
2021-06-14T17:27:19.878282+00:00 heroku[web.1]: State changed from starting to up
2021-06-14T17:27:19.734472+00:00 app[web.1]: server - before call to initial load
2021-06-14T17:27:19.734929+00:00 app[web.1]: server - after call to initial load
2021-06-14T17:27:19.739564+00:00 app[web.1]: (node:21) ExperimentalWarning: Conditional exports is an experimental feature. This feature could change at any time
2021-06-14T17:27:19.740098+00:00 app[web.1]: Server started on port 28159.

您 运行 节点服务器中的堆内存不足。这可能是因为有些语句使用了大量内存。您可以尝试找到它,也可以尝试增加节点内存 like this.