SSDBServer::SSDBServer(SSDB *ssdb, SSDB *meta, const Config &conf, NetworkServer *net){ this->ssdb = (SSDBImpl *)ssdb; this->meta = meta; net->data = this; this->reg_procs(net); int sync_speed = conf.get_num("replication.sync_speed"); backend_dump = new BackendDump(this->ssdb); backend_sync = new BackendSync(this->ssdb, sync_speed); expiration = new ExpirationHandler(this->ssdb); cluster = new Cluster(this->ssdb); if(cluster->init() == -1){ log_fatal("cluster init failed!"); exit(1); } { // slaves const Config *repl_conf = conf.get("replication"); if(repl_conf != NULL){ std::vector<Config *> children = repl_conf->children; for(std::vector<Config *>::iterator it = children.begin(); it != children.end(); it++){ Config *c = *it; if(c->key != "slaveof"){ continue; } std::string ip = c->get_str("ip"); int port = c->get_num("port"); if(ip == ""){ ip = c->get_str("host"); } if(ip == "" || port <= 0 || port > 65535){ continue; } bool is_mirror = false; std::string type = c->get_str("type"); if(type == "mirror"){ is_mirror = true; }else{ type = "sync"; is_mirror = false; } std::string id = c->get_str("id"); log_info("slaveof: %s:%d, type: %s", ip.c_str(), port, type.c_str()); Slave *slave = new Slave(ssdb, meta, ip.c_str(), port, is_mirror); if(!id.empty()){ slave->set_id(id); } slave->auth = c->get_str("auth"); slave->start(); slaves.push_back(slave); } } } // load kv_range int ret = this->get_kv_range(&this->kv_range_s, &this->kv_range_e); if(ret == -1){ log_fatal("load key_range failed!"); exit(1); } log_info("key_range.kv: \"%s\", \"%s\"", str_escape(this->kv_range_s).c_str(), str_escape(this->kv_range_e).c_str() ); }
SSDB* SSDB::open(const Config &conf, const std::string &base_dir){ std::string main_db_path = base_dir + "/data"; std::string meta_db_path = base_dir + "/meta"; int cache_size = conf.get_num("leveldb.cache_size"); int write_buffer_size = conf.get_num("leveldb.write_buffer_size"); int block_size = conf.get_num("leveldb.block_size"); int compaction_speed = conf.get_num("leveldb.compaction_speed"); std::string compression = conf.get_str("leveldb.compression"); std::string binlog_onoff = conf.get_str("replication.binlog"); strtolower(&compression); if(compression != "yes"){ compression = "no"; } strtolower(&binlog_onoff); if(binlog_onoff != "no"){ binlog_onoff = "yes"; } if(cache_size <= 0){ cache_size = 8; } if(write_buffer_size <= 0){ write_buffer_size = 4; } if(block_size <= 0){ block_size = 4; } log_info("main_db : %s", main_db_path.c_str()); log_info("meta_db : %s", meta_db_path.c_str()); log_info("cache_size : %d MB", cache_size); log_info("block_size : %d KB", block_size); log_info("write_buffer : %d MB", write_buffer_size); log_info("compaction_speed : %d MB/s", compaction_speed); log_info("compression : %s", compression.c_str()); log_info("binlog : %s", binlog_onoff.c_str()); SSDB *ssdb = new SSDB(); // ssdb->options.create_if_missing = true; ssdb->options.filter_policy = leveldb::NewBloomFilterPolicy(10); ssdb->options.block_cache = leveldb::NewLRUCache(cache_size * 1048576); ssdb->options.block_size = block_size * 1024; ssdb->options.write_buffer_size = write_buffer_size * 1024 * 1024; ssdb->options.compaction_speed = compaction_speed; if(compression == "yes"){ ssdb->options.compression = leveldb::kSnappyCompression; }else{ ssdb->options.compression = leveldb::kNoCompression; } leveldb::Status status; { leveldb::Options options; options.create_if_missing = true; status = leveldb::DB::Open(options, meta_db_path, &ssdb->meta_db); if(!status.ok()){ goto err; } } status = leveldb::DB::Open(ssdb->options, main_db_path, &ssdb->db); if(!status.ok()){ log_error("open main_db failed"); goto err; } ssdb->binlogs = new BinlogQueue(ssdb->db); if(binlog_onoff == "no"){ ssdb->binlogs->no_log(); } { // slaves const Config *repl_conf = conf.get("replication"); if(repl_conf != NULL){ std::vector<Config *> children = repl_conf->children; for(std::vector<Config *>::iterator it = children.begin(); it != children.end(); it++){ Config *c = *it; if(c->key != "slaveof"){ continue; } std::string ip = c->get_str("ip"); int port = c->get_num("port"); if(ip == "" || port <= 0 || port > 65535){ continue; } bool is_mirror = false; std::string type = c->get_str("type"); if(type == "mirror"){ is_mirror = true; }else{ type = "sync"; is_mirror = false; } std::string id = c->get_str("id"); log_info("slaveof: %s:%d, type: %s", ip.c_str(), port, type.c_str()); Slave *slave = new Slave(ssdb, ssdb->meta_db, ip.c_str(), port, is_mirror); if(!id.empty()){ slave->set_id(id); } slave->start(); ssdb->slaves.push_back(slave); } } } return ssdb; err: if(ssdb){ delete ssdb; } return NULL; }