11#include "cmd_in_second.h"
2- #include "include/memcached/extension.h"
32#include <string.h>
43#include <stdint.h>
54#include <assert.h>
@@ -45,7 +44,7 @@ typedef struct cmd_in_second_timer {
4544typedef struct cmd_in_second_flush_thread {
4645 pthread_t tid ;
4746 pthread_attr_t attr ;
48- pthread_cond_t cond ;
47+ pthread_cond_t cond ;
4948 pthread_mutex_t lock ;
5049 bool sleep ;
5150} flush_thread ;
@@ -59,11 +58,11 @@ struct cmd_in_second {
5958 int32_t log_per_timer ;
6059 state cur_state ;
6160 flush_thread flusher ;
62- EXTENSION_LOGGER_DESCRIPTOR * mc_logger ;
6361 pthread_mutex_t lock ;
6462};
6563
6664static struct cmd_in_second this ;
65+ static EXTENSION_LOGGER_DESCRIPTOR * mc_logger ;
6766
6867static bool is_bulk_cmd ()
6968{
@@ -115,10 +114,8 @@ static void put_flusher_to_sleep() {
115114}
116115
117116static void wake_flusher_up (){
118- printf ("in\n" );
119117 pthread_mutex_lock (& this .flusher .lock );
120118 if (this .flusher .sleep ) {
121- printf ("signaled\n" );
122119 pthread_cond_signal (& this .flusher .cond );
123120 }
124121 pthread_mutex_unlock (& this .flusher .lock );
@@ -130,7 +127,8 @@ static void* flush_buffer()
130127
131128 while (1 ) {
132129 if (fd < 0 ) {
133- perror ("Can't open cmd_in_second log file: cmd_in_second.log" );
130+ mc_logger -> log (EXTENSION_LOG_WARNING , NULL ,
131+ "Can't open cmd_in_second.log" );
134132 break ;
135133 }
136134
@@ -169,7 +167,8 @@ static void* flush_buffer()
169167 timer -> front = (timer -> front + 1 ) % timer -> capacity ;
170168
171169 if (lt == NULL ) {
172- perror ("localtime failed" );
170+ mc_logger -> log (EXTENSION_LOG_WARNING , NULL ,
171+ "localtime failed" );
173172 continue ;
174173 }
175174
@@ -187,11 +186,12 @@ static void* flush_buffer()
187186 }
188187
189188 const int written_length = write (fd , log_str , expected_write_length );
190- printf ("%s" , log_str );
191189 free (log_str );
192190
193191 if (written_length != expected_write_length ) {
194- perror ("write length is difference to expectation." );
192+ printf ("log\n" );
193+ mc_logger -> log (EXTENSION_LOG_WARNING , NULL ,
194+ "write length is difference to expectation" );
195195 break ;
196196 }
197197
@@ -201,7 +201,7 @@ static void* flush_buffer()
201201 if (fd >= 0 ) {
202202 close (fd );
203203 }
204-
204+
205205 free (this .timer .ring );
206206 this .timer .ring = NULL ;
207207
@@ -215,24 +215,18 @@ static void* flush_buffer()
215215
216216static void buffer_add (const logtype * log )
217217{
218- printf ("add ready\n" );
219218 struct cmd_in_second_buffer * const buffer = & this .buffer ;
220219
221220 buffer -> ring [buffer -> rear ] = * log ;
222221 buffer -> rear = (buffer -> rear + 1 ) % buffer -> capacity ;
223- printf ("enqueued\n" );
224222
225223 if (buffer_full ()) {
226- printf ("buffer full\n" );
227224 if (is_bulk_cmd ()) {
228- printf ("bulk_cmd\n" );
229225 this .cur_state = ON_FLUSHING ;
230226 wake_flusher_up ();
231- printf ("flush woked up\n" );
232227 return ;
233228 }
234229 buffer -> front = (buffer -> front + 1 ) % buffer -> capacity ;
235- printf ("buffer popped\n" );
236230 }
237231}
238232
@@ -262,11 +256,9 @@ static bool is_cmd_to_log(const int operation)
262256bool cmd_in_second_write (const int operation , const char * key , const char * client_ip )
263257{
264258 pthread_mutex_lock (& this .lock );
265- printf ("locked %s %d \n" , key , this .cur_state );
266259
267260 if (this .cur_state != ON_LOGGING || !is_cmd_to_log (operation )) {
268261 pthread_mutex_unlock (& this .lock );
269- printf ("already locked\n" );
270262 return false;
271263 }
272264
@@ -279,18 +271,16 @@ bool cmd_in_second_write(const int operation, const char* key, const char* clien
279271 }
280272
281273 buffer_add (& log );
282- printf ("buffer add finished\n" );
283274 this .timer .circular_counter = (this .timer .circular_counter + 1 ) % this .log_per_timer ;
284275
285276 pthread_mutex_unlock (& this .lock );
286- printf ("unlocked\n" );
287277 return true;
288278}
289279
290280/* TODO mc_logger */
291- void cmd_in_second_init (EXTENSION_LOGGER_DESCRIPTOR * mc_logger )
281+ void cmd_in_second_init (EXTENSION_LOGGER_DESCRIPTOR * global_logger )
292282{
293- this . mc_logger = mc_logger ;
283+ mc_logger = global_logger ;
294284 this .cur_state = NOT_STARTED ;
295285
296286 this .buffer .front = 0 ;
@@ -308,7 +298,7 @@ void cmd_in_second_init(EXTENSION_LOGGER_DESCRIPTOR *mc_logger)
308298
309299 flush_thread * const flusher = & this .flusher ;
310300 pthread_attr_init (& flusher -> attr );
311- pthread_mutex_init (& flusher -> lock , NULL );
301+ pthread_mutex_init (& flusher -> lock , NULL );
312302 pthread_cond_init (& flusher -> cond , NULL );
313303}
314304
@@ -328,7 +318,7 @@ int cmd_in_second_start(const int operation, const char cmd_str[], const int bul
328318
329319 if (pthread_attr_init (& flusher -> attr ) != 0 ||
330320 pthread_attr_setdetachstate (& flusher -> attr , PTHREAD_CREATE_DETACHED ) != 0 ||
331- (thread_created = pthread_create (& flusher -> tid , & flusher -> attr ,
321+ (thread_created = pthread_create (& flusher -> tid , & flusher -> attr ,
332322 flush_buffer , NULL )) != 0 )
333323 {
334324 return CMD_IN_SECOND_THREAD_FAILED ;
@@ -358,7 +348,6 @@ int cmd_in_second_start(const int operation, const char cmd_str[], const int bul
358348
359349 this .cur_state = ON_LOGGING ;
360350
361- printf ("log start\n" );
362351 pthread_mutex_unlock (& this .lock );
363352
364353 return CMD_IN_SECOND_START ;
0 commit comments