hello-stephen commented on code in PR #70916:
URL: https://github.com/apache/doris/pull/70916#discussion_r4756783702


##########
be/src/load/group_commit/wal/wal_manager.cpp:
##########
@@ -228,14 +229,40 @@ std::string 
WalManager::get_last_replay_wal_failed_reason(int64_t table_id) {
     return "";
 }
 
+Status WalManager::_sync_wal_parent_dir(const io::Path& table_dir) {
+    auto fs = io::global_local_filesystem();
+    auto sync_directory_entry = [&](const io::Path& dir, WalDirectoryCache& 
cache) -> Status {
+        auto& slot = cache[std::filesystem::hash_value(dir) % cache.size()];
+        std::lock_guard lock(slot.mutex);
+        bool exists;
+        RETURN_IF_ERROR(fs->exists(dir, &exists));
+        if (exists && slot.synced_dir == dir) {
+            return Status::OK();
+        }
+        if (!exists) {
+            // A recreated directory needs a fresh parent sync even if mkdir 
succeeds and sync fails.
+            slot.synced_dir.clear();
+            RETURN_IF_ERROR(fs->create_directory(dir));
+        }
+        // A collision evicts the previous path. Its next lookup will sync its 
parent again.
+        RETURN_IF_ERROR(io::sync_dir(dir.parent_path()));
+        slot.synced_dir = dir;
+        return Status::OK();
+    };
+    RETURN_IF_ERROR(sync_directory_entry(table_dir.parent_path(), 
_wal_db_directories));

Review Comment:
   [P1] Persist newly created WAL roots before acknowledging loads
   
   When `group_commit_wal_path` names a missing absolute directory, such as 
`/mnt/wal-volume/new-wal` with storage on another volume, `_init_wal_dirs()` 
creates it without syncing its parent. This helper syncs only the root/database 
directories, and file creation syncs the table; neither those descendant syncs 
nor WAL `fdatasync` persists `new-wal`'s entry in `/mnt/wal-volume`. With 
`group_commit_wal_sync_before_ack=true`, an insert can therefore succeed while 
a power failure can still make its entire WAL tree unreachable. Persist the 
root and any newly created ancestor entries before serving durable ACKs, and 
cover startup with a previously missing configured root.



##########
be/src/load/group_commit/group_commit_mgr.cpp:
##########
@@ -183,27 +199,102 @@ Status LoadBlockQueue::get_block(RuntimeState* 
runtime_state, Block* block, bool
                    << ". txn_id=" << txn_id << ", label=" << label
                    << ", instance_id=" << load_instance_id << ", load_ids=" << 
_get_load_ids();
     }
-    if (_block_queue.empty() && _need_commit.load() && 
_load_ids_to_write_dep.empty()) {
+    if (_block_queue.empty() && _need_commit.load() && 
_registered_loads.empty()) {
         *eos = true;
     } else {
         *eos = false;
     }
     if (_all_block_queues_bytes->load(std::memory_order_relaxed) <
         config::group_commit_queue_mem_limit) {
-        for (auto& id : _load_ids_to_write_dep) {
-            id.second->set_ready();
+        for (auto& id : _registered_loads) {
+            id.second.write_dep->set_ready();
         }
         VLOG_DEBUG << "set ready for load_ids=" << _get_load_ids()
                    << ". inner load_id=" << load_instance_id << ", label=" << 
label;
     }
     return Status::OK();
 }
 
-Status LoadBlockQueue::remove_load_id(const UniqueId& load_id) {
-    std::unique_lock l(mutex);
-    if (_load_ids_to_write_dep.find(load_id) != _load_ids_to_write_dep.end()) {
-        _load_ids_to_write_dep[load_id]->set_always_ready();
-        _load_ids_to_write_dep.erase(load_id);
+Status LoadBlockQueue::finish_wal_load(const UniqueId& load_id, size_t target,
+                                       std::shared_ptr<WalSyncWaiter> waiter, 
GroupCommitMgr& mgr) {
+    std::lock_guard lock(mutex);
+    RETURN_IF_ERROR(status);
+    if (_wal_closing) {
+        return Status::Cancelled("group commit WAL is closing");
+    }
+    target = std::max(target, _wal_header_end);
+    auto load = _registered_loads.find(load_id);
+    if (load == _registered_loads.end() || target <= _wal_synced_offset) {
+        waiter->result = _remove_load_id_without_lock(load_id);
+        return *waiter->result;
+    }
+    _pending_wal_acks.push_back({load_id, target, waiter});
+    load->second.waiting_for_wal_ack = true;
+    waiter->finish_dep->block();
+    if (!_wal_sync_in_progress) {
+        _wal_sync_in_progress = true;
+        Status st;
+        ASSIGN_STATUS_IF_CATCH_EXCEPTION(
+                st = mgr.submit_wal_sync([queue = shared_from_this()] { 
queue->_run_wal_sync(); }),
+                st);
+        if (!st.ok()) {
+            _wal_sync_in_progress = false;
+            _cancel_without_lock(st);
+            return st;
+        }
+    }
+    return Status::OK();
+}
+
+void LoadBlockQueue::_run_wal_sync() {
+    std::unique_lock lock(mutex);
+    while (!_pending_wal_acks.empty()) {
+        // Appends after this snapshot can overlap the syscall, but are only
+        // credited by a later successful sync.
+        const size_t target = _wal_appended_offset;
+        lock.unlock();
+        Status st;
+        ASSIGN_STATUS_IF_CATCH_EXCEPTION(st = _v_wal_writer->sync(), st);
+        lock.lock();
+        if (!st.ok()) {
+            _cancel_without_lock(st);
+            break;
+        }
+        _wal_synced_offset = target;
+        _complete_wal_acks();
+    }
+    _wal_sync_in_progress = false;
+    _wal_sync_cv.notify_all();
+}
+
+void LoadBlockQueue::_complete_wal_acks() {
+    for (auto it = _pending_wal_acks.begin(); it != _pending_wal_acks.end();) {
+        if (status.ok() && it->end_offset > _wal_synced_offset) {
+            ++it;
+            continue;
+        }
+        auto st = _remove_load_id_without_lock(it->load_id);

Review Comment:
   [P2] Retire cancelled ACK ownership before reusing a load ID
   
   With sync-before-ACK enabled, let a prepared insert's producer time out 
while its sync is outstanding, with the table interval/internal timeout longer 
than the producer timeout, then execute the cached statement again on the same 
BE. Cancellation returns the first RPC and removes its stream/query 
registrations, but `_load_id_removed=true` leaves its pending ACK owning the 
queue's load ID. The next execution retains that ID and 
`get_first_block_load_queue()` borrows the old registration. Completing the old 
ACK here erases it; the new execution's later ACK then receives `NotFound` 
despite a successful sync. Give registrations/ACKs execution-specific 
ownership, or detach a cancelled ACK before its ID becomes reusable, and cover 
timeout followed by cached-plan reuse.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to