{"action":"created","comment":{"url":"https://api.github.com/repos/bluebore/bfs/pulls/comments/63276673","id":63276673,"diff_hunk":"@@ -0,0 +1,545 @@\n+// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved.\n+// Use of this source code is governed by a BSD-style license that can be\n+// found in the LICENSE file.\n+//\n+// Author: yanshiguang02@baidu.com\n+\n+#include \"nameserver/raft_node.h\"\n+\n+#include <boost/bind.hpp>\n+#include <gflags/gflags.h>\n+#include <common/mutex.h>\n+#include <common/logging.h>\n+#include <common/string_util.h>\n+#include <common/timer.h>\n+\n+#include \"rpc/rpc_client.h\"\n+\n+namespace baidu {\n+namespace bfs {\n+\n+const std::string kLogEndMark = \"Raft:\";\n+\n+RaftNodeImpl::RaftNodeImpl(const std::string& raft_nodes,\n+                           int node_index,\n+                           const std::string& db_path)\n+    : current_term_(0), log_index_(0), log_term_(0), commit_index_(0),\n+      last_applied_(0), applying_(false), node_stop_(false), election_taskid_(-1),\n+      node_state_(kFollower) {\n+    common::SplitString(raft_nodes, \",\", &nodes_);\n+    if (nodes_.size() < 1U || static_cast<int>(nodes_.size()) <= node_index) {\n+        LOG(FATAL, \"Wrong flags raft_nodes: %s %d\", raft_nodes.c_str(),\n+            node_index);\n+    }\n+    self_ = nodes_[node_index];\n+\n+    LoadStorage(db_path);\n+    LOG(INFO, \"Start RaftNode %s (%s)\", self_.c_str(), raft_nodes.c_str());\n+\n+    rpc_client_ = new RpcClient();\n+    srand(common::timer::get_micros());\n+    thread_pool_ = new common::ThreadPool();\n+\n+    MutexLock lock(&mu_);\n+    ResetElection();\n+}\n+\n+RaftNodeImpl::~RaftNodeImpl() {\n+    node_stop_ = true;\n+    delete thread_pool_;\n+    for (uint32_t i = 0; i < follower_context_.size(); i++) {\n+        follower_context_[i]->condition.Signal();\n+        follower_context_[i]->worker.Stop(true);\n+        delete follower_context_[i];\n+        follower_context_[i] = NULL;\n+    }\n+}\n+\n+void RaftNodeImpl::LoadStorage(const std::string& db_path) {\n+    leveldb::Options options;\n+    options.create_if_missing = true;\n+    leveldb::Status s = leveldb::DB::Open(options, db_path, &log_db_);\n+    if (!s.ok()) {\n+        log_db_ = NULL;\n+        LOG(FATAL, \"Open raft db fail: %s\\n\", s.ToString().c_str());\n+    }\n+    if (!GetContext(\"last_applied\", &last_applied_)) {\n+        last_applied_ = 0;\n+    }\n+    if (!GetContext(\"current_term\", &current_term_)) {\n+        current_term_ = 0;\n+    }\n+    if (!GetContext(\"voted_for_\", &voted_for_)) {\n+        voted_for_ = \"\";\n+    }\n+    leveldb::Iterator* it = log_db_->NewIterator(leveldb::ReadOptions());\n+    it->Seek(kLogEndMark);\n+    if (!it->Valid() || (it->Prev(), !it->Valid())) {\n+        log_index_ = 0;\n+    } else {\n+        LogEntry entry;\n+        if (!entry.ParseFromString(it->value().ToString())) {\n+            LOG(FATAL, \"Parse log entry failed\");\n+        }\n+        log_index_ = entry.index();\n+    }\n+    delete it;\n+    LOG(INFO, \"LoadStorage term %ld index %ld applied %ld\",\n+        current_term_, log_index_, last_applied_);\n+    for (uint32_t i = 0; i < nodes_.size(); i++) {\n+        if (nodes_[i] == self_) {\n+            follower_context_.push_back(NULL);\n+            continue;\n+        }\n+        FollowerContext* ctx = new FollowerContext(&mu_);\n+        follower_context_.push_back(ctx);\n+        LOG(INFO, \"New follower context %u %s\", i, nodes_[i].c_str());\n+        ctx->next_index = log_index_ + 1;\n+        ctx->worker.AddTask(boost::bind(&RaftNodeImpl::ReplicateLogWorker, this, i));\n+    }\n+}\n+\n+void RaftNodeImpl::Election() {","path":"src/nameserver/raft_node.cc","position":102,"original_position":102,"commit_id":"3ef2f418947a27518c0d16750c45ae8aead975ff","original_commit_id":"3ef2f418947a27518c0d16750c45ae8aead975ff","user":{"login":"yvxiang","id":1382038,"avatar_url":"https://avatars.githubusercontent.com/u/1382038?v=3","gravatar_id":"","url":"https://api.github.com/users/yvxiang","html_url":"https://github.com/yvxiang","followers_url":"https://api.github.com/users/yvxiang/followers","following_url":"https://api.github.com/users/yvxiang/following{/other_user}","gists_url":"https://api.github.com/users/yvxiang/gists{/gist_id}","starred_url":"https://api.github.com/users/yvxiang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yvxiang/subscriptions","organizations_url":"https://api.github.com/users/yvxiang/orgs","repos_url":"https://api.github.com/users/yvxiang/repos","events_url":"https://api.github.com/users/yvxiang/events{/privacy}","received_events_url":"https://api.github.com/users/yvxiang/received_events","type":"User","site_admin":false},"body":"是不是应该定时发个空日志过来，要不然如果一段时间内没有namespace操作，kFollwer状态的会主动发起选举将现任Leader干掉","created_at":"2016-05-14T14:04:35Z","updated_at":"2016-05-14T14:04:35Z","html_url":"https://github.com/bluebore/bfs/pull/280#discussion_r63276673","pull_request_url":"https://api.github.com/repos/bluebore/bfs/pulls/280","_links":{"self":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/comments/63276673"},"html":{"href":"https://github.com/bluebore/bfs/pull/280#discussion_r63276673"},"pull_request":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/280"}}},"pull_request":{"url":"https://api.github.com/repos/bluebore/bfs/pulls/280","id":70086997,"html_url":"https://github.com/bluebore/bfs/pull/280","diff_url":"https://github.com/bluebore/bfs/pull/280.diff","patch_url":"https://github.com/bluebore/bfs/pull/280.patch","issue_url":"https://api.github.com/repos/bluebore/bfs/issues/280","number":280,"state":"open","locked":false,"title":"NameServer HA","user":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-05-14T09:14:13Z","updated_at":"2016-05-14T14:04:35Z","closed_at":null,"merged_at":null,"merge_commit_sha":"b2ab03a2de92fe953162b391f9f327dd59d98256","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/bluebore/bfs/pulls/280/commits","review_comments_url":"https://api.github.com/repos/bluebore/bfs/pulls/280/comments","review_comment_url":"https://api.github.com/repos/bluebore/bfs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bluebore/bfs/issues/280/comments","statuses_url":"https://api.github.com/repos/bluebore/bfs/statuses/3ef2f418947a27518c0d16750c45ae8aead975ff","head":{"label":"bluebore:ha","ref":"ha","sha":"3ef2f418947a27518c0d16750c45ae8aead975ff","user":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"repo":{"id":26562172,"name":"bfs","full_name":"bluebore/bfs","owner":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bluebore/bfs","description":"The Baidu File System.","fork":false,"url":"https://api.github.com/repos/bluebore/bfs","forks_url":"https://api.github.com/repos/bluebore/bfs/forks","keys_url":"https://api.github.com/repos/bluebore/bfs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bluebore/bfs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bluebore/bfs/teams","hooks_url":"https://api.github.com/repos/bluebore/bfs/hooks","issue_events_url":"https://api.github.com/repos/bluebore/bfs/issues/events{/number}","events_url":"https://api.github.com/repos/bluebore/bfs/events","assignees_url":"https://api.github.com/repos/bluebore/bfs/assignees{/user}","branches_url":"https://api.github.com/repos/bluebore/bfs/branches{/branch}","tags_url":"https://api.github.com/repos/bluebore/bfs/tags","blobs_url":"https://api.github.com/repos/bluebore/bfs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bluebore/bfs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bluebore/bfs/git/refs{/sha}","trees_url":"https://api.github.com/repos/bluebore/bfs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bluebore/bfs/statuses/{sha}","languages_url":"https://api.github.com/repos/bluebore/bfs/languages","stargazers_url":"https://api.github.com/repos/bluebore/bfs/stargazers","contributors_url":"https://api.github.com/repos/bluebore/bfs/contributors","subscribers_url":"https://api.github.com/repos/bluebore/bfs/subscribers","subscription_url":"https://api.github.com/repos/bluebore/bfs/subscription","commits_url":"https://api.github.com/repos/bluebore/bfs/commits{/sha}","git_commits_url":"https://api.github.com/repos/bluebore/bfs/git/commits{/sha}","comments_url":"https://api.github.com/repos/bluebore/bfs/comments{/number}","issue_comment_url":"https://api.github.com/repos/bluebore/bfs/issues/comments{/number}","contents_url":"https://api.github.com/repos/bluebore/bfs/contents/{+path}","compare_url":"https://api.github.com/repos/bluebore/bfs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bluebore/bfs/merges","archive_url":"https://api.github.com/repos/bluebore/bfs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bluebore/bfs/downloads","issues_url":"https://api.github.com/repos/bluebore/bfs/issues{/number}","pulls_url":"https://api.github.com/repos/bluebore/bfs/pulls{/number}","milestones_url":"https://api.github.com/repos/bluebore/bfs/milestones{/number}","notifications_url":"https://api.github.com/repos/bluebore/bfs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bluebore/bfs/labels{/name}","releases_url":"https://api.github.com/repos/bluebore/bfs/releases{/id}","deployments_url":"https://api.github.com/repos/bluebore/bfs/deployments","created_at":"2014-11-13T00:00:29Z","updated_at":"2016-05-06T22:39:07Z","pushed_at":"2016-05-14T09:14:14Z","git_url":"git://github.com/bluebore/bfs.git","ssh_url":"git@github.com:bluebore/bfs.git","clone_url":"https://github.com/bluebore/bfs.git","svn_url":"https://github.com/bluebore/bfs","homepage":"","size":4609,"stargazers_count":22,"watchers_count":22,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":17,"mirror_url":null,"open_issues_count":46,"forks":17,"open_issues":46,"watchers":22,"default_branch":"master"}},"base":{"label":"bluebore:master","ref":"master","sha":"f3d5b7b9a3d267542a177abbb8648ce50817cac9","user":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"repo":{"id":26562172,"name":"bfs","full_name":"bluebore/bfs","owner":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bluebore/bfs","description":"The Baidu File System.","fork":false,"url":"https://api.github.com/repos/bluebore/bfs","forks_url":"https://api.github.com/repos/bluebore/bfs/forks","keys_url":"https://api.github.com/repos/bluebore/bfs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bluebore/bfs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bluebore/bfs/teams","hooks_url":"https://api.github.com/repos/bluebore/bfs/hooks","issue_events_url":"https://api.github.com/repos/bluebore/bfs/issues/events{/number}","events_url":"https://api.github.com/repos/bluebore/bfs/events","assignees_url":"https://api.github.com/repos/bluebore/bfs/assignees{/user}","branches_url":"https://api.github.com/repos/bluebore/bfs/branches{/branch}","tags_url":"https://api.github.com/repos/bluebore/bfs/tags","blobs_url":"https://api.github.com/repos/bluebore/bfs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bluebore/bfs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bluebore/bfs/git/refs{/sha}","trees_url":"https://api.github.com/repos/bluebore/bfs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bluebore/bfs/statuses/{sha}","languages_url":"https://api.github.com/repos/bluebore/bfs/languages","stargazers_url":"https://api.github.com/repos/bluebore/bfs/stargazers","contributors_url":"https://api.github.com/repos/bluebore/bfs/contributors","subscribers_url":"https://api.github.com/repos/bluebore/bfs/subscribers","subscription_url":"https://api.github.com/repos/bluebore/bfs/subscription","commits_url":"https://api.github.com/repos/bluebore/bfs/commits{/sha}","git_commits_url":"https://api.github.com/repos/bluebore/bfs/git/commits{/sha}","comments_url":"https://api.github.com/repos/bluebore/bfs/comments{/number}","issue_comment_url":"https://api.github.com/repos/bluebore/bfs/issues/comments{/number}","contents_url":"https://api.github.com/repos/bluebore/bfs/contents/{+path}","compare_url":"https://api.github.com/repos/bluebore/bfs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bluebore/bfs/merges","archive_url":"https://api.github.com/repos/bluebore/bfs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bluebore/bfs/downloads","issues_url":"https://api.github.com/repos/bluebore/bfs/issues{/number}","pulls_url":"https://api.github.com/repos/bluebore/bfs/pulls{/number}","milestones_url":"https://api.github.com/repos/bluebore/bfs/milestones{/number}","notifications_url":"https://api.github.com/repos/bluebore/bfs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bluebore/bfs/labels{/name}","releases_url":"https://api.github.com/repos/bluebore/bfs/releases{/id}","deployments_url":"https://api.github.com/repos/bluebore/bfs/deployments","created_at":"2014-11-13T00:00:29Z","updated_at":"2016-05-06T22:39:07Z","pushed_at":"2016-05-14T09:14:14Z","git_url":"git://github.com/bluebore/bfs.git","ssh_url":"git@github.com:bluebore/bfs.git","clone_url":"https://github.com/bluebore/bfs.git","svn_url":"https://github.com/bluebore/bfs","homepage":"","size":4609,"stargazers_count":22,"watchers_count":22,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":17,"mirror_url":null,"open_issues_count":46,"forks":17,"open_issues":46,"watchers":22,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/280"},"html":{"href":"https://github.com/bluebore/bfs/pull/280"},"issue":{"href":"https://api.github.com/repos/bluebore/bfs/issues/280"},"comments":{"href":"https://api.github.com/repos/bluebore/bfs/issues/280/comments"},"review_comments":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/280/comments"},"review_comment":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/280/commits"},"statuses":{"href":"https://api.github.com/repos/bluebore/bfs/statuses/3ef2f418947a27518c0d16750c45ae8aead975ff"}}}}