diff --git a/api/api-doc/raft.json b/api/api-doc/raft.json index 9673682069..34dba0c6f3 100644 --- a/api/api-doc/raft.json +++ b/api/api-doc/raft.json @@ -38,6 +38,30 @@ ] } ] + }, + { + "path":"/raft/leader_host", + "operations":[ + { + "method":"GET", + "summary":"Returns host ID of the current leader of the given Raft group", + "type":"string", + "nickname":"get_leader_host", + "produces":[ + "application/json" + ], + "parameters":[ + { + "name":"group_id", + "description":"The ID of the group. When absent, group0 is used.", + "required":false, + "allowMultiple":false, + "type":"string", + "paramType":"query" + } + ] + } + ] } ] } diff --git a/api/raft.cc b/api/raft.cc index eab13889dd..f62d524d16 100644 --- a/api/raft.cc +++ b/api/raft.cc @@ -60,10 +60,24 @@ void set_raft(http_context&, httpd::routes& r, sharded req) -> future { + return smp::submit_to(0, [&] { + auto& srv = std::invoke([&] () -> raft::server& { + if (req->query_parameters.contains("group_id")) { + raft::group_id id{utils::UUID{req->get_query_param("group_id")}}; + return raft_gr.local().get_server(id); + } else { + return raft_gr.local().group0(); + } + }); + return json_return_type(srv.current_leader().to_sstring()); + }); + }); } void unset_raft(http_context&, httpd::routes& r) { r::trigger_snapshot.unset(r); + r::get_leader_host.unset(r); } }