github-actions[bot] commented on code in PR #19237:
URL: https://github.com/apache/doris/pull/19237#discussion_r1186659336


##########
be/src/olap/tablet.cpp:
##########
@@ -432,6 +440,94 @@ Status Tablet::add_rowset(RowsetSharedPtr rowset) {
     return Status::OK();
 }
 
+bool Tablet::should_fetch_from_master(Version& output_version){
+    //  get peer version from master
+    TReplicaInfo addr;
+    std::string token;
+    if(!StorageEngine::instance()->get_master_replica(tablet_id(), addr, 
token)) {
+        LOG(INFO) << tablet_id() <<  " tablet don't have master peer";
+        return false;
+    }
+    PGetTabletVersionsRequest request;
+    request.set_tablet_id(tablet_id());
+    PGetTabletVersionsResponse response;
+    std::shared_ptr<PBackendService_Stub> stub =
+            
ExecEnv::GetInstance()->brpc_internal_client_cache()->get_client(addr.host,
+                                                                            
addr.brpc_port);
+    brpc::Controller cntl;
+    stub->get_tablet_versions(&cntl, &request, &response, nullptr);
+    if (cntl.Failed()) {
+        LOG(WARNING) << "open brpc connection to " << addr.host << " failed: " 
<< cntl.ErrorText();
+        return false;
+    }
+    if (response.versions_size() == 0) {
+        return false;
+    }
+    std::vector<Version> peer_versions;
+    for (int i = 0; i < response.versions_size(); ++i) {
+        peer_versions.emplace_back(Version(response.versions(i).first(), 
response.versions(i).second()));
+    }
+
+    //  get local versions
+    std::vector<Version> local_versions = get_all_versions();
+    std::sort(local_versions.begin(), local_versions.end(),
+              [](const Version& left, const Version& right) {
+                  return left.first < right.first;
+              });
+    for (const auto & v : local_versions) {
+        VLOG_CRITICAL << tablet_id() << " tablet local version: " << v.first 
<< " - " << v.second;
+    }
+    for (const auto & v : peer_versions) {
+        VLOG_CRITICAL << tablet_id() << " tablet peer version: " << v.first << 
" - " << v.second;
+    }
+
+    bool find = false;
+    int index_peer = 0;
+    int index_local = 0;
+
+    // peer_versions  [0-0] [1-1] [2-2] [3-5] [6-7]
+    // local_versions [0-0] [1-1] [2-2] [3-3] [4-4] [5-5] [6-7]
+    // return output_version [3-5]
+    //  1: skip same versions
+    while (index_local < local_versions.size() && index_peer < 
peer_versions.size()) {
+        if (peer_versions[index_peer].first == 
local_versions[index_local].first
+            && peer_versions[index_peer].second == 
local_versions[index_local].second) {
+                ++index_peer;
+                ++index_local;
+                continue;
+        } 
+        break;    
+    }
+    if(index_peer >= peer_versions.size() || index_local >= 
local_versions.size()) {
+        return false;
+    }
+
+    //  2: first match
+    if (peer_versions[index_peer].first != local_versions[index_local].first) {
+        return false;
+    }
+
+    //  3: second match
+    if (peer_versions[index_peer].contains(local_versions[index_local])) {
+        ++index_local;
+        while (index_local < local_versions.size()) {
+            if 
(peer_versions[index_peer].contains(local_versions[index_local])) {
+                ++index_local;
+                continue;
+            }
+            break;
+        }
+        --index_local;
+        if (local_versions[index_local].second == 
peer_versions[index_peer].second) {
+            find = true;
+            output_version = peer_versions[index_peer];
+            VLOG_CRITICAL << tablet_id() << " tablet should fetch peer 
replica: " << output_version.first
+                          << " - " << output_version.second;
+        }
+    }
+    return find;
+}
+

Review Comment:
   warning: method 'modify_rowsets' can be made const 
[readability-make-member-function-const]
   
   be/src/olap/tablet.cpp:530:
   ```diff
   -                               std::vector<RowsetSharedPtr>& to_delete, 
bool check_delete) {
   +                               std::vector<RowsetSharedPtr>& to_delete, 
bool check_delete) const {
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to