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 the memcached_io_read function #341

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

ing-eoking
Copy link
Collaborator

๐Ÿ”— Related Issue

โŒจ๏ธ What I did

  • ๋ณ„๋„์˜ PR๋กœ ๋‘๋Š” ํŽธ์ด ๋” ์ ํ•ฉํ•  ๊ฒƒ ๊ฐ™์•„ ๋”ฐ๋กœ ์ž‘์—…ํ–ˆ์Šต๋‹ˆ๋‹ค.
  • memcached_io_read ๋กœ์ง์„ ๋ฆฌํŒฉํ† ๋งํ–ˆ์Šต๋‹ˆ๋‹ค.
    • buffer ์ธ์ž๊ฐ€ NULL์ธ ๊ฒฝ์šฐ, read buffer์—์„œ length๋งŒํผ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฒ„๋ฆฌ๋„๋ก ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • memcached_io_read๋Š” ๋‚ด๋ถ€ ํ•จ์ˆ˜์ด๊ธฐ์— ์กฐ์‹ฌํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ์—†์„ ๊ฒƒ์œผ๋กœ ์ƒ๊ฐ๋˜์ง€๋งŒ, ํ˜น์‹œ ๋ชจ๋ฅผ ๊ฒฝ์šฐ์— ๋Œ€๋น„ํ•ด nread๊ฐ€ NULL์ธ ๊ฒฝ์šฐ์— unused_nread๋ฅผ ํ™œ์šฉํ•˜๋„๋ก ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.
    • ๊ธฐ์กด ๋กœ์ง์—์„œ๋Š” length๊ฐ€ 1์ธ ๊ฒฝ์šฐ์™€ 1์„ ์ดˆ๊ณผํ•œ ๊ฒฝ์šฐ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฒ˜๋ฆฌํ–ˆ๋Š”๋ฐ, memcpy ๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ  ์„ฑ๋Šฅ ์ƒ ํฐ ๋ฌธ์ œ๋Š” ์—†์„ ๊ฒƒ์œผ๋กœ ํŒ๋‹จํ•˜์—ฌ PR๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

@jhpark816
Copy link
Contributor

memcached_safe_read() ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ memcached_io_read() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•œ ๊ฒƒ์ธ๊ฐ€์š”?

@ing-eoking
Copy link
Collaborator Author

memcached_safe_read() ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ memcached_io_read() ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ•œ ๊ฒƒ์ธ๊ฐ€์š”?

๋„ค. io_read์—์„œ ์ˆ˜์ •์„ ํ•˜๋ฉด, ๋ณ„๋„์˜ buffer๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์•„ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ˜„์žฌ CI test(ubuntu)๋ฅผ ์‹คํŒจํ•˜์—ฌ ์ž ์‹œ ์ˆ˜์ •ํ•  ๋•Œ๊นŒ์ง€ draft๋กœ ๋ณ€๊ฒฝํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

@ing-eoking
Copy link
Collaborator Author

@jhpark816

์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

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

๋ฆฌ๋ทฐ ์™„๋ฃŒ

@@ -538,8 +538,12 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
return MEMCACHED_CONNECTION_FAILURE;
}

ssize_t unused_nread;
if (nread == NULL) nread= &unused_nread;
Copy link
Contributor

Choose a reason for hiding this comment

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

๊ธฐ์กด ์ฝ”๋“œ์—์„œ
nread == NULL ๊ฒ€์‚ฌ ์—†์ด *nread= xxx; ์ฝ”๋“œ๊ฐ€ ์กด์žฌํ–ˆ๋‹ค๋Š” ๊ฒƒ์€
nread๊ฐ€ NULL์ธ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ, memcached_io_read() ํ˜ธ์ถœํ•˜๋Š” ์ชฝ์˜ ๋ณ€๊ฒฝ์ด ์—†๋Š” ์ƒํ™ฉ์—์„œ
nread == NULL ์กฐ๊ฑด์„ ๊ฒ€์‚ฌํ•  ํ•„์š”๋Š” ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค.

