{"action":"created","comment":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/40165065","id":40165065,"diff_hunk":"@@ -0,0 +1,225 @@\n+// Copyright 2015 The Cockroach Authors.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a 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\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n+// implied. See the License for the specific language governing\n+// permissions and limitations under the License. See the AUTHORS file\n+// for names of contributors.\n+//\n+// Author: Ben Darnell\n+\n+// This example uses a log-style table in an approximation of the\n+// \"fake real time\" system used at Friendfeed. Two tables are used: a\n+// `messages` table stores the complete data for all messages\n+// organized by channel, and a global `updates` table stores metadata\n+// about recently-updated channels.\n+//\n+// The example currently runs a number of writers, which update both\n+// tables transactionally. Future work includes implementing a reader\n+// which will effectively tail the `updates` table to learn when it\n+// needs to query a channel's messages from the `messages` table.\n+//\n+// The implementation currently guarantees that `update_ids` are\n+// strictly monotonic, which is extremely expensive under contention\n+// (two writer threads will fight with each other for 20 seconds or\n+// more before making progress).\n+//\n+// One alternate solution is to relax the monotonicity constraint (by\n+// using a timestmp instead of a sequential ID), at the expense of\n+// making the reader more complicated (since it would need to re-scan\n+// records it had already read to see if anything \"in the past\"\n+// committed since it last read). We would still need to put some sort\n+// of bound on the reader's scans; any updates that went longer than\n+// this without being committed would go unnoticed.\n+//\n+// Another alternative is to reduce contention by adding a small\n+// random number (in range (0, N)) to the beginning of the `updates`\n+// table primary key. This reduces contention but makes the reader do\n+// N queries to see all updates. This is effectively what Friendfeed\n+// did, since there was one `updates` table per MySQL shard.\n+package main\n+\n+import (\n+\t\"database/sql\"\n+\t\"flag\"\n+\t\"fmt\"\n+\t\"math/rand\"\n+\t\"sync\"\n+\t\"time\"\n+\n+\t\"github.com/cockroachdb/cockroach/security\"\n+\t\"github.com/cockroachdb/cockroach/security/securitytest\"\n+\t\"github.com/cockroachdb/cockroach/server\"\n+\t\"github.com/cockroachdb/cockroach/util/log\"\n+\t\"github.com/montanaflynn/stats\"\n+)\n+\n+func createTables(db *sql.DB) error {\n+\tstatements := []string{\n+\t\t`create database if not exists fakerealtime`,\n+\n+\t\t`drop table if exists fakerealtime.updates`,\n+\t\t`create table fakerealtime.updates (\n+        update_id int,\n+        channel string,\n+        msg_id int,\n+        primary key (update_id, channel)\n+    )`,\n+\n+\t\t`drop table if exists fakerealtime.messages`,\n+\t\t`create table fakerealtime.messages (\n+        channel string,\n+        msg_id int,\n+        message string,\n+        primary key (channel, msg_id)\n+    )`,\n+\t}\n+\tfor _, stmt := range statements {\n+\t\tif _, err := db.Exec(stmt); err != nil {\n+\t\t\treturn err\n+\t\t}\n+\t}\n+\treturn nil\n+}\n+\n+type statistics struct {\n+\tsync.Mutex\n+\twriteTimes stats.Float64Data\n+}\n+\n+func (s *statistics) recordWrite(start time.Time) {\n+\tduration := time.Now().Sub(start)\n+\ts.Lock()\n+\tdefer s.Unlock()\n+\ts.writeTimes = append(s.writeTimes, float64(duration.Nanoseconds()))\n+}\n+\n+func (s *statistics) report() {\n+\tfor range time.Tick(time.Second) {\n+\t\ts.Lock()\n+\t\twriteTimes := s.writeTimes\n+\t\ts.writeTimes = nil\n+\t\ts.Unlock()\n+\n+\t\t// The stats functions return an error only when the input is empty.\n+\t\tmean, _ := stats.Mean(writeTimes)\n+\t\tstddev, _ := stats.StandardDeviation(writeTimes)\n+\t\tlog.Infof(\"wrote %d messages, latency mean=%s, stddev=%s\",\n+\t\t\tlen(writeTimes), time.Duration(mean), time.Duration(stddev))\n+\t}\n+}\n+\n+type writer struct {\n+\tdb          *sql.DB\n+\tnumChannels int\n+\twg          *sync.WaitGroup\n+\tstats       *statistics\n+}\n+\n+func (w writer) run() {\n+\tdefer w.wg.Done()\n+\tfor {\n+\t\tif err := w.writeMessage(); err != nil {\n+\t\t\tlog.Errorf(\"error writing message: %s\", err)\n+\t\t}\n+\t}\n+}\n+\n+func (w writer) writeMessage() error {\n+\tstart := time.Now()\n+\tdefer w.stats.recordWrite(start)\n+\tchannel := fmt.Sprintf(\"room-%d\", rand.Int31n(int32(w.numChannels)))\n+\tmessage := start.String()\n+\n+\t// TODO(bdarnell): retry only on certain errors.\n+\tfor {\n+\t\ttxn, err := w.db.Begin()\n+\t\tif err != nil {\n+\t\t\tcontinue\n+\t\t}\n+\n+\t\t// TODO(bdarnell): make this a subquery when subqueries are supported on insert.\n+\t\trow := txn.QueryRow(`select max(msg_id) from fakerealtime.messages where channel=$1`, channel)","path":"examples/fakerealtime/main.go","position":149,"original_position":149,"commit_id":"aed3ca3034dacca799f4c46fd30d5a7954f3e920","original_commit_id":"aed3ca3034dacca799f4c46fd30d5a7954f3e920","user":{"login":"petermattis","id":6201335,"avatar_url":"https://avatars.githubusercontent.com/u/6201335?v=3","gravatar_id":"","url":"https://api.github.com/users/petermattis","html_url":"https://github.com/petermattis","followers_url":"https://api.github.com/users/petermattis/followers","following_url":"https://api.github.com/users/petermattis/following{/other_user}","gists_url":"https://api.github.com/users/petermattis/gists{/gist_id}","starred_url":"https://api.github.com/users/petermattis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petermattis/subscriptions","organizations_url":"https://api.github.com/users/petermattis/orgs","repos_url":"https://api.github.com/users/petermattis/repos","events_url":"https://api.github.com/users/petermattis/events{/privacy}","received_events_url":"https://api.github.com/users/petermattis/received_events","type":"User","site_admin":false},"body":"FYI, `max(msg_id)` currently performs a table scan. :(","created_at":"2015-09-23T01:51:52Z","updated_at":"2015-09-23T01:51:52Z","html_url":"https://github.com/cockroachdb/cockroach/pull/2627#discussion_r40165065","pull_request_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627","_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments/40165065"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2627#discussion_r40165065"},"pull_request":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627"}}},"pull_request":{"url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627","id":45724509,"html_url":"https://github.com/cockroachdb/cockroach/pull/2627","diff_url":"https://github.com/cockroachdb/cockroach/pull/2627.diff","patch_url":"https://github.com/cockroachdb/cockroach/pull/2627.patch","issue_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2627","number":2627,"state":"open","locked":false,"title":"First implementation of \"fakerealtime\" example.","user":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"body":"See #2432.","created_at":"2015-09-22T22:19:25Z","updated_at":"2015-09-23T01:51:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"20e79012e48c18f2ada7ab250d05a8ad112327b5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627/commits","review_comments_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627/comments","review_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/2627/comments","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/aed3ca3034dacca799f4c46fd30d5a7954f3e920","head":{"label":"bdarnell:fakerealtime","ref":"fakerealtime","sha":"aed3ca3034dacca799f4c46fd30d5a7954f3e920","user":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"repo":{"id":23885381,"name":"cockroach","full_name":"bdarnell/cockroach","owner":{"login":"bdarnell","id":160562,"avatar_url":"https://avatars.githubusercontent.com/u/160562?v=3","gravatar_id":"","url":"https://api.github.com/users/bdarnell","html_url":"https://github.com/bdarnell","followers_url":"https://api.github.com/users/bdarnell/followers","following_url":"https://api.github.com/users/bdarnell/following{/other_user}","gists_url":"https://api.github.com/users/bdarnell/gists{/gist_id}","starred_url":"https://api.github.com/users/bdarnell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bdarnell/subscriptions","organizations_url":"https://api.github.com/users/bdarnell/orgs","repos_url":"https://api.github.com/users/bdarnell/repos","events_url":"https://api.github.com/users/bdarnell/events{/privacy}","received_events_url":"https://api.github.com/users/bdarnell/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bdarnell/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":true,"url":"https://api.github.com/repos/bdarnell/cockroach","forks_url":"https://api.github.com/repos/bdarnell/cockroach/forks","keys_url":"https://api.github.com/repos/bdarnell/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bdarnell/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bdarnell/cockroach/teams","hooks_url":"https://api.github.com/repos/bdarnell/cockroach/hooks","issue_events_url":"https://api.github.com/repos/bdarnell/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/bdarnell/cockroach/events","assignees_url":"https://api.github.com/repos/bdarnell/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/bdarnell/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/bdarnell/cockroach/tags","blobs_url":"https://api.github.com/repos/bdarnell/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bdarnell/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bdarnell/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/bdarnell/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bdarnell/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/bdarnell/cockroach/languages","stargazers_url":"https://api.github.com/repos/bdarnell/cockroach/stargazers","contributors_url":"https://api.github.com/repos/bdarnell/cockroach/contributors","subscribers_url":"https://api.github.com/repos/bdarnell/cockroach/subscribers","subscription_url":"https://api.github.com/repos/bdarnell/cockroach/subscription","commits_url":"https://api.github.com/repos/bdarnell/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/bdarnell/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/bdarnell/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/bdarnell/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/bdarnell/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/bdarnell/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bdarnell/cockroach/merges","archive_url":"https://api.github.com/repos/bdarnell/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bdarnell/cockroach/downloads","issues_url":"https://api.github.com/repos/bdarnell/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/bdarnell/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/bdarnell/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/bdarnell/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bdarnell/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/bdarnell/cockroach/releases{/id}","created_at":"2014-09-10T17:55:56Z","updated_at":"2015-03-31T18:00:59Z","pushed_at":"2015-09-22T22:18:07Z","git_url":"git://github.com/bdarnell/cockroach.git","ssh_url":"git@github.com:bdarnell/cockroach.git","clone_url":"https://github.com/bdarnell/cockroach.git","svn_url":"https://github.com/bdarnell/cockroach","homepage":null,"size":31642,"stargazers_count":0,"watchers_count":0,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"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":"cockroachdb:master","ref":"master","sha":"5b315a2f3ade0bd38a21974f452bbd099cac20df","user":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"repo":{"id":16563587,"name":"cockroach","full_name":"cockroachdb/cockroach","owner":{"login":"cockroachdb","id":6748139,"avatar_url":"https://avatars.githubusercontent.com/u/6748139?v=3","gravatar_id":"","url":"https://api.github.com/users/cockroachdb","html_url":"https://github.com/cockroachdb","followers_url":"https://api.github.com/users/cockroachdb/followers","following_url":"https://api.github.com/users/cockroachdb/following{/other_user}","gists_url":"https://api.github.com/users/cockroachdb/gists{/gist_id}","starred_url":"https://api.github.com/users/cockroachdb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cockroachdb/subscriptions","organizations_url":"https://api.github.com/users/cockroachdb/orgs","repos_url":"https://api.github.com/users/cockroachdb/repos","events_url":"https://api.github.com/users/cockroachdb/events{/privacy}","received_events_url":"https://api.github.com/users/cockroachdb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cockroachdb/cockroach","description":"A Scalable, Geo-Replicated, Transactional Datastore","fork":false,"url":"https://api.github.com/repos/cockroachdb/cockroach","forks_url":"https://api.github.com/repos/cockroachdb/cockroach/forks","keys_url":"https://api.github.com/repos/cockroachdb/cockroach/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cockroachdb/cockroach/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cockroachdb/cockroach/teams","hooks_url":"https://api.github.com/repos/cockroachdb/cockroach/hooks","issue_events_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/events{/number}","events_url":"https://api.github.com/repos/cockroachdb/cockroach/events","assignees_url":"https://api.github.com/repos/cockroachdb/cockroach/assignees{/user}","branches_url":"https://api.github.com/repos/cockroachdb/cockroach/branches{/branch}","tags_url":"https://api.github.com/repos/cockroachdb/cockroach/tags","blobs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cockroachdb/cockroach/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cockroachdb/cockroach/git/refs{/sha}","trees_url":"https://api.github.com/repos/cockroachdb/cockroach/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cockroachdb/cockroach/statuses/{sha}","languages_url":"https://api.github.com/repos/cockroachdb/cockroach/languages","stargazers_url":"https://api.github.com/repos/cockroachdb/cockroach/stargazers","contributors_url":"https://api.github.com/repos/cockroachdb/cockroach/contributors","subscribers_url":"https://api.github.com/repos/cockroachdb/cockroach/subscribers","subscription_url":"https://api.github.com/repos/cockroachdb/cockroach/subscription","commits_url":"https://api.github.com/repos/cockroachdb/cockroach/commits{/sha}","git_commits_url":"https://api.github.com/repos/cockroachdb/cockroach/git/commits{/sha}","comments_url":"https://api.github.com/repos/cockroachdb/cockroach/comments{/number}","issue_comment_url":"https://api.github.com/repos/cockroachdb/cockroach/issues/comments{/number}","contents_url":"https://api.github.com/repos/cockroachdb/cockroach/contents/{+path}","compare_url":"https://api.github.com/repos/cockroachdb/cockroach/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cockroachdb/cockroach/merges","archive_url":"https://api.github.com/repos/cockroachdb/cockroach/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cockroachdb/cockroach/downloads","issues_url":"https://api.github.com/repos/cockroachdb/cockroach/issues{/number}","pulls_url":"https://api.github.com/repos/cockroachdb/cockroach/pulls{/number}","milestones_url":"https://api.github.com/repos/cockroachdb/cockroach/milestones{/number}","notifications_url":"https://api.github.com/repos/cockroachdb/cockroach/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cockroachdb/cockroach/labels{/name}","releases_url":"https://api.github.com/repos/cockroachdb/cockroach/releases{/id}","created_at":"2014-02-06T00:18:47Z","updated_at":"2015-09-22T20:30:37Z","pushed_at":"2015-09-23T00:33:18Z","git_url":"git://github.com/cockroachdb/cockroach.git","ssh_url":"git@github.com:cockroachdb/cockroach.git","clone_url":"https://github.com/cockroachdb/cockroach.git","svn_url":"https://github.com/cockroachdb/cockroach","homepage":null,"size":57354,"stargazers_count":5311,"watchers_count":5311,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":434,"mirror_url":null,"open_issues_count":170,"forks":434,"open_issues":170,"watchers":5311,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627"},"html":{"href":"https://github.com/cockroachdb/cockroach/pull/2627"},"issue":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2627"},"comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/issues/2627/comments"},"review_comments":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627/comments"},"review_comment":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cockroachdb/cockroach/pulls/2627/commits"},"statuses":{"href":"https://api.github.com/repos/cockroachdb/cockroach/statuses/aed3ca3034dacca799f4c46fd30d5a7954f3e920"}}}}