私のサーバーはAmazon Ec2 linuxで実行されています。中にmongodbサーバーがあります。mongodbサーバーは高負荷で実行されており、不幸なことに、私はそれに問題が発生しました:/
知られているように、mongodbはすべてのクライアント接続に対して新しいスレッドを作成し、これは以前は問題なく機能していました。理由はわかりませんが、MongoDBは非特権ユーザー(mongodユーザーで実行)としてホスト上に975を超える接続を作成できません。しかし、それをrootユーザーとして実行している場合、最大20000の接続を処理できます(mongodb内部制限)。しかし、さらに調査すると、その問題はMongoDBサーバーではなく、Linux自体にあることがわかります。
最大接続数をチェックする単純なプログラムを見つけました。
/* compile with:   gcc -lpthread -o thread-limit thread-limit.c */
/* originally from: http://www.volano.com/linuxnotes.html */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define MAX_THREADS 100000
#define PTHREAD_STACK_MIN 1*1024*1024*1024
int i;
void run(void) {
  sleep(60 * 60);
}
int main(int argc, char *argv[]) {
  int rc = 0;
  pthread_t thread[MAX_THREADS];
  pthread_attr_t thread_attr;
  pthread_attr_init(&thread_attr);
  pthread_attr_setstacksize(&thread_attr, PTHREAD_STACK_MIN);
  printf("Creating threads ...\n");
  for (i = 0; i < MAX_THREADS && rc == 0; i++) {
    rc = pthread_create(&(thread[i]), &thread_attr, (void *) &run, NULL);
    if (rc == 0) {
      pthread_detach(thread[i]);
      if ((i + 1) % 100 == 0)
    printf("%i threads so far ...\n", i + 1);
    }
    else
    {
      printf("Failed with return code %i creating thread %i (%s).\n",
         rc, i + 1, strerror(rc));
      // can we allocate memory?
      char *block = NULL;
      block = malloc(65545);
      if(block == NULL)
        printf("Malloc failed too :( \n");
      else
        printf("Malloc worked, hmmm\n");
    }
  }
sleep(60*60); // ctrl+c to exit; makes it easier to see mem use
  exit(0);
}
そして、sutuationが再度繰り返され、rootユーザーとして約32kのスレッドを作成でき、非特権ユーザー(mongodまたはec2-user)として約1000作成できます。
これはrootユーザーのulimitです:
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 59470
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 60000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
これはmongodユーザーのulimitです:
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 59470
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 60000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 1024
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
カーネル最大スレッド:
bash-4.1$ cat /proc/sys/kernel/threads-max 
118940
SELinuxが無効になっています。この奇妙な問題を解決する方法がわかりません...おそらく、誰かがしますか?