知识库

增加 Systemd 线程限制

问题

在一些高负载和大规模多数据库环境中,您可能会发现 Systemd 单元配置将最大进程(“任务”)数量限制得过低,无法满足您的使用场景。

当出现此问题时,您可以在 Neo4j debug.log 文件中看到如下信息

java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached

如果您查看 systemctl status neo4j 的输出,您会看到运行中的任务数已达到或接近限制,例如本例中的 4915

● neo4j.service - Neo4j Graph Database
Loaded: loaded (/lib/systemd/system/neo4j.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 1970-01-01 00:00:00 UTC; 48min ago
Main PID: 20000 (java)
Tasks: 4915 (limit: 4915)
CGroup: /system.slice/neo4j.service
  ├─20000 /usr/bin/java     org.neo4j.server.startup.Neo4jCommand console
  └─20047 /usr/lib/jvm/java-11-openjdk-amd64/bin/java     com.neo4j.server.enterprise.EnterpriseEntryPoint

进一步查看时,您可能会在 Linux syslogmessages 文件中发现类似如下的报告

Jan 01 00:48:00 neo4j-core1 kernel: [ TIME ] cgroup: fork rejected by pids controller in /system.slice/neo4j.service
Jan 01 00:48:02 neo4j-core1 neo4j[ PID ]: [ TIME ][warning][os,thread] Failed to start thread - pthread_create failed (EAGAIN) for attributes: stacksize: 1024k, guardsize: 0k, detached.
Jan 01 00:48:02 neo4j-core1 neo4j[ PID ]: Exception in thread "neo4j.Scheduler-1" java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached

Systemd 可能随后终止并重启 Neo4j Server。

变通方案

在这些情况下,您可能希望保守地提升最大线程限制作为变通方案。除极端情况外,这通常仅需临时使用。

首先,确保 Neo4j 已停止

systemctl stop neo4j

然后编辑 neo4j.service 单元文件

systemctl edit neo4j

并添加/增加 TasksMax 设置

[Service]
# The user and group which the service runs as.
User=neo4j
Group=neo4j

# If it takes longer than this then the shutdown is considered to have failed.
# This may need to be increased if the system serves long-running transactions.
TimeoutSec=120

# Increase the systemd process / task limit
TasksMax=6500

取消上述行的注释后,重新启动 neo4j。

systemctl daemon-reload
systemctl start neo4j

再次强调,在提升系统服务的任务限制时请非常保守。高进程线程计数可能是配置不当、规格不足或插件异常的强烈指示。

与所有变通方案一样,应在根本原因完全查明并制定更永久的解决方案之前,尽量少用。

© . This site is unofficial and not affiliated with Neo4j, Inc.