libmemcached/io.cc Show resolved Hide resolved
@ing-eoking ing-eoking force-pushed the resp branch 4 times, most recently from 6405f01 to fbe9fc1 Compare January 22, 2025 01:10
Copy link
Contributor

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

๋ฆฌ๋ทฐ ์™„๋ฃŒ

libmemcached/io.cc Show resolved Hide resolved
Copy link
Contributor

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

๋ฆฌ๋ทฐ ์™„๋ฃŒ

@@ -539,6 +539,7 @@ memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
}

char *buffer_ptr= static_cast<char *>(buffer);
*nread= 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

0์œผ๋กœ ์„ค์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

if (buffer_ptr) {
memcpy(buffer_ptr, ptr->read_ptr, difference);
buffer_ptr+= difference;
}

length-= difference;
Copy link
Contributor

Choose a reason for hiding this comment

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

length ๋ณ€๊ฒฝ์€ ์•„๋ž˜๋กœ ๋‚ด๋ ค์ฃผ์„ธ์š”.

์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์ˆ˜์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

์–ด๋””์„œ ์ˆ˜์ •๋˜์—ˆ๋‚˜์š”?


offset+= (size_t) nread;
Copy link
Contributor

Choose a reason for hiding this comment

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

์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ฌด์—‡์ธ๊ฐ€์š”?

Copy link
Collaborator Author

@ing-eoking ing-eoking Jan 22, 2025

Choose a reason for hiding this comment

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

@jhpark816
ํ˜„์žฌ ๋ณ€๊ฒฝ ๋ฐฉ์‹์—์„œ buffer(data) ์ธ์ž๊ฐ€ NULL์ธ ๊ฒฝ์šฐ, nread ๊ฐ’์€ 0์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๊ธฐ์กด safe_read๋Š” nread ๊ฒฐ๊ณผ ๊ฐ’์œผ๋กœ๋ถ€ํ„ฐ offset์„ ๊ณ„์‚ฐํ–ˆ์œผ๋ฏ€๋กœ buffer๊ฐ€ NULL์ธ ์ƒํ™ฉ์—์„œ
offset ๊ฐ’์ด ์ฆ๊ฐ€ํ•˜์ง€ ์•Š์•„ ๋ฌดํ•œ๋ฃจํ”„์— ๋น ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ฏธ memcached_io_read์—์„œ while ๋ฌธ์„ ํ†ตํ•ด ์ง€์ •๋œ ๊ธธ์ด๋งŒํผ ๋ชจ๋‘ ์ฝ๋Š” ๊ฒƒ์ด ๋ณด์žฅ๋˜๋ฏ€๋กœ, memcached_safe_read์—์„œ๋Š” while (offset < size) ๋ฌธ์„ ๋‘˜ ํ•„์š”๊ฐ€ ์—†์–ด ์ œ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ing-eoking

ํ˜„์žฌ ๋ณ€๊ฒฝ ๋ฐฉ์‹์—์„œ buffer(data) ์ธ์ž๊ฐ€ NULL์ธ ๊ฒฝ์šฐ, nread ๊ฐ’์€ 0์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์–ด๋–ค ์ฝ”๋“œ ๋•Œ๋ฌธ์— nread ๊ฐ’์ด 0์ด ๋˜๋‚˜์š”?

Copy link
Collaborator Author

@ing-eoking ing-eoking Jan 22, 2025

Choose a reason for hiding this comment

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

์ˆ˜์ • ๋‚ด์šฉ์ด ๋ฐ˜์˜๋˜์ง€ ์•Š์•„ ๋‹ค์‹œ ์˜ฌ๋ ธ์Šต๋‹ˆ๋‹ค.

์–ด๋–ค ์ฝ”๋“œ ๋•Œ๋ฌธ์— nread ๊ฐ’์ด 0์ด ๋˜๋‚˜์š”?

*nread = (ssize_t)(buffer_ptr - (char*)buffer); ์— ์˜ํ•˜์—ฌ 0์ด ๋ฉ๋‹ˆ๋‹ค.

Copy link
Contributor

