Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CBRD-25414] The phenomenon of heap memory usage increasing each time utilities are executed #5529

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

H2SU
Copy link
Contributor

@H2SU H2SU commented Oct 7, 2024

http://jira.cubrid.org/browse/CBRD-25414

Purpose

  • 문제 상황

    1. memmon 유틸리티를 반복적으로 실행 시 힙메모리가 1160bytes씩 증가하는 문제
    2. 세션 생성 후 정상적인 종료가 아닌 kill 명령어를 통한 강제 종료 시 힙메모리가 1160bytes 증가된채로 정리되지 않는 문제
    3. 세션이 강제로 끊어졌을 때 main thread에서 세션을 정리하기 전 worker thread에서 세션이 끊어짐을 감지하고
      세션포인터와 세션아이디, 세션의 ref_count를 decrease하는 문제
  • 1번 문제 원인

    • 유틸리티 실행 시 세션을 생성하게 되는데, 이 때 세션 파라미터값을 저장하기 위한 힙메모리 1160bytes를 할당받습니다.
    • 하지만 세션 종료 시, 위에서 할당받았던 힙메모리 영역을 반환시켜주지 않고 종료하고 있기 때문에 발생하는 문제입니다.
    • 따라서 memmon 유틸리티 뿐만 아니라 세션을 생성하는 모든 유틸리티에서 발생하는 문제임을 확인하였고,
      세션을 할당받는 모든 유틸리티 대상으로 세션을 반환하도록 수정하였습니다.
  • 2번 문제 원인

    • 유틸리티가 세션을 생성한 후 강제로 종료하게 되면,
      강제로 끊어진 세션을 체크하는 쓰레드에서 세션이 끊어짐을 감지하게 되는데
      이 때, 처리하는 작업에 세션이 할당받았던 힙메모리 영역을 반환시켜주는 작업이 빠져있었습니다.
    • 따라서, 해당 함수 내부에 세션이 할당받은 힙메모리 영역을 반환하도록 수정하였습니다.
  • 3번 문제 원인

    • main thread에서 세션을 정리하기 위해 session_state_destroy 함수를 호출 할 때 해당 함수에서 session의 ref_count를 decrease하기 전 assert 구문을 통해 ref_count가 0이하라면 assert를 발생시키고 있습니다.
    • 이 때, worker thread에서 ref_count를 1에서 0으로 decrease하였기 때문에 assert가 발생합니다.
    • 따라서 assert 구문이 아닌 조건문을 통해 해당 작업이 이미 처리되었다면 건너뛰고 다음 작업을 진행하도록 수정하였습니다.

주요 함수

  • db_find_or_create_session()
    • 세션을 생성하는 함수, 해당 함수를 호출하게 되면 세션파라미터 저장을 위한 힙메모리 영역을 생성하게 됩니다.
    • 따라서 db_end_session()을 사용하여 해당 영역을 정리해주는 작업이 필요합니다.
  • db_end_session()
    • 클라이언트가 서버에게 세션 종료를 요청하는 함수
    • 해당 함수를 호출하면 서버에게 net_request를 사용하여 서버 종료를 요청하게 됩니다.
  • net_server_conn_down()
    • 클라이언트와의 연결이 끊어졌을 때 사용되는 함수
  • session_get_session_id (thread_p, &session_id)
    • 세션 아이디 getter
  • session_state_destroy (thread_p, session_id)
    • db_end_session() 을 호출하게 되면 서버쪽에서 세션을 정리할 때 사용하는 함수
    • 해당 함수 내부에서 session uninitialize를 진행하게 됩니다.
    • 세션 연결을 감지하는 쓰레드는 서버side에서 돌아가기 때문에 클라이언트에서 요청하는 db_end_session을 사용하지 않고
      해당 함수를 사용하였습니다.

