{"action":"created","comment":{"url":"https://api.github.com/repos/m3db/m3db/pulls/comments/176142519","pull_request_review_id":105794132,"id":176142519,"diff_hunk":"@@ -379,17 +381,57 @@ func (s *dbShard) OnEvictedFromWiredList(id ident.ID, blockStart time.Time) {\n }\n \n func (s *dbShard) forEachShardEntry(entryFn dbShardEntryWorkFn) error {\n+\treturn s.forEachShardEntryBatch(func(currEntries []*dbShardEntry) bool {\n+\t\tfor _, entry := range currEntries {\n+\t\t\tif continueForEach := entryFn(entry); !continueForEach {\n+\t\t\t\treturn false\n+\t\t\t}\n+\t\t}\n+\t\treturn true\n+\t})\n+}\n+\n+func iterateBatchSize(elemsLen int) int {\n+\tif elemsLen < shardIterateBatchMinSize {\n+\t\treturn elemsLen","path":"storage/shard.go","position":34,"original_position":34,"commit_id":"7275b46e96f93344c9cadf1b34f833547f09a9bd","original_commit_id":"7275b46e96f93344c9cadf1b34f833547f09a9bd","user":{"login":"prateek","id":199982,"avatar_url":"https://avatars3.githubusercontent.com/u/199982?v=4","gravatar_id":"","url":"https://api.github.com/users/prateek","html_url":"https://github.com/prateek","followers_url":"https://api.github.com/users/prateek/followers","following_url":"https://api.github.com/users/prateek/following{/other_user}","gists_url":"https://api.github.com/users/prateek/gists{/gist_id}","starred_url":"https://api.github.com/users/prateek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prateek/subscriptions","organizations_url":"https://api.github.com/users/prateek/orgs","repos_url":"https://api.github.com/users/prateek/repos","events_url":"https://api.github.com/users/prateek/events{/privacy}","received_events_url":"https://api.github.com/users/prateek/received_events","type":"User","site_admin":false},"body":"We do have that: there's a math.Max in the return path. i.e. we're guaranteed to have a batch size of at least `shardIterateBatchMinSize`  if `elemsLen>=shardIterateBatchMinSize`. There's a test that checks exactly the situation you're worried about btw - \r\n```\r\nfunc TestShardIterateBatchSize(t *testing.T) {\r\n...\r\n\trequire.Equal(t, shardIterateBatchMinSize, iterateBatchSize(shardIterateBatchMinSize+1))\r\n...\r\n}\r\n```\r\n\r\n\r\n\r\n","created_at":"2018-03-21T16:12:13Z","updated_at":"2018-03-21T16:12:14Z","html_url":"https://github.com/m3db/m3db/pull/502#discussion_r176142519","pull_request_url":"https://api.github.com/repos/m3db/m3db/pulls/502","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/m3db/m3db/pulls/comments/176142519"},"html":{"href":"https://github.com/m3db/m3db/pull/502#discussion_r176142519"},"pull_request":{"href":"https://api.github.com/repos/m3db/m3db/pulls/502"}},"in_reply_to_id":176123596},"pull_request":{"url":"https://api.github.com/repos/m3db/m3db/pulls/502","id":175664899,"html_url":"https://github.com/m3db/m3db/pull/502","diff_url":"https://github.com/m3db/m3db/pull/502.diff","patch_url":"https://github.com/m3db/m3db/pull/502.patch","issue_url":"https://api.github.com/repos/m3db/m3db/issues/502","number":502,"state":"open","locked":false,"title":"Fix Race Conditions in Shard","user":{"login":"prateek","id":199982,"avatar_url":"https://avatars3.githubusercontent.com/u/199982?v=4","gravatar_id":"","url":"https://api.github.com/users/prateek","html_url":"https://github.com/prateek","followers_url":"https://api.github.com/users/prateek/followers","following_url":"https://api.github.com/users/prateek/following{/other_user}","gists_url":"https://api.github.com/users/prateek/gists{/gist_id}","starred_url":"https://api.github.com/users/prateek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/prateek/subscriptions","organizations_url":"https://api.github.com/users/prateek/orgs","repos_url":"https://api.github.com/users/prateek/repos","events_url":"https://api.github.com/users/prateek/events{/privacy}","received_events_url":"https://api.github.com/users/prateek/received_events","type":"User","site_admin":false},"body":"- `tickAndExpire`s usual path of calling `purgeExpired` within the `forEachShardEntry` always skipped cleanup for entries present in the underlying batch; it cleaned up stuff in the lagging if check at the end. \r\n- A race in `forEachShardEntry`: took a reference to the front of the list but didn't increment the entry's reference count immediately, and released a lock. This was the bug allowing ticks to cleanup data while something else held a reference to it.\r\n- Another race in `forEachShardEntry`: similar to the point above, we held onto an entry pointer with zero reference counts while transitioning from one batch to another, this pointer could get invalidated during a tick and cause panics. \r\n- Tests to ensure we are guarding against these issues\r\n\r\n/cc @robskillington @richardartoul ","created_at":"2018-03-16T23:06:24Z","updated_at":"2018-03-21T16:12:14Z","closed_at":null,"merged_at":null,"merge_commit_sha":"42c9aa56ab1f655f20ea79d1c33134cf603930b0","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/m3db/m3db/pulls/502/commits","review_comments_url":"https://api.github.com/repos/m3db/m3db/pulls/502/comments","review_comment_url":"https://api.github.com/repos/m3db/m3db/pulls/comments{/number}","comments_url":"https://api.github.com/repos/m3db/m3db/issues/502/comments","statuses_url":"https://api.github.com/repos/m3db/m3db/statuses/7275b46e96f93344c9cadf1b34f833547f09a9bd","head":{"label":"m3db:prateek/misc/shard-decref-test","ref":"prateek/misc/shard-decref-test","sha":"7275b46e96f93344c9cadf1b34f833547f09a9bd","user":{"login":"m3db","id":19937171,"avatar_url":"https://avatars0.githubusercontent.com/u/19937171?v=4","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"repo":{"id":61153677,"name":"m3db","full_name":"m3db/m3db","owner":{"login":"m3db","id":19937171,"avatar_url":"https://avatars0.githubusercontent.com/u/19937171?v=4","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/m3db/m3db","description":"A distributed time series database using M3TSZ float64 compression ","fork":false,"url":"https://api.github.com/repos/m3db/m3db","forks_url":"https://api.github.com/repos/m3db/m3db/forks","keys_url":"https://api.github.com/repos/m3db/m3db/keys{/key_id}","collaborators_url":"https://api.github.com/repos/m3db/m3db/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/m3db/m3db/teams","hooks_url":"https://api.github.com/repos/m3db/m3db/hooks","issue_events_url":"https://api.github.com/repos/m3db/m3db/issues/events{/number}","events_url":"https://api.github.com/repos/m3db/m3db/events","assignees_url":"https://api.github.com/repos/m3db/m3db/assignees{/user}","branches_url":"https://api.github.com/repos/m3db/m3db/branches{/branch}","tags_url":"https://api.github.com/repos/m3db/m3db/tags","blobs_url":"https://api.github.com/repos/m3db/m3db/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/m3db/m3db/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/m3db/m3db/git/refs{/sha}","trees_url":"https://api.github.com/repos/m3db/m3db/git/trees{/sha}","statuses_url":"https://api.github.com/repos/m3db/m3db/statuses/{sha}","languages_url":"https://api.github.com/repos/m3db/m3db/languages","stargazers_url":"https://api.github.com/repos/m3db/m3db/stargazers","contributors_url":"https://api.github.com/repos/m3db/m3db/contributors","subscribers_url":"https://api.github.com/repos/m3db/m3db/subscribers","subscription_url":"https://api.github.com/repos/m3db/m3db/subscription","commits_url":"https://api.github.com/repos/m3db/m3db/commits{/sha}","git_commits_url":"https://api.github.com/repos/m3db/m3db/git/commits{/sha}","comments_url":"https://api.github.com/repos/m3db/m3db/comments{/number}","issue_comment_url":"https://api.github.com/repos/m3db/m3db/issues/comments{/number}","contents_url":"https://api.github.com/repos/m3db/m3db/contents/{+path}","compare_url":"https://api.github.com/repos/m3db/m3db/compare/{base}...{head}","merges_url":"https://api.github.com/repos/m3db/m3db/merges","archive_url":"https://api.github.com/repos/m3db/m3db/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/m3db/m3db/downloads","issues_url":"https://api.github.com/repos/m3db/m3db/issues{/number}","pulls_url":"https://api.github.com/repos/m3db/m3db/pulls{/number}","milestones_url":"https://api.github.com/repos/m3db/m3db/milestones{/number}","notifications_url":"https://api.github.com/repos/m3db/m3db/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/m3db/m3db/labels{/name}","releases_url":"https://api.github.com/repos/m3db/m3db/releases{/id}","deployments_url":"https://api.github.com/repos/m3db/m3db/deployments","created_at":"2016-06-14T20:29:45Z","updated_at":"2018-03-21T15:30:02Z","pushed_at":"2018-03-21T15:30:04Z","git_url":"git://github.com/m3db/m3db.git","ssh_url":"git@github.com:m3db/m3db.git","clone_url":"https://github.com/m3db/m3db.git","svn_url":"https://github.com/m3db/m3db","homepage":"http://m3db.github.io/m3db/","size":6561,"stargazers_count":139,"watchers_count":139,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":10,"mirror_url":null,"archived":false,"open_issues_count":58,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":10,"open_issues":58,"watchers":139,"default_branch":"master"}},"base":{"label":"m3db:master","ref":"master","sha":"383cb7ce1d28c39f275e5be2f1501f5f31e7762e","user":{"login":"m3db","id":19937171,"avatar_url":"https://avatars0.githubusercontent.com/u/19937171?v=4","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"repo":{"id":61153677,"name":"m3db","full_name":"m3db/m3db","owner":{"login":"m3db","id":19937171,"avatar_url":"https://avatars0.githubusercontent.com/u/19937171?v=4","gravatar_id":"","url":"https://api.github.com/users/m3db","html_url":"https://github.com/m3db","followers_url":"https://api.github.com/users/m3db/followers","following_url":"https://api.github.com/users/m3db/following{/other_user}","gists_url":"https://api.github.com/users/m3db/gists{/gist_id}","starred_url":"https://api.github.com/users/m3db/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/m3db/subscriptions","organizations_url":"https://api.github.com/users/m3db/orgs","repos_url":"https://api.github.com/users/m3db/repos","events_url":"https://api.github.com/users/m3db/events{/privacy}","received_events_url":"https://api.github.com/users/m3db/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/m3db/m3db","description":"A distributed time series database using M3TSZ float64 compression ","fork":false,"url":"https://api.github.com/repos/m3db/m3db","forks_url":"https://api.github.com/repos/m3db/m3db/forks","keys_url":"https://api.github.com/repos/m3db/m3db/keys{/key_id}","collaborators_url":"https://api.github.com/repos/m3db/m3db/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/m3db/m3db/teams","hooks_url":"https://api.github.com/repos/m3db/m3db/hooks","issue_events_url":"https://api.github.com/repos/m3db/m3db/issues/events{/number}","events_url":"https://api.github.com/repos/m3db/m3db/events","assignees_url":"https://api.github.com/repos/m3db/m3db/assignees{/user}","branches_url":"https://api.github.com/repos/m3db/m3db/branches{/branch}","tags_url":"https://api.github.com/repos/m3db/m3db/tags","blobs_url":"https://api.github.com/repos/m3db/m3db/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/m3db/m3db/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/m3db/m3db/git/refs{/sha}","trees_url":"https://api.github.com/repos/m3db/m3db/git/trees{/sha}","statuses_url":"https://api.github.com/repos/m3db/m3db/statuses/{sha}","languages_url":"https://api.github.com/repos/m3db/m3db/languages","stargazers_url":"https://api.github.com/repos/m3db/m3db/stargazers","contributors_url":"https://api.github.com/repos/m3db/m3db/contributors","subscribers_url":"https://api.github.com/repos/m3db/m3db/subscribers","subscription_url":"https://api.github.com/repos/m3db/m3db/subscription","commits_url":"https://api.github.com/repos/m3db/m3db/commits{/sha}","git_commits_url":"https://api.github.com/repos/m3db/m3db/git/commits{/sha}","comments_url":"https://api.github.com/repos/m3db/m3db/comments{/number}","issue_comment_url":"https://api.github.com/repos/m3db/m3db/issues/comments{/number}","contents_url":"https://api.github.com/repos/m3db/m3db/contents/{+path}","compare_url":"https://api.github.com/repos/m3db/m3db/compare/{base}...{head}","merges_url":"https://api.github.com/repos/m3db/m3db/merges","archive_url":"https://api.github.com/repos/m3db/m3db/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/m3db/m3db/downloads","issues_url":"https://api.github.com/repos/m3db/m3db/issues{/number}","pulls_url":"https://api.github.com/repos/m3db/m3db/pulls{/number}","milestones_url":"https://api.github.com/repos/m3db/m3db/milestones{/number}","notifications_url":"https://api.github.com/repos/m3db/m3db/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/m3db/m3db/labels{/name}","releases_url":"https://api.github.com/repos/m3db/m3db/releases{/id}","deployments_url":"https://api.github.com/repos/m3db/m3db/deployments","created_at":"2016-06-14T20:29:45Z","updated_at":"2018-03-21T15:30:02Z","pushed_at":"2018-03-21T15:30:04Z","git_url":"git://github.com/m3db/m3db.git","ssh_url":"git@github.com:m3db/m3db.git","clone_url":"https://github.com/m3db/m3db.git","svn_url":"https://github.com/m3db/m3db","homepage":"http://m3db.github.io/m3db/","size":6561,"stargazers_count":139,"watchers_count":139,"language":"Go","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":10,"mirror_url":null,"archived":false,"open_issues_count":58,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":10,"open_issues":58,"watchers":139,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/m3db/m3db/pulls/502"},"html":{"href":"https://github.com/m3db/m3db/pull/502"},"issue":{"href":"https://api.github.com/repos/m3db/m3db/issues/502"},"comments":{"href":"https://api.github.com/repos/m3db/m3db/issues/502/comments"},"review_comments":{"href":"https://api.github.com/repos/m3db/m3db/pulls/502/comments"},"review_comment":{"href":"https://api.github.com/repos/m3db/m3db/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/m3db/m3db/pulls/502/commits"},"statuses":{"href":"https://api.github.com/repos/m3db/m3db/statuses/7275b46e96f93344c9cadf1b34f833547f09a9bd"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"prateek"}}