EDITAR:

EDITAR:

Estou tentando alcançar 2.000 usuários simultâneos com minha ferramenta de benchmarking. Estou usando gafanhotos para simulá-los.

Meu servidor tem 24vCPUs, 128GB de RAM, 25SSD.

Quero poder atender 2.000 usuários simultâneos sem erros, mas depois de apenas 700 usuários, tenho problemas.

Gunicórnio

Instalei o gevent para poder servir de forma assíncrona, mas isso não mudou nada no meu teste de carga (o gevent pode não estar funcionando?). Meu arquivo systemd é o seguinte:

meusite-produção.conf

    [Unit]
    Description=mysite production daemon
    After=network.target

    [Service]
    User=www-data
    Group=www-data
    WorkingDirectory=/var/www/mysite/production/src
    ExecStart=/var/www/mysite/production/venv/bin/gunicorn --worker-class=gevent --worker-connections=1000  --workers=49 --bind unix:/var/www/mysite/production/sock/gunicorn --log-level DEBUG --log-file '/var/www/mysite/production/log/gunicorn.log' mysite.wsgi:application
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s TERM $MAINPID

    [Install]
    WantedBy=multi-user.target

De acordo com meus cálculos: 49 x 1.000 = 49.000 solicitações por segundo que eu poderia atender.

Em vez disso, com cerca de 700 usuários, recebo o seguinte erro na guia de falhas do gafanhoto:

# fails Method  Name    Type
1227    GET     //  HTTPError('500 Server Error: Internal Server Error for url: http://my.site.com//')

Basicamente diz que tenho um erro interno do servidor.

quando abro meu gunicorn.log eu vejoIgnorando o EPIPE:

gunicorn.log

[2020-01-27 20:22:30 +0000] [13121] [DEBUG] Ignoring EPIPE
[2020-01-27 20:22:31 +0000] [13121] [DEBUG] Ignoring EPIPE
[2020-01-27 20:22:31 +0000] [13121] [DEBUG] Ignoring EPIPE
[2020-01-27 20:22:31 +0000] [13121] [DEBUG] Ignoring EPIPE
[2020-01-27 20:22:31 +0000] [13121] [DEBUG] Ignoring EPIPE

Nginx

Meu nginx access.log mostra alguns erros 499 e 500:

acesso.log

185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 200 30727 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 499 0 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 499 0 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 200 30727 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 500 2453 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 500 2453 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 499 0 "-" "python-requests/2.22.0"
185.159.126.246 - - [27/Jan/2020:19:22:25 +0000] "GET // HTTP/1.1" 499 0 "-" "python-requests/2.22.0"

o nginx error.log não mostra nada, mas no teste anterior mostrouRecurso temporariamente indisponível:

erro.log


2020/01/27 19:15:32 [error] 1514#1514: *57151 connect() to unix:/var/www/mysite/production/sock/gunicorn failed (11: Resource temporarily unavailable) while connecting to upstream, client: 185.159.126.246, server: my.site.com, request: "GET // HTTP/1.1", upstream: "http://unix:/var/www/mysite/production/sock/gunicorn://", host: "my.site.com"
2020/01/27 19:15:32 [error] 1514#1514: *56133 connect() to unix:/var/www/mysite/production/sock/gunicorn failed (11: Resource temporarily unavailable) while connecting to upstream, client: 185.159.126.246, server: my.site.com, request: "GET // HTTP/1.1", upstream: "http://unix:/var/www/mysite/production/sock/gunicorn://", host: "my.site.com"

Aqui está meu nginx.conf:

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
worker_rlimit_nofile 40000;

events {
        worker_connections 4096;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
   }

e aqui está meu bloco de servidor:

my.site.com (bloco de servidor)


upstream mysite-production {
    server unix:/var/www/mysite/production/sock/gunicorn;
}
server {
    listen [::]:80;
    listen 80;
    server_name my.site.com;

    # set client body size to 100M #
    client_max_body_size 100M;

    location / {
      include proxy_params;
      proxy_pass http://unix:/var/www/mysite/production/sock/gunicorn;
    }

    location /static/ {
        root /var/www/mysite/production/;
        expires 30d;
        add_header Vary Accept-Encoding;
        access_log off;
        gzip on;
        gzip_comp_level 6;
        gzip_vary on;
        gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;
    }

    location /media/ {
        root /var/www/mysite/production/;
        expires 30d;
        add_header Vary Accept-Encoding;
        access_log off;
    }
}

Questões

Perguntas que surgem quando olho para tudo isso:

De onde vem o erro 502? Não me mostra informações suficientes para entender o que está acontecendo... Todos os meus funcionários estão ocupados depois de 700 usuários causarem os erros?

De onde vem o erro 499?

Isso tem alguma coisa a ver com meu banco de dados postgresql? se sim, como faço para descobrir?

Como posso saber se gevent funcionou ou não? os resultados dos testes de carga com ab e locust realmente não mudaram.