@jhpark816 jhpark816 Jan 22, 2025

Choose a reason for hiding this comment

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

nread ์ •๋ณด๋Š” ์ •ํ™•ํžˆ ์ „๋‹ฌ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ, ์•ž์„œ PR์—์„œ nread์—๋‹ค ๋งค๋ฒˆ ์ฝ์€ ํฌ๊ธฐ๋ฅผ ๋”ํ•œ ๊ฒƒ์ด๊ตฐ์š”.

nread์—๋‹ค ๊ณ„์† ๋”ํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค,
ํ˜„์žฌ length ๊ฐ’์„ ๊ฐ์†Œ์‹œํ‚ค๊ณ  ์žˆ์œผ๋‹ˆ,

  • request = length; ๊ฐ’์„ ๊ฐ€์ง€๋Š” request ๋ณ€์ˆ˜๋ฅผ ๋‘๊ณ ,
  • ๋‚˜์ค‘์— *nread = request - length; ํ˜•ํƒœ๋กœ ์„ค์ •ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ณ€์ˆ˜ ๋ช…์ด ์กฐ๊ธˆ ์–ด์ƒ‰ํ•˜๋„ค์š”.
length๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ์ž”์—ฌํฌ๊ธฐ ์˜๋ฏธ์˜ ๋ณ€์ˆ˜๋ฅผ ๋‘์–ด readํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ์†Œ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ด์— ๋Œ€ํ•ด ์˜๊ฒฌ ์ฃผ์„ธ์š”.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋ณ€์ˆ˜ ๋ช…์ด ์กฐ๊ธˆ ์–ด์ƒ‰ํ•˜๋„ค์š”.
length๋ฅผ ๊ทธ๋Œ€๋กœ ๋‘๊ณ , ์ž”์—ฌํฌ๊ธฐ ์˜๋ฏธ์˜ ๋ณ€์ˆ˜๋ฅผ ๋‘์–ด readํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ์†Œ์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋‚˜์„ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

์ €๋Š” remain ๋˜๋Š” left๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์ž์—ฐ์Šค๋Ÿฌ์šธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

remains ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์‹œ์ฃ .

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

remain์œผ๋กœ ๋จผ์ € ๋ฐ”๊ฟ”์„œ remains๋กœ ์žฌ๋ณ€๊ฒฝํ•˜์—ฌ ๋‹ค์‹œ push ํ–ˆ์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

nread ๊ฐ’์„ ๋ณ€๊ฒฝํ–ˆ์œผ๋‹ˆ,
memcached_safe_read() ๋ณ€๊ฒฝ์€ ์—†์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

buffer๊ฐ€ NULL์ผ ๊ฒฝ์šฐ ์ž˜๋ชป๋œ ๊ณ„์‚ฐ์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์ผ๋ถ€๋Š” ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

@ing-eoking ing-eoking force-pushed the resp branch 3 times, most recently from 7047c10 to 9dc7b0d Compare January 22, 2025 05:49
Copy link
Contributor

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

๋ฆฌ๋ทฐ ์™„๋ฃŒ

