Skip to content

INTERNAL: Add zset element upsert at the engine layer - #1053

Merged
jhpark816 merged 1 commit into
naver:developfrom
zhy2on:zset-elem-upsert
Nov 19, 2026
Merged

jhpark816 merged 1 commit into
naver:developfrom
zhy2on:zset-elem-upsert

Conversation

@zhy2on

@zhy2on zhy2on commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

🔗 Related Issue

⌨️ What I did

zset 원소 upsert를 엔진 레이어에 추가했습니다.
zset_elem_insert에 replace_if_exist를 받도록 했고, 같은 member가 있으면 원소를 교체합니다.

교체 방식

  • score가 같으면 bplus tree에서 같은 자리에 바꿔 끼웁니다.
  • score가 다르면 bplus tree의 새 위치에 넣고 기존 원소를 뺍니다.
  • 마지막으로 hash tree에서 원소를 바꿔 끼웁니다.

실패할 수 있는 건 bplus tree에 넣는 단계뿐이라 이 단계를 가장 먼저 합니다. 실패해도 두 트리가 그대로라 롤백이 필요 없습니다.


변경 사항

  • delete_post: 교체 중 기존 원소를 뺄 때는 delete_arg를 NULL로 넘기고, zset_elem_delete_post는 NULL이면 아무것도 하지 않습니다. 원소 처리는 교체 함수에서 직접 합니다.
  • htree_elem_replace: assert(old_elem->linked == 0)을 제거했습니다.
  • 새 원소 추가 부분을 do_zset_elem_add로 분리했습니다. btree의 do_btree_elem_add와 같은 구조입니다.

논의 사항

  • score가 바뀌면 bplus tree에 새 원소를 먼저 넣고 기존 원소를 빼서, 위치가 제자리여도 node split이 생길 수 있습니다.
    Redis는 갱신 후에도 위치가 그대로면 제자리에서 score만 바꾸도록 최적화되어 있습니다.
    추후 같은 leaf 안의 이동을 따로 처리하는 개선을 진행할 수 있습니다.

  • CLOG, 프로토콜 연결은 아직 진행하지 않았습니다.
  • GT/LT 옵션을 다음 PR로 진행합니다.

@zhy2on
zhy2on requested a review from jhpark816 November 18, 2026 03:10
@jhpark816

Copy link
Copy Markdown
Collaborator

htree_elem_replace: assert(old_elem->linked == 0)을 제거했습니다.

어떤 이유가 있나요?

@zhy2on

zhy2on commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator Author

htree_elem_replace: assert(old_elem->linked == 0)을 제거했습니다.
어떤 이유가 있나요?

이제 한 원소가 여러 트리에 링크될 수 있어, htree_elem_replace 시점에 linked가 0이 아닐 수 있습니다.
assert를 두면 hash tree를 항상 마지막에 교체해야 하는 제약이 생겨 제거했고, 대신 호출처에서 확인하는 것으로 변경했습니다.

@jhpark816 jhpark816 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

리뷰 완료

Comment thread engines/default/coll_zset.c Resolved

@jhpark816 jhpark816 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

리뷰 완료

Comment thread engines/default/coll_zset.c Outdated
#ifdef ENABLE_STICKY_ITEM
/* sticky memory limit check */
if (IS_STICKY_COLLFLG(info)) {
if (BKEY_ISNE(find->data, find->nbkey, elem->data, elem->nbkey)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

이 부분이 수정된 것 같은 데, 원래대로 두어야 합니다.

           if ((find->neflag + find->nbytes) < (elem->neflag + elem->nbytes)) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

btree와 다른 조건으로 의도적으로 수정한 것입니다.

btree는 bkey로 찾기 때문에 교체 시 eflag와 value 크기(nbytes)가 달라질 수 있지만,
zset은 member(value)로 찾기 때문에 nbytes는 항상 같고 score(bkey)와 eflag만 달라집니다.

달라지는 것 do_zset_elem_replace에서 일어나는 일 메모리 증가
bkey와 eflag 모두 같음 없음 아무것도 안 함 없음
eflag만 다름 eflag (최대 31바이트) bplus_elem_replace로 제자리 교체 eflag 크기 증가분
bkey가 다름 bkey (binary면 최대 31바이트), eflag bplus_elem_add와 bplus_elem_delete 원소 크기 증가분 + node split 시 split된 level마다 새 노드 할당

처음에는 btree arithmetic이 incr/decr로 인한 value 크기 변화를 sticky limit check 없이 넘기는 것을 보고, eflag 크기 변화도 무시할 수 있다고 생각했습니다.
다시 보니 incr/decr은 크기 변화가 무시할 만한 수준이지만 eflag는 그렇지 않아서, eflag 크기 증가도 체크하도록 추가했습니다.

if (BKEY_ISNE(find->data, find->nbkey, elem->data, elem->nbkey) ||
    find->neflag < elem->neflag) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

논의한 대로 zset replace에서는 sticky limit check를 생략하도록 수정했습니다.

zset 교체에서 바뀌는 것은 score와 eflag뿐이라, node split을 포함해도 공간 차이가 크지 않습니다.
공간 변화는 do_coll_space_incr/decr에서 정확히 집계되므로 체크를 건너뛰어도 문제없습니다.

다른 collection과 달리 코드가 아예 없으면 헷갈릴 수 있어, #ifdef ENABLE_STICKY_ITEM 블록은 두고 생략 이유를 주석으로 남겼습니다.

@zhy2on
zhy2on force-pushed the zset-elem-upsert branch 2 times, most recently from 74cb8e7 to 75960bd Compare November 19, 2026 08:44
@jhpark816

Copy link
Copy Markdown
Collaborator

@zhy2on
CI 실패 확인 바랍니다.

@zhy2on
zhy2on requested a review from jhpark816 November 19, 2026 09:14
@jhpark816
jhpark816 merged commit 3436c62 into naver:develop Nov 19, 2026
1 check passed
@zhy2on
zhy2on deleted the zset-elem-upsert branch November 19, 2026 09:26
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.

2 participants