{"action":"created","comment":{"url":"https://api.github.com/repos/mediachain/mediachain/pulls/comments/62703448","id":62703448,"diff_hunk":"@@ -0,0 +1,92 @@\n+package io.mediachain.datastore\n+\n+import java.util.Random\n+import java.util.concurrent.LinkedBlockingDeque\n+import com.amazonaws.AmazonServiceException\n+import org.slf4j.{Logger, LoggerFactory}\n+import io.mediachain.multihash.MultiHash\n+\n+class PersistentDatastore(config: PersistentDatastore.Config)\n+  extends BinaryDatastore with AutoCloseable with Runnable {\n+  \n+  val logger = LoggerFactory.getLogger(classOf[PersistentDatastore])\n+  val dynamo = new DynamoDatastore(config.dynamo)\n+  val rocks = new RocksDatastore(config.rocks)\n+  val random = new Random\n+  val maxBackoffRetry = 60 // second\n+  val queue = new LinkedBlockingDeque[MultiHash]\n+  val writer = new Thread(this, \"PersistentDataStore#write\")\n+  writer.start()\n+  \n+  override def putData(key: MultiHash, value: Array[Byte]) {\n+    rocks.putData(key, value)\n+    queue.putLast(key)\n+  }\n+\n+  override def getData(key: MultiHash): Option[Array[Byte]] = {\n+    rocks.getData(key).orElse {dynamo.getData(key)}\n+  }\n+  \n+  override def close() {\n+    writer.interrupt()\n+    writer.join()\n+    rocks.close()\n+    dynamo.close()\n+  }\n+  \n+  // background writer\n+  override def run() {\n+    try {\n+      recover()\n+      loop(0)\n+    } catch {\n+      case e: InterruptedException => ()\n+      case e: Throwable => logger.error(\"Unhandled exception\", e)\n+    }\n+  }\n+  \n+  private def recover() {\n+    // Crash recovery: schedule all keys still in rocks db for\n+    //  write-through to dynamo\n+    rocks.getKeys.foreach {key => queue.putLast(key)}\n+  }\n+  \n+  private def loop(backoff: Int) {\n+    if (!Thread.interrupted) {\n+      writeNext() match {\n+        case Some(key) => {","path":"transactor/src/main/scala/io/mediachain/datastore/PersistentDatastore.scala","position":57,"original_position":57,"commit_id":"e1d4609ebb020aa092423f49367bc61d071ad8ce","original_commit_id":"cd643a393fa127c6f2b48869c79d0ff2dec3ad5f","user":{"login":"vyzo","id":36426,"avatar_url":"https://avatars.githubusercontent.com/u/36426?v=3","gravatar_id":"","url":"https://api.github.com/users/vyzo","html_url":"https://github.com/vyzo","followers_url":"https://api.github.com/users/vyzo/followers","following_url":"https://api.github.com/users/vyzo/following{/other_user}","gists_url":"https://api.github.com/users/vyzo/gists{/gist_id}","starred_url":"https://api.github.com/users/vyzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vyzo/subscriptions","organizations_url":"https://api.github.com/users/vyzo/orgs","repos_url":"https://api.github.com/users/vyzo/repos","events_url":"https://api.github.com/users/vyzo/events{/privacy}","received_events_url":"https://api.github.com/users/vyzo/received_events","type":"User","site_admin":false},"body":"ok, will remove noise.","created_at":"2016-05-10T16:18:57Z","updated_at":"2016-05-10T16:18:57Z","html_url":"https://github.com/mediachain/mediachain/pull/68#discussion_r62703448","pull_request_url":"https://api.github.com/repos/mediachain/mediachain/pulls/68","_links":{"self":{"href":"https://api.github.com/repos/mediachain/mediachain/pulls/comments/62703448"},"html":{"href":"https://github.com/mediachain/mediachain/pull/68#discussion_r62703448"},"pull_request":{"href":"https://api.github.com/repos/mediachain/mediachain/pulls/68"}}},"pull_request":{"url":"https://api.github.com/repos/mediachain/mediachain/pulls/68","id":69480513,"html_url":"https://github.com/mediachain/mediachain/pull/68","diff_url":"https://github.com/mediachain/mediachain/pull/68.diff","patch_url":"https://github.com/mediachain/mediachain/pull/68.patch","issue_url":"https://api.github.com/repos/mediachain/mediachain/issues/68","number":68,"state":"open","locked":false,"title":"Perisistent Datastore implementation","user":{"login":"vyzo","id":36426,"avatar_url":"https://avatars.githubusercontent.com/u/36426?v=3","gravatar_id":"","url":"https://api.github.com/users/vyzo","html_url":"https://github.com/vyzo","followers_url":"https://api.github.com/users/vyzo/followers","following_url":"https://api.github.com/users/vyzo/following{/other_user}","gists_url":"https://api.github.com/users/vyzo/gists{/gist_id}","starred_url":"https://api.github.com/users/vyzo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vyzo/subscriptions","organizations_url":"https://api.github.com/users/vyzo/orgs","repos_url":"https://api.github.com/users/vyzo/repos","events_url":"https://api.github.com/users/vyzo/events{/privacy}","received_events_url":"https://api.github.com/users/vyzo/received_events","type":"User","site_admin":false},"body":"#61 #26 \r\n\r\nImplements a PersistentDatastore which acts a write-through cache to Dynamo:\r\ndata are disk-persisted in a local rocks datastore until they are written through to Dynamo by a background thread.\r\n\r\nFurther refinements:\r\n- more complete error handling\r\n- perhaps chattier logs to help with debugging","created_at":"2016-05-10T10:12:13Z","updated_at":"2016-05-10T16:18:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"721bb78291073215aa673ecf0cd0822e7b305570","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mediachain/mediachain/pulls/68/commits","review_comments_url":"https://api.github.com/repos/mediachain/mediachain/pulls/68/comments","review_comment_url":"https://api.github.com/repos/mediachain/mediachain/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mediachain/mediachain/issues/68/comments","statuses_url":"https://api.github.com/repos/mediachain/mediachain/statuses/e1d4609ebb020aa092423f49367bc61d071ad8ce","head":{"label":"mediachain:vyzo-persistent-datastore","ref":"vyzo-persistent-datastore","sha":"e1d4609ebb020aa092423f49367bc61d071ad8ce","user":{"login":"mediachain","id":16711838,"avatar_url":"https://avatars.githubusercontent.com/u/16711838?v=3","gravatar_id":"","url":"https://api.github.com/users/mediachain","html_url":"https://github.com/mediachain","followers_url":"https://api.github.com/users/mediachain/followers","following_url":"https://api.github.com/users/mediachain/following{/other_user}","gists_url":"https://api.github.com/users/mediachain/gists{/gist_id}","starred_url":"https://api.github.com/users/mediachain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mediachain/subscriptions","organizations_url":"https://api.github.com/users/mediachain/orgs","repos_url":"https://api.github.com/users/mediachain/repos","events_url":"https://api.github.com/users/mediachain/events{/privacy}","received_events_url":"https://api.github.com/users/mediachain/received_events","type":"Organization","site_admin":false},"repo":{"id":49516983,"name":"mediachain","full_name":"mediachain/mediachain","owner":{"login":"mediachain","id":16711838,"avatar_url":"https://avatars.githubusercontent.com/u/16711838?v=3","gravatar_id":"","url":"https://api.github.com/users/mediachain","html_url":"https://github.com/mediachain","followers_url":"https://api.github.com/users/mediachain/followers","following_url":"https://api.github.com/users/mediachain/following{/other_user}","gists_url":"https://api.github.com/users/mediachain/gists{/gist_id}","starred_url":"https://api.github.com/users/mediachain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mediachain/subscriptions","organizations_url":"https://api.github.com/users/mediachain/orgs","repos_url":"https://api.github.com/users/mediachain/repos","events_url":"https://api.github.com/users/mediachain/events{/privacy}","received_events_url":"https://api.github.com/users/mediachain/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mediachain/mediachain","description":"A distributed metadata protocol","fork":false,"url":"https://api.github.com/repos/mediachain/mediachain","forks_url":"https://api.github.com/repos/mediachain/mediachain/forks","keys_url":"https://api.github.com/repos/mediachain/mediachain/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mediachain/mediachain/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mediachain/mediachain/teams","hooks_url":"https://api.github.com/repos/mediachain/mediachain/hooks","issue_events_url":"https://api.github.com/repos/mediachain/mediachain/issues/events{/number}","events_url":"https://api.github.com/repos/mediachain/mediachain/events","assignees_url":"https://api.github.com/repos/mediachain/mediachain/assignees{/user}","branches_url":"https://api.github.com/repos/mediachain/mediachain/branches{/branch}","tags_url":"https://api.github.com/repos/mediachain/mediachain/tags","blobs_url":"https://api.github.com/repos/mediachain/mediachain/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mediachain/mediachain/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mediachain/mediachain/git/refs{/sha}","trees_url":"https://api.github.com/repos/mediachain/mediachain/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mediachain/mediachain/statuses/{sha}","languages_url":"https://api.github.com/repos/mediachain/mediachain/languages","stargazers_url":"https://api.github.com/repos/mediachain/mediachain/stargazers","contributors_url":"https://api.github.com/repos/mediachain/mediachain/contributors","subscribers_url":"https://api.github.com/repos/mediachain/mediachain/subscribers","subscription_url":"https://api.github.com/repos/mediachain/mediachain/subscription","commits_url":"https://api.github.com/repos/mediachain/mediachain/commits{/sha}","git_commits_url":"https://api.github.com/repos/mediachain/mediachain/git/commits{/sha}","comments_url":"https://api.github.com/repos/mediachain/mediachain/comments{/number}","issue_comment_url":"https://api.github.com/repos/mediachain/mediachain/issues/comments{/number}","contents_url":"https://api.github.com/repos/mediachain/mediachain/contents/{+path}","compare_url":"https://api.github.com/repos/mediachain/mediachain/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mediachain/mediachain/merges","archive_url":"https://api.github.com/repos/mediachain/mediachain/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mediachain/mediachain/downloads","issues_url":"https://api.github.com/repos/mediachain/mediachain/issues{/number}","pulls_url":"https://api.github.com/repos/mediachain/mediachain/pulls{/number}","milestones_url":"https://api.github.com/repos/mediachain/mediachain/milestones{/number}","notifications_url":"https://api.github.com/repos/mediachain/mediachain/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mediachain/mediachain/labels{/name}","releases_url":"https://api.github.com/repos/mediachain/mediachain/releases{/id}","deployments_url":"https://api.github.com/repos/mediachain/mediachain/deployments","created_at":"2016-01-12T17:29:52Z","updated_at":"2016-05-09T10:09:48Z","pushed_at":"2016-05-10T15:12:54Z","git_url":"git://github.com/mediachain/mediachain.git","ssh_url":"git@github.com:mediachain/mediachain.git","clone_url":"https://github.com/mediachain/mediachain.git","svn_url":"https://github.com/mediachain/mediachain","homepage":"https://medium.com/mine-labs/mediachain-483f49cbe37a","size":1175,"stargazers_count":34,"watchers_count":34,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":19,"forks":0,"open_issues":19,"watchers":34,"default_branch":"master"}},"base":{"label":"mediachain:master","ref":"master","sha":"e5413958a0cd6a4683051f71bd226d42ae7655a4","user":{"login":"mediachain","id":16711838,"avatar_url":"https://avatars.githubusercontent.com/u/16711838?v=3","gravatar_id":"","url":"https://api.github.com/users/mediachain","html_url":"https://github.com/mediachain","followers_url":"https://api.github.com/users/mediachain/followers","following_url":"https://api.github.com/users/mediachain/following{/other_user}","gists_url":"https://api.github.com/users/mediachain/gists{/gist_id}","starred_url":"https://api.github.com/users/mediachain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mediachain/subscriptions","organizations_url":"https://api.github.com/users/mediachain/orgs","repos_url":"https://api.github.com/users/mediachain/repos","events_url":"https://api.github.com/users/mediachain/events{/privacy}","received_events_url":"https://api.github.com/users/mediachain/received_events","type":"Organization","site_admin":false},"repo":{"id":49516983,"name":"mediachain","full_name":"mediachain/mediachain","owner":{"login":"mediachain","id":16711838,"avatar_url":"https://avatars.githubusercontent.com/u/16711838?v=3","gravatar_id":"","url":"https://api.github.com/users/mediachain","html_url":"https://github.com/mediachain","followers_url":"https://api.github.com/users/mediachain/followers","following_url":"https://api.github.com/users/mediachain/following{/other_user}","gists_url":"https://api.github.com/users/mediachain/gists{/gist_id}","starred_url":"https://api.github.com/users/mediachain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mediachain/subscriptions","organizations_url":"https://api.github.com/users/mediachain/orgs","repos_url":"https://api.github.com/users/mediachain/repos","events_url":"https://api.github.com/users/mediachain/events{/privacy}","received_events_url":"https://api.github.com/users/mediachain/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mediachain/mediachain","description":"A distributed metadata protocol","fork":false,"url":"https://api.github.com/repos/mediachain/mediachain","forks_url":"https://api.github.com/repos/mediachain/mediachain/forks","keys_url":"https://api.github.com/repos/mediachain/mediachain/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mediachain/mediachain/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mediachain/mediachain/teams","hooks_url":"https://api.github.com/repos/mediachain/mediachain/hooks","issue_events_url":"https://api.github.com/repos/mediachain/mediachain/issues/events{/number}","events_url":"https://api.github.com/repos/mediachain/mediachain/events","assignees_url":"https://api.github.com/repos/mediachain/mediachain/assignees{/user}","branches_url":"https://api.github.com/repos/mediachain/mediachain/branches{/branch}","tags_url":"https://api.github.com/repos/mediachain/mediachain/tags","blobs_url":"https://api.github.com/repos/mediachain/mediachain/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mediachain/mediachain/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mediachain/mediachain/git/refs{/sha}","trees_url":"https://api.github.com/repos/mediachain/mediachain/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mediachain/mediachain/statuses/{sha}","languages_url":"https://api.github.com/repos/mediachain/mediachain/languages","stargazers_url":"https://api.github.com/repos/mediachain/mediachain/stargazers","contributors_url":"https://api.github.com/repos/mediachain/mediachain/contributors","subscribers_url":"https://api.github.com/repos/mediachain/mediachain/subscribers","subscription_url":"https://api.github.com/repos/mediachain/mediachain/subscription","commits_url":"https://api.github.com/repos/mediachain/mediachain/commits{/sha}","git_commits_url":"https://api.github.com/repos/mediachain/mediachain/git/commits{/sha}","comments_url":"https://api.github.com/repos/mediachain/mediachain/comments{/number}","issue_comment_url":"https://api.github.com/repos/mediachain/mediachain/issues/comments{/number}","contents_url":"https://api.github.com/repos/mediachain/mediachain/contents/{+path}","compare_url":"https://api.github.com/repos/mediachain/mediachain/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mediachain/mediachain/merges","archive_url":"https://api.github.com/repos/mediachain/mediachain/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mediachain/mediachain/downloads","issues_url":"https://api.github.com/repos/mediachain/mediachain/issues{/number}","pulls_url":"https://api.github.com/repos/mediachain/mediachain/pulls{/number}","milestones_url":"https://api.github.com/repos/mediachain/mediachain/milestones{/number}","notifications_url":"https://api.github.com/repos/mediachain/mediachain/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mediachain/mediachain/labels{/name}","releases_url":"https://api.github.com/repos/mediachain/mediachain/releases{/id}","deployments_url":"https://api.github.com/repos/mediachain/mediachain/deployments","created_at":"2016-01-12T17:29:52Z","updated_at":"2016-05-09T10:09:48Z","pushed_at":"2016-05-10T15:12:54Z","git_url":"git://github.com/mediachain/mediachain.git","ssh_url":"git@github.com:mediachain/mediachain.git","clone_url":"https://github.com/mediachain/mediachain.git","svn_url":"https://github.com/mediachain/mediachain","homepage":"https://medium.com/mine-labs/mediachain-483f49cbe37a","size":1175,"stargazers_count":34,"watchers_count":34,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":19,"forks":0,"open_issues":19,"watchers":34,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mediachain/mediachain/pulls/68"},"html":{"href":"https://github.com/mediachain/mediachain/pull/68"},"issue":{"href":"https://api.github.com/repos/mediachain/mediachain/issues/68"},"comments":{"href":"https://api.github.com/repos/mediachain/mediachain/issues/68/comments"},"review_comments":{"href":"https://api.github.com/repos/mediachain/mediachain/pulls/68/comments"},"review_comment":{"href":"https://api.github.com/repos/mediachain/mediachain/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mediachain/mediachain/pulls/68/commits"},"statuses":{"href":"https://api.github.com/repos/mediachain/mediachain/statuses/e1d4609ebb020aa092423f49367bc61d071ad8ce"}}}}