Skip to content

Commit

Permalink
add dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
LLiuJJ committed Aug 28, 2024
1 parent 7a508b4 commit 92e468d
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 11 deletions.
21 changes: 15 additions & 6 deletions example/src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@

Client::Client() {
this->metaserver_addrs_ = StringUtil::Split(DEFAULT_METASERVER_ADDRS, ',');
bool is_chan_create_ok = false;
for (auto metaserver_addr : this->metaserver_addrs_) {
SPDLOG_INFO("init rpc link to {} ", metaserver_addr);
auto chan_ = grpc::CreateChannel(metaserver_addr,
auto chan_ = grpc::CreateChannel(metaserver_addr,
grpc::InsecureChannelCredentials());
std::unique_ptr<ERaftKv::Stub> stub_(ERaftKv::NewStub(chan_));
this->meta_svr_stubs_[metaserver_addr] = std::move(stub_);
if (chan_ != nullptr) {
std::unique_ptr<ERaftKv::Stub> stub_(ERaftKv::NewStub(chan_));
this->meta_svr_stubs_[metaserver_addr] = std::move(stub_);
is_chan_create_ok = true;
}
}
if (is_chan_create_ok) {
this->UpdateMetaServerLeaderStub();
}
this->UpdateMetaServerLeaderStub();
this->client_id_ = StringUtil::RandStr(16);
this->command_id_ = 0;
}
Expand Down Expand Up @@ -105,8 +111,11 @@ bool Client::AddServerGroupToMeta(int64_t shard_id,
req.mutable_shard_group()->set_leader_id(leader_id);
req.set_change_type(eraftkv::ChangeType::ShardJoin);
eraftkv::ClusterConfigChangeResp resp;
auto st = this->meta_leader_stub_->ClusterConfigChange(&context, req, &resp);
assert(st.ok());
if (this->meta_leader_stub_ != nullptr) {
auto st =
this->meta_leader_stub_->ClusterConfigChange(&context, req, &resp);
assert(st.ok());
}
this->command_id_++;
return resp.success();
}
Expand Down
47 changes: 44 additions & 3 deletions example/src/eraftkv_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ void ERaftKvServer::UpdateMetaStats() {
StringUtil::Split(DEFAULT_SHARD_MONITOR_ADDRS, ',');
for (auto shard_svr : shard_svr_addrs) {
httplib::Client cli(shard_svr);
auto res = cli.Get("/collect_stats");
cli.set_connection_timeout(0, 200);
cli.set_read_timeout(0, 200);
cli.set_write_timeout(0, 200);
auto res = cli.Get("/collect_stats");
}
for (auto server : sg->servers()) {
*cluster_stats_json_str_ += ("{\"id\":" + std::to_string(server.id()));
Expand Down Expand Up @@ -350,8 +353,46 @@ void ERaftKvServer::ReportStats() {
metaserver_cli.AddServerGroupToMeta(
1, raft_context_->GetLeaderId(), group_server_addresses);
*stat_json_str_ = "{";
*stat_json_str_ += "\"commit_index\":\"" +
std::to_string(raft_context_->GetCommitIndex()) + "\"}";
*stat_json_str_ +=
"\"commit_index\":" + std::to_string(raft_context_->GetCommitIndex()) +
",";
*stat_json_str_ += "\"applied_index\":" +
std::to_string(raft_context_->GetAppliedIndex()) + ",";
*stat_json_str_ += "\"prefix_logs\":[";
for (auto log : raft_context_->GetPrefixLogs()) {
*stat_json_str_ += "{\"index\":" + std::to_string(log->id()) + ",";
*stat_json_str_ += "\"term\":" + std::to_string(log->term()) + ",";
switch (log->e_type()) {
case eraftkv::EntryType::Normal: {
eraftkv::KvOpPair* op_pair = new eraftkv::KvOpPair();
op_pair->ParseFromString(log->data());
*stat_json_str_ +=
"\"val\":\"" + op_pair->value().substr(0, 6) + "\"},";
break;
}
default:
break;
}
}
(*stat_json_str_).pop_back();
*stat_json_str_ += "],\"suffix_logs\":[";
for (auto log : raft_context_->GetSuffixLogs()) {
*stat_json_str_ += "{\"index\":" + std::to_string(log->id()) + ",";
*stat_json_str_ += "\"term\":" + std::to_string(log->term()) + ",";
switch (log->e_type()) {
case eraftkv::EntryType::Normal: {
eraftkv::KvOpPair* op_pair = new eraftkv::KvOpPair();
op_pair->ParseFromString(log->data());
*stat_json_str_ +=
"\"val\":\"" + op_pair->value().substr(0, 6) + "\"},";
break;
}
default:
break;
}
}
(*stat_json_str_).pop_back();
*stat_json_str_ += "]}";
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
}
Expand Down
76 changes: 76 additions & 0 deletions example/static/dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<html lang="en">
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<canvas id="canvas1" width="800" height="400"></canvas>
<script type="application/javascript">
function drawRaftCluster() {
const canvas = document.getElementById("canvas");
if (canvas.getContext) {
const ctx = canvas.getContext("2d");
const Http = new XMLHttpRequest();
const url = "http://127.0.0.1:19080/v1_cluster_stats";
Http.open("GET", url);
Http.send();
Http.onreadystatechange = (e) => {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const nodes = JSON.parse(Http.response);
followerCount = 0;
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].state === 'L') {
ctx.beginPath();
ctx.fillStyle = "rgb(56 138 247)";
ctx.arc(100, 100, 50, 0, Math.PI * 2.0, false);
ctx.strokeStyle = 'gray';
ctx.strokeText("L: " + nodes[i].addr, 45, 35);
ctx.fill();
ctx.beginPath();
} else {
ctx.beginPath();
ctx.fillStyle = "rgb(221 222 227)";
ctx.arc(100 + (followerCount * 120), 280, 50, 0, Math.PI * 2.0, false);
ctx.strokeStyle = 'gray';
ctx.strokeText("L: " + nodes[i].addr, 60 + (followerCount * 100), 200);
ctx.fill();
ctx.beginPath();
followerCount += 1;
}
}
}
}
}

