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

INTERNAL: Refactor lqdetect show #780

Merged
merged 1 commit into from
Oct 21, 2024
Merged

Conversation

cheesecrust
Copy link

@cheesecrust cheesecrust commented Jul 15, 2024

πŸ”— Related Issue

⌨️ What I did

  • κΈ°μ‘΄ <value, length> ꡬ쑰의 배열을 λ¦¬ν„΄ν•˜λŠ” λ‘œμ§μ—μ„œ κ°„λ‹¨ν•˜κ²Œ filed_t ν•˜λ‚˜λ§Œμ„ λ°˜ν™˜ν•˜λŠ” 둜직으둜 λ³€κ²½ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • 기쑴의 char * λ₯Ό ꡬ쑰체에 λ‹΄λŠ” λ°©μ‹μ—μ„œ μ‹€μ œ 좜λ ₯ ν•˜λŠ” λ¬Έμžμ—΄μ„ 이어뢙여 λ§Œλ“€μ–΄μ„œ λ¦¬ν„΄ν•˜λ„λ‘ ν•¨μˆ˜λ₯Ό λ³€κ²½ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • λ¬Έμžμ—΄μ„ 이어뢙이기 μœ„ν•œ 곡간을 ν• λ‹Ήν•˜κΈ° 전에 이어뢙일 λ¬Έμžμ—΄μ˜ 길이λ₯Ό μΈ‘μ •ν•˜μ—¬ 이에 λ§žλŠ” 곡간 만큼만 ν• λ‹Ήν•˜λ„λ‘ ν•©λ‹ˆλ‹€.
    • μ΄λ•Œ 길이λ₯Ό μΈ‘μ •ν•  λ•Œ header λŠ” κΈ°μ‘΄κ³Ό 같이 μ΅œλŒ€ 길이λ₯Ό 32 라고 두고 μ΄ˆκΈ°ν™” ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
    • body λŠ” lqdetect 에 total_offset 을 μ„ μ–Έν•˜μ—¬ buffer 의 offset 이 μ¦κ°€ν• λ•Œ 같이 μ¦κ°€ν•˜μ—¬ 이λ₯Ό ν™œμš©ν–ˆμŠ΅λ‹ˆλ‹€.
  • μ‹€νŒ¨ ν•  경우의 message λŠ” SERVER ERROR out of memory writing show response. λ₯Ό 좜λ ₯ν•˜λ„λ‘ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • lq->result λ₯Ό μ‚­μ œν•¨μœΌλ‘œμ„œ lq_release μ‚­μ œν–ˆμŠ΅λ‹ˆλ‹€.

@cheesecrust cheesecrust marked this pull request as draft July 16, 2024 00:11
@cheesecrust cheesecrust changed the title INTERNAL lqdetect show INTERNAL: refactor lqdetect show Jul 16, 2024
@cheesecrust cheesecrust changed the title INTERNAL: refactor lqdetect show INTERNAL: Refactor lqdetect show Jul 16, 2024
@cheesecrust cheesecrust marked this pull request as ready for review July 16, 2024 07:14
@ing-eoking

This comment was marked as resolved.

lqdetect.c Outdated Show resolved Hide resolved
lqdetect.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@namsic namsic left a comment

Choose a reason for hiding this comment

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

λ”°λΌμ„œ ν•¨μˆ˜ 호좜 μ‹œ κ°€μ§ˆ 수 μžˆλŠ” 졜μž₯ 길이 λ§ŒνΌμ„ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήν•˜κ³  write_and_free λ₯Ό μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μƒλ‹Ήνžˆ 큰 λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ήν•˜κ³  ν•΄μ œν•˜κ²Œ λ˜λŠ”λ°, 생각해 λ³Έ λ‹€λ₯Έ κ΅¬ν˜„ λ°©μ•ˆμ΄ μžˆλ‚˜μš”?

memcached.c Outdated Show resolved Hide resolved
@cheesecrust
Copy link
Author

