RedisConn * RedisDBSlice::GetConn(int ioRole) { RedisConn *pRedisConn = NULL; if (!mHaveSlave) { ioRole = MASTER; } if (MASTER == ioRole) { pRedisConn = GetMasterConn(); } else if (SLAVE == ioRole) { pRedisConn = GetSlaveConn(); } else { pRedisConn = NULL; } return pRedisConn; }
void Master::AddSlave(Channel* slave, RedisCommandFrame& cmd) { INFO_LOG("[Master]Recv sync command:%s", cmd.ToString().c_str()); slave->Flush(); SlaveConn& conn = GetSlaveConn(slave); if (!strcasecmp(cmd.GetCommand().c_str(), "sync")) { //Redis 2.6/2.4 send 'sync' conn.isRedisSlave = true; conn.sync_offset = -1; } else { conn.server_key = cmd.GetArguments()[0]; const std::string& offset_str = cmd.GetArguments()[1]; if (!string_toint64(offset_str, conn.sync_offset)) { ERROR_LOG("Invalid offset argument:%s", offset_str.c_str()); slave->Close(); return; } conn.isRedisSlave = true; for (uint32 i = 2; i < cmd.GetArguments().size(); i += 2) { if (cmd.GetArguments()[i] == "cksm") { conn.isRedisSlave = false; if (!string_touint64(cmd.GetArguments()[i + 1], conn.sync_cksm)) { ERROR_LOG("Invalid checksum argument:%s", cmd.GetArguments()[i + 1].c_str()); slave->Close(); return; } } } } slave->GetService().DetachChannel(slave, true); if (g_repl->GetIOServ().IsInLoopThread()) { AddSlave(&conn); } else { g_repl->GetIOServ().AsyncIO(0, OnAddSlave, &conn); } }
void Master::SetSlavePort(Channel* slave, uint32 port) { GetSlaveConn(slave).port = port; }