Implementation

  • db_shutdown 함수 내부에 db_end_session 함수를 추가하여
    shutdown 이전 세션이 할당받은 힙메모리 영역을 반환하도록 수정하였습니다.
  • 세션의 연결을 감지하는 쓰레드에서 연결이 끊어진 세션을 처리할 때
    end_session을 하여 힙메모리 영역을 반환하도록 수정하였습니다.
  • db_shutdown 함수 내부에 db_end_session이 추가됨에 따라
    db_shutdown 함수 호출 전 db_end_session을 호출하는 코드를 제거하였습니다.

Remarks

  • db_end_session 수행 직후 비정상 종료 시 net_server_conn_down 함수 내부에서 session_state_destroy 동작방식
    • db_end_session이 수행되었다면 세션정보가 전부 uninitialize 되었기 때문에 net_server_conn_down 함수 내부에서 세션 포인터가 NULL로 출력되게 됩니다.
    • 때문에 NULL 체크하는 조건문을 추가하였습니다.

@H2SU H2SU requested a review from beyondykk9 as a code owner October 7, 2024 03:09
@H2SU H2SU marked this pull request as draft October 7, 2024 03:09
@H2SU H2SU self-assigned this Oct 7, 2024
@H2SU H2SU marked this pull request as ready for review October 7, 2024 04:13
src/communication/network_sr.c Outdated Show resolved Hide resolved
src/compat/db_admin.c Show resolved Hide resolved
…_end_session before the db_shutdown function
@H2SU H2SU requested a review from kisoo-han as a code owner October 7, 2024 05:15
@H2SU H2SU requested a review from joohok October 7, 2024 05:16
@H2SU H2SU changed the title [CBRD-25414] The phenomenon of heap memory usage increasing by 1128 bytes each time the memmon utility is executed [CBRD-25414] The phenomenon of heap memory usage increasing by 1128 bytes each time utilities are executed Oct 7, 2024
@H2SU H2SU changed the title [CBRD-25414] The phenomenon of heap memory usage increasing by 1128 bytes each time utilities are executed [CBRD-25414] The phenomenon of heap memory usage increasing each time utilities are executed Oct 7, 2024
@H2SU H2SU requested review from YeunjunLee and joohok October 8, 2024 04:31
@hornetmj hornetmj removed the request for review from joohok October 10, 2024 09:46
Comment on lines -765 to +772

if (thread_p != NULL && thread_p->conn_entry != NULL && thread_p->conn_entry->session_p != NULL
&& thread_p->conn_entry->session_p == session_p)
if (session_p->ref_count > 0)
{
thread_p->conn_entry->session_p = NULL;
thread_p->conn_entry->session_id = DB_EMPTY_SESSION;
if (thread_p != NULL && thread_p->conn_entry != NULL && thread_p->conn_entry->session_p != NULL
&& thread_p->conn_entry->session_p == session_p)
{
thread_p->conn_entry->session_p = NULL;
thread_p->conn_entry->session_id = DB_EMPTY_SESSION;

session_state_decrease_ref_count (thread_p, session_p);
}
else
{
/* do we accept this case?? if we don't, add safe-guard here. */
}
session_state_decrease_ref_count (thread_p, session_p);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 코드부는 main 쓰레드에서 수행되고, worker 쓰레드는 css_shutdown_conn() -> session_state_decrease_ref_count()를 수행할 경우 ref_count < 0인 경우 등이 발생할 수 있을 것으로 보여집니다.

이 경우에 대해 어떻게 방어되어지고 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

방어되어지고 있지 않아서 해당 부분을 어떻게 처리할까 고민하다
삭제하는 방향으로 수정하였습니다.

세션을 정리해줄 떄 hashmap에서 erase해주는 부분이 있는데 erase하기 위해서는 thread_p 정보가 필요합니다.
하지만 worker thread에서는 thread_p 정보를 받고있지 않아, 매개변수를 추가로 받는 것보다
main thread에서 destroy함수로 전부 처리하도록 수정하는 것이 나아보여 위와같이 수정하였습니다.

@H2SU H2SU requested a review from hornetmj October 11, 2024 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants