Skip to content

INTERNAL: Add zset element update at the engine layer - #1057

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

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

Conversation

@zhy2on

@zhy2on zhy2on commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

🔗 Related Issue

⌨️ What I did

member로 원소를 찾아 score와 eflag를 변경하는 zset_elem_update를 추가했습니다.

ENGINE_ERROR_CODE zset_elem_update(const char *key, const uint32_t nkey,
                                   const char *member, const uint32_t nbytes,
                                   const void *score, const uint32_t nscore,
                                   ENGINE_UPDATE_COND cond, const eflag_update *eupdate,
                                   const void *cookie);
  • score가 NULL이면 score를 유지하고, eupdate가 NULL이면 eflag를 유지합니다.
  • cond(GT/LT)는 score를 줄 때만 쓰고, 조건이 맞지 않으면 NOT_STORED를 반환합니다.
  • score와 eflag 크기가 그대로이고 refcount가 0이면 제자리에서 eflag만 바꿉니다. 그 외에는 새 원소를 만들어 do_zset_elem_replace로 교체하고, 실패하면 새 원소를 해제합니다.
  • sticky limit check는 upsert와 같은 이유로 생략했습니다.

btree_elem_update와 같은 구조로 작성했고, 다른 점은 아래와 같습니다.

  • 대상 지정: btree는 bkey_range를 받아 SIN 타입인지 확인하지만, zset은 시그니처부터 member 하나만 받습니다.
  • 처리 순서: btree는 원소 구조 <bkey, [eflag,] value> 중 바뀌는 eflag → value 순으로 처리합니다. zset은 <score, [eflag,] member> 중 바뀌는 score → eflag 순으로 크기 계산, 비교, 새 원소 생성을 맞췄습니다.
  • 검사 순서: 위 순서와 달리 eflag 검사(EBADEFLAG)를 score 조건 검사(NOT_STORED)보다 먼저 합니다. 잘못된 요청인지부터 확인하기 위해서입니다.

@zhy2on
zhy2on marked this pull request as draft November 19, 2026 10:45
@zhy2on
zhy2on marked this pull request as ready for review November 20, 2026 08:33
@zhy2on
zhy2on requested a review from jhpark816 November 20, 2026 08:33

@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.

리뷰 완료


if (elem->refcount == 0 &&
(score == NULL || BKEY_ISEQ(score, nscore, elem->data, elem->nbkey)) &&
elem->neflag == new_neflag) {

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.

아래 조건을 사용해야 하지 않는지?

  • (elem->nbkey+elem->neflag) == (new_nbkey+new_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.

해당 조건은 score가 달라도 nbkey+neflag가 같으면 in-place update하게 되어, 다른 위치로 옮겨져야 할 elem이 제자리에 남아 bplus tree 정렬 순서가 깨질 수 있습니다.
그래서 현재는 score가 다르면 항상 새 elem을 bplus tree에 insert하여 정확한 위치를 찾도록 했습니다.

@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.

리뷰 완료

new_elem->hval = elem->hval;

/* replace the element */
ENGINE_ERROR_CODE ret = do_zset_elem_replace(info, &pinfo, new_elem);

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.

do_zset_elem_replace() 함수의 구현 중
bplus tree에서 교체 로직이 여기 의미에 맞지 않는 것 같습니다.
항상 new elem 추가하고 old elem 제거하는 방식이어야 합니다.

    /* replace in bplus tree */
    if (bplus_elem_find(&info->bplus, new_elem->data, new_elem->nbkey,
                        member, new_elem->nbytes, path) != NULL) {
        /* same score: replace in place */
        replaced = (zset_elem_item *)bplus_elem_replace(&path[0], (bplus_elem_item *)new_elem);
        assert(replaced == old_elem);
    } else {
        /* score changed: add at the new position, then remove the old one */
        ENGINE_ERROR_CODE ret = bplus_elem_add(&info->bplus, path, (bplus_elem_item *)new_elem,
                                               &space_increased);
        if (ret != ENGINE_SUCCESS) {
            return ret;
        }
        uint32_t deleted = bplus_elem_delete(&info->bplus, old_elem->data, old_elem->nbkey,
                                             member, old_elem->nbytes, NULL,
                                             NULL, NULL, &space_decreased);
        assert(deleted == 1);
    }

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.

@jhpark816
do_zset_elem_update()에서 아래와 같은 경우에 do_zset_elem_replace()로 들어올 수 있습니다.

경우 score bplus tree 교체
score는 같고 eflag 크기가 바뀜 같음 찾은 위치에서 bplus_elem_replace()
score는 같고 refcount > 0 같음 찾은 위치에서 bplus_elem_replace()
score가 바뀜 다름 new elem 추가 후 old elem 제거

new elem과 old elem의 (score, member)가 같은 경우, new elem을 추가한 뒤 old elem을 key로 삭제하면 둘 중 어느 쪽이 삭제될지 알 수 없습니다.
그래서 score가 같을 때는 찾은 위치에서 교체하도록 했습니다.

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.

bkey 만 가지고 찾은 것이 아니라 <bkey, member>로 찾은 것이네요.
따라서, element 가 있다면, 해당 member의 element 이군요.

@jhpark816
jhpark816 merged commit c1bdf13 into naver:develop Nov 22, 2026
1 check passed
@zhy2on
zhy2on deleted the zset-elem-update branch November 22, 2026 14:14
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