λ”°λΌμ„œ ν•¨μˆ˜ 호좜 μ‹œ κ°€μ§ˆ 수 μžˆλŠ” 졜μž₯ 길이 λ§ŒνΌμ„ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήν•˜κ³  write_and_free λ₯Ό μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μƒλ‹Ήνžˆ 큰 λ©”λͺ¨λ¦¬λ₯Ό ν• λ‹Ήν•˜κ³  ν•΄μ œν•˜κ²Œ λ˜λŠ”λ°, 생각해 λ³Έ λ‹€λ₯Έ κ΅¬ν˜„ λ°©μ•ˆμ΄ μžˆλ‚˜μš”?

방식을 λ°”κΎΈμ–΄μ„œ λ¨Όμ € 좜λ ₯ν•  λ¬Έμžμ—΄μ˜ 길이λ₯Ό 잰 λ‹€μŒμ— 그에 맞게 ν• λ‹Ήν•˜λŠ” λ°©μ‹μœΌλ‘œ κ΅¬ν˜„μ„ λ°”κΎΈμ—ˆμŠ΅λ‹ˆλ‹€.

@cheesecrust cheesecrust marked this pull request as ready for review July 17, 2024 09:20
lqdetect.c Outdated Show resolved Hide resolved
@cheesecrust cheesecrust marked this pull request as draft July 18, 2024 00:58
@cheesecrust cheesecrust marked this pull request as ready for review July 18, 2024 06:41
@cheesecrust cheesecrust force-pushed the lqdetect branch 2 times, most recently from 0c1130a to db6aee3 Compare July 18, 2024 08:09
Copy link
Collaborator

@ing-eoking ing-eoking left a comment

Choose a reason for hiding this comment

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

μ•„λž˜μ™€ 같은 방식은 μ–΄λ–€κ°€μš”?

field_t lqdetect_result_get(void)
{
    int hdrlen = 32;
    field_t result = {NULL, 0};

    pthread_mutex_lock(&lqdetect.lock);
    int bytes = hdrlen * LQ_CMD_NUM;
    for (int i = 0; i < LQ_CMD_NUM; i++) {
        bytes += lqdetect.buffer[i].offset;
    }
    if ((result.value = (char*)malloc(bytes)) != NULL) {
        char *nxt = result.value;
        for (int i = 0; i < LQ_CMD_NUM; i++) {
            struct lq_detect_buffer ldb = lqdetect.buffer[i];
            nxt += snprintf(nxt, bytes, "%s : %u\n", command_str[i], ldb.ntotal);
            bytes -= (nxt - result.value);
            if (ldb.ntotal != 0) {
                int length = bytes < ldb.offset ? bytes : ldb.offset;
                nxt += snprintf(nxt, length, "%s", ldb.data);
                bytes -= length;
            }
        }
        result.length = nxt - result.value;
    }
    pthread_mutex_unlock(&lqdetect.lock);

    return result;
}

lqdetect.c Outdated Show resolved Hide resolved
lqdetect.c Outdated Show resolved Hide resolved
lqdetect.c Outdated Show resolved Hide resolved
@cheesecrust
Copy link
Author

        nxt += snprintf(nxt, bytes, "%s : %u\n", command_str[i], ldb.ntotal);
        bytes -= (nxt - result.value);
        if (ldb.ntotal != 0) {
            int length = bytes < ldb.offset ? bytes : ldb.offset;
            nxt += snprintf(nxt, length, "%s", ldb.data);
            bytes -= length;
  • byte 계산에 μžˆμ–΄μ„œ 이미 λΊ€ λ°”μ΄νŠΈ κ°’ 만큼 μ—°μ†ν•΄μ„œ 계속 λΉΌλŠ” 였λ₯˜κ°€ μžˆμ–΄ μ•„λž˜μ™€ 같이 μ •μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
  • snprintf 의 μ΅œλŒ€ λ°”μ΄νŠΈ 뢀뢄을 ldb.offset + 1 둜 μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
       nxt += snprintf(nxt, bytes - (nxt - result.value), "%s : %u\n", command_str[i], ldb.ntotal);
       if (ldb.ntotal != 0) {
           nxt += snprintf(nxt, ldb.offset + 1, "%s", ldb.data);
       }
   }

