{"action":"created","comment":{"url":"https://api.github.com/repos/amplab/tachyon/pulls/comments/38816375","id":38816375,"diff_hunk":"@@ -0,0 +1,571 @@\n+/*\n+ * Licensed to the University of California, Berkeley under one or more contributor license\n+ * agreements. See the NOTICE file distributed with this work for additional information regarding\n+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance with the License. You may obtain a\n+ * copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software distributed under the License\n+ * is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\n+ * or implied. See the License for the specific language governing permissions and limitations under\n+ * the License.\n+ */\n+\n+package tachyon.master.block;\n+\n+import java.io.IOException;\n+import java.util.ArrayList;\n+import java.util.Collection;\n+import java.util.Collections;\n+import java.util.HashMap;\n+import java.util.HashSet;\n+import java.util.List;\n+import java.util.Map;\n+import java.util.Map.Entry;\n+import java.util.Set;\n+import java.util.concurrent.BlockingQueue;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.Future;\n+import java.util.concurrent.LinkedBlockingQueue;\n+import java.util.concurrent.atomic.AtomicLong;\n+\n+import org.apache.thrift.TProcessor;\n+import org.slf4j.Logger;\n+import org.slf4j.LoggerFactory;\n+\n+import tachyon.Constants;\n+import tachyon.HeartbeatExecutor;\n+import tachyon.HeartbeatThread;\n+import tachyon.StorageDirId;\n+import tachyon.StorageLevelAlias;\n+import tachyon.conf.TachyonConf;\n+import tachyon.master.IndexedSet;\n+import tachyon.master.MasterBase;\n+import tachyon.master.block.journal.BlockIdGeneratorEntry;\n+import tachyon.master.block.journal.BlockInfoEntry;\n+import tachyon.master.block.journal.WorkerIdGeneratorEntry;\n+import tachyon.master.block.meta.MasterBlockInfo;\n+import tachyon.master.block.meta.MasterBlockLocation;\n+import tachyon.master.block.meta.MasterWorkerInfo;\n+import tachyon.master.journal.Journal;\n+import tachyon.master.journal.JournalEntry;\n+import tachyon.master.journal.JournalInputStream;\n+import tachyon.master.journal.JournalOutputStream;\n+import tachyon.thrift.BlockInfo;\n+import tachyon.thrift.BlockInfoException;\n+import tachyon.thrift.BlockLocation;\n+import tachyon.thrift.BlockMasterService;\n+import tachyon.thrift.Command;\n+import tachyon.thrift.CommandType;\n+import tachyon.thrift.NetAddress;\n+import tachyon.thrift.WorkerInfo;\n+import tachyon.util.CommonUtils;\n+import tachyon.util.FormatUtils;\n+import tachyon.util.ThreadFactoryUtils;\n+import tachyon.util.io.PathUtils;\n+\n+// TODO (Gene): Add javadoc\n+public final class BlockMaster extends MasterBase implements ContainerIdGenerator {\n+  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);\n+\n+  // Block metadata management.\n+  /**\n+   * blocks ever placed in all Tachyon workers, including both the active blocks and lost ones. This\n+   * state much be journaled.\n+   */\n+  private final Map<Long, MasterBlockInfo> mBlocks = new HashMap<Long, MasterBlockInfo>();\n+  /**\n+   * This state much be journaled.\n+   */\n+  private final BlockIdGenerator mBlockIdGenerator = new BlockIdGenerator();\n+  private final Set<Long> mLostBlocks = new HashSet<Long>();\n+  private final BlockingQueue<MasterWorkerInfo> mLostWorkers =\n+      new LinkedBlockingQueue<MasterWorkerInfo>();\n+  private final TachyonConf mTachyonConf;\n+\n+  /**\n+   * the service that detects lost worker nodes, and tries to restart the failed workers\n+   */\n+  private Future<?> mLostWorkerDetectionService;\n+\n+  // Worker metadata management.\n+  private final IndexedSet.FieldIndex<MasterWorkerInfo> mIdIndex =\n+      new IndexedSet.FieldIndex<MasterWorkerInfo>() {\n+        @Override\n+        public Object getFieldValue(MasterWorkerInfo o) {\n+          return o.getId();\n+        }\n+      };\n+  private final IndexedSet.FieldIndex<MasterWorkerInfo> mAddressIndex =\n+      new IndexedSet.FieldIndex<MasterWorkerInfo>() {\n+        @Override\n+        public Object getFieldValue(MasterWorkerInfo o) {\n+          return o.getAddress();\n+        }\n+      };\n+\n+  @SuppressWarnings(\"unchecked\")\n+  private final IndexedSet<MasterWorkerInfo> mWorkers =\n+      new IndexedSet<MasterWorkerInfo>(mIdIndex, mAddressIndex);\n+  // This state must be journaled.\n+  private final AtomicLong mNextWorkerId = new AtomicLong(1);\n+\n+  public static String getJournalDirectory(String baseDirectory) {\n+    return PathUtils.concatPath(baseDirectory, Constants.BLOCK_MASTER_SERVICE_NAME);\n+  }\n+\n+  public BlockMaster(Journal journal, TachyonConf tachyonConf) {\n+    super(journal,\n+        Executors.newFixedThreadPool(2, ThreadFactoryUtils.build(\"block-master-%d\", true)));\n+    mTachyonConf = tachyonConf;\n+  }\n+\n+  @Override\n+  public TProcessor getProcessor() {\n+    return new BlockMasterService.Processor<BlockMasterServiceHandler>(\n+        new BlockMasterServiceHandler(this));\n+  }\n+\n+  @Override\n+  public String getProcessorName() {\n+    return Constants.BLOCK_MASTER_SERVICE_NAME;\n+  }\n+\n+  @Override\n+  public void processJournalCheckpoint(JournalInputStream inputStream) throws IOException {\n+    // clear state before processing checkpoint.\n+    mBlocks.clear();\n+\n+    super.processJournalCheckpoint(inputStream);\n+  }\n+\n+  @Override\n+  public void processJournalEntry(JournalEntry entry) throws IOException {\n+    if (entry instanceof BlockIdGeneratorEntry) {\n+      mBlockIdGenerator.setNextContainerId(((BlockIdGeneratorEntry) entry).getNextContainerId());\n+    } else if (entry instanceof WorkerIdGeneratorEntry) {\n+      mNextWorkerId.set(((WorkerIdGeneratorEntry) entry).getNextWorkerId());\n+    } else if (entry instanceof BlockInfoEntry) {\n+      BlockInfoEntry bie = (BlockInfoEntry) entry;\n+      mBlocks.put(bie.getBlockId(), new MasterBlockInfo(bie.getBlockId(), bie.getLength()));\n+    } else {\n+      throw new IOException(\"unexpected entry in journal: \" + entry);\n+    }\n+  }\n+\n+  @Override\n+  public void writeToJournal(JournalOutputStream outputStream) throws IOException {\n+    mBlockIdGenerator.writeToJournal(outputStream);\n+    outputStream.writeEntry(new WorkerIdGeneratorEntry(mNextWorkerId.get()));\n+    for (MasterBlockInfo blockInfo : mBlocks.values()) {\n+      outputStream.writeEntry(new BlockInfoEntry(blockInfo.getBlockId(), blockInfo.getLength()));\n+    }\n+  }\n+\n+  @Override\n+  public void start(boolean isLeader) throws IOException {\n+    super.start(isLeader);\n+    if (isLeaderMode()) {\n+      mLostWorkerDetectionService =\n+          getExecutorService().submit(\n+              new HeartbeatThread(\"Lost worker detection service\",\n+                  new LostWorkerDetectionHeartbeatExecutor(), mTachyonConf\n+                      .getInt(Constants.MASTER_HEARTBEAT_INTERVAL_MS)));\n+    }","path":"servers/src/main/java/tachyon/master/block/BlockMaster.java","position":null,"original_position":176,"commit_id":"32bc53169b1ba6af35b4c9ca8b9cc31dc3d1491c","original_commit_id":"54d12f527c5d5abad44a53be8dd54a3b3a5fefc2","user":{"login":"gpang","id":429032,"avatar_url":"https://avatars.githubusercontent.com/u/429032?v=3","gravatar_id":"","url":"https://api.github.com/users/gpang","html_url":"https://github.com/gpang","followers_url":"https://api.github.com/users/gpang/followers","following_url":"https://api.github.com/users/gpang/following{/other_user}","gists_url":"https://api.github.com/users/gpang/gists{/gist_id}","starred_url":"https://api.github.com/users/gpang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gpang/subscriptions","organizations_url":"https://api.github.com/users/gpang/orgs","repos_url":"https://api.github.com/users/gpang/repos","events_url":"https://api.github.com/users/gpang/events{/privacy}","received_events_url":"https://api.github.com/users/gpang/received_events","type":"User","site_admin":false},"body":"re-formatted.","created_at":"2015-09-06T01:09:17Z","updated_at":"2015-09-06T01:09:17Z","html_url":"https://github.com/amplab/tachyon/pull/1475#discussion_r38816375","pull_request_url":"https://api.github.com/repos/amplab/tachyon/pulls/1475","_links":{"self":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/comments/38816375"},"html":{"href":"https://github.com/amplab/tachyon/pull/1475#discussion_r38816375"},"pull_request":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/1475"}}},"pull_request":{"url":"https://api.github.com/repos/amplab/tachyon/pulls/1475","id":44263041,"html_url":"https://github.com/amplab/tachyon/pull/1475","diff_url":"https://github.com/amplab/tachyon/pull/1475.diff","patch_url":"https://github.com/amplab/tachyon/pull/1475.patch","issue_url":"https://api.github.com/repos/amplab/tachyon/issues/1475","number":1475,"state":"open","locked":false,"title":"[WIP] Common and Master Changes for #1464","user":{"login":"calvinjia","id":2223813,"avatar_url":"https://avatars.githubusercontent.com/u/2223813?v=3","gravatar_id":"","url":"https://api.github.com/users/calvinjia","html_url":"https://github.com/calvinjia","followers_url":"https://api.github.com/users/calvinjia/followers","following_url":"https://api.github.com/users/calvinjia/following{/other_user}","gists_url":"https://api.github.com/users/calvinjia/gists{/gist_id}","starred_url":"https://api.github.com/users/calvinjia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/calvinjia/subscriptions","organizations_url":"https://api.github.com/users/calvinjia/orgs","repos_url":"https://api.github.com/users/calvinjia/repos","events_url":"https://api.github.com/users/calvinjia/events{/privacy}","received_events_url":"https://api.github.com/users/calvinjia/received_events","type":"User","site_admin":false},"body":"This is a subset of #1464 with all the integration tests and client changes reverted. This branch should only be used to review and should not be merged. Changes should be directed to the original wip branch and will be merged into this branch.","created_at":"2015-09-04T21:25:21Z","updated_at":"2015-09-06T01:09:17Z","closed_at":null,"merged_at":null,"merge_commit_sha":"38e7d7e126d21323fd7ffce47559d7afec1d489b","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/amplab/tachyon/pulls/1475/commits","review_comments_url":"https://api.github.com/repos/amplab/tachyon/pulls/1475/comments","review_comment_url":"https://api.github.com/repos/amplab/tachyon/pulls/comments{/number}","comments_url":"https://api.github.com/repos/amplab/tachyon/issues/1475/comments","statuses_url":"https://api.github.com/repos/amplab/tachyon/statuses/32bc53169b1ba6af35b4c9ca8b9cc31dc3d1491c","head":{"label":"amplab:wip_master_client_review_1","ref":"wip_master_client_review_1","sha":"32bc53169b1ba6af35b4c9ca8b9cc31dc3d1491c","user":{"login":"amplab","id":1438454,"avatar_url":"https://avatars.githubusercontent.com/u/1438454?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab","html_url":"https://github.com/amplab","followers_url":"https://api.github.com/users/amplab/followers","following_url":"https://api.github.com/users/amplab/following{/other_user}","gists_url":"https://api.github.com/users/amplab/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab/subscriptions","organizations_url":"https://api.github.com/users/amplab/orgs","repos_url":"https://api.github.com/users/amplab/repos","events_url":"https://api.github.com/users/amplab/events{/privacy}","received_events_url":"https://api.github.com/users/amplab/received_events","type":"Organization","site_admin":false},"repo":{"id":7276954,"name":"tachyon","full_name":"amplab/tachyon","owner":{"login":"amplab","id":1438454,"avatar_url":"https://avatars.githubusercontent.com/u/1438454?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab","html_url":"https://github.com/amplab","followers_url":"https://api.github.com/users/amplab/followers","following_url":"https://api.github.com/users/amplab/following{/other_user}","gists_url":"https://api.github.com/users/amplab/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab/subscriptions","organizations_url":"https://api.github.com/users/amplab/orgs","repos_url":"https://api.github.com/users/amplab/repos","events_url":"https://api.github.com/users/amplab/events{/privacy}","received_events_url":"https://api.github.com/users/amplab/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/amplab/tachyon","description":"Memory Centric Reliable Distributed Storage","fork":false,"url":"https://api.github.com/repos/amplab/tachyon","forks_url":"https://api.github.com/repos/amplab/tachyon/forks","keys_url":"https://api.github.com/repos/amplab/tachyon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/amplab/tachyon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/amplab/tachyon/teams","hooks_url":"https://api.github.com/repos/amplab/tachyon/hooks","issue_events_url":"https://api.github.com/repos/amplab/tachyon/issues/events{/number}","events_url":"https://api.github.com/repos/amplab/tachyon/events","assignees_url":"https://api.github.com/repos/amplab/tachyon/assignees{/user}","branches_url":"https://api.github.com/repos/amplab/tachyon/branches{/branch}","tags_url":"https://api.github.com/repos/amplab/tachyon/tags","blobs_url":"https://api.github.com/repos/amplab/tachyon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/amplab/tachyon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/amplab/tachyon/git/refs{/sha}","trees_url":"https://api.github.com/repos/amplab/tachyon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/amplab/tachyon/statuses/{sha}","languages_url":"https://api.github.com/repos/amplab/tachyon/languages","stargazers_url":"https://api.github.com/repos/amplab/tachyon/stargazers","contributors_url":"https://api.github.com/repos/amplab/tachyon/contributors","subscribers_url":"https://api.github.com/repos/amplab/tachyon/subscribers","subscription_url":"https://api.github.com/repos/amplab/tachyon/subscription","commits_url":"https://api.github.com/repos/amplab/tachyon/commits{/sha}","git_commits_url":"https://api.github.com/repos/amplab/tachyon/git/commits{/sha}","comments_url":"https://api.github.com/repos/amplab/tachyon/comments{/number}","issue_comment_url":"https://api.github.com/repos/amplab/tachyon/issues/comments{/number}","contents_url":"https://api.github.com/repos/amplab/tachyon/contents/{+path}","compare_url":"https://api.github.com/repos/amplab/tachyon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/amplab/tachyon/merges","archive_url":"https://api.github.com/repos/amplab/tachyon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/amplab/tachyon/downloads","issues_url":"https://api.github.com/repos/amplab/tachyon/issues{/number}","pulls_url":"https://api.github.com/repos/amplab/tachyon/pulls{/number}","milestones_url":"https://api.github.com/repos/amplab/tachyon/milestones{/number}","notifications_url":"https://api.github.com/repos/amplab/tachyon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/amplab/tachyon/labels{/name}","releases_url":"https://api.github.com/repos/amplab/tachyon/releases{/id}","created_at":"2012-12-21T17:43:46Z","updated_at":"2015-09-05T10:35:40Z","pushed_at":"2015-09-06T01:08:39Z","git_url":"git://github.com/amplab/tachyon.git","ssh_url":"git@github.com:amplab/tachyon.git","clone_url":"https://github.com/amplab/tachyon.git","svn_url":"https://github.com/amplab/tachyon","homepage":"http://tachyon-project.org","size":44632,"stargazers_count":1554,"watchers_count":1554,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":605,"mirror_url":null,"open_issues_count":41,"forks":605,"open_issues":41,"watchers":1554,"default_branch":"master"}},"base":{"label":"amplab:master","ref":"master","sha":"98dd5f80166a458a4f421eee653da9f9b93684a7","user":{"login":"amplab","id":1438454,"avatar_url":"https://avatars.githubusercontent.com/u/1438454?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab","html_url":"https://github.com/amplab","followers_url":"https://api.github.com/users/amplab/followers","following_url":"https://api.github.com/users/amplab/following{/other_user}","gists_url":"https://api.github.com/users/amplab/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab/subscriptions","organizations_url":"https://api.github.com/users/amplab/orgs","repos_url":"https://api.github.com/users/amplab/repos","events_url":"https://api.github.com/users/amplab/events{/privacy}","received_events_url":"https://api.github.com/users/amplab/received_events","type":"Organization","site_admin":false},"repo":{"id":7276954,"name":"tachyon","full_name":"amplab/tachyon","owner":{"login":"amplab","id":1438454,"avatar_url":"https://avatars.githubusercontent.com/u/1438454?v=3","gravatar_id":"","url":"https://api.github.com/users/amplab","html_url":"https://github.com/amplab","followers_url":"https://api.github.com/users/amplab/followers","following_url":"https://api.github.com/users/amplab/following{/other_user}","gists_url":"https://api.github.com/users/amplab/gists{/gist_id}","starred_url":"https://api.github.com/users/amplab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/amplab/subscriptions","organizations_url":"https://api.github.com/users/amplab/orgs","repos_url":"https://api.github.com/users/amplab/repos","events_url":"https://api.github.com/users/amplab/events{/privacy}","received_events_url":"https://api.github.com/users/amplab/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/amplab/tachyon","description":"Memory Centric Reliable Distributed Storage","fork":false,"url":"https://api.github.com/repos/amplab/tachyon","forks_url":"https://api.github.com/repos/amplab/tachyon/forks","keys_url":"https://api.github.com/repos/amplab/tachyon/keys{/key_id}","collaborators_url":"https://api.github.com/repos/amplab/tachyon/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/amplab/tachyon/teams","hooks_url":"https://api.github.com/repos/amplab/tachyon/hooks","issue_events_url":"https://api.github.com/repos/amplab/tachyon/issues/events{/number}","events_url":"https://api.github.com/repos/amplab/tachyon/events","assignees_url":"https://api.github.com/repos/amplab/tachyon/assignees{/user}","branches_url":"https://api.github.com/repos/amplab/tachyon/branches{/branch}","tags_url":"https://api.github.com/repos/amplab/tachyon/tags","blobs_url":"https://api.github.com/repos/amplab/tachyon/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/amplab/tachyon/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/amplab/tachyon/git/refs{/sha}","trees_url":"https://api.github.com/repos/amplab/tachyon/git/trees{/sha}","statuses_url":"https://api.github.com/repos/amplab/tachyon/statuses/{sha}","languages_url":"https://api.github.com/repos/amplab/tachyon/languages","stargazers_url":"https://api.github.com/repos/amplab/tachyon/stargazers","contributors_url":"https://api.github.com/repos/amplab/tachyon/contributors","subscribers_url":"https://api.github.com/repos/amplab/tachyon/subscribers","subscription_url":"https://api.github.com/repos/amplab/tachyon/subscription","commits_url":"https://api.github.com/repos/amplab/tachyon/commits{/sha}","git_commits_url":"https://api.github.com/repos/amplab/tachyon/git/commits{/sha}","comments_url":"https://api.github.com/repos/amplab/tachyon/comments{/number}","issue_comment_url":"https://api.github.com/repos/amplab/tachyon/issues/comments{/number}","contents_url":"https://api.github.com/repos/amplab/tachyon/contents/{+path}","compare_url":"https://api.github.com/repos/amplab/tachyon/compare/{base}...{head}","merges_url":"https://api.github.com/repos/amplab/tachyon/merges","archive_url":"https://api.github.com/repos/amplab/tachyon/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/amplab/tachyon/downloads","issues_url":"https://api.github.com/repos/amplab/tachyon/issues{/number}","pulls_url":"https://api.github.com/repos/amplab/tachyon/pulls{/number}","milestones_url":"https://api.github.com/repos/amplab/tachyon/milestones{/number}","notifications_url":"https://api.github.com/repos/amplab/tachyon/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/amplab/tachyon/labels{/name}","releases_url":"https://api.github.com/repos/amplab/tachyon/releases{/id}","created_at":"2012-12-21T17:43:46Z","updated_at":"2015-09-05T10:35:40Z","pushed_at":"2015-09-06T01:08:39Z","git_url":"git://github.com/amplab/tachyon.git","ssh_url":"git@github.com:amplab/tachyon.git","clone_url":"https://github.com/amplab/tachyon.git","svn_url":"https://github.com/amplab/tachyon","homepage":"http://tachyon-project.org","size":44632,"stargazers_count":1554,"watchers_count":1554,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":605,"mirror_url":null,"open_issues_count":41,"forks":605,"open_issues":41,"watchers":1554,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/1475"},"html":{"href":"https://github.com/amplab/tachyon/pull/1475"},"issue":{"href":"https://api.github.com/repos/amplab/tachyon/issues/1475"},"comments":{"href":"https://api.github.com/repos/amplab/tachyon/issues/1475/comments"},"review_comments":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/1475/comments"},"review_comment":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/amplab/tachyon/pulls/1475/commits"},"statuses":{"href":"https://api.github.com/repos/amplab/tachyon/statuses/32bc53169b1ba6af35b4c9ca8b9cc31dc3d1491c"}}}}