void masterconn_serve(struct pollfd *pdesc) { double now; masterconn *eptr = masterconnsingleton; now = monotonic_seconds(); if (eptr->mode==CONNECTING) { if (eptr->sock>=0 && eptr->pdescpos>=0 && (pdesc[eptr->pdescpos].revents & (POLLOUT | POLLHUP | POLLERR))) { // FD_ISSET(eptr->sock,wset)) { masterconn_connecttest(eptr); } } else { if (eptr->pdescpos>=0) { if ((pdesc[eptr->pdescpos].revents & (POLLERR|POLLIN))==POLLIN && eptr->mode==DATA) { masterconn_read(eptr,now); } if (pdesc[eptr->pdescpos].revents & (POLLERR|POLLHUP)) { eptr->input_end = 1; } masterconn_parse(eptr); } if (eptr->mode==DATA && eptr->lastwrite+(Timeout/3.0)<now && eptr->outputhead==NULL) { masterconn_createpacket(eptr,ANTOAN_NOP,0); } if (eptr->pdescpos>=0) { if ((((pdesc[eptr->pdescpos].events & POLLOUT)==0 && (eptr->outputhead)) || (pdesc[eptr->pdescpos].revents & POLLOUT)) && eptr->mode==DATA) { masterconn_write(eptr,now); } } if (eptr->mode==DATA && eptr->lastread+Timeout<now) { eptr->mode = KILL; } } masterconn_disconnection_check(); }
void masterconn_download_next(masterconn *eptr) { uint8_t *ptr; uint8_t filenum; int64_t dltime; if (eptr->dloffset>=eptr->filesize) { // end of file filenum = eptr->downloading; if (masterconn_download_end(eptr)<0) { return; } dltime = main_utime()-eptr->dlstartuts; if (dltime<=0) { dltime=1; } syslog(LOG_NOTICE,"%s downloaded %"PRIu64"B/%"PRIu64".%06"PRIu32"s (%.3lf MB/s)",(filenum==1)?"metadata":(filenum==2)?"sessions":(filenum==11)?"changelog_0":(filenum==12)?"changelog_1":"???",eptr->filesize,dltime/1000000,(uint32_t)(dltime%1000000),(double)(eptr->filesize)/(double)(dltime)); if (filenum==1) { if (masterconn_metadata_check("metadata_ml.tmp")==0) { if (BackMetaCopies>0) { char metaname1[100],metaname2[100]; int i; for (i=BackMetaCopies-1 ; i>0 ; i--) { snprintf(metaname1,100,"metadata_ml.mfs.back.%"PRIu32,i+1); snprintf(metaname2,100,"metadata_ml.mfs.back.%"PRIu32,i); rename(metaname2,metaname1); } rename("metadata_ml.mfs.back","metadata_ml.mfs.back.1"); } if (rename("metadata_ml.tmp","metadata_ml.mfs.back")<0) { syslog(LOG_NOTICE,"can't rename downloaded metadata - do it manually before next download"); } } if (eptr->oldmode==0) { masterconn_download_init(eptr,11); } else { masterconn_download_init(eptr,2); } } else if (filenum==11) { if (rename("changelog_ml.tmp","changelog_ml_back.0.mfs")<0) { syslog(LOG_NOTICE,"can't rename downloaded changelog - do it manually before next download"); } masterconn_download_init(eptr,12); } else if (filenum==12) { if (rename("changelog_ml.tmp","changelog_ml_back.1.mfs")<0) { syslog(LOG_NOTICE,"can't rename downloaded changelog - do it manually before next download"); } masterconn_download_init(eptr,2); } else if (filenum==2) { if (rename("sessions_ml.tmp","sessions_ml.mfs")<0) { syslog(LOG_NOTICE,"can't rename downloaded sessions - do it manually before next download"); } } } else { // send request for next data packet ptr = masterconn_createpacket(eptr,MLTOMA_DOWNLOAD_DATA,12); put64bit(&ptr,eptr->dloffset); if (eptr->filesize-eptr->dloffset>META_DL_BLOCK) { put32bit(&ptr,META_DL_BLOCK); } else { put32bit(&ptr,eptr->filesize-eptr->dloffset); } } }
//confirm metalog trans ready void masterconn_changelog_ready(serventry *eptr) { uint8_t *ptr; ptr = masterconn_createpacket(eptr,SLATOMA_CHANGELOG_READY,0); if (ptr == NULL) { eptr->mode = KILL; MFSLOG(LOG_ERR,"can't createpacket"); } }
void masterconn_download_init(masterconn *eptr,uint8_t filenum) { uint8_t *ptr; // syslog(LOG_NOTICE,"download_init %d",filenum); if (eptr->mode==DATA && eptr->downloading==0) { // syslog(LOG_NOTICE,"sending packet"); ptr = masterconn_createpacket(eptr,ANTOMA_DOWNLOAD_START,1); put8bit(&ptr,filenum); eptr->downloading=filenum; } }
//confirm changelog replayed successful void masterconn_ack_changelog(serventry *eptr,int flag) { uint8_t *ptr; ptr = masterconn_createpacket(eptr,SLATOMA_ACK_CHANGELOG,1); if (ptr == NULL) { eptr->mode = KILL; return; } put8bit(&ptr,flag); }
void masterconn_download_next(serventry *eptr) { uint8_t *ptr; uint8_t filenum; uint64_t dltime; uint64_t starttime; if (eptr->dloffset>=eptr->filesize) { // end of file filenum = eptr->downloading; dltime = main_utime()-eptr->dlstartuts; MFSLOG(LOG_NOTICE,"%s downloaded %"PRIu64"B/%"PRIu64".%06"PRIu32"s (%.3lf MB/s)",(filenum==1)?"metadata":(filenum==2)?"changelog.0":"???",eptr->filesize,dltime/1000000,(uint32_t)(dltime%1000000),(double)(eptr->filesize)/(double)(dltime)); if (filenum==1) { if (rename("metadata.tmp","metadata.mfs")<0) { MFSLOG(LOG_NOTICE,"can't rename downloaded metadata - do it manually before next download"); } else { MFSLOG(LOG_NOTICE,"loading metadata ..."); fs_strinit(); chunk_strinit(); starttime = get_current_time(); if (fs_loadall(NULL)<0) { return; MFSLOG(LOG_NOTICE,"fs init failed"); } MFSLOG(LOG_NOTICE,"metadata file has been loaded"); } } else if (filenum==2) { if (rename("changelog.0.tmp","changelog.0.mfs")<0) { MFSLOG(LOG_NOTICE,"can't rename downloaded changelog.0.mfs - do it manually before next download"); } else { if (restore() < 0) { MFSLOG(LOG_ERR,"restore failed"); } else { meta_ready = 0; } } } //filenum:1,metadata finish ;2,changelog.0 finish if (masterconn_download_end(eptr,filenum)<0) { return; } } else { // send request for next data packet ptr = masterconn_createpacket(eptr,SLATOMA_DOWNLOAD_DATA,12); if (ptr==NULL) { eptr->mode=KILL; return; } put64bit(&ptr,eptr->dloffset); if (eptr->filesize-eptr->dloffset>META_DL_BLOCK) { put32bit(&ptr,META_DL_BLOCK); } else { put32bit(&ptr,eptr->filesize-eptr->dloffset); } } }
void masterconn_serve(struct pollfd *pdesc) { uint32_t now=main_time(); packetstruct *pptr,*paptr; masterconn *eptr = masterconnsingleton; if (eptr->pdescpos>=0 && (pdesc[eptr->pdescpos].revents & (POLLHUP | POLLERR))) { if (eptr->mode==CONNECTING) { masterconn_connecttest(eptr); } else { eptr->mode = KILL; } } if (eptr->mode==CONNECTING) { if (eptr->sock>=0 && eptr->pdescpos>=0 && (pdesc[eptr->pdescpos].revents & POLLOUT)) { // FD_ISSET(eptr->sock,wset)) { masterconn_connecttest(eptr); } } else { if (eptr->pdescpos>=0) { if ((eptr->mode==HEADER || eptr->mode==DATA) && (pdesc[eptr->pdescpos].revents & POLLIN)) { // FD_ISSET(eptr->sock,rset)) { eptr->lastread = now; masterconn_read(eptr); } if ((eptr->mode==HEADER || eptr->mode==DATA) && (pdesc[eptr->pdescpos].revents & POLLOUT)) { // FD_ISSET(eptr->sock,wset)) { eptr->lastwrite = now; masterconn_write(eptr); } if ((eptr->mode==HEADER || eptr->mode==DATA) && eptr->lastread+Timeout<now) { eptr->mode = KILL; } if ((eptr->mode==HEADER || eptr->mode==DATA) && eptr->lastwrite+(Timeout/3)<now && eptr->outputhead==NULL) { masterconn_createpacket(eptr,ANTOAN_NOP,0); } } } if (eptr->mode == KILL) { masterconn_beforeclose(eptr); tcpclose(eptr->sock); if (eptr->inputpacket.packet) { free(eptr->inputpacket.packet); } pptr = eptr->outputhead; while (pptr) { if (pptr->packet) { free(pptr->packet); } paptr = pptr; pptr = pptr->next; free(paptr); } eptr->mode = FREE; } }
int masterconn_download_end(masterconn *eptr) { eptr->downloading=0; masterconn_createpacket(eptr,ANTOMA_DOWNLOAD_END,0); if (eptr->metafd>=0) { if (close(eptr->metafd)<0) { mfs_errlog_silent(LOG_NOTICE,"error closing metafile"); eptr->metafd=-1; return -1; } eptr->metafd=-1; } return 0; }
void masterconn_sendregister(masterconn *eptr) { uint8_t *buff; eptr->downloading=0; eptr->metafd=-1; eptr->logfd=NULL; buff = masterconn_createpacket(eptr,MLTOMA_REGISTER,1+4+2); put8bit(&buff,1); put16bit(&buff,VERSMAJ); put8bit(&buff,VERSMID); put8bit(&buff,VERSMIN); put16bit(&buff,Timeout); }
void masterconn_download_init(serventry *eptr,uint8_t filenum) { uint8_t *ptr; MFSLOG(LOG_NOTICE,"download_init %d",filenum); if ((eptr->mode==HEADER || eptr->mode==DATA) && eptr->downloading==0) { MFSLOG(LOG_NOTICE,"sending packet"); ptr = masterconn_createpacket(eptr,SLATOMA_DOWNLOAD_START,1); if (ptr==NULL) { eptr->mode=KILL; return; } put8bit(&ptr,filenum); eptr->downloading=filenum; } }
void masterconn_sendregister(masterconn *eptr) { uint8_t *buff; eptr->downloading=0; eptr->metafd=-1; if (currentlogversion>0) { buff = masterconn_createpacket(eptr,MLTOMA_REGISTER,1+4+2+8); put8bit(&buff,2); put16bit(&buff,VERSMAJ); put8bit(&buff,VERSMID); put8bit(&buff,VERSMIN); put16bit(&buff,Timeout); put64bit(&buff,currentlogversion); } else { buff = masterconn_createpacket(eptr,MLTOMA_REGISTER,1+4+2); put8bit(&buff,1); put16bit(&buff,VERSMAJ); put8bit(&buff,VERSMID); put8bit(&buff,VERSMIN); put16bit(&buff,Timeout); } }
void masterconn_sendregister(serventry *eptr) { uint8_t *buff; eptr->downloading=0; eptr->metafd=-1; eptr->logfd=NULL; buff = masterconn_createpacket(eptr,SLATOMA_REGISTER,1+4+2); if (buff==NULL) { eptr->mode=KILL; return; } put8bit(&buff,1); put16bit(&buff,VERSMAJ); put8bit(&buff,VERSMID); put8bit(&buff,VERSMIN); put16bit(&buff,Timeout); }
int masterconn_download_end(serventry *eptr,uint8_t filenum) { uint8_t *buff; eptr->downloading=0; buff = masterconn_createpacket(eptr,SLATOMA_DOWNLOAD_END,1); if (buff==NULL) { eptr->mode=KILL; return -1; } put8bit(&buff,filenum); if (eptr->metafd>0) { if (close(eptr->metafd)<0) { MFSLOG(LOG_NOTICE,"error closing metafile: %m"); eptr->metafd=-1; return -1; } eptr->metafd=-1; } return 0; }
void masterconn_serve(int epoll_fd,int count,struct epoll_event *pdesc) { //uint32_t now=main_time(); serventry *weptr = NULL,*eptr = NULL; int ns; int ret; struct epoll_event ev; if(ismaster()) { return; } weptr = (serventry *)pdesc[count].data.ptr; if ((weptr->listen_sock == 1) && (pdesc[count].events & EPOLLIN) && (weptr->mode != KILL)) { ns = tcpaccept(lsock); if (ns<0) { MFSLOG(LOG_INFO,"master sync thread<->shadow master socket: accept error: %m"); } else { tcpnonblock(ns); tcpnodelay(ns); master_serve = (serventry *)malloc(sizeof(serventry)); eptr = master_serve; eptr->sock = ns; eptr->mode = HEADER; eptr->lastread = eptr->lastwrite = get_current_time(); eptr->inputpacket.next = NULL; eptr->inputpacket.bytesleft = 8; eptr->inputpacket.startptr = eptr->hdrbuff; eptr->inputpacket.packet = NULL; eptr->outputhead = NULL; eptr->outputtail = &(eptr->outputhead); eptr->downloading = 0; eptr->metafd=-1; eptr->logfd=NULL; eptr->listen_sock = 0; eptr->connection = 3; ev.data.ptr = eptr; ev.events = EPOLLIN; ret = epoll_ctl(epoll_fd,EPOLL_CTL_ADD,ns,&ev); if (ret < 0) { MFSLOG(LOG_NOTICE,"add epoll fail"); } } } if (weptr->listen_sock == 0) { if (weptr->mode == CONNECTING) { if (pdesc[count].events & (EPOLLHUP | EPOLLERR)) { masterconn_connecttest(weptr); } if (pdesc[count].events & EPOLLOUT) { masterconn_connecttest(weptr); } } else { if ((pdesc[count].events & (EPOLLHUP | EPOLLERR)) && (weptr->mode != KILL)) { MFSLOG(LOG_NOTICE, "set to NULL"); weptr->mode = KILL; } if ((weptr->mode==HEADER || weptr->mode==DATA) && (pdesc[count].events & EPOLLIN)) { // FD_ISSET(eptr->sock,rset)) { masterconn_read(weptr); weptr->lastread = get_current_time(); } if ((weptr->mode==HEADER || weptr->mode==DATA) && (pdesc[count].events & EPOLLOUT)) { // FD_ISSET(eptr->sock,wset)) { masterconn_write(weptr); weptr->lastwrite = get_current_time(); } if ((weptr->mode==HEADER || weptr->mode==DATA) && weptr->lastread+Timeout<get_current_time()) { MFSLOG(LOG_NOTICE, "set to NULL"); weptr->mode = KILL; } if ((weptr->mode==HEADER || weptr->mode==DATA) && weptr->lastwrite+5<get_current_time() && weptr->outputhead==NULL) { masterconn_createpacket(weptr,ANTOAN_NOP,0); } } } }