if (buffer_ptr) {
memcpy(buffer_ptr, ptr->read_ptr, difference);
buffer_ptr+= difference;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ empty line์€ ์ œ๊ฑฐํ•ด ์ฃผ์‹œ๊ณ ์š”.

if (buffer_ptr) {
*buffer_ptr= *ptr->read_ptr;
buffer_ptr++;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

์•„๋ž˜์—์„œ remains--; ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ptr->read_ptr++;
ptr->read_buffer_length--;
buffer_ptr++;
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

reamins--; ์ถ”๊ฐ€๋˜๋ฉด, break ๋ฌธ์€ ์—†์–ด๋„ ๋ฉ๋‹ˆ๋‹ค.

ptr->read_ptr+= difference;
ptr->read_buffer_length-= difference;
buffer_ptr+= difference;
remains-= difference;
Copy link
Contributor

Choose a reason for hiding this comment

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

remains > 1 ๊ฒฝ์šฐ๊ฐ€ remains == 1์ธ ๊ฒฝ์šฐ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š” ๋ฐ,
remains > 1 ๊ฒฝ์šฐ์˜ ์ฝ”๋“œ๋งŒ ์žˆ์–ด๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

remains > 1์ธ ๊ฒฝ์šฐ์™€ remains == 1์„ ๊ตฌ๋ถ„ํ•˜๊ณ  ์žˆ๋Š” ์ด์œ ๊ฐ€ remains > 1์ธ ๊ฒฝ์šฐ memcpy๋ฅผ ์“ฐ๊ณ , remains == 1์ธ ๊ฒฝ์šฐ ๋“ฑํ˜ธ ์—ฐ์‚ฐ์ž(=)๋ฅผ ์“ฐ๋ ค๋Š” ์˜๋„๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.

๋ชจ๋‘ memcpy๋กœ ํ†ต์ผํ• ๊นŒ์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์šฐ์„  ํ•ด๋‹น ๋ถ€๋ถ„์€ ๊ทธ๋Œ€๋กœ ๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋จธ์ง€ ์ˆ˜์ •๋˜์—‡์Šต๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค.
remains == 1์ธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์„ ์ˆ˜ ์žˆ๋‚˜์š”?
๋งŽ์ง€ ์•Š๋‹ค๋ฉด, ์•„๋ž˜ ์ฝ”๋“œ๋งŒ์œผ๋กœ ์ถฉ๋ถ„ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

      size_t difference= (remains > ptr->read_buffer_length) ? ptr->read_buffer_length : remains;
      if (buffer_ptr) {
        memcpy(buffer_ptr, ptr->read_ptr, difference);
        buffer_ptr+= difference;
      }
      ptr->read_ptr+= difference;
      ptr->read_buffer_length-= difference;
      remains-= difference;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

๋‚ด๋ถ€ ์ฝ”๋“œ์—์„œ length๋ฅผ 1๋กœ ๋‘๊ณ  ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. memcached_io_readline:
    • read_buffer๊ฐ€ ๋น„์–ด ์žˆ์„ ๊ฒฝ์šฐ, memcached_io์˜ length ์ธ์ž์— 1์„ ๋„˜๊ฒจ read_buffer๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค.
  2. fetch_value_header:
    • ์ปฌ๋ ‰์…˜ ์š”์†Œ์˜ bkey, eflag ๋“ฑ์„ ์ฝ์„ ๋•Œ, ํ•˜๋‚˜์”ฉ ์ฝ์–ด๋“ค์ด๋ฉฐ, space, \r, \n์ด ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

Copy link
Contributor

Choose a reason for hiding this comment

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

์ฝ์–ด์•ผ ํ•  ๊ธธ์ด๋Š” ๋ชจ๋ฅด๊ณ ,
๋‹จ์ง€ ํŠน์ • ๋ฌธ์ž(\n)๊ฐ€ ๋‚˜์˜ฌ ๋–„๊นŒ์ง€ ์ฝ๊ธฐ ์œ„ํ•จ์ด๊ตฐ์š”.

๊ทธ๋Ÿฌ๋ฉด, remains == 1 ์ธ ๊ฒฝ์šฐ์˜ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜์‹œ์ฃ .

@jhpark816
Copy link
Contributor

local์—์„œ ํ•œ๋ฒˆ ์ˆ˜์ •ํ•  ๋•Œ ์ „์ฒด๋ฅผ ๋‹ค ์ˆ˜์ •ํ•ด ์ฃผ์‹œ๊ณ ,
๋ฌธ์ œ ์—†๋Š” ์ง€๋ฅผ ํ™•์ธํ•œ ํ›„์— github์— ์˜ฌ๋ ค์ฃผ์„ธ์š”.

Copy link
Contributor

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

๋ฆฌ๋ทฐ ์™„๋ฃŒ

@jhpark816 jhpark816 merged commit 6804888 into naver:develop Jan 22, 2025
1 check passed
@ing-eoking ing-eoking deleted the resp branch January 22, 2025 08:22
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