o queIgnorando o EPIPEsignifica e por que isso está acontecendo?

Agradeço por você dedicar seu tempo para analisar isso. Agradecemos antecipadamente por todos os seus comentários e respostas!

Se precisar de mais informações, entre em contato e adicionarei à minha pergunta.

EDITAR:

Com base no comentário do @ServerFaults, verifiquei quanta memória é drenada quando o teste de carga acontece. aparentemente nada realmente significativo:

MemTotal:       132027088 kB
MemFree:        127013484 kB
MemAvailable:   126458072 kB
Buffers:           57788 kB
Cached:           401088 kB

Depois de correr netstat -s(não tenho conhecimento para entender o que tudo significa):

Ip:
    Forwarding: 2
    6956023 total packets received
    8 with invalid addresses
    0 forwarded
    0 incoming packets discarded
    6953503 incoming packets delivered
    6722961 requests sent out
    20 outgoing packets dropped
Icmp:
    107 ICMP messages received
    0 input ICMP message failed
    ICMP input histogram:
        destination unreachable: 45
        echo requests: 62
    968 ICMP messages sent
    0 ICMP messages failed
    ICMP output histogram:
        destination unreachable: 906
        echo replies: 62
IcmpMsg:
        InType3: 45
        InType8: 62
        OutType0: 62
        OutType3: 906
Tcp:
    222160 active connection openings
    136492 passive connection openings
    78293 failed connection attempts
    161239 connection resets received
    2 connections established
    6245053 segments received
    6487229 segments sent out
    11978 segments retransmitted
    0 bad segments received
    185530 resets sent
Udp:
    667101 packets received
    41158 packets to unknown port received
    0 packet receive errors
    727164 packets sent
    0 receive buffer errors
    0 send buffer errors
UdpLite:
TcpExt:
    2939 SYN cookies sent
    28 SYN cookies received
    215 invalid SYN cookies received
    113 resets received for embryonic SYN_RECV sockets
    18751 TCP sockets finished time wait in fast timer
    175 packetes rejected in established connections because of timestamp
    61997 delayed acks sent
    54 delayed acks further delayed because of locked socket
    Quick ack mode was activated 3475 times
    2687504 packet headers predicted
    1171485 acknowledgments not containing data payload received
    2270054 predicted acknowledgments
    TCPSackRecovery: 921
    Detected reordering 22500 times using SACK
    Detected reordering 596 times using time stamp
    139 congestion windows fully recovered without slow start
    490 congestion windows partially recovered using Hoe heuristic
    TCPDSACKUndo: 14
    413 congestion windows recovered without slow start after partial ack
    TCPLostRetransmit: 47
    TCPSackFailures: 5
    7 timeouts in loss state
    2416 fast retransmits
    123 retransmits in slow start
    TCPTimeouts: 5279
    TCPLossProbes: 4284
    TCPLossProbeRecovery: 22
    TCPSackRecoveryFail: 1
    TCPDSACKOldSent: 3482
    TCPDSACKRecv: 1996
    TCPDSACKOfoRecv: 4
    52968 connections reset due to unexpected data
    54079 connections reset due to early user close
    51 connections aborted due to timeout
    TCPDSACKIgnoredOld: 25
    TCPDSACKIgnoredNoUndo: 822
    TCPSpuriousRTOs: 81
    TCPSackShifted: 4787
    TCPSackMerged: 5128
    TCPSackShiftFallback: 33199
    TCPReqQFullDoCookies: 2939
    TCPRcvCoalesce: 84434
    TCPOFOQueue: 26
    TCPChallengeACK: 3
    TCPAutoCorking: 7
    TCPFromZeroWindowAdv: 125
    TCPToZeroWindowAdv: 125
    TCPWantZeroWindowAdv: 1376
    TCPSynRetrans: 4695
    TCPOrigDataSent: 5023492
    TCPHystartTrainDetect: 2403
    TCPHystartTrainCwnd: 53596
    TCPHystartDelayDetect: 346
    TCPHystartDelayCwnd: 9883
    TCPACKSkippedSynRecv: 6
    TCPACKSkippedPAWS: 10
    TCPACKSkippedSeq: 15
    TCPWinProbe: 15
IpExt:
    InOctets: 6090026491
    OutOctets: 7098177391
    InNoECTPkts: 6966279
    InECT0Pkts: 11
    InCEPkts: 1

Editar 2:

Depois de criar uma página HTML estática sem gerar Gunicorn, ela foi capaz de atender facilmente 4.000 usuários sem atrasos. Depois de passar o work_connections que recebi:

/loadtest/  ConnectionError(MaxRetryError("HTTPConnectionPool(host='ll-my.site.com', port=80): Max retries exceeded with url: /loadtest/ (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x....>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known'))"))

Pelo que entendi, o NGINX não é um problema aqui.

Editar 3:

Verifiquei meus logs do postgresql e descobri que estão cheios deste erro:

FATAL:  remaining connection slots are reserved for non-replication superuser connections

informação relacionada