PSQL 연결 문제로 인한 Discourse 충돌

포럼에서 이 메시지가 계속 표시되고 있습니다. (대략 3~4시간마다 발생합니다.) CPU 16코어와 32GB RAM을 갖추고 있습니다. 리소스가 문제라고 생각하지는 않습니다.

Oops
The software powering this discussion forum encountered an unexpected problem. We apologize for the inconvenience.

Detailed information about the error was logged, and an automatic notification generated. We'll take a look at it.

No further action is necessary. However, if the error condition persists, you can provide additional detail, including steps to reproduce the error, by posting a discussion topic in the site's feedback category.

프로덕션 로그에는 다음과 같이 표시됩니다.


app/models/user_auth_token.rb:125:in `lookup'
lib/auth/default_current_user_provider.rb:131:in `current_user'
lib/current_user.rb:35:in `current_user'
app/controllers/application_controller.rb:1047:in `rate_limit_crawlers'
lib/middleware/omniauth_bypass_middleware.rb:64:in `call'
lib/content_security_policy/middleware.rb:12:in `call'
lib/middleware/anonymous_cache.rb:393:in `call'
lib/middleware/csp_script_nonce_injector.rb:12:in `call'
config/initializers/008-rack-cors.rb:14:in `call'
config/initializers/100-silence_logger.rb:27:in `call'
lib/middleware/enforce_hostname.rb:24:in `call'
lib/middleware/request_tracker.rb:236:in `call'
Unexpected error in Message Bus : ActiveRecord::ConnectionNotEstablished : connection to server at "172.17.0.2", port 5432 failed: FATAL:  remaining connection slots are reserved for non-replication superuser connections

Unexpected error in Message Bus : ActiveRecord::ConnectionNotEstablished : connection to server at "172.17.0.2", port 5432 failed: FATAL:  remaining connection slots are reserved for non-replication superuser connections

Unexpected error in Message Bus : ActiveRecord::ConnectionNotEstablished : connection to server at "172.17.0.2", port 5432 failed: FATAL:  remaining connection slots are reserved for non-replication superuser connections

Unexpected error in Message Bus : ActiveRecord::ConnectionNotEstablished : connection to server at "172.17.0.2", port 5432 failed: FATAL:  remaining connection slots are reserved for non-replication superuser connections

다음과 같은 설정을 적용했습니다.

UNICORN_WORKERS: 32
UNICORN_SIDEKIQS: 2

그리고 psql의 경우

db_shared_buffers: "4096MB"

서버가 충돌하지 않도록 설정을 개선하고 안정성을 확보하기 위해 추가로 할 수 있는 일이 있다면 알려주세요.

postgres(db_shared_buffers)에는 최소 16GB, 가능하면 20GB를 할당하는 것이 좋습니다.

하지만 더 많은 DB 연결이 필요합니다. 정확히 어떻게 설정하는지 기억이 나지 않네요.

postgres가 실행 중인 컨테이너 내부의 /etc/postgresql/postgresql.conf 파일에서 max_connections 값을 변경해야 한다고 생각합니다.

저도 그렇게 생각합니다. :+1:

정말 그렇게 하신 건가요? 그 수치가 꽤 높은 것 같습니다. PostgreSQL: Documentation: 13: 19.4. Resource Consumption 를 참고해 보세요.

1GB 이상의 RAM이 있는 전용 데이터베이스 서버의 경우, shared_buffers의 합리적인 시작 값은 시스템 메모리의 25%입니다. 일부 워크로드에서는 shared_buffers에 더 큰 설정 값이 효과적일 수 있지만, PostgreSQL은 운영 체제 캐시에 또한 의존하기 때문에 RAM의 40% 이상을 shared_buffers에 할당하는 것이 더 작은 양보다 더 나은 결과를 내기 어렵습니다.

그리고 이것은 전용 데이터베이스 서버가 아니며, 해당 서버에는 32개의 unicorn 프로세스도 실행되고 있습니다.

이런 문제에서는 항상 당신을 따르고, 과거에 당신이 주신 조언을 인용하고 있다고 생각했기 때문에, 아니요, 확신하지 못합니다. :rofl:

연결 수가 문제라는 것은 꽤 분명해 보입니다. RAM을 32GB의 25%로 늘리는 것이 전반적으로 도움이 될 수는 있지만, 오류의 근본 원인은 아닙니다.

수정:

하! 내가 기억하던 바로 그 내용이었네요. 다만 50%를 넘길 것 같았던 것 같습니다. . .

어느 정도 유죄를 인정합니다

하지만 그건 그때의 이야기였죠 … :wink:

:100:

왜 그랬나요? 그 설정들이 연결 슬롯이 소진되는 원인이었습니다.

어딘가에서 UNICORN WORKER는 2 * CPU여야 한다는 내용을 본 것 같습니다. 그래서 계산을 해보니 32가 나왔습니다. 규모를 줄여야 할까요?

PSQL의 타임아웃 설정을 변경하기 위해 ALTER ROLE discourse SET statement_timeout = '30000'; 명령을 실행해 보았습니다. 그리고 몇 시간마다 한 번씩 이 쿼리가 차단되었습니다.


무슨 일이 발생했는지 당신이나 다른 분들에게 아이디어가 있는지 잘 모르겠습니다.

아니요, 이 설정을 제거하고 기본값이 적용되도록 해 주세요. 이것은 전형적인 조기 최적화(premature optimization) 사례입니다.

네, 건드리지 말아야 할 부분을 건드렸네요 :wink: