{"action":"created","comment":{"url":"https://api.github.com/repos/bluebore/bfs/pulls/comments/69235596","id":69235596,"diff_hunk":"@@ -322,31 +310,109 @@ bool LogDB::BuildFileCache() {\n             FILE* idx_fp = fopen(idx_name.c_str(), \"r\");\n             if (idx_fp == NULL) {\n                 LOG(WARNING, \"[LogDB] open index file failed %s\", file_name.c_str());\n-                closedir(dir_ptr);\n-                return false;\n+                error = true;\n+                break;\n             }\n             FILE* log_fp = fopen(log_name.c_str(), \"r\");\n             if (log_fp == NULL) {\n                 LOG(WARNING, \"[LogDB] open log file failed %s\", file_name.c_str());\n-                closedir(dir_ptr);\n-                return false;\n+                fclose(idx_fp);\n+                error = true;\n+                break;\n             }\n             read_log_[index] = std::make_pair(idx_fp, log_fp);\n             LOG(INFO, \"[LogDB] Add file cache %ld to %s \", index, file_name.c_str());\n         }\n     }\n     closedir(dir_ptr);\n-    // build largest index\n+    if (error) {\n+        for (FileCache::iterator it = read_log_.begin(); it != read_log_.end(); ++it) {\n+            fclose((it->second).first);\n+            fclose((it->second).second);\n+        }\n+        read_log_.clear();\n+        return false;\n+    }\n+    // check log & idx match, build largest index\n+    if (!CheckLogIdx()) {\n+        LOG(WARNING, \"[LogDB] CheckLogIdx failed\");\n+        for (FileCache::iterator it = read_log_.begin(); it != read_log_.end(); ++it) {\n+            fclose((it->second).first);\n+            fclose((it->second).second);\n+        }\n+        read_log_.clear();\n+        return false;\n+    }\n+    return true;\n+}\n+\n+bool LogDB::CheckLogIdx() {\n     if (read_log_.empty()) {\n-        LOG(INFO, \"[LogDB] No previous log, largest_index_ = -1\");\n+        if (smallest_index_ == -1) {\n+            next_index_ = 0;\n+        } else {\n+            next_index_ = smallest_index_;\n+        }\n+        LOG(INFO, \"[LogDB] No previous log, next_index_ = %ld \", next_index_);\n         return true;\n     }\n-    FileCache::reverse_iterator it = read_log_.rbegin();\n-    FILE* fp = (it->second).first;\n-    fseek(fp, 0, SEEK_END);\n-    int size = ftell(fp);\n-    largest_index_ = it->first + (size / 16) - 1;\n-    LOG(INFO, \"[LogDB] Set largest_index_ to %ld\", largest_index_);\n+    FileCache::iterator it = read_log_.begin();\n+    if (smallest_index_ < it->first) {\n+        LOG(WARNING, \"[LogDB] log does not contain smallest_index_ %ld %ld\",\n+                smallest_index_, it->first);\n+        return false;\n+    }\n+    next_index_ = it->first;\n+    bool error = false;\n+    for (; it != read_log_.end(); ++it) {\n+        if (it->first != next_index_) {\n+            LOG(WARNING, \"[LogDB] log is not continous, current index %ld \", it->first);\n+            return false;\n+        }\n+        FILE* idx = (it->second).first;\n+        FILE* log = (it->second).second;\n+        fseek(idx, 0, SEEK_END);\n+        int idx_size = ftell(idx);\n+        if (idx_size < 16) {","path":"src/nameserver/logdb.cc","position":431,"original_position":431,"commit_id":"9eabb23207883a77405e30597cedd9b009243625","original_commit_id":"9eabb23207883a77405e30597cedd9b009243625","user":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"body":"如果最后一个index文件，刚打开还没写，上层就突出了，这是不是会悲剧？","created_at":"2016-07-01T01:15:58Z","updated_at":"2016-07-01T01:15:58Z","html_url":"https://github.com/bluebore/bfs/pull/335#discussion_r69235596","pull_request_url":"https://api.github.com/repos/bluebore/bfs/pulls/335","_links":{"self":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/comments/69235596"},"html":{"href":"https://github.com/bluebore/bfs/pull/335#discussion_r69235596"},"pull_request":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/335"}}},"pull_request":{"url":"https://api.github.com/repos/bluebore/bfs/pulls/335","id":75603174,"html_url":"https://github.com/bluebore/bfs/pull/335","diff_url":"https://github.com/bluebore/bfs/pull/335.diff","patch_url":"https://github.com/bluebore/bfs/pull/335.patch","issue_url":"https://api.github.com/repos/bluebore/bfs/issues/335","number":335,"state":"open","locked":false,"title":"Logdb","user":{"login":"lylei","id":2767333,"avatar_url":"https://avatars.githubusercontent.com/u/2767333?v=3","gravatar_id":"","url":"https://api.github.com/users/lylei","html_url":"https://github.com/lylei","followers_url":"https://api.github.com/users/lylei/followers","following_url":"https://api.github.com/users/lylei/following{/other_user}","gists_url":"https://api.github.com/users/lylei/gists{/gist_id}","starred_url":"https://api.github.com/users/lylei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lylei/subscriptions","organizations_url":"https://api.github.com/users/lylei/orgs","repos_url":"https://api.github.com/users/lylei/repos","events_url":"https://api.github.com/users/lylei/events{/privacy}","received_events_url":"https://api.github.com/users/lylei/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-06-29T07:54:05Z","updated_at":"2016-07-01T01:15:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"55b082102ec50e74f844738409d386ddc167ec0f","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/bluebore/bfs/pulls/335/commits","review_comments_url":"https://api.github.com/repos/bluebore/bfs/pulls/335/comments","review_comment_url":"https://api.github.com/repos/bluebore/bfs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/bluebore/bfs/issues/335/comments","statuses_url":"https://api.github.com/repos/bluebore/bfs/statuses/9eabb23207883a77405e30597cedd9b009243625","head":{"label":"lylei:logdb","ref":"logdb","sha":"9eabb23207883a77405e30597cedd9b009243625","user":{"login":"lylei","id":2767333,"avatar_url":"https://avatars.githubusercontent.com/u/2767333?v=3","gravatar_id":"","url":"https://api.github.com/users/lylei","html_url":"https://github.com/lylei","followers_url":"https://api.github.com/users/lylei/followers","following_url":"https://api.github.com/users/lylei/following{/other_user}","gists_url":"https://api.github.com/users/lylei/gists{/gist_id}","starred_url":"https://api.github.com/users/lylei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lylei/subscriptions","organizations_url":"https://api.github.com/users/lylei/orgs","repos_url":"https://api.github.com/users/lylei/repos","events_url":"https://api.github.com/users/lylei/events{/privacy}","received_events_url":"https://api.github.com/users/lylei/received_events","type":"User","site_admin":false},"repo":{"id":48720316,"name":"dfs","full_name":"lylei/dfs","owner":{"login":"lylei","id":2767333,"avatar_url":"https://avatars.githubusercontent.com/u/2767333?v=3","gravatar_id":"","url":"https://api.github.com/users/lylei","html_url":"https://github.com/lylei","followers_url":"https://api.github.com/users/lylei/followers","following_url":"https://api.github.com/users/lylei/following{/other_user}","gists_url":"https://api.github.com/users/lylei/gists{/gist_id}","starred_url":"https://api.github.com/users/lylei/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lylei/subscriptions","organizations_url":"https://api.github.com/users/lylei/orgs","repos_url":"https://api.github.com/users/lylei/repos","events_url":"https://api.github.com/users/lylei/events{/privacy}","received_events_url":"https://api.github.com/users/lylei/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lylei/dfs","description":"突然想写个分布式文件系统~","fork":true,"url":"https://api.github.com/repos/lylei/dfs","forks_url":"https://api.github.com/repos/lylei/dfs/forks","keys_url":"https://api.github.com/repos/lylei/dfs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lylei/dfs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lylei/dfs/teams","hooks_url":"https://api.github.com/repos/lylei/dfs/hooks","issue_events_url":"https://api.github.com/repos/lylei/dfs/issues/events{/number}","events_url":"https://api.github.com/repos/lylei/dfs/events","assignees_url":"https://api.github.com/repos/lylei/dfs/assignees{/user}","branches_url":"https://api.github.com/repos/lylei/dfs/branches{/branch}","tags_url":"https://api.github.com/repos/lylei/dfs/tags","blobs_url":"https://api.github.com/repos/lylei/dfs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lylei/dfs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lylei/dfs/git/refs{/sha}","trees_url":"https://api.github.com/repos/lylei/dfs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lylei/dfs/statuses/{sha}","languages_url":"https://api.github.com/repos/lylei/dfs/languages","stargazers_url":"https://api.github.com/repos/lylei/dfs/stargazers","contributors_url":"https://api.github.com/repos/lylei/dfs/contributors","subscribers_url":"https://api.github.com/repos/lylei/dfs/subscribers","subscription_url":"https://api.github.com/repos/lylei/dfs/subscription","commits_url":"https://api.github.com/repos/lylei/dfs/commits{/sha}","git_commits_url":"https://api.github.com/repos/lylei/dfs/git/commits{/sha}","comments_url":"https://api.github.com/repos/lylei/dfs/comments{/number}","issue_comment_url":"https://api.github.com/repos/lylei/dfs/issues/comments{/number}","contents_url":"https://api.github.com/repos/lylei/dfs/contents/{+path}","compare_url":"https://api.github.com/repos/lylei/dfs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lylei/dfs/merges","archive_url":"https://api.github.com/repos/lylei/dfs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lylei/dfs/downloads","issues_url":"https://api.github.com/repos/lylei/dfs/issues{/number}","pulls_url":"https://api.github.com/repos/lylei/dfs/pulls{/number}","milestones_url":"https://api.github.com/repos/lylei/dfs/milestones{/number}","notifications_url":"https://api.github.com/repos/lylei/dfs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lylei/dfs/labels{/name}","releases_url":"https://api.github.com/repos/lylei/dfs/releases{/id}","deployments_url":"https://api.github.com/repos/lylei/dfs/deployments","created_at":"2015-12-29T01:20:31Z","updated_at":"2016-02-03T08:42:36Z","pushed_at":"2016-06-30T06:29:56Z","git_url":"git://github.com/lylei/dfs.git","ssh_url":"git@github.com:lylei/dfs.git","clone_url":"https://github.com/lylei/dfs.git","svn_url":"https://github.com/lylei/dfs","homepage":null,"size":4328,"stargazers_count":1,"watchers_count":1,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"bluebore:master","ref":"master","sha":"77d8c69f7894e0332e2a6fb05eda96f47da33115","user":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"repo":{"id":26562172,"name":"bfs","full_name":"bluebore/bfs","owner":{"login":"bluebore","id":6851389,"avatar_url":"https://avatars.githubusercontent.com/u/6851389?v=3","gravatar_id":"","url":"https://api.github.com/users/bluebore","html_url":"https://github.com/bluebore","followers_url":"https://api.github.com/users/bluebore/followers","following_url":"https://api.github.com/users/bluebore/following{/other_user}","gists_url":"https://api.github.com/users/bluebore/gists{/gist_id}","starred_url":"https://api.github.com/users/bluebore/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bluebore/subscriptions","organizations_url":"https://api.github.com/users/bluebore/orgs","repos_url":"https://api.github.com/users/bluebore/repos","events_url":"https://api.github.com/users/bluebore/events{/privacy}","received_events_url":"https://api.github.com/users/bluebore/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bluebore/bfs","description":"The Baidu File System.","fork":false,"url":"https://api.github.com/repos/bluebore/bfs","forks_url":"https://api.github.com/repos/bluebore/bfs/forks","keys_url":"https://api.github.com/repos/bluebore/bfs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bluebore/bfs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bluebore/bfs/teams","hooks_url":"https://api.github.com/repos/bluebore/bfs/hooks","issue_events_url":"https://api.github.com/repos/bluebore/bfs/issues/events{/number}","events_url":"https://api.github.com/repos/bluebore/bfs/events","assignees_url":"https://api.github.com/repos/bluebore/bfs/assignees{/user}","branches_url":"https://api.github.com/repos/bluebore/bfs/branches{/branch}","tags_url":"https://api.github.com/repos/bluebore/bfs/tags","blobs_url":"https://api.github.com/repos/bluebore/bfs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bluebore/bfs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bluebore/bfs/git/refs{/sha}","trees_url":"https://api.github.com/repos/bluebore/bfs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bluebore/bfs/statuses/{sha}","languages_url":"https://api.github.com/repos/bluebore/bfs/languages","stargazers_url":"https://api.github.com/repos/bluebore/bfs/stargazers","contributors_url":"https://api.github.com/repos/bluebore/bfs/contributors","subscribers_url":"https://api.github.com/repos/bluebore/bfs/subscribers","subscription_url":"https://api.github.com/repos/bluebore/bfs/subscription","commits_url":"https://api.github.com/repos/bluebore/bfs/commits{/sha}","git_commits_url":"https://api.github.com/repos/bluebore/bfs/git/commits{/sha}","comments_url":"https://api.github.com/repos/bluebore/bfs/comments{/number}","issue_comment_url":"https://api.github.com/repos/bluebore/bfs/issues/comments{/number}","contents_url":"https://api.github.com/repos/bluebore/bfs/contents/{+path}","compare_url":"https://api.github.com/repos/bluebore/bfs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bluebore/bfs/merges","archive_url":"https://api.github.com/repos/bluebore/bfs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bluebore/bfs/downloads","issues_url":"https://api.github.com/repos/bluebore/bfs/issues{/number}","pulls_url":"https://api.github.com/repos/bluebore/bfs/pulls{/number}","milestones_url":"https://api.github.com/repos/bluebore/bfs/milestones{/number}","notifications_url":"https://api.github.com/repos/bluebore/bfs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bluebore/bfs/labels{/name}","releases_url":"https://api.github.com/repos/bluebore/bfs/releases{/id}","deployments_url":"https://api.github.com/repos/bluebore/bfs/deployments","created_at":"2014-11-13T00:00:29Z","updated_at":"2016-06-20T10:21:51Z","pushed_at":"2016-06-30T06:29:56Z","git_url":"git://github.com/bluebore/bfs.git","ssh_url":"git@github.com:bluebore/bfs.git","clone_url":"https://github.com/bluebore/bfs.git","svn_url":"https://github.com/bluebore/bfs","homepage":"","size":11098,"stargazers_count":34,"watchers_count":34,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":23,"mirror_url":null,"open_issues_count":56,"forks":23,"open_issues":56,"watchers":34,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/335"},"html":{"href":"https://github.com/bluebore/bfs/pull/335"},"issue":{"href":"https://api.github.com/repos/bluebore/bfs/issues/335"},"comments":{"href":"https://api.github.com/repos/bluebore/bfs/issues/335/comments"},"review_comments":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/335/comments"},"review_comment":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/bluebore/bfs/pulls/335/commits"},"statuses":{"href":"https://api.github.com/repos/bluebore/bfs/statuses/9eabb23207883a77405e30597cedd9b009243625"}}}}
{"actor":{"display_login":"bluebore"}}