From 480010f64bbe1e0529969da0d3be4d20bf288c4d Mon Sep 17 00:00:00 2001
From: Wallace <bupt2013211450@gmail.com>
Date: Tue, 5 Jul 2022 16:00:55 +0800
Subject: [PATCH] fix(storage): fix race condition between pin version and new
 shared-buffer (#3651)

* fix

Signed-off-by: Little-Wallace <bupt2013211450@gmail.com>

* fix warn

Signed-off-by: Little-Wallace <bupt2013211450@gmail.com>
---
 src/storage/src/hummock/local_version_manager.rs | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/storage/src/hummock/local_version_manager.rs b/src/storage/src/hummock/local_version_manager.rs
index 598ac82026964..f031b2ce1f9b5 100644
--- a/src/storage/src/hummock/local_version_manager.rs
+++ b/src/storage/src/hummock/local_version_manager.rs
@@ -21,7 +21,7 @@ use std::time::Duration;
 use bytes::Bytes;
 use futures::future::{join_all, try_join_all};
 use itertools::Itertools;
-use parking_lot::{RwLock, RwLockWriteGuard};
+use parking_lot::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};
 use risingwave_common::config::StorageConfig;
 use risingwave_hummock_sdk::key::FullKey;
 use risingwave_hummock_sdk::{CompactionGroupId, LocalSstableInfo};
@@ -221,7 +221,7 @@ impl LocalVersionManager {
             }
         }
 
-        let old_version = self.local_version.read().clone();
+        let old_version = self.local_version.upgradable_read();
         if old_version.pinned_version().id() >= new_version_id {
             return false;
         }
@@ -230,10 +230,10 @@ impl LocalVersionManager {
             conflict_detector.set_watermark(newly_pinned_version.max_committed_epoch);
         }
 
-        let mut new_version = old_version;
+        let mut new_version = old_version.clone();
         new_version.set_pinned_version(newly_pinned_version);
         {
-            let mut guard = self.local_version.write();
+            let mut guard = RwLockUpgradableReadGuard::upgrade(old_version);
             *guard = new_version;
             RwLockWriteGuard::unlock_fair(guard);
         }