void Slave::load_status(){ std::string key; std::string seq; meta->hget(status_key(), "last_key", &key); meta->hget(status_key(), "last_seq", &seq); if(!key.empty()){ this->last_key = key; } if(!seq.empty()){ this->last_seq = str_to_uint64(seq); } }
void Slave::save_status(){ std::string key = status_key(); std::string status; status.append((char *)&this->next_seq, sizeof(uint64_t)); status.append(this->last_key); meta_db->Put(leveldb::WriteOptions(), key, status); }
/* Print an error message based on the status contained in *RES. */ void socket99_fprintf(FILE *f, socket99_result *res) { if (f == NULL || res == NULL) { return; } fprintf(f, "%s: %s\n", status_key(res->status), (res->status == SOCKET99_ERROR_GETADDRINFO ? gai_strerror(res->getaddrinfo_error) : strerror(res->saved_errno))); }
/* Construct an error message in BUF, based on the status codes * in *RES. This has the same return value and general behavior * as snprintf -- if the return value is >= buf_size, the string * has been truncated. Returns -1 if either BUF or RES are NULL. */ int socket99_snprintf(char *buf, size_t buf_size, socket99_result *res) { if (buf == NULL || res == NULL) { return 0; } return snprintf(buf, buf_size, "%s: %s", status_key(res->status), (res->status == SOCKET99_ERROR_GETADDRINFO ? gai_strerror(res->getaddrinfo_error) : strerror(res->saved_errno))); }
void Slave::start(){ migrate_old_status(); load_status(); log_debug("status_key: %s last_seq: %" PRIu64 ", last_key: %s", status_key().c_str(),last_seq, hexmem(last_key.data(), last_key.size()).c_str()); thread_quit = false; int err = pthread_create(&run_thread_tid, NULL, &Slave::_run_thread, this); if(err != 0){ log_error("can't create thread: %s", strerror(err)); } }
void Slave::load_status(){ std::string key = status_key(); std::string val; leveldb::Status s = meta_db->Get(leveldb::ReadOptions(), key, &val); if(s.ok()){ if(val.size() < sizeof(uint64_t)){ log_error("invalid format of status"); }else{ last_seq = *((uint64_t *)(val.data())); last_key.assign(val.data() + sizeof(uint64_t), val.size() - sizeof(uint64_t)); } } }
void Slave::load_status(){ std::string key = status_key(); std::string status; leveldb::Status s; s = meta_db->Get(leveldb::ReadOptions(), key, &status); if(s.IsNotFound()){ return; } if(s.ok()){ if(status.size() < sizeof(uint64_t)){ log_error("invlid format of status"); }else{ next_seq = *((uint64_t *)(status.data())); last_key = std::string(status.data() + sizeof(uint64_t), status.size() - sizeof(uint64_t)); log_debug("load_status seq: %llu, key: %s", next_seq, hexmem(last_key.data(), last_key.size()).c_str()); } } }
void Slave::save_status(){ std::string seq = str(this->last_seq); meta->hset(status_key(), "last_key", this->last_key); meta->hset(status_key(), "last_seq", seq); }