-
Notifications
You must be signed in to change notification settings - Fork 56
Bspark/bulk bop insert #235
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
Open
computerphilosopher
wants to merge
10
commits into
naver:develop
Choose a base branch
from
computerphilosopher:bspark/bulk_bop_insert
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
be69b0c
FEATURE: bulk command logging for bop insert
computerphilosopher 499bc4b
FEATURE: bulk command logging for all commmand
computerphilosopher 42d796a
fix warning
computerphilosopher 638e828
code tag
computerphilosopher 964e935
implement own lock
computerphilosopher b633ab1
str to enum
computerphilosopher 40b5b32
thread design
computerphilosopher f22e658
logger add
computerphilosopher 0a97164
change int32_t to int
computerphilosopher c9997e1
fix coding style, add cmd_in_second_stop feature
computerphilosopher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| #include "cmd_in_second.h" | ||
| #include "include/memcached/extension.h" | ||
| #include <string.h> | ||
| #include <stdint.h> | ||
| #include <assert.h> | ||
| #include <unistd.h> | ||
| #include <stdio.h> | ||
| #include <sys/uio.h> | ||
| #include <pthread.h> | ||
| #include <stdlib.h> | ||
| #include <fcntl.h> | ||
|
|
||
| #define LOG_PER_TIMER 500 | ||
| #define LOG_LENGTH 400 | ||
| #define IP_LENGTH 16 | ||
| #define KEY_LENGTH 256 | ||
|
|
||
| typedef enum cmd_in_second_state{ | ||
| NOT_STARTED, | ||
| ON_LOGGING, | ||
| ON_FLUSHING | ||
| }state; | ||
|
|
||
| typedef struct cmd_in_second_log{ | ||
| char key[KEY_LENGTH]; | ||
| char client_ip[IP_LENGTH]; | ||
| int32_t timer_idx; | ||
| }logtype; | ||
|
|
||
| typedef struct cmd_in_second_buffer { | ||
| logtype *ring; | ||
| int32_t front; | ||
| int32_t rear; | ||
| int32_t capacity; | ||
| }buffertype; | ||
|
|
||
| typedef struct cmd_in_second_timer{ | ||
| struct timeval* times; | ||
| int32_t size; | ||
| int32_t counter; | ||
| }timertype; | ||
|
|
||
| struct cmd_in_second { | ||
| char cmd[10]; | ||
| char collection_name[4]; | ||
| struct cmd_in_second_buffer buffer; | ||
| int32_t bulk_limit; | ||
| timertype timer; | ||
| state cur_state; | ||
| }; | ||
|
|
||
| static EXTENSION_LOGGER_DESCRIPTOR *mc_logger; | ||
| static struct cmd_in_second this; | ||
|
|
||
| static bool is_bulk_cmd() | ||
| { | ||
| const logtype* front = &this.buffer.ring[this.buffer.front]; | ||
| const logtype* rear = &this.buffer.ring[this.buffer.rear]; | ||
|
|
||
| struct timeval front_time = this.timer.times[front->timer_idx]; | ||
| struct timeval rear_time = this.timer.times[rear->timer_idx]; | ||
|
|
||
| return rear_time.tv_sec - front_time.tv_sec <= 1; | ||
| } | ||
|
|
||
| static void get_whole_cmd(char* whole_cmd) | ||
| { | ||
| if (strlen(this.collection_name)) { | ||
| sprintf(whole_cmd, "%s %s", this.collection_name, this.cmd); | ||
| return; | ||
| } | ||
| sprintf(whole_cmd, "%s", this.cmd); | ||
| } | ||
|
|
||
| static bool buffer_empty() | ||
| { | ||
| return this.buffer.front == this.buffer.rear; | ||
| } | ||
|
|
||
| static void* buffer_flush_thread() | ||
| { | ||
| int32_t fd = open("cmd_in_second.log", O_CREAT | O_WRONLY | O_TRUNC, 0644); | ||
|
|
||
| if (fd < 0) { | ||
| mc_logger->log(EXTENSION_LOG_WARNING, NULL, | ||
| "Can't open cmd_in_second log file: %s\n", "cmd_in_second.log"); | ||
| return NULL; | ||
| } | ||
|
|
||
| char whole_cmd[20] = ""; | ||
| get_whole_cmd(whole_cmd); | ||
|
|
||
| buffertype* buffer = &this.buffer; | ||
| timertype* timer = &this.timer; | ||
|
|
||
| int32_t timer_idx = -1; | ||
|
|
||
| char* log_str = (char*)malloc(LOG_LENGTH * this.bulk_limit * sizeof(char)); | ||
|
|
||
| if (log_str == NULL) { | ||
| mc_logger->log(EXTENSION_LOG_WARNING, NULL, "Can't allocate memory"); | ||
| return NULL; | ||
| } | ||
|
|
||
| int32_t expected_write_length = 0; | ||
|
|
||
| while (!buffer_empty()) { | ||
|
|
||
| logtype front = buffer->ring[buffer->front++]; | ||
|
|
||
| char time_str[50] = ""; | ||
|
|
||
| if (front.timer_idx != timer_idx) { | ||
|
|
||
| const struct timeval* front_time = &timer->times[front.timer_idx]; | ||
| const struct tm *lt = localtime((time_t*)&front_time->tv_sec); | ||
|
|
||
| sprintf(time_str, "%04d-%02d-%02d %02d:%02d:%02d.%06d\n", lt ->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, (int32_t)front_time->tv_usec); | ||
| timer_idx = front.timer_idx; | ||
|
|
||
| expected_write_length += 27; | ||
| } | ||
| char log[LOG_LENGTH] = ""; | ||
| sprintf(log, "%s%s %s %s\n", time_str, whole_cmd, front.key, front.client_ip); | ||
| strncat(log_str, log, LOG_LENGTH); | ||
|
|
||
| expected_write_length += LOG_LENGTH; | ||
| } | ||
|
|
||
| if (write(fd, log_str, expected_write_length) != expected_write_length) { | ||
| mc_logger->log(EXTENSION_LOG_WARNING, NULL, "write length is difference to expectation."); | ||
| } | ||
|
|
||
| close(fd); | ||
|
|
||
| free(log_str); | ||
|
|
||
| free(this.timer.times); | ||
| this.timer.times = NULL; | ||
|
|
||
| free(this.buffer.ring); | ||
| this.buffer.ring = NULL; | ||
|
|
||
| this.cur_state = NOT_STARTED; | ||
|
|
||
| return NULL; | ||
| } | ||
|
|
||
| static int32_t buffer_flush() | ||
| { | ||
|
|
||
| this.cur_state = ON_FLUSHING; | ||
|
|
||
| int32_t ret = 0; | ||
| pthread_t tid; | ||
| pthread_attr_t attr; | ||
|
|
||
| if (pthread_attr_init(&attr) != 0 || | ||
computerphilosopher marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0 || | ||
| (ret = pthread_create(&tid, &attr, buffer_flush_thread, NULL)) != 0) | ||
| { | ||
| mc_logger->log(EXTENSION_LOG_WARNING, NULL, | ||
| "Can't create buffer flush thread: %s\n", strerror(ret)); | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static void buffer_add(const logtype* log) | ||
| { | ||
|
|
||
| struct cmd_in_second_buffer* buffer = &this.buffer; | ||
|
|
||
| const bool buffer_full = (buffer->rear+1) % buffer->capacity == buffer->front; | ||
|
|
||
| if (buffer_full) { | ||
| if (is_bulk_cmd()) { | ||
| buffer_flush(); | ||
| return; | ||
| } | ||
| buffer->front = (buffer->front+1) % buffer->capacity; | ||
| } | ||
|
|
||
| buffer->ring[buffer->rear] = *log; | ||
| buffer->rear = (buffer->rear+1) % buffer->capacity; | ||
| } | ||
|
|
||
| static bool is_cmd_to_log(const char* collection_name, const char* cmd) | ||
| { | ||
| return strcmp(this.collection_name, collection_name) == 0 && strcmp(this.cmd, cmd) == 0; | ||
| } | ||
|
|
||
| bool cmd_in_second_write(const char* collection_name, const char* cmd, const char* key, const char* client_ip) | ||
| { | ||
| if (this.cur_state != ON_LOGGING || !is_cmd_to_log(collection_name, cmd)) { | ||
| return false; | ||
| } | ||
|
|
||
| timertype *timer = &this.timer; | ||
|
|
||
| logtype log = {"", "", 0}; | ||
| snprintf(log.client_ip, IP_LENGTH, "%s", client_ip); | ||
| snprintf(log.key, KEY_LENGTH, "%s", key); | ||
|
|
||
| if (timer->counter == 0) { | ||
| timer->size++; | ||
| gettimeofday(&timer->times[timer->size-1], NULL); | ||
| } | ||
|
|
||
| log.timer_idx = timer->size-1; | ||
|
|
||
| buffer_add(&log); | ||
|
||
| timer->counter = (timer->counter+1) % LOG_PER_TIMER; | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| int32_t cmd_in_second_start(const char* collection_name, const char* cmd, const int32_t bulk_limit) | ||
| { | ||
|
|
||
| if (this.cur_state != NOT_STARTED) { | ||
| return CMD_IN_SECOND_STARTED_ALREADY; | ||
| } | ||
|
|
||
| this.bulk_limit = bulk_limit; | ||
|
|
||
| this.buffer.capacity = bulk_limit+1; | ||
| this.buffer.front = 0; | ||
| this.buffer.rear = 0; | ||
| this.buffer.ring = (logtype*)malloc(this.buffer.capacity * sizeof(logtype)); | ||
|
|
||
| if (this.buffer.ring == NULL) { | ||
| return CMD_IN_SECOND_NO_MEM; | ||
| } | ||
|
|
||
| this.timer.size = 0; | ||
| this.timer.counter = 0; | ||
| this.timer.times = (struct timeval*)malloc(bulk_limit * sizeof(struct timeval)); | ||
computerphilosopher marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (this.timer.times == NULL) { | ||
| free(this.buffer.ring); | ||
| return CMD_IN_SECOND_NO_MEM; | ||
| } | ||
|
|
||
| sprintf(this.collection_name, "%s", collection_name); | ||
| sprintf(this.cmd, "%s", cmd); | ||
|
|
||
| this.cur_state = ON_LOGGING; | ||
|
|
||
| return CMD_IN_SECOND_START; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #ifndef __CMD_IN_SECOND_LOG__ | ||
| #define __CMD_IN_SECOND_LOG__ | ||
| #endif | ||
|
|
||
| #include <stdbool.h> | ||
| #include <stdint.h> | ||
| #include <sys/time.h> | ||
|
|
||
| #define CMD_IN_SECOND_START 0 | ||
| #define CMD_IN_SECOND_STARTED_ALREADY 1 | ||
| #define CMD_IN_SECOND_NO_MEM 2 | ||
|
|
||
| int32_t cmd_in_second_start(const char* collection_name, const char* cmd, const int32_t bulk_limit); | ||
| bool cmd_in_second_write(const char* collection_name, const char* cmd, const char* key, const char* client_ip); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.