spx_private void ydb_storage_syncquery_nio_body_reader(int fd, struct spx_job_context *jc){/*{{{*/ spx_nio_reader_body_handler(fd,jc); if(ENOENT == jc->err){ SpxLog1(jc->log,SpxLogWarn,\ "not find out the sync storages."); goto r1; } if(0 != jc->err){ SpxLog2(jc->log,SpxLogError,jc->err,\ "recv the regedit response is fail."); goto r1; } struct spx_msg *ctx = jc->reader_body_ctx; if(NULL == ctx){ SpxLog2(jc->log,SpxLogError,jc->err,\ "no recved the body ctx."); goto r1; } struct ydb_storage_slave *s = NULL; size_t unit_size = YDB_MACHINEID_LEN + SpxIpv4Size + SpxI32Size + SpxI32Size; size_t numbs = jc->reader_header->bodylen / unit_size; size_t i = 0; for( ; i < numbs; i++){ string_t machineid = spx_msg_unpack_string(ctx,YDB_MACHINEID_LEN,&(jc->err)); string_t ip = spx_msg_unpack_string(ctx,SpxIpv4Size,&(jc->err)); i32_t port = spx_msg_unpack_i32(ctx); u32_t status = spx_msg_unpack_u32(ctx); spx_map_get(g_ydb_storage_slaves,machineid,spx_string_len(machineid),\ (void **) &s,NULL); if(NULL == s){ s = spx_alloc_alone(sizeof(*s),&(jc->err)); if(NULL == s){ continue; } s->state = status; s->machineid = machineid; s->host.port = port; s->host.ip = ip; spx_map_insert(g_ydb_storage_slaves,machineid,spx_string_len(machineid), s,sizeof(s)); } if(port != s->host.port){ s->host.port = port; } if(0 != spx_string_casecmp_string(machineid,s->machineid)){ spx_string_free(s->machineid); s->machineid = machineid; } s->state = status; } r1: spx_job_context_clear(jc); }/*}}}*/
struct IdcreatorServerContextPool *idcreatorServerContextPoolNew(SpxLogDelegate *log,\ void *c, size_t size, IdcreatorWatcherDelegate *inHandler, IdcreatorWatcherDelegate *outHandler, err_t *err){/*{{{*/ if(0 == size){ *err = EINVAL; } struct IdcreatorServerContextPool *pool = NULL; pool = spx_alloc_alone(sizeof(*pool),err); if(NULL == pool){ return NULL; } struct IdcreatorServerContextTransport arg; SpxZero(arg); arg.log = log; arg.c = c; arg.inHandler = inHandler; arg.outHandler = outHandler; pool->pool = spx_fixed_vector_new(log,size,\ idcreatorServerContextNew,\ &arg,\ idcreatorServerContextFree,\ err); if(NULL == pool->pool){ SpxFree(pool); return NULL; } return pool; }/*}}}*/
spx_private err_t ydb_storage_query_remote(SpxLogDelegate *log, struct ydb_tracker *t,string_t groupname, string_t machineid,string_t syncgroup){/*{{{*/ err_t err = 0; struct spx_job_context *sjc = t->sjc; int fd = spx_socket_new(&err); if(0 >= fd){ SpxLog2(log,SpxLogError,err,"create query sync socket fd is fail."); return err; } if(0 != (err = spx_set_nb(fd))){ SpxLog2(log,SpxLogError,err,"set socket nonblacking is fail."); goto r1; } if(0 != (err = spx_socket_set(fd,SpxKeepAlive,SpxAliveTimeout,\ SpxDetectTimes,SpxDetectTimeout,\ SpxLinger,SpxLingerTimeout,\ SpxNodelay,\ true,30))){ SpxLog2(log,SpxLogError,err,"set socket operator is fail."); goto r1; } if(0 != (err = spx_socket_connect(fd,t->host.ip,t->host.port))){ SpxLogFmt2(log,SpxLogError,err,\ "conntect to tracker:%s:%d is fail.",\ t->host.ip,t->host.port); goto r1; } struct spx_msg_header *writer_header = NULL; writer_header = spx_alloc_alone(sizeof(*writer_header),&err); if(NULL == writer_header){ SpxLog2(log,SpxLogError,err,\ "alloc writer header is fail."); goto r1; } sjc->writer_header = writer_header; writer_header->version = YDB_VERSION; writer_header->protocol = YDB_QUERY_SYNC_STORAGES; writer_header->bodylen = YDB_GROUPNAME_LEN + YDB_MACHINEID_LEN \ + YDB_SYNCGROUP_LEN ; struct spx_msg *ctx = spx_msg_new(writer_header->bodylen,&err); if(NULL == ctx){ SpxLog2(log,SpxLogError,err,\ "alloc writer body is fail."); goto r1; } sjc->writer_body_ctx = ctx; spx_msg_pack_fixed_string(ctx,groupname,YDB_GROUPNAME_LEN); spx_msg_pack_fixed_string(ctx,machineid,YDB_MACHINEID_LEN); spx_msg_pack_fixed_string(ctx,syncgroup,YDB_SYNCGROUP_LEN); sjc->fd = fd; spx_nio_regedit_writer(sloop,sjc->fd,sjc); return err; r1: SpxClose(fd); return err; }/*}}}*/
spx_private void *spx_nio_thread_context_new(size_t idx,void *arg,err_t *err){ struct spx_thread_context_node *tcn = (struct spx_thread_context_node *)arg; struct spx_nio_thread_context *context = spx_alloc_alone(sizeof(*context),err); if(NULL == context){ SpxLog2(tcn->log,SpxLogError,*err,\ "alloc nio thread context is fail."); return NULL; } context->loop = ev_loop_new(EVFLAG_AUTO); context->log = tcn->log; context->idx = idx; context->thread_notify_handle = tcn->thread_notify; if(-1 == pipe(context->pipe)){ SpxLog2(tcn->log,SpxLogError,*err,\ "open the nio thread pips is fail."); *err = errno; SpxFree(context); return NULL; } if((0 != (*err = spx_set_nb(context->pipe[0]))) \ ||(0 != (*err = spx_set_nb(context->pipe[0])))){ SpxLog2(tcn->log,SpxLogError,*err,\ "set pipe noblacking is fail."); SpxFree(context); return NULL; } return context; }
void *logdb_tracker_config_before_handle(SpxLogDelegate *log,err_t *err){ struct logdb_tracker_configurtion *config = (struct logdb_tracker_configurtion *) \ spx_alloc_alone(sizeof(*config),err); if(NULL == config){ SpxLog2(log,SpxLogError,*err,\ "alloc the tracker config is fail."); return NULL; } config->log = log; config->port = 1404; config->timeout = 30; config->waitting = 10; config->trytimes = 3; config->logsize = 10 * SpxMB; config->loglevel = SpxLogInfo; config->balance = YDB_TRACKER_BALANCE_LOOP; config->heartbeat = 35; config->daemon = true; config->stacksize = 128 * SpxKB; config->network_module_thread_size = 8; // config->notifier_module_thread_size = 4; config->task_module_thread_size = 4; config->context_size = 64; return config; }
struct spx_msg *spx_msg_new(const size_t len,err_t *err){/*{{{*/ if (0 == len){ *err = EINVAL; return NULL; } struct spx_msg *ctx = spx_alloc_alone(sizeof(*ctx),err); if(NULL == ctx){ return NULL; } ctx->buf = spx_alloc_alone(len ,err); if(NULL == ctx->buf){ goto r1; } ctx->last = ctx->buf; ctx->s = len; ctx->busylen = 0; return ctx; r1: SpxFree(ctx); return NULL; }/*}}}*/
struct spx_thread_context *spx_thread_context_new_alone(SpxLogDelegate *log,err_t *err){ struct spx_thread_context *tc = (struct spx_thread_context *) \ spx_alloc_alone(sizeof(*tc),err); if(NULL == tc){ SpxLog2(log,SpxLogError,*err,\ "alloc thread context is fail."); return NULL; } // tc->loop = ev_loop_new(0); tc->log = log; return tc; }
spx_private void *idcreatorServerContextNew(size_t idx,void *arg,err_t *err){/*{{{*/ SpxTypeConvert2(struct IdcreatorServerContextTransport,isct,arg); struct IdcreatorServerContext *isc = NULL; isc = spx_alloc_alone(sizeof(*isc),err); if(NULL == isc){ return NULL; } isc->log = isct->log; isc->idx = idx; isc->c = isct->c; isc->inHandler = isct->inHandler; isc->outHandler = isct->outHandler; isc->inlen = SpxMsgHeaderSize + sizeof(i32_t); isc->outlen = SpxMsgHeaderSize + sizeof(u64_t); return isc; }/*}}}*/
spx_private void *spx_receiver_new(size_t idx,void *arg,err_t *err){/*{{{*/ struct spx_recvive_context_transport *rct = (struct spx_recvive_context_transport *) arg; if(NULL == rct){ *err = EINVAL; return NULL; } struct spx_receive_context *t = (struct spx_receive_context *) \ spx_alloc_alone(sizeof(*t),err); if(NULL == t){ SpxLog2(rct->log,SpxLogError,*err,\ "alloc trigger context is fail."); return NULL; } t->log = rct->log; t->idx = idx; t->receive_handler = rct->recviver; return t; }/*}}}*/
struct spx_msg_header *spx_msg_to_header(struct spx_msg *ctx,err_t *err){/*{{{*/ struct spx_msg_header *header = NULL; if(NULL == ctx){ *err = EINVAL; return NULL; } header = spx_alloc_alone(sizeof(*header),err); if(NULL == header){ return NULL; } header->version = spx_msg_unpack_u32(ctx); header->protocol = spx_msg_unpack_u32(ctx); header->bodylen = spx_msg_unpack_u64(ctx); header->offset = spx_msg_unpack_u64(ctx); header->is_keepalive = spx_msg_unpack_bool(ctx); header->err = spx_msg_unpack_u32(ctx); return header; }/*}}}*/
spx_private void *spx_thread_context_new(size_t idx,void *arg,err_t *err){/*{{{*/ SpxLogDelegate *log = (SpxLogDelegate *) arg; struct spx_thread_context *tc = (struct spx_thread_context *) \ spx_alloc_alone(sizeof(*tc),err); if(NULL == tc){ SpxLog2(log,SpxLogError,*err,\ "alloc thread context is fail."); return NULL; } tc->idx = idx; tc->loop = ev_loop_new(0); tc->log = log; if(-1 == pipe(tc->pipe)){ *err = errno; ev_loop_destroy(tc->loop); SpxFree(tc); return NULL; } spx_set_nb(tc->pipe[0]); spx_set_nb(tc->pipe[1]); return tc; }/*}}}*/
int main(int argc,char **argv) { err_t rc = 0; SpxLogDelegate *log = spx_log; struct spx_skiplist *spl = NULL; if(0 != (rc = spx_skiplist_new(spx_log,\ SPX_SKIPLIST_IDX_I32,15,false,\ spx_skiplist_i32_default_cmper,\ NULL,\ spx_skiplist_i32_default_printf,\ skiplist_kfree,\ skiplist_vfree,\ &spl))) { SpxLog2(log,SpxLogError,rc,\ "create skiplist is fail;"); return 0; } int i = 0; for(i = 1; i< 100; i++) { if(0 == i % 10) { i32_t *k = NULL; if(0 != (rc = spx_alloc_alone(sizeof(i32_t),(void **)&k))) { SpxLog2(log,SpxLogError,rc,\ "alloc for key is fail."); return 0; } *k = i; i32_t *v = NULL; if(0 != (rc = spx_alloc_alone(sizeof(i32_t),(void **)&v))) { SpxLog2(log,SpxLogError,rc,\ "alloc for value is fail."); return 0; } *v = *k; if(0 != (rc = spx_skiplist_insert(spl,k,sizeof(*k),v,sizeof(*v),-1))) { SpxLog2(log,SpxLogError,rc,\ "insert key to skiplist is fail."); } } } spx_skiplist_printf(spl); for(i = 0; i< 100; i++) { if(0 == i % 11) { i32_t *k = NULL; if(0 != (rc = spx_alloc_alone(sizeof(i32_t),(void **)&k))) { SpxLog2(log,SpxLogError,rc,\ "alloc for key is fail."); return 0; } *k = i; i32_t *v = NULL; if(0 != (rc = spx_alloc_alone(sizeof(i32_t),(void **)&v))) { SpxLog2(log,SpxLogError,rc,\ "alloc for value is fail."); return 0; } *v = *k; if(0 != (rc = spx_skiplist_insert(spl,k,sizeof(*k),v,sizeof(*v),-1))) { SpxLog2(log,SpxLogError,rc,\ "insert key to skiplist is fail."); } } } spx_skiplist_printf(spl); for(i = 0; i< 100; i++) { if(0 == i % 11) { if(0 != (rc = spx_skiplist_delete(spl,&i,sizeof(i)))) { SpxLog2(log,SpxLogError,rc,\ "delete the key:50 is fail."); } } } i32_t delk = 0; if(0 != (rc = spx_skiplist_delete(spl,&delk,sizeof(delk)))) { SpxLog2(log,SpxLogError,rc,\ "delete the key:50 is fail."); } spx_skiplist_printf(spl); /* delk = 10; if(0 != (rc = spx_skiplist_delete(spl,&delk,sizeof(delk)))){ SpxLog2(log,SpxLogError,rc,\ "delete the key:50 is fail."); } spx_skiplist_printf(spl); */ for(i = 0; i< 100; i++) { if(0 == i % 9) { i32_t *v = NULL; u64_t vl = 0; if(0 != (rc = spx_skiplist_get_and_move(spl,(void *)&i,(u32_t) sizeof(i),\ (void **) &v,&vl,\ skiplist_range_cmper))) { SpxLog2(log,SpxLogError,rc,\ "get and move key:9 is fail."); return 0; } } spx_skiplist_printf(spl); } return 0; }
struct spx_module_context *spx_module_new(\ SpxLogDelegate *log,\ u32_t threadsize,\ size_t stack_size, SpxReceiveTriggerDelegate *receive_handler, err_t *err){/*{{{*/ struct spx_module_context *mc = (struct spx_module_context *)\ spx_alloc_alone(sizeof(*mc),err); if(NULL == mc){ SpxLog2(log,SpxLogError,*err,\ "alloc module context is fail."); return NULL; } mc->log = log; mc->threadpool = spx_list_init(log,\ threadsize,\ spx_thread_context_new,\ log,\ spx_thread_context_free,\ err); if(NULL == mc->threadpool){ SpxLog2(log,SpxLogError,*err,\ "alloc threadpool for module is fail."); goto r2; } struct spx_recvive_context_transport rct; SpxZero(rct); rct.log = log; rct.event = EV_READ; rct.recviver = receive_handler;; mc->receive_triggers = spx_list_init(log,\ threadsize,\ spx_receiver_new,\ &rct,\ spx_receiver_free,\ err); if(NULL == mc->receive_triggers){ SpxLog2(log,SpxLogError,*err,\ "alloc receive triggers are fail."); goto r2; } pthread_attr_t attr; pthread_attr_init(&attr); size_t ostack_size = 0; pthread_attr_getstacksize(&attr, &ostack_size); do{ if (ostack_size != stack_size && (0 != (*err = pthread_attr_setstacksize(&attr,stack_size)))){ SpxLog2(log,SpxLogError,*err,\ "set thread stack size is fail."); goto r1; } u32_t i = 0; struct spx_thread_pending_transport *tpt; for( ; i < threadsize; i++){ tpt = spx_alloc_alone(sizeof(*tpt),err); tpt->mc = mc; tpt->idx = i; struct spx_thread_context *n = spx_list_get(mc->threadpool,i); if (0 !=(*err = pthread_create(&(n->tid), &attr, spx_thread_listening, tpt))){ SpxLog2(log,SpxLogError,*err,\ "create nio thread is fail."); goto r1; } } }while(false); pthread_attr_destroy(&attr); return mc; r1: pthread_attr_destroy(&attr); r2: spx_module_free(&mc); return NULL; }/*}}}*/
pthread_t ydb_storage_syncquery_init( SpxLogDelegate *log, u32_t timeout, struct ydb_storage_configurtion *config,\ err_t *err){/*{{{*/ struct spx_job_context_transport arg; SpxZero(arg); arg.timeout = timeout; arg.nio_reader = spx_nio_reader; arg.nio_writer = spx_nio_writer; arg.log = log; arg.reader_body_process = ydb_storage_syncquery_nio_body_reader; arg.writer_body_process = ydb_storage_syncquery_nio_body_writer; arg.config = config; sync_timer = spx_alloc_alone(sizeof(*sync_timer),err); if(NULL == sync_timer){ SpxLog2(log,SpxLogError,*err,"alloc sync timer is fail."); goto r1; } sloop = ev_loop_new(0); if(NULL == sloop){ *err = errno; SpxLog2(log,SpxLogError,*err,\ "new event loop for sync is fail."); goto r1; } if(!ydb_storage_get_remote(config,&arg)){ goto r1; } pthread_t tid = 0; pthread_attr_t attr; pthread_attr_init(&attr); size_t ostack_size = 0; pthread_attr_getstacksize(&attr, &ostack_size); do{ if (ostack_size != config->stacksize && (0 != (*err = pthread_attr_setstacksize(&attr,config->stacksize)))){ pthread_attr_destroy(&attr); SpxLog2(log,SpxLogError,*err,\ "set thread stack size is fail."); goto r1; } if (0 !=(*err = pthread_create(&(tid), &attr, ydb_storage_syncquery_heartbeat, NULL))){ pthread_attr_destroy(&attr); SpxLog2(log,SpxLogError,*err,\ "create heartbeat thread is fail."); goto r1; } }while(false); pthread_attr_destroy(&attr); return tid; r1: if(NULL != sync_timer){ SpxFree(sync_timer); } return 0; }/*}}}*/
err_t ydb_storage_dio_delete(struct ev_loop *loop,\ struct ydb_storage_dio_context *dc){/*{{{*/ err_t err = 0; struct spx_task_context *tc = dc->tc; struct spx_job_context *jc = dc->jc; struct ydb_storage_configurtion *c = jc->config; struct spx_msg *ctx = jc->reader_body_ctx; size_t len = jc->reader_header->bodylen; dc->rfid = spx_msg_unpack_string(ctx,len,&(err)); if(NULL == dc->rfid){ SpxLogFmt2(c->log,SpxLogError,err,\ "unpack the fid from client:%s is fail.", jc->client_ip); goto r1; } if(0 != ( err = ydb_storage_dio_parser_fileid(jc->log,dc->rfid, &(dc->groupname),&(dc->machineid),&(dc->syncgroup), &(dc->issinglefile),&(dc->mp_idx),&(dc->p1), &(dc->p2),&(dc->tidx),&(dc->file_createtime), &(dc->rand),&(dc->begin),&(dc->realsize), &(dc->totalsize),&(dc->ver),&(dc->opver), &(dc->lastmodifytime),&(dc->hashcode), &(dc->has_suffix),&(dc->suffix)))){ SpxLogFmt2(dc->log,SpxLogError,err,\ "parser fid:%s from client:%s is fail.", dc->rfid,jc->client_ip); goto r1; } dc->filename = ydb_storage_dio_make_filename(dc->log,dc->issinglefile, c->mountpoints, dc->mp_idx, dc->p1,dc->p2, dc->machineid,dc->tidx,dc->file_createtime, dc->rand,dc->suffix,&err); if(NULL == dc->filename){ SpxLogFmt2(dc->log,SpxLogError,err,\ "make filename with fid:%s from client:%s is fail.", dc->rfid,jc->client_ip); goto r1; } if(!SpxFileExist(dc->filename)) { SpxLogFmt1(dc->log,SpxLogWarn,\ "deleting-file:%s is not exist.request from:%s.", dc->filename,jc->client_ip); goto r1; } if(dc->issinglefile){ if(0 != remove(dc->filename)){ err = errno; SpxLogFmt2(dc->log,SpxLogError,err,\ "delete file :%s is fail.request from:%s.", dc->filename,jc->client_ip); } YdbStorageBinlogDeleteWriter(dc->rfid); struct ydb_storage_mountpoint *mp = spx_list_get(c->mountpoints, dc->mp_idx); mp->last_modify_time = spx_now(); goto r1; } else { spx_dio_regedit_async(&(dc->async), ydb_storage_dio_do_delete_form_chunkfile,dc); ev_async_start(loop,&(dc->async)); ev_async_send(loop,&(dc->async)); } return err; r1: spx_task_pool_push(g_spx_task_pool,tc); ydb_storage_dio_pool_push(g_ydb_storage_dio_pool,dc); jc->writer_header = (struct spx_msg_header *) spx_alloc_alone(sizeof(*(jc->writer_header)),&err); if(NULL == jc->writer_header){ SpxLog2(dc->log,SpxLogError,err,\ "new response header is fail." "no notify client and push jc force."); spx_job_pool_push(g_spx_job_pool,jc); return err; } jc->writer_header->protocol = jc->reader_header->protocol; jc->writer_header->bodylen = 0; jc->writer_header->version = YDB_VERSION; jc->writer_header->err = err; jc->err = err; jc->moore = SpxNioMooreResponse; size_t idx = spx_network_module_wakeup_idx(jc); struct spx_thread_context *threadcontext = spx_get_thread(g_spx_network_module,idx); jc->tc = threadcontext; SpxModuleDispatch(spx_network_module_wakeup_handler,jc); return 0; }/*}}}*/
void ydb_storage_dio_do_delete_form_chunkfile( struct ev_loop *loop,ev_async *w,int revents){/*{{{*/ ev_async_stop(loop,w); err_t err = 0; struct ydb_storage_dio_context *dc = (struct ydb_storage_dio_context *) w->data; struct spx_task_context *tc = dc->tc; struct spx_job_context *jc = dc->jc; struct ydb_storage_configurtion *c = jc->config; if(0 != (err = ydb_storage_dio_delete_context_from_chunkfile( c,dc->filename,dc->begin,dc->totalsize, dc->opver,dc->ver,dc->lastmodifytime, dc->realsize,spx_now()))){ SpxLogFmt2(dc->log,SpxLogError,err, "delete context begin:%lld,realsize:%lld,totalsize:%lld " "form chunkfile:%s is fail.", dc->begin,dc->realsize,dc->totalsize, dc->filename); goto r1; } YdbStorageBinlogDeleteWriter(dc->rfid); struct ydb_storage_mountpoint *mp = spx_list_get(c->mountpoints, dc->mp_idx); mp->last_modify_time = spx_now(); struct spx_msg_header *wh = (struct spx_msg_header *) \ spx_alloc_alone(sizeof(*wh),&err); if(NULL == wh){ SpxLogFmt2(dc->log,SpxLogError,err, "delete context begin:%lld,realsize:%lld,totalsize:%lld " "form chunkfile:%s is success bug new response header is fail.", dc->begin,dc->realsize,dc->totalsize, dc->filename); goto r1; } jc->writer_header = wh; wh->version = YDB_VERSION; wh->protocol = YDB_C2S_DELETE; wh->offset = 0; wh->bodylen = 0; err = 0; goto r2; r1: jc->writer_header = (struct spx_msg_header *) spx_alloc_alone(sizeof(*(jc->writer_header)),&err); if(NULL == jc->writer_header){ SpxLog2(dc->log,SpxLogError,err,\ "new response header is fail." "no notify client and push jc force."); spx_task_pool_push(g_spx_task_pool,tc); ydb_storage_dio_pool_push(g_ydb_storage_dio_pool,dc); spx_job_pool_push(g_spx_job_pool,jc); return; } jc->writer_header->protocol = jc->reader_header->protocol; jc->writer_header->bodylen = 0; jc->writer_header->version = YDB_VERSION; jc->writer_header->err = err; r2: spx_task_pool_push(g_spx_task_pool,tc); ydb_storage_dio_pool_push(g_ydb_storage_dio_pool,dc); jc->err = err; jc->moore = SpxNioMooreResponse; size_t idx = spx_network_module_wakeup_idx(jc); struct spx_thread_context *threadcontext = spx_get_thread(g_spx_network_module,idx); jc->tc = threadcontext; SpxModuleDispatch(spx_network_module_wakeup_handler,jc); return; }/*}}}*/