{"action":"created","comment":{"url":"https://api.github.com/repos/google/certificate-transparency/pulls/comments/23091789","id":23091789,"diff_hunk":"@@ -0,0 +1,203 @@\n+#ifndef CERT_TRANS_LOG_CLUSTER_STATE_CONTROLLER_INL_H_\n+#define CERT_TRANS_LOG_CLUSTER_STATE_CONTROLLER_INL_H_\n+\n+#include \"log/cluster_state_controller.h\"\n+#include \"log/etcd_consistent_store-inl.h\"\n+\n+#include <functional>\n+\n+namespace cert_trans {\n+\n+\n+template <class Logged>\n+ClusterStateController<Logged>::ClusterStateController(\n+    util::Executor* executor, ConsistentStore<Logged>* store,\n+    const MasterElection* election, const int min_serving_nodes,\n+    const double min_serving_fraction)\n+    : executor_(CHECK_NOTNULL(executor)),\n+      store_(CHECK_NOTNULL(store)),\n+      election_(CHECK_NOTNULL(election)),\n+      min_serving_nodes_(min_serving_nodes),\n+      min_serving_fraction_(min_serving_fraction),\n+      exiting_(false),\n+      update_required_(false),\n+      cluster_serving_sth_update_thread_(\n+          std::bind(&ClusterStateController<Logged>::ClusterServingSTHUpdater,\n+                    this)) {\n+  store_->WatchClusterNodeStates(\n+      std::bind(&ClusterStateController::OnClusterStateUpdated, this,\n+                std::placeholders::_1));\n+}\n+\n+\n+template <class Logged>\n+ClusterStateController<Logged>::~ClusterStateController() {\n+  {\n+    std::unique_lock<std::mutex> lock(mutex_);\n+    exiting_ = true;\n+    update_required_cv_.notify_all();\n+  }\n+  cluster_serving_sth_update_thread_.join();\n+}\n+\n+\n+template <class Logged>\n+void ClusterStateController<Logged>::NewTreeHead(\n+    const ct::SignedTreeHead& sth) {\n+  CHECK(sth.IsInitialized());\n+  std::unique_lock<std::mutex> lock(mutex_);\n+  if (local_node_state_.has_newest_sth()) {\n+    CHECK_GE(sth.timestamp(), local_node_state_.newest_sth().timestamp());\n+  }\n+  local_node_state_.mutable_newest_sth()->CopyFrom(sth);\n+  PushLocalNodeState(lock);\n+}\n+\n+\n+template <class Logged>\n+void ClusterStateController<Logged>::ContiguousTreeSizeUpdated(\n+    const int64_t new_contiguous_tree_size) {\n+  CHECK_GE(new_contiguous_tree_size, 0);\n+  std::unique_lock<std::mutex> lock(mutex_);\n+  CHECK_GE(new_contiguous_tree_size, local_node_state_.contiguous_tree_size());\n+  local_node_state_.set_contiguous_tree_size(new_contiguous_tree_size);\n+  PushLocalNodeState(lock);\n+}\n+\n+\n+template <class Logged>\n+bool ClusterStateController<Logged>::GetCalculatedServingSTH(\n+    SignedTreeHead* sth) const {\n+  CHECK_NOTNULL(sth);\n+  std::lock_guard<std::mutex> lock(mutex_);\n+  if (calculated_serving_sth_.get() == nullptr) {\n+    return false;\n+  }\n+  *sth = *calculated_serving_sth_;\n+  return true;\n+}\n+\n+\n+template <class Logged>\n+void ClusterStateController<Logged>::PushLocalNodeState(\n+    const std::unique_lock<std::mutex>& lock) {\n+  CHECK(lock.owns_lock());\n+  const util::Status status(store_->SetClusterNodeState(local_node_state_));\n+  if (!status.ok()) {\n+    LOG(WARNING) << status;\n+  }\n+}\n+\n+\n+template <class Logged>\n+void ClusterStateController<Logged>::OnClusterStateUpdated(\n+    const std::vector<Update<ct::ClusterNodeState>>& updates) {\n+  std::unique_lock<std::mutex> lock(mutex_);\n+  for (const auto& update : updates) {\n+    const std::string& node_id(update.handle_.Entry().node_id());\n+    if (update.exists_) {\n+      all_node_states_[node_id] = update.handle_.Entry();\n+    } else {\n+      CHECK_EQ(1, all_node_states_.erase(node_id));\n+    }\n+  }\n+\n+  if (CalculateServingSTH(lock) && election_->IsMaster()) {\n+    update_required_ = true;\n+    update_required_cv_.notify_all();\n+  }\n+}\n+\n+\n+template <class Logged>\n+bool ClusterStateController<Logged>::CalculateServingSTH(\n+    const std::unique_lock<std::mutex>& lock) {\n+  VLOG(1) << \"Calculating new ServingSTH...\";\n+  CHECK(lock.owns_lock());\n+\n+  // First, create a mapping of tree size to number of nodes at that size, and\n+  // a mapping of the newst STH for any given size:\n+  std::map<uint64_t, ct::SignedTreeHead> sth_by_size;\n+  std::map<uint64_t, int> num_nodes_by_sth_size;\n+  for (const auto& node : all_node_states_) {\n+    if (node.second.has_newest_sth()) {\n+      const uint64_t tree_size(node.second.newest_sth().tree_size());\n+      num_nodes_by_sth_size[tree_size]++;\n+      if (node.second.newest_sth().timestamp() >\n+          sth_by_size[tree_size].timestamp()) {","path":"cpp/log/cluster_state_controller-inl.h","position":128,"original_position":127,"commit_id":"bd8711f681f8aadeb6cf51cfe136a18c8ee38800","original_commit_id":"0bbb457b0c21428ba18c30d3feb4743f0ad61af0","user":{"login":"AlCutter","id":7648032,"avatar_url":"https://avatars.githubusercontent.com/u/7648032?v=3","gravatar_id":"","url":"https://api.github.com/users/AlCutter","html_url":"https://github.com/AlCutter","followers_url":"https://api.github.com/users/AlCutter/followers","following_url":"https://api.github.com/users/AlCutter/following{/other_user}","gists_url":"https://api.github.com/users/AlCutter/gists{/gist_id}","starred_url":"https://api.github.com/users/AlCutter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlCutter/subscriptions","organizations_url":"https://api.github.com/users/AlCutter/orgs","repos_url":"https://api.github.com/users/AlCutter/repos","events_url":"https://api.github.com/users/AlCutter/events{/privacy}","received_events_url":"https://api.github.com/users/AlCutter/received_events","type":"User","site_admin":false},"body":"Yep, exactly.\r\nAdded a comment.","created_at":"2015-01-16T16:42:14Z","updated_at":"2015-01-16T16:42:14Z","html_url":"https://github.com/google/certificate-transparency/pull/400#discussion_r23091789","pull_request_url":"https://api.github.com/repos/google/certificate-transparency/pulls/400","_links":{"self":{"href":"https://api.github.com/repos/google/certificate-transparency/pulls/comments/23091789"},"html":{"href":"https://github.com/google/certificate-transparency/pull/400#discussion_r23091789"},"pull_request":{"href":"https://api.github.com/repos/google/certificate-transparency/pulls/400"}}},"pull_request":{"url":"https://api.github.com/repos/google/certificate-transparency/pulls/400","id":27423710,"html_url":"https://github.com/google/certificate-transparency/pull/400","diff_url":"https://github.com/google/certificate-transparency/pull/400.diff","patch_url":"https://github.com/google/certificate-transparency/pull/400.patch","issue_url":"https://api.github.com/repos/google/certificate-transparency/issues/400","number":400,"state":"open","locked":false,"title":"Add ClusterStateController to determine current serving STH.","user":{"login":"AlCutter","id":7648032,"avatar_url":"https://avatars.githubusercontent.com/u/7648032?v=3","gravatar_id":"","url":"https://api.github.com/users/AlCutter","html_url":"https://github.com/AlCutter","followers_url":"https://api.github.com/users/AlCutter/followers","following_url":"https://api.github.com/users/AlCutter/following{/other_user}","gists_url":"https://api.github.com/users/AlCutter/gists{/gist_id}","starred_url":"https://api.github.com/users/AlCutter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlCutter/subscriptions","organizations_url":"https://api.github.com/users/AlCutter/orgs","repos_url":"https://api.github.com/users/AlCutter/repos","events_url":"https://api.github.com/users/AlCutter/events{/privacy}","received_events_url":"https://api.github.com/users/AlCutter/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-01-15T12:05:57Z","updated_at":"2015-01-16T16:42:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bb650159fbfecc3389727dad14a67474cc5dda57","assignee":{"login":"pphaneuf","id":40400,"avatar_url":"https://avatars.githubusercontent.com/u/40400?v=3","gravatar_id":"","url":"https://api.github.com/users/pphaneuf","html_url":"https://github.com/pphaneuf","followers_url":"https://api.github.com/users/pphaneuf/followers","following_url":"https://api.github.com/users/pphaneuf/following{/other_user}","gists_url":"https://api.github.com/users/pphaneuf/gists{/gist_id}","starred_url":"https://api.github.com/users/pphaneuf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pphaneuf/subscriptions","organizations_url":"https://api.github.com/users/pphaneuf/orgs","repos_url":"https://api.github.com/users/pphaneuf/repos","events_url":"https://api.github.com/users/pphaneuf/events{/privacy}","received_events_url":"https://api.github.com/users/pphaneuf/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/google/certificate-transparency/pulls/400/commits","review_comments_url":"https://api.github.com/repos/google/certificate-transparency/pulls/400/comments","review_comment_url":"https://api.github.com/repos/google/certificate-transparency/pulls/comments/{number}","comments_url":"https://api.github.com/repos/google/certificate-transparency/issues/400/comments","statuses_url":"https://api.github.com/repos/google/certificate-transparency/statuses/bd8711f681f8aadeb6cf51cfe136a18c8ee38800","head":{"label":"AlCutter:cluster_state","ref":"cluster_state","sha":"bd8711f681f8aadeb6cf51cfe136a18c8ee38800","user":{"login":"AlCutter","id":7648032,"avatar_url":"https://avatars.githubusercontent.com/u/7648032?v=3","gravatar_id":"","url":"https://api.github.com/users/AlCutter","html_url":"https://github.com/AlCutter","followers_url":"https://api.github.com/users/AlCutter/followers","following_url":"https://api.github.com/users/AlCutter/following{/other_user}","gists_url":"https://api.github.com/users/AlCutter/gists{/gist_id}","starred_url":"https://api.github.com/users/AlCutter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlCutter/subscriptions","organizations_url":"https://api.github.com/users/AlCutter/orgs","repos_url":"https://api.github.com/users/AlCutter/repos","events_url":"https://api.github.com/users/AlCutter/events{/privacy}","received_events_url":"https://api.github.com/users/AlCutter/received_events","type":"User","site_admin":false},"repo":{"id":23961092,"name":"certificate-transparency","full_name":"AlCutter/certificate-transparency","owner":{"login":"AlCutter","id":7648032,"avatar_url":"https://avatars.githubusercontent.com/u/7648032?v=3","gravatar_id":"","url":"https://api.github.com/users/AlCutter","html_url":"https://github.com/AlCutter","followers_url":"https://api.github.com/users/AlCutter/followers","following_url":"https://api.github.com/users/AlCutter/following{/other_user}","gists_url":"https://api.github.com/users/AlCutter/gists{/gist_id}","starred_url":"https://api.github.com/users/AlCutter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AlCutter/subscriptions","organizations_url":"https://api.github.com/users/AlCutter/orgs","repos_url":"https://api.github.com/users/AlCutter/repos","events_url":"https://api.github.com/users/AlCutter/events{/privacy}","received_events_url":"https://api.github.com/users/AlCutter/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/AlCutter/certificate-transparency","description":"Auditing for TLS certificates.","fork":true,"url":"https://api.github.com/repos/AlCutter/certificate-transparency","forks_url":"https://api.github.com/repos/AlCutter/certificate-transparency/forks","keys_url":"https://api.github.com/repos/AlCutter/certificate-transparency/keys{/key_id}","collaborators_url":"https://api.github.com/repos/AlCutter/certificate-transparency/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/AlCutter/certificate-transparency/teams","hooks_url":"https://api.github.com/repos/AlCutter/certificate-transparency/hooks","issue_events_url":"https://api.github.com/repos/AlCutter/certificate-transparency/issues/events{/number}","events_url":"https://api.github.com/repos/AlCutter/certificate-transparency/events","assignees_url":"https://api.github.com/repos/AlCutter/certificate-transparency/assignees{/user}","branches_url":"https://api.github.com/repos/AlCutter/certificate-transparency/branches{/branch}","tags_url":"https://api.github.com/repos/AlCutter/certificate-transparency/tags","blobs_url":"https://api.github.com/repos/AlCutter/certificate-transparency/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/AlCutter/certificate-transparency/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/AlCutter/certificate-transparency/git/refs{/sha}","trees_url":"https://api.github.com/repos/AlCutter/certificate-transparency/git/trees{/sha}","statuses_url":"https://api.github.com/repos/AlCutter/certificate-transparency/statuses/{sha}","languages_url":"https://api.github.com/repos/AlCutter/certificate-transparency/languages","stargazers_url":"https://api.github.com/repos/AlCutter/certificate-transparency/stargazers","contributors_url":"https://api.github.com/repos/AlCutter/certificate-transparency/contributors","subscribers_url":"https://api.github.com/repos/AlCutter/certificate-transparency/subscribers","subscription_url":"https://api.github.com/repos/AlCutter/certificate-transparency/subscription","commits_url":"https://api.github.com/repos/AlCutter/certificate-transparency/commits{/sha}","git_commits_url":"https://api.github.com/repos/AlCutter/certificate-transparency/git/commits{/sha}","comments_url":"https://api.github.com/repos/AlCutter/certificate-transparency/comments{/number}","issue_comment_url":"https://api.github.com/repos/AlCutter/certificate-transparency/issues/comments/{number}","contents_url":"https://api.github.com/repos/AlCutter/certificate-transparency/contents/{+path}","compare_url":"https://api.github.com/repos/AlCutter/certificate-transparency/compare/{base}...{head}","merges_url":"https://api.github.com/repos/AlCutter/certificate-transparency/merges","archive_url":"https://api.github.com/repos/AlCutter/certificate-transparency/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/AlCutter/certificate-transparency/downloads","issues_url":"https://api.github.com/repos/AlCutter/certificate-transparency/issues{/number}","pulls_url":"https://api.github.com/repos/AlCutter/certificate-transparency/pulls{/number}","milestones_url":"https://api.github.com/repos/AlCutter/certificate-transparency/milestones{/number}","notifications_url":"https://api.github.com/repos/AlCutter/certificate-transparency/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/AlCutter/certificate-transparency/labels{/name}","releases_url":"https://api.github.com/repos/AlCutter/certificate-transparency/releases{/id}","created_at":"2014-09-12T13:11:02Z","updated_at":"2014-11-27T12:57:17Z","pushed_at":"2015-01-16T16:13:32Z","git_url":"git://github.com/AlCutter/certificate-transparency.git","ssh_url":"git@github.com:AlCutter/certificate-transparency.git","clone_url":"https://github.com/AlCutter/certificate-transparency.git","svn_url":"https://github.com/AlCutter/certificate-transparency","homepage":"http://www.certificate-transparency.org/","size":18204,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"google:super_duper","ref":"super_duper","sha":"36cb671981400c97db43dd3b6844a8ff88f9214c","user":{"login":"google","id":1342004,"avatar_url":"https://avatars.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"repo":{"id":19989605,"name":"certificate-transparency","full_name":"google/certificate-transparency","owner":{"login":"google","id":1342004,"avatar_url":"https://avatars.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/google/certificate-transparency","description":"Auditing for TLS certificates.","fork":false,"url":"https://api.github.com/repos/google/certificate-transparency","forks_url":"https://api.github.com/repos/google/certificate-transparency/forks","keys_url":"https://api.github.com/repos/google/certificate-transparency/keys{/key_id}","collaborators_url":"https://api.github.com/repos/google/certificate-transparency/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/google/certificate-transparency/teams","hooks_url":"https://api.github.com/repos/google/certificate-transparency/hooks","issue_events_url":"https://api.github.com/repos/google/certificate-transparency/issues/events{/number}","events_url":"https://api.github.com/repos/google/certificate-transparency/events","assignees_url":"https://api.github.com/repos/google/certificate-transparency/assignees{/user}","branches_url":"https://api.github.com/repos/google/certificate-transparency/branches{/branch}","tags_url":"https://api.github.com/repos/google/certificate-transparency/tags","blobs_url":"https://api.github.com/repos/google/certificate-transparency/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/google/certificate-transparency/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/google/certificate-transparency/git/refs{/sha}","trees_url":"https://api.github.com/repos/google/certificate-transparency/git/trees{/sha}","statuses_url":"https://api.github.com/repos/google/certificate-transparency/statuses/{sha}","languages_url":"https://api.github.com/repos/google/certificate-transparency/languages","stargazers_url":"https://api.github.com/repos/google/certificate-transparency/stargazers","contributors_url":"https://api.github.com/repos/google/certificate-transparency/contributors","subscribers_url":"https://api.github.com/repos/google/certificate-transparency/subscribers","subscription_url":"https://api.github.com/repos/google/certificate-transparency/subscription","commits_url":"https://api.github.com/repos/google/certificate-transparency/commits{/sha}","git_commits_url":"https://api.github.com/repos/google/certificate-transparency/git/commits{/sha}","comments_url":"https://api.github.com/repos/google/certificate-transparency/comments{/number}","issue_comment_url":"https://api.github.com/repos/google/certificate-transparency/issues/comments/{number}","contents_url":"https://api.github.com/repos/google/certificate-transparency/contents/{+path}","compare_url":"https://api.github.com/repos/google/certificate-transparency/compare/{base}...{head}","merges_url":"https://api.github.com/repos/google/certificate-transparency/merges","archive_url":"https://api.github.com/repos/google/certificate-transparency/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/google/certificate-transparency/downloads","issues_url":"https://api.github.com/repos/google/certificate-transparency/issues{/number}","pulls_url":"https://api.github.com/repos/google/certificate-transparency/pulls{/number}","milestones_url":"https://api.github.com/repos/google/certificate-transparency/milestones{/number}","notifications_url":"https://api.github.com/repos/google/certificate-transparency/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/google/certificate-transparency/labels{/name}","releases_url":"https://api.github.com/repos/google/certificate-transparency/releases{/id}","created_at":"2014-05-20T17:03:47Z","updated_at":"2015-01-15T20:29:18Z","pushed_at":"2015-01-15T14:13:51Z","git_url":"git://github.com/google/certificate-transparency.git","ssh_url":"git@github.com:google/certificate-transparency.git","clone_url":"https://github.com/google/certificate-transparency.git","svn_url":"https://github.com/google/certificate-transparency","homepage":"http://www.certificate-transparency.org/","size":21312,"stargazers_count":81,"watchers_count":81,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":30,"mirror_url":null,"open_issues_count":37,"forks":30,"open_issues":37,"watchers":81,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/google/certificate-transparency/pulls/400"},"html":{"href":"https://github.com/google/certificate-transparency/pull/400"},"issue":{"href":"https://api.github.com/repos/google/certificate-transparency/issues/400"},"comments":{"href":"https://api.github.com/repos/google/certificate-transparency/issues/400/comments"},"review_comments":{"href":"https://api.github.com/repos/google/certificate-transparency/pulls/400/comments"},"review_comment":{"href":"https://api.github.com/repos/google/certificate-transparency/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/google/certificate-transparency/pulls/400/commits"},"statuses":{"href":"https://api.github.com/repos/google/certificate-transparency/statuses/bd8711f681f8aadeb6cf51cfe136a18c8ee38800"}}}}