{"action":"created","comment":{"url":"https://api.github.com/repos/baidu/tera/pulls/comments/38818013","id":38818013,"diff_hunk":"@@ -649,78 +677,271 @@ void TabletNodeImpl::ScanTablet(const ScanTabletRequest* request,\n \n void TabletNodeImpl::SplitTablet(const SplitTabletRequest* request,\n                                  SplitTabletResponse* response,\n-                                 google::protobuf::Closure* done) {\n+                                 google::protobuf::Closure* done)\n+{\n     response->set_sequence_id(request->sequence_id());\n-\n-    std::string split_key, path;\n+    \n+    int stage = 1;\n     StatusCode status = kTabletNodeOk;\n-    io::TabletIO* tablet_io = m_tablet_manager->GetTablet(request->tablet_name(),\n-                                                request->key_range().key_start(),\n-                                                request->key_range().key_end(),\n-                                                &status);\n-    if (tablet_io == NULL) {\n-        LOG(WARNING) << \"split fail to get tablet: \" << request->tablet_name()\n-            << \" [\" << DebugString(request->key_range().key_start())\n-            << \", \" << DebugString(request->key_range().key_end())\n-            << \"], status: \" << StatusCodeToString(status);\n-        response->set_status(kKeyNotInRange);\n+    io::TabletIO *parent_tabletio = NULL;\n+      \n+    leveldb::Env *env = io::LeveldbBaseEnv();\n+    std::string parent_tabletname = leveldb::GetTabletPathFromNum(request->table_name(), \n+                                                                request->parent_tablet());\n+    std::string left_tabletname = leveldb::GetChildTabletPath(parent_tabletname, \n+                                                        request->child_tablets(0));\n+    std::string right_tabletname = leveldb::GetChildTabletPath(parent_tabletname, \n+                                                        request->child_tablets(1));\n+    \n+    VLOG(20) << __func__ << \": parent \" << parent_tabletname << \", lchild \"\n+        << left_tabletname << \", rchild \" << right_tabletname << \", start \"\n+        << request->key_range().key_start() << \", end \" \n+        << request->key_range().key_end();\n+\n+    if (m_tablet_manager->TestSplitTablet(request->table_name(),\n+                              request->key_range().key_start(),\n+                              request->mid_key(),\n+                              request->key_range().key_end())) {\n+        // split finish;\n+        response->set_status(kTabletNodeOk);\n         done->Run();\n-        return;\n+        return ;\n+    }\n+    \n+    parent_tabletio = m_tablet_manager->GetTablet(request->table_name(),\n+                                            request->key_range().key_start(),\n+                                            request->key_range().key_end(),\n+                                            &status);\n+    if (parent_tabletio == NULL) {\n+        LOG(INFO) << __func__ << \", \" << parent_tabletname \n+            << \" not found, start \" << request->key_range().key_start() \n+            << \", end \" << request->key_range().key_end();\n     }\n \n-    if (!tablet_io->Split(&split_key, &status)) {\n-        LOG(ERROR) << \"fail to split tablet: \" << tablet_io->GetTablePath()\n-            << \" [\" << DebugString(tablet_io->GetStartKey())\n-            << \", \" << DebugString(tablet_io->GetEndKey())\n-            << \"], status: \" << StatusCodeToString(status);\n-        if (status == kTableNotSupport) {\n-            response->set_status(kTableNotSupport);\n-        } else {\n-            response->set_status((StatusCode)tablet_io->GetStatus());\n+    CHECK(request->child_tablets_size() == 2);\n+    // if parent tablet not in TM\n+    if (parent_tabletio == NULL) {\n+        // test ldb's current file is exit\n+        // 前缀+表名+tablet000+lg[]+current\n+        parent_tabletio = new io::TabletIO();\n+        CHECK(parent_tabletio != NULL);\n+        \n+        int lg_num = request->schema().locality_groups_size(); \n+        for (int i = 0; i < lg_num; i++) {\n+            // discard delete lg\n+            if (request->schema().locality_groups(i).is_del()) {\n+                LOG(INFO) << \"LG[\" << i << \"] is del\";\n+                continue;\n+            }\n+            std::string left_LGname = FLAGS_tera_tabletnode_path_prefix + \"/\" + \n+                                 left_tabletname + \"/\" + leveldb::Uint64ToString(i);\n+            std::string right_LGname = FLAGS_tera_tabletnode_path_prefix + \"/\" + \n+                                 right_tabletname + \"/\" + leveldb::Uint64ToString(i);\n+            \n+            LOG(INFO) << __func__ << \", left LGname \" << left_LGname \n+                << \", right LGname \" << right_LGname;\n+            \n+            DebugTeraTabletServerCrashOrSuspend(DEBUG_ts_split_crash_or_suspend, 20);\n+            \n+            if (env->FileExists(leveldb::CurrentFileName(left_LGname))) {\n+                // step into split stage 2\n+                parent_tabletio->SetStatus(io::TabletIO::kFrozen); \n+                stage = 2;\n+                break;\n+            }\n+            if (env->FileExists(leveldb::CurrentFileName(right_LGname))) {\n+                // step into split stage 2\n+                parent_tabletio->SetStatus(io::TabletIO::kFrozen);\n+                stage = 2;\n+                break;\n+            }\n         }\n-        tablet_io->DecRef();\n-        done->Run();\n-        return;\n+\n+        if (stage == 1) {\n+            parent_tabletio->SetStatus(io::TabletIO::kNotInit);\n+        }\n+            \n+        // add into tabletmanager and  refcount++\n+        if (!m_tablet_manager->AddTablet(request->table_name(), \n+                                    request->key_range().key_start(),\n+                                    request->key_range().key_end(),\n+                                    parent_tabletio)) {\n+            // free parent's mem\n+            parent_tabletio->DecRef();\n+            parent_tabletio = NULL;\n+            // TODO: race condition !!\n+            LOG(WARNING) << \"split condition, \" << request->table_name() << \", start \"\n+                << request->key_range().key_start() << \", end \"  \n+                << request->key_range().key_end() << \", split stage \" << stage;\n+            response->set_status(kTabletSplitError); \n+            done->Run();\n+            return ;\n+        }\n+        DebugTeraTabletServerCrashOrSuspend(DEBUG_ts_split_crash_or_suspend, 25);\n     }\n-    int64_t first_half_size =\n-        tablet_io->GetDataSize(request->key_range().key_start(), split_key);\n-    int64_t second_half_size =\n-        tablet_io->GetDataSize(split_key, request->key_range().key_end());\n-    LOG(INFO) << \"split tablet: \" << tablet_io->GetTablePath()\n-        << \" [\" << DebugString(tablet_io->GetStartKey())\n-        << \", \" << DebugString(tablet_io->GetEndKey())\n-        << \"], split key: \" << DebugString(split_key);\n+    \n+    CHECK(request->snapshots_id_size() == request->snapshots_sequence_size());\n+    std::map<uint64_t, uint64_t> snapshots;\n+    for (int i = 0; i < request->snapshots_id_size(); i++) {\n+        snapshots[request->snapshots_id(i)] = request->snapshots_sequence(i);\n+    }\n+    // double check wether parent tablet in TM\n+    if (parent_tabletio) {\n+        if (stage == 1) {\n+            std::vector<uint64_t> null_parent_tablets;\n+            VLOG(20) << __func__ << \", load parent \" << parent_tabletname \n+                << \", start \" << request->key_range().key_start() \n+                << \", end \" << request->key_range().key_end();\n+            \n+            DebugTeraTabletServerCrashOrSuspend(DEBUG_ts_split_crash_or_suspend, 30);\n+            \n+            if (!parent_tabletio->Load(request->schema(), request->key_range().key_start(),\n+                                  request->key_range().key_end(), parent_tabletname,\n+                                  null_parent_tablets, snapshots, m_ldb_logger,\n+                                  m_ldb_block_cache, m_ldb_table_cache, &status)) {\n+                parent_tabletio->DecRef();\n+                response->set_status(kTabletSplitError);\n+                done->Run();\n+                return ;","path":"src/tabletnode/tabletnode_impl.cc","position":205,"original_position":205,"commit_id":"8aae5c159781a88c1b5942f60861b25f58834985","original_commit_id":"8aae5c159781a88c1b5942f60861b25f58834985","user":{"login":"00k","id":9716419,"avatar_url":"https://avatars.githubusercontent.com/u/9716419?v=3","gravatar_id":"","url":"https://api.github.com/users/00k","html_url":"https://github.com/00k","followers_url":"https://api.github.com/users/00k/followers","following_url":"https://api.github.com/users/00k/following{/other_user}","gists_url":"https://api.github.com/users/00k/gists{/gist_id}","starred_url":"https://api.github.com/users/00k/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/00k/subscriptions","organizations_url":"https://api.github.com/users/00k/orgs","repos_url":"https://api.github.com/users/00k/repos","events_url":"https://api.github.com/users/00k/events{/privacy}","received_events_url":"https://api.github.com/users/00k/received_events","type":"User","site_admin":false},"body":"不需要从TM中清掉parent？","created_at":"2015-09-06T06:16:46Z","updated_at":"2015-09-06T06:16:46Z","html_url":"https://github.com/baidu/tera/pull/253#discussion_r38818013","pull_request_url":"https://api.github.com/repos/baidu/tera/pulls/253","_links":{"self":{"href":"https://api.github.com/repos/baidu/tera/pulls/comments/38818013"},"html":{"href":"https://github.com/baidu/tera/pull/253#discussion_r38818013"},"pull_request":{"href":"https://api.github.com/repos/baidu/tera/pulls/253"}}},"pull_request":{"url":"https://api.github.com/repos/baidu/tera/pulls/253","id":43033352,"html_url":"https://github.com/baidu/tera/pull/253","diff_url":"https://github.com/baidu/tera/pull/253.diff","patch_url":"https://github.com/baidu/tera/pull/253.patch","issue_url":"https://api.github.com/repos/baidu/tera/issues/253","number":253,"state":"open","locked":false,"title":"Upgrade split ","user":{"login":"caijieming-baidu","id":13461134,"avatar_url":"https://avatars.githubusercontent.com/u/13461134?v=3","gravatar_id":"","url":"https://api.github.com/users/caijieming-baidu","html_url":"https://github.com/caijieming-baidu","followers_url":"https://api.github.com/users/caijieming-baidu/followers","following_url":"https://api.github.com/users/caijieming-baidu/following{/other_user}","gists_url":"https://api.github.com/users/caijieming-baidu/gists{/gist_id}","starred_url":"https://api.github.com/users/caijieming-baidu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caijieming-baidu/subscriptions","organizations_url":"https://api.github.com/users/caijieming-baidu/orgs","repos_url":"https://api.github.com/users/caijieming-baidu/repos","events_url":"https://api.github.com/users/caijieming-baidu/events{/privacy}","received_events_url":"https://api.github.com/users/caijieming-baidu/received_events","type":"User","site_admin":false},"body":"modify master && tabletserver 's split interface","created_at":"2015-08-21T12:40:08Z","updated_at":"2015-09-06T06:16:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e6e34fd652c2171bb0026f97d4b0dbd4ccd0d2b5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/baidu/tera/pulls/253/commits","review_comments_url":"https://api.github.com/repos/baidu/tera/pulls/253/comments","review_comment_url":"https://api.github.com/repos/baidu/tera/pulls/comments{/number}","comments_url":"https://api.github.com/repos/baidu/tera/issues/253/comments","statuses_url":"https://api.github.com/repos/baidu/tera/statuses/8aae5c159781a88c1b5942f60861b25f58834985","head":{"label":"caijieming-baidu:CodeMerge","ref":"CodeMerge","sha":"8aae5c159781a88c1b5942f60861b25f58834985","user":{"login":"caijieming-baidu","id":13461134,"avatar_url":"https://avatars.githubusercontent.com/u/13461134?v=3","gravatar_id":"","url":"https://api.github.com/users/caijieming-baidu","html_url":"https://github.com/caijieming-baidu","followers_url":"https://api.github.com/users/caijieming-baidu/followers","following_url":"https://api.github.com/users/caijieming-baidu/following{/other_user}","gists_url":"https://api.github.com/users/caijieming-baidu/gists{/gist_id}","starred_url":"https://api.github.com/users/caijieming-baidu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caijieming-baidu/subscriptions","organizations_url":"https://api.github.com/users/caijieming-baidu/orgs","repos_url":"https://api.github.com/users/caijieming-baidu/repos","events_url":"https://api.github.com/users/caijieming-baidu/events{/privacy}","received_events_url":"https://api.github.com/users/caijieming-baidu/received_events","type":"User","site_admin":false},"repo":{"id":39835330,"name":"tera","full_name":"caijieming-baidu/tera","owner":{"login":"caijieming-baidu","id":13461134,"avatar_url":"https://avatars.githubusercontent.com/u/13461134?v=3","gravatar_id":"","url":"https://api.github.com/users/caijieming-baidu","html_url":"https://github.com/caijieming-baidu","followers_url":"https://api.github.com/users/caijieming-baidu/followers","following_url":"https://api.github.com/users/caijieming-baidu/following{/other_user}","gists_url":"https://api.github.com/users/caijieming-baidu/gists{/gist_id}","starred_url":"https://api.github.com/users/caijieming-baidu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/caijieming-baidu/subscriptions","organizations_url":"https://api.github.com/users/caijieming-baidu/orgs","repos_url":"https://api.github.com/users/caijieming-baidu/repos","events_url":"https://api.github.com/users/caijieming-baidu/events{/privacy}","received_events_url":"https://api.github.com/users/caijieming-baidu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/caijieming-baidu/tera","description":"A distributed, elastic, structured database.","fork":true,"url":"https://api.github.com/repos/caijieming-baidu/tera","forks_url":"https://api.github.com/repos/caijieming-baidu/tera/forks","keys_url":"https://api.github.com/repos/caijieming-baidu/tera/keys{/key_id}","collaborators_url":"https://api.github.com/repos/caijieming-baidu/tera/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/caijieming-baidu/tera/teams","hooks_url":"https://api.github.com/repos/caijieming-baidu/tera/hooks","issue_events_url":"https://api.github.com/repos/caijieming-baidu/tera/issues/events{/number}","events_url":"https://api.github.com/repos/caijieming-baidu/tera/events","assignees_url":"https://api.github.com/repos/caijieming-baidu/tera/assignees{/user}","branches_url":"https://api.github.com/repos/caijieming-baidu/tera/branches{/branch}","tags_url":"https://api.github.com/repos/caijieming-baidu/tera/tags","blobs_url":"https://api.github.com/repos/caijieming-baidu/tera/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/caijieming-baidu/tera/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/caijieming-baidu/tera/git/refs{/sha}","trees_url":"https://api.github.com/repos/caijieming-baidu/tera/git/trees{/sha}","statuses_url":"https://api.github.com/repos/caijieming-baidu/tera/statuses/{sha}","languages_url":"https://api.github.com/repos/caijieming-baidu/tera/languages","stargazers_url":"https://api.github.com/repos/caijieming-baidu/tera/stargazers","contributors_url":"https://api.github.com/repos/caijieming-baidu/tera/contributors","subscribers_url":"https://api.github.com/repos/caijieming-baidu/tera/subscribers","subscription_url":"https://api.github.com/repos/caijieming-baidu/tera/subscription","commits_url":"https://api.github.com/repos/caijieming-baidu/tera/commits{/sha}","git_commits_url":"https://api.github.com/repos/caijieming-baidu/tera/git/commits{/sha}","comments_url":"https://api.github.com/repos/caijieming-baidu/tera/comments{/number}","issue_comment_url":"https://api.github.com/repos/caijieming-baidu/tera/issues/comments{/number}","contents_url":"https://api.github.com/repos/caijieming-baidu/tera/contents/{+path}","compare_url":"https://api.github.com/repos/caijieming-baidu/tera/compare/{base}...{head}","merges_url":"https://api.github.com/repos/caijieming-baidu/tera/merges","archive_url":"https://api.github.com/repos/caijieming-baidu/tera/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/caijieming-baidu/tera/downloads","issues_url":"https://api.github.com/repos/caijieming-baidu/tera/issues{/number}","pulls_url":"https://api.github.com/repos/caijieming-baidu/tera/pulls{/number}","milestones_url":"https://api.github.com/repos/caijieming-baidu/tera/milestones{/number}","notifications_url":"https://api.github.com/repos/caijieming-baidu/tera/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/caijieming-baidu/tera/labels{/name}","releases_url":"https://api.github.com/repos/caijieming-baidu/tera/releases{/id}","created_at":"2015-07-28T13:25:54Z","updated_at":"2015-07-28T13:25:55Z","pushed_at":"2015-09-02T05:23:39Z","git_url":"git://github.com/caijieming-baidu/tera.git","ssh_url":"git@github.com:caijieming-baidu/tera.git","clone_url":"https://github.com/caijieming-baidu/tera.git","svn_url":"https://github.com/caijieming-baidu/tera","homepage":"","size":16794,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"baidu:master","ref":"master","sha":"42d47e51963b46d5a4d2805ea37f863d93e17c7b","user":{"login":"baidu","id":13245940,"avatar_url":"https://avatars.githubusercontent.com/u/13245940?v=3","gravatar_id":"","url":"https://api.github.com/users/baidu","html_url":"https://github.com/baidu","followers_url":"https://api.github.com/users/baidu/followers","following_url":"https://api.github.com/users/baidu/following{/other_user}","gists_url":"https://api.github.com/users/baidu/gists{/gist_id}","starred_url":"https://api.github.com/users/baidu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/baidu/subscriptions","organizations_url":"https://api.github.com/users/baidu/orgs","repos_url":"https://api.github.com/users/baidu/repos","events_url":"https://api.github.com/users/baidu/events{/privacy}","received_events_url":"https://api.github.com/users/baidu/received_events","type":"Organization","site_admin":false},"repo":{"id":18129915,"name":"tera","full_name":"baidu/tera","owner":{"login":"baidu","id":13245940,"avatar_url":"https://avatars.githubusercontent.com/u/13245940?v=3","gravatar_id":"","url":"https://api.github.com/users/baidu","html_url":"https://github.com/baidu","followers_url":"https://api.github.com/users/baidu/followers","following_url":"https://api.github.com/users/baidu/following{/other_user}","gists_url":"https://api.github.com/users/baidu/gists{/gist_id}","starred_url":"https://api.github.com/users/baidu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/baidu/subscriptions","organizations_url":"https://api.github.com/users/baidu/orgs","repos_url":"https://api.github.com/users/baidu/repos","events_url":"https://api.github.com/users/baidu/events{/privacy}","received_events_url":"https://api.github.com/users/baidu/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/baidu/tera","description":"A distributed, elastic, structured database.","fork":false,"url":"https://api.github.com/repos/baidu/tera","forks_url":"https://api.github.com/repos/baidu/tera/forks","keys_url":"https://api.github.com/repos/baidu/tera/keys{/key_id}","collaborators_url":"https://api.github.com/repos/baidu/tera/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/baidu/tera/teams","hooks_url":"https://api.github.com/repos/baidu/tera/hooks","issue_events_url":"https://api.github.com/repos/baidu/tera/issues/events{/number}","events_url":"https://api.github.com/repos/baidu/tera/events","assignees_url":"https://api.github.com/repos/baidu/tera/assignees{/user}","branches_url":"https://api.github.com/repos/baidu/tera/branches{/branch}","tags_url":"https://api.github.com/repos/baidu/tera/tags","blobs_url":"https://api.github.com/repos/baidu/tera/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/baidu/tera/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/baidu/tera/git/refs{/sha}","trees_url":"https://api.github.com/repos/baidu/tera/git/trees{/sha}","statuses_url":"https://api.github.com/repos/baidu/tera/statuses/{sha}","languages_url":"https://api.github.com/repos/baidu/tera/languages","stargazers_url":"https://api.github.com/repos/baidu/tera/stargazers","contributors_url":"https://api.github.com/repos/baidu/tera/contributors","subscribers_url":"https://api.github.com/repos/baidu/tera/subscribers","subscription_url":"https://api.github.com/repos/baidu/tera/subscription","commits_url":"https://api.github.com/repos/baidu/tera/commits{/sha}","git_commits_url":"https://api.github.com/repos/baidu/tera/git/commits{/sha}","comments_url":"https://api.github.com/repos/baidu/tera/comments{/number}","issue_comment_url":"https://api.github.com/repos/baidu/tera/issues/comments{/number}","contents_url":"https://api.github.com/repos/baidu/tera/contents/{+path}","compare_url":"https://api.github.com/repos/baidu/tera/compare/{base}...{head}","merges_url":"https://api.github.com/repos/baidu/tera/merges","archive_url":"https://api.github.com/repos/baidu/tera/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/baidu/tera/downloads","issues_url":"https://api.github.com/repos/baidu/tera/issues{/number}","pulls_url":"https://api.github.com/repos/baidu/tera/pulls{/number}","milestones_url":"https://api.github.com/repos/baidu/tera/milestones{/number}","notifications_url":"https://api.github.com/repos/baidu/tera/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/baidu/tera/labels{/name}","releases_url":"https://api.github.com/repos/baidu/tera/releases{/id}","created_at":"2014-03-26T07:22:05Z","updated_at":"2015-09-02T09:59:25Z","pushed_at":"2015-09-02T05:23:40Z","git_url":"git://github.com/baidu/tera.git","ssh_url":"git@github.com:baidu/tera.git","clone_url":"https://github.com/baidu/tera.git","svn_url":"https://github.com/baidu/tera","homepage":"","size":15824,"stargazers_count":281,"watchers_count":281,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":85,"mirror_url":null,"open_issues_count":35,"forks":85,"open_issues":35,"watchers":281,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/baidu/tera/pulls/253"},"html":{"href":"https://github.com/baidu/tera/pull/253"},"issue":{"href":"https://api.github.com/repos/baidu/tera/issues/253"},"comments":{"href":"https://api.github.com/repos/baidu/tera/issues/253/comments"},"review_comments":{"href":"https://api.github.com/repos/baidu/tera/pulls/253/comments"},"review_comment":{"href":"https://api.github.com/repos/baidu/tera/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/baidu/tera/pulls/253/commits"},"statuses":{"href":"https://api.github.com/repos/baidu/tera/statuses/8aae5c159781a88c1b5942f60861b25f58834985"}}}}