function drawRaftLogsNode1() {
const canvas1 = document.getElementById("canvas1");
if (canvas1.getContext) {
const ctx = canvas1.getContext('2d');
const Http = new XMLHttpRequest();
const stat_url = "http://127.0.0.1:18080/collect_stats";
Http.open("GET", stat_url);
Http.send();
Http.onreadystatechange = (e) => {
const stats = JSON.parse(Http.response);
ctx.beginPath();
ctx.clearRect(0, 0, canvas1.width, canvas1.height);
ctx.strokeStyle = 'gray';
ctx.strokeText("node logs: ", 45, 15);
for (var i = 0; i < stats.prefix_logs.length; i ++) {
ctx.strokeText("index: " + stats.prefix_logs[i].index, 86 + (i * 100), 66);
ctx.strokeText("term: " + stats.prefix_logs[i].term, 86 + (i * 100), 86);
ctx.strokeText("val: " + stats.prefix_logs[i].val, 86 + (i * 100), 106);
ctx.strokeRect(80 + (i * 100), 56, 80, 80);
}
for (var i = 0; i < stats.suffix_logs.length; i ++) {
ctx.strokeText("index: " + stats.suffix_logs[i].index, 86 + (i * 100), 196);
ctx.strokeText("term: " + stats.suffix_logs[i].term, 86 + (i * 100), 206);
ctx.strokeText("val: " + stats.suffix_logs[i].val, 86 + (i * 100), 236);
ctx.strokeRect(80 + (i * 100), 180, 80, 80);
}
}
}
}
setInterval(drawRaftCluster, 1000);
setInterval(drawRaftLogsNode1, 1000);
</script>
</body>
</html>

4 changes: 4 additions & 0 deletions raftcore/include/eraft/raft_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ class RaftServer {

int64_t GetAppliedIndex();

std::vector<eraftkv::Entry*> GetPrefixLogs();

std::vector<eraftkv::Entry*> GetSuffixLogs();

/**
* @brief
*
Expand Down
15 changes: 13 additions & 2 deletions raftcore/src/raft_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ RaftServer::RaftServer(RaftConfig raft_config,
, max_entries_per_append_req_(100)
, tick_interval_(100)
, granted_votes_(0)
, snap_threshold_log_count_(10000)
, snap_threshold_log_count_(100000)
, open_auto_apply_(true)
, is_snapshoting_(false)
, snap_db_path_(raft_config.snap_path)
Expand Down Expand Up @@ -1213,4 +1213,15 @@ int64_t RaftServer::GetCommitIndex() {

int64_t RaftServer::GetAppliedIndex() {
return last_applied_idx_;
}
}


std::vector<eraftkv::Entry*> RaftServer::GetPrefixLogs() {
return log_store_->Gets(this->log_store_->FirstIndex(),
this->log_store_->FirstIndex() + 5);
}

std::vector<eraftkv::Entry*> RaftServer::GetSuffixLogs() {
return log_store_->Gets(this->log_store_->LastIndex() - 5,
this->log_store_->LastIndex());
}

0 comments on commit 92e468d

Please sign in to comment.