Skip to content

Commit af42900

Browse files
yuwmaoyuwmao
andauthored
Return accepted if a new servier is already in cluster (#634)
Co-authored-by: yuwmao <yuwmao@ebaychina.com>
1 parent bfc91b8 commit af42900

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/handle_join_leave.cxx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ limitations under the License.
3030
#include "tracer.hxx"
3131

3232
#include <cassert>
33+
#include <set>
3334
#include <sstream>
3435

3536
namespace nuraft {
@@ -167,7 +168,36 @@ ptr<resp_msg> raft_server::handle_join_cluster_req(req_msg& req) {
167168

168169
ptr<cluster_config> cur_config = get_config();
169170
if (cur_config->get_servers().size() > 1) {
170-
p_in("this server is already in a cluster, ignore the request");
171+
// This server is already in a cluster.
172+
// Validate that the request is from the same cluster by comparing
173+
// cluster configurations. The request should be accepted only if:
174+
// req->cluster_config == this->cluster_config - this_server
175+
176+
// Build a set of server IDs from the request's cluster config
177+
ptr<cluster_config> req_config =
178+
cluster_config::deserialize(entries[0]->get_buf());
179+
std::set<int32> req_server_ids;
180+
for (const auto& srv: req_config->get_servers()) {
181+
req_server_ids.insert(srv->get_id());
182+
}
183+
184+
// Build a set of server IDs from current cluster config, excluding this server
185+
std::set<int32> cur_server_ids_minus_self;
186+
for (const auto& srv: cur_config->get_servers()) {
187+
if (srv->get_id() != id_) {
188+
cur_server_ids_minus_self.insert(srv->get_id());
189+
}
190+
}
191+
192+
// Compare the two sets
193+
if (req_server_ids == cur_server_ids_minus_self) {
194+
p_in("this server is already in the cluster and request is from the same "
195+
"cluster, accepting");
196+
resp->accept(quick_commit_index_.load() + 1);
197+
return resp;
198+
}
199+
p_in("this server is already in a cluster and request is from a different "
200+
"cluster, rejecting");
171201
return resp;
172202
}
173203
// Handle Race Condition: Simultaneous Add Server

0 commit comments

Comments
 (0)