@namsic
Copy link
Collaborator

namsic commented Jul 19, 2024

μ•„λž˜ ν˜•νƒœμ˜ κ΅¬ν˜„μ„ κ²€ν† ν•΄ μ£ΌκΈ° λ°”λžλ‹ˆλ‹€.

int main(int argc, char **argv) {
    int buflen = 200;
    char *buf = (char*)malloc(buflen);
    if (!buf) {
        return 1;
    }

    int offset = 0;
    int i;
    for (i=0; i<20; i++) {
        offset += snprintf(buf + offset, buflen - offset, "%d,", i);
        //if (offset >= buflen) {
        //    break;
        //}
    }

    printf("%s\n", buf);
    free(buf);
    return 0;
}

@cheesecrust
Copy link
Author

cheesecrust commented Jul 22, 2024

μ•„λž˜ ν˜•νƒœμ˜ κ΅¬ν˜„μ„ κ²€ν† ν•΄ μ£ΌκΈ° λ°”λžλ‹ˆλ‹€.

int main(int argc, char **argv) {
    int buflen = 200;
    char *buf = (char*)malloc(buflen);
    if (!buf) {
        return 1;
    }

    int offset = 0;
    int i;
    for (i=0; i<20; i++) {
        offset += snprintf(buf + offset, buflen - offset, "%d,", i);
        //if (offset >= buflen) {
        //    break;
        //}
    }

    printf("%s\n", buf);
    free(buf);
    return 0;
}

μœ„λ₯Ό κ²€ν† ν•˜μ—¬ μ•„λž˜μ™€ 같이 λ³€κ²½ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

   if (result.value != NULL) {
       int offset = 0;
       for (int i = 0; i < LQ_CMD_NUM; i++) {
           struct lq_detect_buffer ldb = lqdetect.buffer[i];
           offset += snprintf(result.value + offset, bytes - offset, "%s : %u\n", command_str[i], ldb.ntotal);
           if (ldb.ntotal != 0) {
               offset += snprintf(result.value + offset, ldb.offset + 1, "%s", ldb.data);
           }
       }
       result.length = offset;
   }

@cheesecrust cheesecrust force-pushed the lqdetect branch 2 times, most recently from d221555 to c7f59dc Compare July 22, 2024 02:19
memcached.c Outdated Show resolved Hide resolved
lqdetect.c Outdated Show resolved Hide resolved
lqdetect.c Show resolved Hide resolved
@namsic namsic requested a review from jhpark816 July 22, 2024 08:47
Copy link
Collaborator

@jhpark816 jhpark816 left a comment

Choose a reason for hiding this comment

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

리뷰 μ™„λ£Œ

