fuyou001 commented on code in PR #8663:
URL: https://github.com/apache/rocketmq/pull/8663#discussion_r1804047581


##########
store/src/main/java/org/apache/rocketmq/store/CommitLog.java:
##########
@@ -130,7 +132,11 @@ protected PutMessageThreadLocal initialValue() {
                 return new 
PutMessageThreadLocal(defaultMessageStore.getMessageStoreConfig());
             }
         };
-        this.putMessageLock = 
messageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage() ? new 
PutMessageReentrantLock() : new PutMessageSpinLock();
+
+        AdaptiveBackOffSpinLock adaptiveBackOffSpinLock = new 
AdaptiveBackOffSpinLockImpl();

Review Comment:
     PutMessageLock adaptiveBackOffSpinLock = new AdaptiveBackOffSpinLockImpl() 
may be better



##########
store/src/main/java/org/apache/rocketmq/store/lock/AdaptiveBackOffSpinLock.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+package org.apache.rocketmq.store.lock;
+
+import org.apache.rocketmq.store.PutMessageLock;
+import org.apache.rocketmq.store.config.MessageStoreConfig;
+
+public interface AdaptiveBackOffSpinLock extends PutMessageLock {
+
+    void lock();

Review Comment:
   The PutMessageLock class includes methods for locking and unlocking.



##########
store/src/main/java/org/apache/rocketmq/store/lock/AdaptiveBackOffSpinLockImpl.java:
##########
@@ -0,0 +1,193 @@
+/*
+ * 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.
+ */
+package org.apache.rocketmq.store.lock;
+
+import org.apache.rocketmq.store.config.MessageStoreConfig;
+
+import java.time.LocalTime;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+public class AdaptiveBackOffSpinLockImpl implements AdaptiveBackOffSpinLock {
+    private AdaptiveBackOffSpinLock adaptiveLock;
+    //state
+    private AtomicBoolean state = new AtomicBoolean(true);
+
+    private final static float SWAP_SPIN_LOCK_RATIO = 0.8f;
+
+    private final static int SPIN_LOCK_ADAPTIVE_RATIO = 4;
+
+    private final static int BASE_SWAP_LOCK_RATIO = 320;
+
+    private Map<String, AdaptiveBackOffSpinLock> locks;
+
+    private final List<AtomicInteger> tpsTable;
+
+    private final List<Map<Thread, Byte>> threadTable;
+
+    private int swapCriticalPoint;
+
+    private AtomicInteger currentThreadNum = new AtomicInteger(0);
+
+    private AtomicBoolean isOpen = new AtomicBoolean(true);
+
+    public AdaptiveBackOffSpinLockImpl() {
+        this.locks = new HashMap<>();
+        this.locks.put("Reentrant", new BackOffReentrantLock());
+        this.locks.put("Spin", new BackOffSpinLock());
+
+        this.threadTable = new ArrayList<>(2);
+        this.threadTable.add(new ConcurrentHashMap<>());
+        this.threadTable.add(new ConcurrentHashMap<>());
+
+        this.tpsTable = new ArrayList<>(2);
+        this.tpsTable.add(new AtomicInteger(0));
+        this.tpsTable.add(new AtomicInteger(0));
+
+        adaptiveLock = this.locks.get("Spin");
+    }
+
+    @Override
+    public void lock() {
+        int slot = LocalTime.now().getSecond() % 2;
+        this.threadTable.get(slot).putIfAbsent(Thread.currentThread(), 
Byte.MAX_VALUE);
+        this.tpsTable.get(slot).getAndIncrement();
+        boolean state;
+        do {
+            state = this.state.get();
+        } while (!state);
+
+        currentThreadNum.incrementAndGet();
+        this.adaptiveLock.lock();
+    }
+
+    @Override
+    public void unlock() {
+        this.adaptiveLock.unlock();
+        currentThreadNum.decrementAndGet();
+        if (isOpen.get()) {
+            swap();
+        }
+    }
+
+    @Override
+    public void update(MessageStoreConfig messageStoreConfig) {
+        this.adaptiveLock.update(messageStoreConfig);
+    }
+
+    @Override
+    public void swap() {
+        if (!this.state.get()) {
+            return;
+        }
+        boolean needSwap = false;
+        int slot = 1 - LocalTime.now().getSecond() % 2;
+        int tps = this.tpsTable.get(slot).get() + 1;
+        int threadNum = this.threadTable.get(slot).size();
+        this.tpsTable.get(slot).set(-1);
+        this.threadTable.get(slot).clear();
+        if (tps == 0) {
+            return;
+        }
+
+        if (this.adaptiveLock instanceof BackOffSpinLock) {
+            BackOffSpinLock lock = (BackOffSpinLock) this.adaptiveLock;
+            if (lock.getNumberOfRetreat(slot) * BASE_SWAP_LOCK_RATIO >= tps) {
+                if (lock.isAdapt()) {
+                    lock.adapt(true);
+                } else {
+                    this.swapCriticalPoint = tps * threadNum;
+                    needSwap = true;
+                }
+            } else if (lock.getNumberOfRetreat(slot) * BASE_SWAP_LOCK_RATIO * 
SPIN_LOCK_ADAPTIVE_RATIO <= tps) {
+                lock.adapt(false);
+            }
+            lock.setNumberOfRetreat(slot, 0);
+        } else {
+            if (tps * threadNum <= this.swapCriticalPoint * 
SWAP_SPIN_LOCK_RATIO) {
+                needSwap = true;
+            }
+        }
+
+        if (needSwap) {

Review Comment:
   Add some comments to explain the principles and the rationale behind these 
numbers.



-- 
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...@rocketmq.apache.org

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

Reply via email to