//Set new max_user_connections parameter void update_user_limit(char *user_name, unsigned int limit, MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; char sql_buffer[_DBGOVERNOR_BUFFER_8192]; char user_name_alloc[USERNAMEMAXLEN * 2]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); (*_mysql_real_escape_string)(mysql_do_command, user_name_alloc, user_name, strlen(user_name)); snprintf(sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_USER_CONN_LIMIT, (int) limit, user_name_alloc); if (db_mysql_exec_query(sql_buffer, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't execute sql request. Restriction aborted", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode,"Can't execute sql request. Restriction aborted", sql_buffer); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); flush_user_priv(debug_mode); }
int zeromq_term(struct zeromq_pool* zpool){ int err = ERR_OK; WRITE_FMT_LOG(LOG_DEBUG, "%p", zpool); if ( !zpool ) return ERR_BAD_ARG; /*free array*/ if ( zpool->sockf_array ){ for ( int i=0; i < zpool->count_max; i++ ){ free(zpool->sockf_array[i].tempbuf); } free(zpool->sockf_array), zpool->sockf_array = NULL; } /*destroy zmq context*/ if (zpool->context){ WRITE_LOG(LOG_ERR, "zmq_term trying...\n");fflush(0); int err= zmq_term(zpool->context); if ( err != 0 ){ err = ERR_ERROR; WRITE_FMT_LOG(LOG_ERR, "zmq_term err %d, errno %d errtext %s\n", err, zmq_errno(), zmq_strerror(zmq_errno())); } else{ err= ERR_OK; zpool->context = NULL; WRITE_FMT_LOG(LOG_ERR, "zmq_term ok, sent %u ; recv %u bytes\n", __bytes_sent, __bytes_recv); } } else{ WRITE_LOG(LOG_ERR, "context error NULL"); err = ERR_ERROR; } return err; }
int MAMain() { WRITE_LOG("MAMain() 1\n"); on_exit(exit_status_save, NULL); // switch stdout from console to maWriteLog. WRITE_LOG("open_maWriteLog\n"); int wlfd = open_maWriteLog(); dup2(wlfd, 1); dup2(wlfd, 2); close(wlfd); WRITE_LOG("setup_filesystem\n"); setup_filesystem(); chdir("/"); WRITE_LOG("setup_stdin\n"); setup_stdin(); if(!stdin) { printf("Error opening stdin: %i (%m)\n", errno); exit(1); } printf("MAMain()\n"); install_stdmalloc_hooks(); int res = main(gArgc, gArgv); printf("main returned. exit(%i)\n", res); exit(res); }
//LVE CONNECTION request void lve_connection(char *user_name, MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; char sql_buffer[_DBGOVERNOR_BUFFER_8192]; char user_name_alloc[USERNAMEMAXLEN * 2]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); (*_mysql_real_escape_string)(mysql_do_command, user_name_alloc, user_name, strlen(user_name)); snprintf(sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_LVE_USER_CONNECTION, user_name_alloc); if (db_mysql_exec_query(sql_buffer, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't execute sql request. LVE connection", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, "Can't execute sql request. LVE connection", sql_buffer); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); }
/* \fn Click \brief Click link to run once, only work in Stopped Link(Trigger-outside mode). \return bool */ virtual bool Click(bool isSync) { if (triggerInside) return false; bool rtVal = true; if (!isSync) { if (m_section.try_lock()) { rtVal = BThread::Click(); m_section.unlock(); } else { rtVal = false; WRITE_LOG(severity_level::warning, ""); } } else { if (m_section.try_lock()) { Task(); rtVal = true; m_section.unlock(); } else { rtVal = false; WRITE_LOG(severity_level::warning, ""); } } return rtVal; }
/* \fn Set \brief Set running link \param[in] char* setting as json string \return bool */ virtual bool Set(char* setting, long settingLen) { try { string settingStr = setting; stringstream stream; stream << settingStr; property_tree::ptree pRoot; read_json(stream, pRoot); if (m_section.try_lock()) { strcpy_s(name, sizeof(name), pRoot.get<string>("name").data()); frameTimes = pRoot.get<long>("frameTimes"); frameTime = pRoot.get<long>("frameTime"); includeTaskTime = pRoot.get<bool>("includeTaskTime"); readAndUpdate = pRoot.get<int>("readAndUpdate"); readBlocked = pRoot.get<bool>("readBlocked"); writeAndClear = pRoot.get<int>("writeAndClear"); writeBlocked = pRoot.get<bool>("writeBlocked"); m_section.unlock(); } else { WRITE_LOG(severity_level::warning, ""); } } catch (...) { WRITE_LOG(severity_level::error, ""); return false; } return true; }
int close_sockf(struct zeromq_pool* zpool, struct sock_file_t *sockf){ WRITE_FMT_LOG(LOG_DEBUG, "%p, %p", zpool, sockf); int err = ERR_OK; if ( !zpool || !sockf ) return ERR_BAD_ARG; WRITE_FMT_LOG(LOG_DEBUG, "fd=%d", sockf->fs_fd); int zmq_sock_is_used_twice = 0; for (int i=0; i < zpool->count_max; i++){ if ( !zpool->sockf_array[i].unused && zpool->sockf_array[i].netw_socket == sockf->netw_socket ) zmq_sock_is_used_twice++; } if ( zmq_sock_is_used_twice > 1 ){ if ( sockf->tempbuf ){ free(sockf->tempbuf->buf), sockf->tempbuf->buf=NULL; free(sockf->tempbuf), sockf->tempbuf = NULL; } WRITE_LOG(LOG_DEBUG, "dual direction zmq socket should be closed later"); }else if( sockf->netw_socket ){ WRITE_LOG(LOG_DEBUG, "zmq socket closing..."); int err = zmq_close( sockf->netw_socket ); sockf->netw_socket = NULL; WRITE_FMT_LOG(LOG_DEBUG, "zmq_close status err %d, errno %d, status %s\n", err, zmq_errno(), zmq_strerror(zmq_errno())); } if ( ERR_OK == err ) return remove_sockf_from_array_by_fd(zpool, sockf->fs_fd ); else return err; }
void account_analyze(gpointer * key, Account * ac, void *data) { stats_limit_cfg cfg_buf; stats_limit_cfg *sl = config_get_account_limit(ac->id, &cfg_buf); int restrict_period = 0; char tmp_buf[_DBGOVERNOR_BUFFER_8192]; struct governor_config data_cfg; get_config_data( &data_cfg ); if(data_cfg.all_lve || !data_cfg.use_lve) return; if (sl->mode != IGNORE_MODE) { if (0 == ac->timeout) { //I am not restricted, but I might be in penalty period. Restrict account if need if (check_restrict(ac)) { //If restricted, just exit return; } } time_t now; time(&now); if (ac->start_count > 0) { //check if account need to unrestrict if (0 != ac->timeout && ac->start_count + ac->timeout <= now) { ac->timeout = 0; time(&ac->start_count); ac->info.field_restrict = NO_PERIOD; ac->info.field_level_restrict = NORESTRICT_PARAM; if (!check_restrict(ac)) account_unrestrict(ac); else { sprintf(tmp_buf, "No unrestrict yet for %s %d %d\n", ac->id, ac->timeout, ac->start_count); WRITE_LOG( NULL, 1, tmp_buf, _DBGOVERNOR_BUFFER_8192, tmp_buf, data_cfg.log_mode); } //} else if (ac->start_count + data_cfg.timeout <= now) { } else if (((ac->start_count + data_cfg.timeout) <= now) && (!ac->timeout)) { //Check if penalty period ended ac->start_count = 0; ac->restricted = 0; ac->info.field_restrict = NO_PERIOD; ac->info.field_level_restrict = NORESTRICT_PARAM2; ac->timeout = 0; sprintf(tmp_buf, "Penalty period is over %s %d %d\n", ac->id, ac->timeout, ac->start_count); WRITE_LOG( NULL, 1, tmp_buf, _DBGOVERNOR_BUFFER_8192, tmp_buf, data_cfg.log_mode); } } } }
void send_commands(Command * cmd, void *data) { char buffer[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); if (cmd) { switch (cmd->command) { case FREEZE: { if (data_cfg.use_lve) { if (add_user_to_list(cmd->username, data_cfg.all_lve) < 0) { if (data_cfg.log_mode == DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't add user to BAD list %s", data_cfg.log_mode, cmd->username); } } else { if(data_cfg.max_user_connections) { update_user_limit_no_flush(cmd->username, (unsigned int) data_cfg.max_user_connections, data_cfg.log_mode); is_any_flush = 1; } } } /*else update_user_limit(cmd->username, (unsigned int) -1, data_cfg.log_mode); if(data_cfg.killuser==1) kill_connection(cmd->username, data_cfg.log_mode);*/ //lve_connection(cmd->username, data_cfg.log_mode); if( data_cfg.logqueries_use == 1 ) log_user_queries( cmd->username, data_cfg.log_mode ); } break; case UNFREEZE: { if (data_cfg.use_lve) { if (delete_user_from_list(cmd->username) < 0) { if(data_cfg.log_mode == DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't delete user form BAD list %s", data_cfg.log_mode, cmd->username); } } if(data_cfg.max_user_connections) { update_user_limit_no_flush(cmd->username, (unsigned int) 0, data_cfg.log_mode); is_any_flush = 1; } //kill_connection(cmd->username, data_cfg.log_mode); } else { if(data_cfg.max_user_connections) { update_user_limit_no_flush(cmd->username, 0, data_cfg.log_mode); is_any_flush = 1; } } } break; } } }
int check_mysql_version(MODE_TYPE debug_mode) { MYSQL_RES *res; MYSQL_ROW row; unsigned long *lengths; char buffer[_DBGOVERNOR_BUFFER_2048], outbuffer[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); if (mysql_send_governor != NULL) { if (db_mysql_exec_query(QUERY_GET_SERVER_INFO, mysql_send_governor, debug_mode)) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Get mysql vesrion request failed", data_cfg.log_mode); return 0; } res = (*_mysql_store_result)(mysql_send_governor); row = (*_mysql_fetch_row)(res); if (row) { lengths = (*_mysql_fetch_lengths)(res); db_mysql_get_string(buffer, row[0], lengths[0]); if (strstr(buffer, "-cll-lve")) { sprintf(outbuffer, "MySQL version correct %s", buffer); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, outbuffer, data_cfg.log_mode); (*_mysql_free_result)(res); return 1; } else { sprintf( outbuffer, "Update your MySQL to CLL version from repo.cloudlinux.com. Current is %s", buffer); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, outbuffer, data_cfg.log_mode); (*_mysql_free_result)(res); return 0; } } else { (*_mysql_free_result)(res); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Unknown mysql version", data_cfg.log_mode); return 0; } } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Connection to db is absent", data_cfg.log_mode); return 0; } }
void log_user_queries( char *user_name, MODE_TYPE debug_mode ) { char buffer[ _DBGOVERNOR_BUFFER_8192 ]; char sql_buffer[ _DBGOVERNOR_BUFFER_8192 ]; char user_name_alloc[ USERNAMEMAXLEN * 2 ]; char file_name[ USERNAMEMAXLEN + 1 + 10 ]; unsigned long counts; unsigned long *lengths; MYSQL_RES *res; MYSQL_ROW row; FILE *log_queries; const time_t timestamp = time( NULL ); struct governor_config data_cfg; get_config_data( &data_cfg ); (*_mysql_real_escape_string)( mysql_do_command, user_name_alloc, user_name, strlen( user_name ) ); snprintf( sql_buffer, _DBGOVERNOR_BUFFER_8192, QUERY_GET_PROCESSLIST_INFO ); if( db_mysql_exec_query( sql_buffer, mysql_do_command, debug_mode ) ) { WRITE_LOG( NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Get show processlist failed", data_cfg.log_mode ); return; } res = (*_mysql_store_result)( mysql_do_command ); counts = (*_mysql_num_rows)( res ); int tek_r = 0; if( create_dir() && counts > 0 ) { snprintf( file_name, USERNAMEMAXLEN + 1 + 10, "%s.%d", user_name, timestamp ); log_queries = fopen( file_name, "w" ); while( ( row = (*_mysql_fetch_row)( res ) ) ) { if( row ) { if( strcmp( row[ 1 ], user_name ) == 0 ) { lengths = (*_mysql_fetch_lengths)( res ); db_mysql_get_string( buffer, row[ 7 ], lengths[ 7 ] ); fprintf( log_queries, "%s\n", buffer ); } } else { (*_mysql_free_result)( res ); WRITE_LOG( NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "No queries retrieved", data_cfg.log_mode ); fclose( log_queries ); return; } } fclose( log_queries ); } (*_mysql_free_result)( res ); }
int redis_request::append_vcommand(const char *format, va_list ap) { char *cmd = NULL; int len = redisvFormatCommand(&cmd, format, ap); if (len == -1) { WRITE_LOG(LOG_FATAL, "outof memory"); return -1; } reqs.push_back(std::make_pair(cmd, static_cast<size_t>(len))); WRITE_LOG(LOG_DEBUG, "append command '%s' len %d OK", cmd, len); return 0; }
inline scoped_helper(const std::string& val, logger& log, logger::LogLevel level = logger::logDEBUG): mLogger(log), mVal(val), mLevel(level) { WRITE_LOG(mLogger, mLevel) << mVal << ": enter"; }
/*READ 1x struct packet_data_t {type EPACKET_SEQUENCE_REQUEST} *READ 1x struct request_data_t * @param dst_pid destination process should receive ranges*/ int read_range_request( int fdr, struct request_data_t* sequence ){ int len = 0; WRITE_FMT_LOG(LOG_DEBUG, "reading range request from %d (EPACKET_SEQUENCE_REQUEST)\n", fdr); struct packet_data_t t; t.type = EPACKET_UNKNOWN; read_channel( fdr, (char*)&t, sizeof(t) ); WRITE_FMT_LOG(LOG_DEBUG, "readed packet: type=%d, size=%d, src_node=%d\n", t.type, (int)t.size, t.src_nodeid ); if ( t.type == EPACKET_SEQUENCE_REQUEST ) { struct request_data_t *data = NULL; for ( int j=0; j < t.size; j++ ){ data = &sequence[j]; read_channel(fdr, (char*)data, sizeof(struct request_data_t)); WRITE_FMT_LOG(LOG_DEBUG, "recv range request %d %d %d\n", data->src_nodeid, data->first_item_index, data->last_item_index ); } } else{ WRITE_LOG(LOG_ERR,"channel_recv_sequences_request::packet Unknown\n"); assert(0); } return len; }
ssize_t write_sockf(struct sock_file_t *sockf, const char *buf, size_t size){ int err = 0; ssize_t wrote = 0; WRITE_FMT_LOG(LOG_DEBUG, "%p, %p, %d", sockf, buf, (int)size); if ( !sockf || !buf || !size || size==SIZE_MAX ) return 0; zmq_msg_t msg; err = zmq_msg_init_size (&msg, size); if ( err != 0 ){ wrote = 0; WRITE_FMT_LOG(LOG_ERR, "zmq_msg_init_size err %d, errno %d, status %s\n", err, zmq_errno(), zmq_strerror(zmq_errno())); } else{ memcpy (zmq_msg_data (&msg), buf, size); WRITE_FMT_LOG(LOG_DEBUG, "zmq_sending fd=%d buf %d bytes...", sockf->fs_fd, (int)size ); err = zmq_send ( sockf->netw_socket, &msg, 0); if ( err != 0 ){ WRITE_FMT_LOG(LOG_ERR, "zmq_send err %d, errno %d, status %s\n", err, zmq_errno(), zmq_strerror(zmq_errno())); wrote = 0; /*nothing sent*/ } else{ wrote = size; WRITE_LOG(LOG_DEBUG, "zmq_send ok"); } zmq_msg_close (&msg); } __bytes_sent +=wrote; return wrote; }
inline scoped_helper(const char val[N], logger& log, logger::LogLevel level = logger::logDEBUG): mLogger(log), mVal(val), mLevel(level) { WRITE_LOG(mLogger, mLevel) << mVal << " enter"; }
int add_sockf_copy_to_array(struct zeromq_pool* zpool, struct sock_file_t* sockf){ WRITE_FMT_LOG(LOG_DEBUG, "%p, %p", zpool, sockf); int err = ERR_OK; if ( !zpool || !sockf ) return ERR_BAD_ARG; if ( !zpool->sockf_array) return ERR_BAD_ARG; if( sockf_by_fd(zpool, sockf->fs_fd) ) return ERR_ALREADY_EXIST; struct sock_file_t* sockf_add = NULL; do{ /*search unused array cell*/ for (int i=0; i < zpool->count_max; i++) if ( zpool->sockf_array[i].unused ){ sockf_add = &zpool->sockf_array[i]; break; } /*extend array if not found unused cell*/ if ( !sockf_add ){ zpool->count_max+=ESOCKF_ARRAY_GRANULARITY; zpool->sockf_array = realloc(zpool->sockf_array, zpool->count_max * sizeof(struct sock_file_t)); if ( zpool->sockf_array ){ for (int i=zpool->count_max-ESOCKF_ARRAY_GRANULARITY; i < zpool->count_max; i++) zpool->sockf_array[i].unused = 1; } else{ /*no memory re allocated * This code section can't be covered by unit tests because it's system error*/ err = ERR_ERROR; WRITE_LOG(LOG_ERR, "sockf_array realloc mem failed"); } } }while(!sockf_add || ERR_OK!=err); *sockf_add = *sockf; sockf_add->unused = 0; return err; }
DefaultAllocator::~DefaultAllocator() { if (m_allocatedCount != 0 || m_allocatedSize != 0) { WRITE_LOG(WARNING, GT("Memory leak detected!")); } }
/*READ 1x struct packet_data_t {type EPACKET_SEQUENCE_REQUEST} *READ 1x struct request_data_t * @param dst_pid destination process should receive ranges*/ int read_range_request( const char *readf, struct request_data_t* sequence ){ int len = 0; int fdr = open(readf, O_RDONLY); WRITE_FMT_LOG(LOG_DEBUG, "reading range request from %s (EPACKET_SEQUENCE_REQUEST)", readf); int bytes; struct packet_data_t t; t.type = EPACKET_UNKNOWN; bytes=read( fdr, (char*)&t, sizeof(t) ); WRITE_FMT_LOG(LOG_DEBUG, "r packet_data_t bytes=%d", bytes); WRITE_FMT_LOG(LOG_DEBUG, "readed packet: type=%d, size=%d, src_node=%d", t.type, (int)t.size, t.src_nodeid ); if ( t.type == EPACKET_SEQUENCE_REQUEST ) { struct request_data_t *data = NULL; for ( int j=0; j < t.size; j++ ){ data = &sequence[j]; bytes=read(fdr, data, sizeof(struct request_data_t)); WRITE_FMT_LOG(LOG_DEBUG, "r packet_data_t bytes=%d", bytes); WRITE_FMT_LOG(LOG_DEBUG, "recv range request %d %d %d", data->src_nodeid, data->first_item_index, data->last_item_index ); } } else{ WRITE_LOG(LOG_ERR, "channel_recv_sequences_request::packet Unknown"); assert(0); } close(fdr); return len; }
int redis_connection::connect(const std::string &cfg, const timeval *tv) { const size_t count = std::count(cfg.begin(), cfg.end(), ',') + 1; redis.reserve(count); std::string::size_type pos = 0; std::string::size_type comma = cfg.find_first_of(','); while (true) { redis.push_back(redis_cfg()); redis_cfg &conn = redis.back(); if (NULL != tv) { conn.timeout = *tv; } if (redis_cfg_parse_(&conn, &cfg[pos]) < 0 || redis_connect_(&conn, tv) < 0) { reset(redis); return -1; } if (comma == std::string::npos) { break; } else { pos = comma + 1; comma = cfg.find_first_of(',', pos); } } WRITE_LOG(LOG_INFO, "connect %zd redis ok", redis.size()); return 0; }
void calc_acc_stats(gpointer key, Account * ac, gpointer data) { int i = 1; User_stats *us = (User_stats *) g_ptr_array_index (ac->users, 0); send_to_glib_info *internal_info = (send_to_glib_info *)data; Stats *ptr = fifo_stats_get(us->stats, 0); ac->current = *fifo_stats_get(us->stats, 0); ac->short_average = us->short_average; ac->mid_average = us->mid_average; ac->long_average = us->long_average; while (i < ac->users->len) { us = (User_stats *) g_ptr_array_index (ac->users, i++); if(ac->need_dbg){ char output_buffer[_DBGOVERNOR_BUFFER_2048]; WRITE_LOG( NULL, 1, output_buffer, _DBGOVERNOR_BUFFER_2048, " step 2-%d: proceed user stats %s, c %f, r %llu, w %llu", internal_info->log_mode, i, us->id?us->id:"Unk", fifo_stats_get(us->stats, 0)->cpu, fifo_stats_get(us->stats, 0)->read, fifo_stats_get(us->stats, 0)->write ); } sum_stats(&ac->current, fifo_stats_get(us->stats, 0)); sum_stats(&ac->short_average, &us->short_average); sum_stats(&ac->mid_average, &us->mid_average); sum_stats(&ac->long_average, &us->long_average); ptr = fifo_stats_get(us->stats, 0); } if(ac->need_dbg){ char output_buffer[_DBGOVERNOR_BUFFER_2048]; WRITE_LOG( NULL, 1, output_buffer, _DBGOVERNOR_BUFFER_2048, " step 3: summary, c %f, r %llu, w %llu", internal_info->log_mode, ac->current.cpu, ac->current.read, ac->current.write ); } }
redis_request::redis_request(int retry_count) : retry_count(retry_count) { if (this->retry_count > MAX_RETRY_COUNT) { this->retry_count = MAX_RETRY_COUNT; WRITE_LOG(LOG_WARNING, "redis retry count %d is too big,adjust to %d", retry_count, this->retry_count); } }
//Exec query, if error occurred - try again EXEC_QUERY_TRIES times int db_mysql_exec_query(const char *query, MYSQL * mysql_internal, MODE_TYPE debug_mode) { char buf[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); //Проверим наличие соединения, а вдруг пропало if ((*_mysql_ping)(mysql_internal)) { //База действительно ушла прочь, что даже реконнект не помог char *unm = NULL; char *upwd = NULL; if(global_user_name[0]) unm = global_user_name; if(global_user_password[0]) upwd = global_user_password; my_bool reconnect = 1; //Авторекоонет - подключить (*_mysql_options)(mysql_internal, MYSQL_OPT_RECONNECT, &reconnect); //Еще разок соединимся if (!(*_mysql_real_connect)(mysql_internal, global_host, unm, upwd, global_db_name, 0, _unix_socket_addres, 0)) { return -1; } } int execution_counters = EXEC_QUERY_TRIES; while ((*_mysql_query)(mysql_internal, query)) { execution_counters--; if (execution_counters == 0) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_2048, db_getlasterror (mysql_internal), data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buf, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, db_getlasterror (mysql_internal), query); } return -1; } } return 0; }
int get_line(char* out_buffer,uint32_t out_buffer_size) { const int start=m_pos; while(split!=m_current_buffer[m_pos]) { m_pos++; } if(m_pos<m_buffer_size) { int ret=m_pos-start; int len=std::min((int)out_buffer_size-1,ret); memcpy(out_buffer,m_current_buffer+start,len); out_buffer[len]=0; m_pos++; return ret; } int ret=std::min(out_buffer_size-1,m_pos-start); memcpy(out_buffer,m_current_buffer+start,ret); m_pos=0; if(m_rem_block==0) { out_buffer[ret]=0; return ret==0?-1:ret; } if(m_buffer_size!=pread(m_fileno,m_current_buffer,m_buffer_size,m_offset)) { WRITE_LOG(UL_LOG_FATAL,"pread failed"); return -2; } m_rem_block--; m_offset+=m_buffer_size; m_current_buffer[m_buffer_size]=split; while(split!=m_current_buffer[m_pos]) { m_pos++; } if(m_pos==m_buffer_size) { WRITE_LOG(UL_LOG_FATAL,"The inner buffer size [%u] is shorter than current line size.",m_buffer_size); return -2; } int len=std::min(m_pos,out_buffer_size-1-ret); memcpy(out_buffer+ret,m_current_buffer,len); ret+=len; out_buffer[ret]=0; m_pos++; return ret; }
//Save all privileges to base void flush_user_priv(MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; MYSQL_RES *res; struct governor_config data_cfg; get_config_data( &data_cfg ); if (db_mysql_exec_query(QUERY_FLUSH_USER_PRIV, mysql_do_command, debug_mode)) { if (debug_mode != DEBUG_MODE) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Can't flush user privs", data_cfg.log_mode); } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "%s --- Request: %s", data_cfg.log_mode, "Can't flush user privs", QUERY_FLUSH_USER_PRIV); } } res = (*_mysql_store_result)(mysql_do_command); (*_mysql_free_result)(res); }
int redis_connection::normal_op(const redis_request &req, redis_response &res, int idx) { if (idx < 0) { return redis_op_all_(*this, req, res, &empty_check); } else { if (idx < static_cast<int>(redis.size())) { return redis_op_(&redis[idx], req, res.replys, &empty_check); } else { WRITE_LOG(LOG_ERROR, "bad server index %d", idx); return -1; } } }
int db_connect(const char *host, const char *user_name, const char *user_password, const char *db_name, int argc, char *argv[], MODE_TYPE debug_mode) { char buffer[_DBGOVERNOR_BUFFER_2048]; struct governor_config data_cfg; get_config_data( &data_cfg ); //Обнулим глобальные переменные доступа strcpy(global_user_name, ""); strcpy(global_host, ""); strcpy(global_user_password, ""); strcpy(global_db_name, ""); WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Open govern connection operation", data_cfg.log_mode); if (db_connect_common(&mysql_send_governor, host, user_name, user_password, db_name, debug_mode, argc, argv, 1) < 0) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Send governor connection error", data_cfg.log_mode); return -1; } else { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Open write connection operation", data_cfg.log_mode); if (db_connect_common(&mysql_do_command, host, user_name, user_password, db_name, debug_mode, argc, argv, 0) < 0) { WRITE_LOG(NULL, 0, buffer, _DBGOVERNOR_BUFFER_2048, "Write connection error", data_cfg.log_mode); return -1; } } return 0; }
int create(uint32_t buffer_size,FILE* in_file) { if(in_file==NULL) { WRITE_LOG(UL_LOG_FATAL,"NULL file pointer"); return -1; } m_file=in_file; m_offset=0; struct stat st; if(fstat(fileno(m_file),&st)) { WRITE_LOG(UL_LOG_FATAL,"fstat failed"); return -1; } m_fileno=fileno(in_file); m_file_size = st.st_size; m_buffer_size=std::min(m_file_size,(long long)buffer_size); try { m_current_buffer=new char[m_buffer_size+1]; } catch(std::bad_alloc) { WRITE_LOG(UL_LOG_FATAL,"new buffer failed"); return -1; } m_rem_block=m_file_size/m_buffer_size; m_pos=m_buffer_size; m_current_buffer[m_pos]=split; if(m_file_size%m_buffer_size) { uint32_t read_byte=(m_file_size%m_buffer_size); if(read_byte!=pread(m_fileno,m_current_buffer+m_buffer_size-read_byte,read_byte,0)) { WRITE_LOG(UL_LOG_FATAL,"pread failed"); return -1; } m_offset+=read_byte; m_pos-=read_byte; } return 0; }
int zmain(int argc, char **argv){ /* argv[0] is node name * expecting in format : "name-%d", * format for single node without decimal id: "name" */ WRITE_FMT_LOG(LOG_DEBUG, "Source node started argv[0]=%s\n", argv[0] ); if ( argc < 2 ){ WRITE_LOG(LOG_ERR, "argv[1] is expected, items count need to be passed.\n" ); return -1; } set_items_count_to_sortjob( atoi(argv[1]) ); int ownnodeid= -1; int extracted_name_len=0; int res =0; /*get node type names via environnment*/ char *source_node_type_text = getenv(ENV_SOURCE_NODE_NAME); char *dest_node_type_text = getenv(ENV_DEST_NODE_NAME); char *man_node_type_text = getenv(ENV_MAN_NODE_NAME); assert(source_node_type_text); assert(dest_node_type_text); assert(man_node_type_text); ownnodeid = ExtractNodeNameId( argv[0], &extracted_name_len ); /*nodename should be the same we got via environment and extracted from argv[0]*/ assert( !strncmp(source_node_type_text, argv[0], extracted_name_len ) ); if ( ownnodeid == -1 ) ownnodeid=1; /*node id not specified for single node by default assign nodeid=1*/ /*setup channels conf, now used static data but should be replaced by data from zrt*/ struct ChannelsConfigInterface chan_if; SetupChannelsConfigInterface( &chan_if, ownnodeid, ESourceNode ); /*********************************************************************** Add channels configuration into config object */ res = AddAllChannelsRelatedToNodeTypeFromDir( &chan_if, IN_DIR, EChannelModeRead, EManagerNode, man_node_type_text ); assert( res == 0 ); res = AddAllChannelsRelatedToNodeTypeFromDir( &chan_if, IN_DIR, EChannelModeRead, EDestinationNode, dest_node_type_text ); assert( res == 0 ); res = AddAllChannelsRelatedToNodeTypeFromDir( &chan_if, OUT_DIR, EChannelModeWrite, EManagerNode, man_node_type_text ); assert( res == 0 ); res = AddAllChannelsRelatedToNodeTypeFromDir( &chan_if, OUT_DIR, EChannelModeWrite, EDestinationNode, dest_node_type_text ); assert( res == 0 ); /*add input channel into config*/ res = chan_if.AddChannel( &chan_if, EInputOutputNode, 1, STDIN, EChannelModeRead ) != NULL? 0: -1; assert( res == 0 ); /*--------------*/ res = start_node(&chan_if, ownnodeid); CloseChannels(&chan_if); return res; }
int GARBAGE_COLLECTION(void) { #ifdef FTL_DEBUG printf("[%s] Start\n", __FUNCTION__); #endif int32_t victim_pbn = SELECT_VICTIM_BLOCK(); int copy_page_nb; int32_t lbn; if(victim_pbn == -1){ // printf("[%s] There is no victim block \n", __FUNCTION__); return FAIL; } copy_page_nb = BM_GARBAGE_COLLECTION(victim_pbn); #ifdef DEBUG_MODE9 fp_dbg9_gc = fopen("./data/p_dbg_9_gc.txt", "a"); fprintf(fp_dbg9_gc,"%d\t%d\t%d\t%lf\t%ld\n", gc_count, victim_pbn, copy_page_nb, ((double)copy_page_nb/PAGE_NB)*100, total_empty_block_nb); fclose(fp_dbg9_gc); #endif gc_count++; #ifdef MONITOR_ON char szTemp[1024]; sprintf(szTemp, "GC "); WRITE_LOG(szTemp); sprintf(szTemp, "WB AMP %d", copy_page_nb); WRITE_LOG(szTemp); #endif #ifdef FTL_DEBUG printf("[%s] Complete, total empty block: %ld, %d\n", __FUNCTION__, total_empty_block_nb, copy_page_nb); #endif return SUCCESS; }