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 a memcached_fetch function #337

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 9 additions & 53 deletions libmemcached/fetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,73 +63,29 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
if (not error)
error= &unused;


unlikely (ptr->flags.use_udp)
{
if (value_length)
*value_length= 0;

if (key_length)
*key_length= 0;

if (flags)
*flags= 0;

if (key)
*key= 0;

*error= MEMCACHED_NOT_SUPPORTED;
return NULL;
}

result_buffer= memcached_fetch_result(ptr, result_buffer, error);
if (result_buffer == NULL or memcached_failed(*error))
if (result_buffer == NULL || result_buffer->key_length > MEMCACHED_MAX_KEY)
Copy link
Contributor

Choose a reason for hiding this comment

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

result_buffer != NULL์ด์ง€๋งŒ, *error ๊ฐ’์ด failture์ธ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค๋Š” ๊ฒƒ์ด์ฃ ?

memcached_fetch_result() ํ•จ์ˆ˜์—์„œ
result_buffer != NULL์ด๋ฉด์„œ result_buffer->key_length > MEMCACHED_MAX_KEY ๊ฒฝ์šฐ
MEMCACHED_KEY_TOO_BIG ์—๋Ÿฌ ์„ค์ •ํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋ ค๋Š” ์˜๋„๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด ์•„๋‹Œ์ง€ ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

์˜๋„๊นŒ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜ memcached_fetch_result์—์„œ MEMCACHED_MAX_KEY๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ˜•ํƒœ๊ฐ€ ๋” ์ž์—ฐ์Šค๋Ÿฌ์šด ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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
๋…ผ๋ฆฌ์ ์œผ๋กœ ๋ด์„œ,
result_buffer->key_length > MEMCACHED_MAX_KEY ์ธ ๊ฒฝ์šฐ ์˜ค๋ฅ˜๋ฅผ ๋ฆฌํ„ดํ•  ํ•„์š”๋Š” ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค.

ํŠนํžˆ๋‚˜, memcached_fetch()์—์„œ๋Š” key์— ๋Œ€ํ•ด ๊ด€์‹ฌ์ด ์—†๊ณ  ๋‹จ์ง€ value๋งŒ ์กฐํšŒํ•˜๋Š” ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ์กฐ๊ฑด ๊ฒ€์‚ฌ๊ฐ€ ๋“ค์–ด๊ฐ„ ์ด์œ ๋ฅผ ๊ฐ„๋‹จํžˆ ํ™•์ธํ•ด ๋ณผ ์ˆ˜ ์žˆ์„๊นŒ์š”?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@jhpark816

libmemcached ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•œ ๊ฒฐ๊ณผ, ๋”ฐ๋กœ ์ด์œ ๋ฅผ ์ ์–ด ๋†“์ง€๋Š” ์•Š์•˜์Šต๋‹ˆ๋‹ค.
awesomized/libmemcached#7e592f0

We now check return key size for memcached_fetch() to make sure it is not
too big (compared to original ascii server). result_st is now initialized 
differently. This should make it a bit faster.

ํ•˜์ง€๋งŒ result_buffer->key_length > MEMCACHED_MAX_KEY ๋น„๊ต๋ฅผ memcached_fetch๋‚˜ memcached_fetch_result์—์„œ ํ•˜๋Š” ๊ฒƒ์€ ๋‹ค์†Œ ๋ถ€์ž์—ฐ์Šค๋Ÿฌ์›Œ ๋ณด์ž…๋‹ˆ๋‹ค.

result_buffer ๊ตฌ์กฐ์ฒด ๋‚ด์˜ item_key ๋ณ€์ˆ˜๊ฐ€ ์ด๋ฏธ MEMCACHED_MAX_KEY ํฌ๊ธฐ๋กœ ๊ณ ์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
ํ‚ค๋ฅผ ์ €์žฅํ•  ๋•Œ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์ด ๋” ์ ํ•ฉํ•˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.

struct memcached_result_st {
  ...
  size_t key_length;
  ...
  char item_key[MEMCACHED_MAX_KEY];
  ...
};

{
WATCHPOINT_ASSERT(result_buffer == NULL);
if (value_length)
*value_length= 0;

if (key_length)
*key_length= 0;

if (flags)
*flags= 0;
*error= (result_buffer != NULL) ? MEMCACHED_KEY_TOO_BIG : *error;

if (key)
*key= 0;
if (key) *key= 0;
if (key_length) *key_length= 0;
if (value_length) *value_length= 0;
if (flags) *flags= 0;

return NULL;
}

if (value_length)
{
*value_length= memcached_string_length(&result_buffer->value);
}

if (key)
{
if (result_buffer->key_length > MEMCACHED_MAX_KEY)
{
*error= MEMCACHED_KEY_TOO_BIG;
if (value_length)
*value_length= 0;

if (key_length)
*key_length= 0;

if (flags)
*flags= 0;

if (key)
*key= 0;

return NULL;
}
strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
if (key_length)
*key_length= result_buffer->key_length;
}

if (value_length)
*value_length= memcached_string_length(&result_buffer->value);

if (flags)
*flags= result_buffer->item_flags;

Expand Down
Loading