lqdetect.c Outdated
if (str != NULL) {
int offset = 0;
for (int i = 0; i < LQ_CMD_NUM; i++) {
struct lq_detect_buffer ldb = lqdetect.buffer[i];
Copy link
Collaborator

Choose a reason for hiding this comment

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

μœ„μ˜ μ½”λ“œλŠ” struct lq_detect_buffer 자체λ₯Ό copyν•˜λŠ” κ²ƒμ΄λ―€λ‘œ,
μ•„λž˜μ™€ 같이 pointer μ‚¬μš©ν•©μ‹œλ‹€.

struct lq_detect_buffer *ldb = &lqdetect.buffer[i];

Copy link
Author

Choose a reason for hiding this comment

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

λ°˜μ˜ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

lqdetect.c Outdated
}
*size = offset;
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ½”λ©˜νŠΈμž…λ‹ˆλ‹€.

  • hdrlen λ³€μˆ˜λ₯Ό λ”°λ‘œ λ‘˜ ν•„μš”κ°€ 없을 것 κ°™κ³ ,
  • bytes λŒ€μ‹ μ— length μš©μ–΄κ°€ λ‚˜μ€ 것 κ°™κ³ ,
  • size 좜λ ₯ μΈμžλŠ” 항상 값이 μ„€μ •λ˜λ©΄ μ’‹κ² μŒ.

거의 λ™μΌν•œ μ½”λ“œμ΄μ§€λ§Œ, μ•„λž˜ μ½”λ“œμ΄λ©΄ μ’‹κ² μŠ΅λ‹ˆλ‹€.

char *lqdetect_result_get(int *size)
{
    int offset = 0;
    int length = 32 * LQ_CMD_NUM; // header length
    char *str;

    pthread_mutex_lock(&lqdetect.lock);
    for (int i = 0; i < LQ_CMD_NUM; i++) {
        length += lqdetect.buffer[i].offset;
    }
    str = (char*)malloc(length);
    if (str != NULL) {
        for (int i = 0; i < LQ_CMD_NUM; i++) {
            struct lq_detect_buffer *ldb = &lqdetect.buffer[i];
            offset += snprintf(str + offset, length - offset, "%s : %u\n", command_str[i], ldb->ntotal);
            if (ldb->ntotal > 0) {
                offset += snprintf(str + offset, length - offset, "%s", ldb->data);
            }
        }
    }
    pthread_mutex_unlock(&lqdetect.lock);

    *size = offset;
    return str;
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

@namsic
μ°Έκ³  μ‚¬ν•­μœΌλ‘œ,
μ•„λž˜ buffer κ΅¬μ‘°μ—μ„œ keypos, keylen ν•„λ“œκ°€ μ—†λ‹€λ©΄,
ν•˜λ‚˜μ˜ buffer ꡬ쑰 ν¬κΈ°λŠ” 24 λ°”μ΄νŠΈμ΄λ©΄ μΆ©λΆ„ν•˜λ‹€.

struct lq_detect_buffer {
    char *data;
    uint32_t offset;
    uint32_t ntotal;
    uint32_t nsaved;
    uint32_t keypos[LQ_SAVE_CNT];
    uint32_t keylen[LQ_SAVE_CNT];
};

buffer 크기가 μž‘λ‹€λ©΄(or ν•„μš”ν•œ ν•„λ“œλ§Œ λ³΅μ‚¬ν•œλ‹€λ©΄),
lqdect locking μƒνƒœμ—μ„œ

  • LQ_CMD_NUM(10) 개의 bufferλ₯Ό κ·ΈλŒ€λ‘œ copyν•΄ 두고,
  • refcount μ¦κ°€μ‹œμΌœ λ‘” λ‹€μŒ

without locking μƒνƒœμ—μ„œ

  • 볡사해 λ‘” buffer 정보λ₯Ό 기반으둜 str λ³€μˆ˜λ₯Ό λΉŒλ“œν•œ ν›„

λ‹€μ‹œ lqdect locking μƒνƒœμ—μ„œ recount κ°μ†Œμ‹œν‚¨ 후에
str λ³€μˆ˜λ₯Ό 리턴할 수 μžˆλ‹€.

μ΄λ ‡κ²Œ μ΅œμ ν™”ν•  ν•„μš”λŠ” μ—†κ² μ£ ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

lock을 μž‘κ³ μžˆλŠ” μ‹œκ°„μ„ μ΅œμ†Œν™”ν•˜κΈ° μœ„ν•œ κ΄€μ μ—μ„œμ˜ μ΅œμ ν™”μΈκ°€μš”?

제 생각에 str λ³€μˆ˜λ₯Ό λΉŒλ“œν•˜λŠ” 것이 νŠΉλ³„νžˆ μ‹œκ°„μ„ 많이 ν•„μš”λ‘œ ν•˜λŠ” λ™μž‘μ€ μ•„λ‹Œ 것 κ°™μ•„μ„œ,
κ΅¬ν˜„μ„ λ‹¨μˆœν•˜κ²Œ κ°€μ Έκ°€λŠ” 편이 λ‚˜μ„ κ²ƒμœΌλ‘œ μƒκ°ν•©λ‹ˆλ‹€.

@cheesecrust cheesecrust force-pushed the lqdetect branch 2 times, most recently from 11c25ad to 7e9bde5 Compare October 21, 2024 00:47
refactor lqdetect show filed_t to char *
@jhpark816 jhpark816 merged commit 477fd2c into naver:develop Oct 21, 2024
1 check passed
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.

4 participants