Heroku Java 线程错误

Heroku Java Thread Errors

我目前正在尝试使用免费的 dyno 在 Heroku 上部署我的 Spring 引导后端。我还为我的数据库将后端连接到 MongoDB Atlas。

在 Spring 引导服务器启动期间,我创建了 2 个 Java 线程来同时填充数据库中的 2 个不同集合。这一切都在本地工作,我可以在 Heroku 上启动应用程序并可以访问 REST 端点,但我收到以下关于我的线程的错误。

2020-11-23T23:03:11.942515+00:00 app[web.1]: [7.521s][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 512k, guardsize: 0k, detached.

这里还有完整的 Heroku 日志:

2020-11-23T23:03:02.534403+00:00 heroku[web.1]: Starting process with command `java -Dserver.port=41612 $JAVA_OPTS -jar build/libs/*.jar`
2020-11-23T23:03:04.405570+00:00 app[web.1]: Create a Procfile to customize the command used to run this process: https://devcenter.heroku.com/articles/procfile
2020-11-23T23:03:04.417361+00:00 app[web.1]: Setting JAVA_TOOL_OPTIONS defaults based on dyno size. Custom settings will override them.
2020-11-23T23:03:04.422107+00:00 app[web.1]: Picked up JAVA_TOOL_OPTIONS: -XX:+UseContainerSupport -Xmx300m -Xss512k -XX:CICompilerCount=2 -Dfile.encoding=UTF-8 
2020-11-23T23:03:05.738862+00:00 app[web.1]: 
2020-11-23T23:03:05.738907+00:00 app[web.1]:   .   ____          _            __ _ _
2020-11-23T23:03:05.738973+00:00 app[web.1]:  /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2020-11-23T23:03:05.739037+00:00 app[web.1]: ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2020-11-23T23:03:05.739107+00:00 app[web.1]:  \/  ___)| |_)| | | | | || (_| |  ) ) ) )
2020-11-23T23:03:05.739170+00:00 app[web.1]:   '  |____| .__|_| |_|_| |_\__, | / / / /
2020-11-23T23:03:05.739235+00:00 app[web.1]:  =========|_|==============|___/=/_/_/_/
2020-11-23T23:03:05.740346+00:00 app[web.1]:  :: Spring Boot ::        (v2.3.3.RELEASE)
2020-11-23T23:03:05.740408+00:00 app[web.1]: 
2020-11-23T23:03:05.910449+00:00 app[web.1]: 2020-11-23 23:03:05.907  INFO 4 --- [           main] c.c.Covid19Trackerv2Application          : Starting Covid19Trackerv2Application on a13bc37a-2eb2-45fa-9632-d771ffc7e31a with PID 4 (/app/build/libs/covid-19-trackerv2-0.0.1-SNAPSHOT.jar started by u30247 in /app)
2020-11-23T23:03:05.911061+00:00 app[web.1]: 2020-11-23 23:03:05.910  INFO 4 --- [           main] c.c.Covid19Trackerv2Application          : The following profiles are active: prod
2020-11-23T23:03:06.760984+00:00 app[web.1]: 2020-11-23 23:03:06.759  INFO 4 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2020-11-23T23:03:06.841091+00:00 app[web.1]: 2020-11-23 23:03:06.840  INFO 4 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 74ms. Found 2 MongoDB repository interfaces.
2020-11-23T23:03:07.483569+00:00 app[web.1]: 2020-11-23 23:03:07.483  INFO 4 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 41612 (http)
2020-11-23T23:03:07.499209+00:00 app[web.1]: 2020-11-23 23:03:07.498  INFO 4 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-23T23:03:07.499446+00:00 app[web.1]: 2020-11-23 23:03:07.499  INFO 4 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-11-23T23:03:07.583124+00:00 app[web.1]: 2020-11-23 23:03:07.582  INFO 4 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-23T23:03:07.583281+00:00 app[web.1]: 2020-11-23 23:03:07.583  INFO 4 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1555 ms
2020-11-23T23:03:07.866235+00:00 app[web.1]: 2020-11-23 23:03:07.866  INFO 4 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[127.0.0.1:27017], srvHost=covid-stats.4koxc.mongodb.net, mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', requiredReplicaSetName='atlas-mwzir4-shard-0'}
2020-11-23T23:03:07.913285+00:00 app[web.1]: 2020-11-23 23:03:07.912  INFO 4 --- [oxc.mongodb.net] org.mongodb.driver.cluster               : Adding discovered server covid-stats-shard-00-02.4koxc.mongodb.net:27017 to client view of cluster
2020-11-23T23:03:07.955398+00:00 app[web.1]: 2020-11-23 23:03:07.955  INFO 4 --- [oxc.mongodb.net] org.mongodb.driver.cluster               : Adding discovered server covid-stats-shard-00-01.4koxc.mongodb.net:27017 to client view of cluster
2020-11-23T23:03:07.957062+00:00 app[web.1]: 2020-11-23 23:03:07.956  INFO 4 --- [oxc.mongodb.net] org.mongodb.driver.cluster               : Adding discovered server covid-stats-shard-00-00.4koxc.mongodb.net:27017 to client view of cluster
2020-11-23T23:03:08.400392+00:00 app[web.1]: Start state DB population
2020-11-23T23:03:08.405359+00:00 app[web.1]: Start country DB population
2020-11-23T23:03:08.489114+00:00 app[web.1]: 2020-11-23 23:03:08.488  INFO 4 --- [pulation thread] org.mongodb.driver.cluster               : No server chosen by com.mongodb.client.internal.MongoClientDelegate@1a150eab from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=covid-stats-shard-00-01.4koxc.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=covid-stats-shard-00-02.4koxc.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=covid-stats-shard-00-00.4koxc.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2020-11-23T23:03:08.489124+00:00 app[web.1]: 2020-11-23 23:03:08.488  INFO 4 --- [pulation thread] org.mongodb.driver.cluster               : No server chosen by com.mongodb.client.internal.MongoClientDelegate@4eca20e7 from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=covid-stats-shard-00-01.4koxc.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=covid-stats-shard-00-02.4koxc.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=covid-stats-shard-00-00.4koxc.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
2020-11-23T23:03:08.614420+00:00 app[web.1]: 2020-11-23 23:03:08.614  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:278460}] to covid-stats-shard-00-01.4koxc.mongodb.net:27017
2020-11-23T23:03:08.614466+00:00 app[web.1]: 2020-11-23 23:03:08.614  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:212196}] to covid-stats-shard-00-02.4koxc.mongodb.net:27017
2020-11-23T23:03:08.614929+00:00 app[web.1]: 2020-11-23 23:03:08.614  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:213140}] to covid-stats-shard-00-00.4koxc.mongodb.net:27017
2020-11-23T23:03:08.626796+00:00 app[web.1]: 2020-11-23 23:03:08.626  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=covid-stats-shard-00-00.4koxc.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=8094764, setName='atlas-mwzir4-shard-0', canonicalAddress=covid-stats-shard-00-00.4koxc.mongodb.net:27017, hosts=[covid-stats-shard-00-01.4koxc.mongodb.net:27017, covid-stats-shard-00-02.4koxc.mongodb.net:27017, covid-stats-shard-00-00.4koxc.mongodb.net:27017], passives=[], arbiters=[], primary='covid-stats-shard-00-01.4koxc.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='US_EAST_1'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=null, setVersion=1, lastWriteDate=Mon Nov 23 23:03:07 UTC 2020, lastUpdateTimeNanos=900669778320280}
2020-11-23T23:03:08.626807+00:00 app[web.1]: 2020-11-23 23:03:08.626  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=covid-stats-shard-00-01.4koxc.mongodb.net:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=8748614, setName='atlas-mwzir4-shard-0', canonicalAddress=covid-stats-shard-00-01.4koxc.mongodb.net:27017, hosts=[covid-stats-shard-00-01.4koxc.mongodb.net:27017, covid-stats-shard-00-02.4koxc.mongodb.net:27017, covid-stats-shard-00-00.4koxc.mongodb.net:27017], passives=[], arbiters=[], primary='covid-stats-shard-00-01.4koxc.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='US_EAST_1'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=7fffffff0000000000000004, setVersion=1, lastWriteDate=Mon Nov 23 23:03:07 UTC 2020, lastUpdateTimeNanos=900669778318515}
2020-11-23T23:03:08.626850+00:00 app[web.1]: 2020-11-23 23:03:08.626  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=covid-stats-shard-00-02.4koxc.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=8, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=8337820, setName='atlas-mwzir4-shard-0', canonicalAddress=covid-stats-shard-00-02.4koxc.mongodb.net:27017, hosts=[covid-stats-shard-00-01.4koxc.mongodb.net:27017, covid-stats-shard-00-02.4koxc.mongodb.net:27017, covid-stats-shard-00-00.4koxc.mongodb.net:27017], passives=[], arbiters=[], primary='covid-stats-shard-00-01.4koxc.mongodb.net:27017', tagSet=TagSet{[Tag{name='nodeType', value='ELECTABLE'}, Tag{name='provider', value='AWS'}, Tag{name='region', value='US_EAST_1'}, Tag{name='workloadType', value='OPERATIONAL'}]}, electionId=null, setVersion=1, lastWriteDate=Mon Nov 23 23:03:07 UTC 2020, lastUpdateTimeNanos=900669778318694}
2020-11-23T23:03:08.628924+00:00 app[web.1]: 2020-11-23 23:03:08.628  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Setting max election id to 7fffffff0000000000000004 from replica set primary covid-stats-shard-00-01.4koxc.mongodb.net:27017
2020-11-23T23:03:08.629278+00:00 app[web.1]: 2020-11-23 23:03:08.629  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Setting max set version to 1 from replica set primary covid-stats-shard-00-01.4koxc.mongodb.net:27017
2020-11-23T23:03:08.629444+00:00 app[web.1]: 2020-11-23 23:03:08.629  INFO 4 --- [ngodb.net:27017] org.mongodb.driver.cluster               : Discovered replica set primary covid-stats-shard-00-01.4koxc.mongodb.net:27017
2020-11-23T23:03:08.679102+00:00 app[web.1]: 2020-11-23 23:03:08.678  INFO 4 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-23T23:03:08.848577+00:00 app[web.1]: 2020-11-23 23:03:08.848  INFO 4 --- [pulation thread] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:5, serverValue:278460}] to covid-stats-shard-00-01.4koxc.mongodb.net:27017
2020-11-23T23:03:08.858333+00:00 app[web.1]: 2020-11-23 23:03:08.858  INFO 4 --- [pulation thread] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:278460}] to covid-stats-shard-00-01.4koxc.mongodb.net:27017
2020-11-23T23:03:08.973383+00:00 app[web.1]: 2020-11-23 23:03:08.973  INFO 4 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-11-23T23:03:09.031909+00:00 app[web.1]: 2020-11-23 23:03:09.031  INFO 4 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 41612 (http) with context path ''
2020-11-23T23:03:09.052479+00:00 app[web.1]: 2020-11-23 23:03:09.051  INFO 4 --- [           main] c.c.Covid19Trackerv2Application          : Started Covid19Trackerv2Application in 3.852 seconds (JVM running for 4.63)
2020-11-23T23:03:09.375486+00:00 heroku[web.1]: State changed from starting to up
2020-11-23T23:03:11.000000+00:00 app[api]: Build succeeded
2020-11-23T23:03:11.942515+00:00 app[web.1]: [7.521s][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 512k, guardsize: 0k, detached.
2020-11-23T23:03:11.943390+00:00 app[web.1]: [7.522s][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 512k, guardsize: 0k, detached.
2020-11-23T23:03:11.980858+00:00 app[web.1]: [7.559s][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 512k, guardsize: 0k, detached.
2020-11-23T23:03:11.981282+00:00 app[web.1]: [7.560s][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 512k, guardsize: 0k, detached.

感谢任何帮助!

@BeppeC 指出 Heroku 的帮助论坛 this link 对此有一个答案。本来我没有 Procfile,所以我加了一个,结果变成了这样:

web: java -Dserver.port=$PORT $JAVA_OPTS -Xss2m -jar build/libs/<jar-name>.jar

要注意的命令是-Xss2m。这设置了堆栈大小,Free、Hobby 或 Standard-1X dyno 的默认值为 512k,低于默认的 JVM 大小 1MB。

我最终将它增加到 2MB,这解决了我的线程占用过多资源的问题。