dataroaring commented on code in PR #19917:
URL: https://github.com/apache/doris/pull/19917#discussion_r1268992465


##########
be/src/vec/sink/autoinc_buffer.cpp:
##########
@@ -0,0 +1,178 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "vec/sink/autoinc_buffer.h"
+
+#include <gen_cpp/HeartbeatService_types.h>
+
+#include "runtime/client_cache.h"
+#include "runtime/exec_env.h"
+#include "util/runtime_profile.h"
+#include "util/thrift_rpc_helper.h"
+#include "vec/sink/vtablet_block_convertor.h"
+
+namespace doris {
+namespace stream_load {
+
+FetchAutoIncIDExecutor::FetchAutoIncIDExecutor() {
+    ThreadPoolBuilder("AsyncFetchAutoIncIDExecutor")
+            .set_min_threads(config::auto_inc_fetch_thread_num)
+            .set_max_threads(config::auto_inc_fetch_thread_num)
+            .set_max_queue_size(std::numeric_limits<int>::max())
+            .build(&_pool);
+}
+
+AutoIncIDRequestHandlerExecutor::AutoIncIDRequestHandlerExecutor() {
+    ThreadPoolBuilder("AutoIncIDRequestHandlerExecutor")
+            .set_min_threads(config::auto_inc_request_handler_thread_num)
+            .set_max_threads(config::auto_inc_request_handler_thread_num)
+            .set_max_queue_size(std::numeric_limits<int>::max())
+            .build(&_pool);
+}
+
+AutoIncIDBuffer::AutoIncIDBuffer(int64_t db_id, int64_t table_id, int64_t 
column_id)
+        : _db_id(db_id),
+          _table_id(table_id),
+          _column_id(column_id),
+          _rpc_token(FetchAutoIncIDExecutor::GetInstance()->_pool->new_token(
+                  ThreadPool::ExecutionMode::CONCURRENT)),
+          
_request_handler_token(AutoIncIDRequestHandlerExecutor::GetInstance()->_pool->new_token(
+                  ThreadPool::ExecutionMode::CONCURRENT)) {}
+
+void AutoIncIDBuffer::set_batch_size_at_least(size_t batch_size) {
+    if (batch_size > _batch_size) {
+        _batch_size = batch_size;
+    }
+}
+
+void AutoIncIDBuffer::_wait_for_prefetching() {
+    if (_is_fetching) {
+        _rpc_token->wait();
+    }
+}
+
+Status AutoIncIDBuffer::sync_request_ids(size_t length,
+                                         std::vector<std::pair<int64_t, 
size_t>>* result) {
+    std::unique_lock<std::mutex> lock(_mutex);
+    _prefetch_ids(_prefetch_size());
+    if (_front_buffer.second > 0) {
+        auto min_length = std::min(_front_buffer.second, length);
+        length -= min_length;
+        result->emplace_back(_front_buffer.first, min_length);
+        _front_buffer.first += min_length;
+        _front_buffer.second -= min_length;
+    }
+    if (length > 0) {
+        _wait_for_prefetching();
+        if (_rpc_status != Status::OK()) {
+            return _rpc_status;
+        }
+
+        {
+            std::lock_guard<std::mutex> lock(_backend_buffer_latch);
+            std::swap(_front_buffer, _backend_buffer);
+        }
+
+        DCHECK(length <= _front_buffer.second);
+        result->emplace_back(_front_buffer.first, length);
+        _front_buffer.first += length;
+        _front_buffer.second -= length;
+    }
+    return Status::OK();
+}
+
+void AutoIncIDBuffer::async_request_ids(OlapTableBlockConvertor* 
block_convertor, size_t length) {
+    length = std::max(length, MIN_BATCH_SIZE);
+    block_convertor->set_is_waiting_for_auto_inc(true);
+    std::shared_ptr<std::promise<Status>> promise 
{std::make_shared<std::promise<Status>>()};
+    block_convertor->set_request_future(*promise);
+    _request_handler_token->submit_func([this, length, block_convertor,
+                                         promise = std::move(promise)]() 
mutable {
+        std::unique_lock<std::mutex> lock(_mutex);
+        _prefetch_ids(_prefetch_size());
+        if (_front_buffer.second > 0) {
+            auto min_length = std::min(_front_buffer.second, length);
+            length -= min_length;
+            
block_convertor->auto_inc_id_allocator().insert_ids(_front_buffer.first, 
min_length);
+            _front_buffer.first += min_length;
+            _front_buffer.second -= min_length;
+        }
+        if (length > 0) {
+            _wait_for_prefetching();
+            if (_rpc_status != Status::OK()) {
+                block_convertor->set_is_waiting_for_auto_inc(false);
+                promise->set_value(_rpc_status);
+                return;
+            }
+
+            {
+                std::lock_guard<std::mutex> lock(_backend_buffer_latch);
+                std::swap(_front_buffer, _backend_buffer);
+            }
+
+            DCHECK(length <= _front_buffer.second);
+            
block_convertor->auto_inc_id_allocator().insert_ids(_front_buffer.first, 
length);
+            _front_buffer.first += length;
+            _front_buffer.second -= length;
+        }
+        block_convertor->set_is_waiting_for_auto_inc(false);
+        promise->set_value(_rpc_status);

Review Comment:
   please use a defer to replace line 117 118 and line 132 133.



-- 
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