Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RIP-70-3]Extract adaptive lock mechanism #8663

Merged
merged 65 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 63 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
ac6b13d
extract the adaptive lock
3424672656 Aug 25, 2024
c4bdf51
extract the adaptive lock
3424672656 Aug 25, 2024
12094b4
feat(): perfect the adaptive lock
3424672656 Aug 31, 2024
2b3c647
feat(): perfect the adaptive lock
3424672656 Aug 31, 2024
6d02e7e
Optimized code type
3424672656 Aug 31, 2024
2a5e23e
Optimized code type
3424672656 Aug 31, 2024
9714ea0
Optimized code type
3424672656 Sep 1, 2024
ff9f994
fix fail test
3424672656 Sep 1, 2024
439e2b8
Optimize the adaptive locking mechanism logic
Sep 20, 2024
1d00404
Optimize the adaptive locking mechanism logic
Sep 20, 2024
0904c50
feat:Adaptive locking mechanism adjustment
Sep 21, 2024
99e07d6
feat:Adaptive locking mechanism adjustment
Sep 21, 2024
ef10b0a
feat:Adaptive locking mechanism adjustment
Sep 21, 2024
8cfcf83
Merge remote-tracking branch 'origin/adaptive_lock' into adaptive_lock
Sep 23, 2024
a49deda
Optimize the adaptive locking mechanism logic
Sep 23, 2024
429f9e3
Optimize the adaptive locking mechanism logic
Sep 23, 2024
8b98e66
Optimize the adaptive locking mechanism logic
Sep 23, 2024
27a0a05
feat:Supports the hot activation of ABS locks
Sep 26, 2024
c8b4339
feat:Supports the hot activation of ABS locks
Sep 26, 2024
154fe75
feat:Supports the hot activation of ABS locks
Sep 26, 2024
8b9722d
Merge branch 'apache:develop' into adaptive_lock
3424672656 Sep 26, 2024
568f412
feat:Supports the hot activation of ABS locks
Sep 26, 2024
f674f1b
Optimize code style
Sep 26, 2024
61a9354
Optimize code style
Sep 26, 2024
c7abdd3
Optimize code style
Sep 27, 2024
8de385e
Optimize code style
Sep 27, 2024
70783b6
Optimize code style
Sep 27, 2024
56606d3
Optimize code style
Sep 27, 2024
993bcde
Updated the locking mechanism name
Oct 9, 2024
e44da59
Optimize the logic of switching to spin locks
Oct 9, 2024
fd4272a
Optimize the logic of switching to spin locks
Oct 9, 2024
efafc71
Optimize the logic of switching to spin locks
Oct 9, 2024
293212e
Optimize the logic of switching to spin locks
Oct 9, 2024
454bba6
Optimize the logic of switching to spin locks
Oct 9, 2024
ba0b4d0
Optimize the logic of switching to spin locks
Oct 9, 2024
e83b0a6
Optimize the logic of switching to spin locks
Oct 9, 2024
f257111
Optimize the logic of switching to spin locks
Oct 9, 2024
6b5a40b
delete unused import
Oct 9, 2024
1d7bac5
Optimize the logic of switching to spin locks
Oct 9, 2024
2174bd5
Revert "Optimize the logic of switching to spin locks"
Oct 9, 2024
b68b059
Optimize the logic of switching to spin locks
Oct 9, 2024
1b546f2
Optimize the logic of switching to spin locks
Oct 9, 2024
1ba5545
Merge branch 'apache:develop' into adaptive_lock
3424672656 Oct 11, 2024
ade9084
Optimize the logic of switching to spin locks
Oct 11, 2024
5557818
Merge remote-tracking branch 'origin/adaptive_lock' into adaptive_lock
Oct 11, 2024
1323ed0
Optimize the logic of switching to spin locks
Oct 11, 2024
02e6328
Optimize the logic of switching to spin locks
Oct 11, 2024
081b5e0
Optimize the logic of switching to spin locks
Oct 11, 2024
882f7ed
Optimize the logic of switching to spin locks
Oct 11, 2024
cb96e72
Optimized locking logic
Oct 16, 2024
c0751c7
Merge branch 'apache:develop' into adaptive_lock
3424672656 Oct 16, 2024
7b4401e
Optimized locking logic
Oct 16, 2024
52c359d
Merge remote-tracking branch 'origin/adaptive_lock' into adaptive_lock
Oct 16, 2024
3ed1e35
Optimized locking logic
Oct 16, 2024
c401dca
fix test
Oct 16, 2024
3c6bcef
fix test
Oct 16, 2024
0302a49
fix test
Oct 16, 2024
530cb62
fix test
Oct 16, 2024
cfc0240
Optimize code style
Oct 17, 2024
f526056
Optimize code style
Oct 17, 2024
447ad62
fix test
Oct 17, 2024
e886447
fix test
Oct 17, 2024
4b7fa65
Merge branch 'develop' of https://github.com/3424672656/rocketmq into…
Oct 19, 2024
d056c40
Merge branch 'develop' into adaptive_lock
Oct 23, 2024
39180a3
optimize client rebalancing logic
Oct 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion store/src/main/java/org/apache/rocketmq/store/CommitLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.rocketmq.store.config.MessageStoreConfig;
import org.apache.rocketmq.store.ha.HAService;
import org.apache.rocketmq.store.ha.autoswitch.AutoSwitchHAService;
import org.apache.rocketmq.store.lock.AdaptiveBackOffSpinLockImpl;
import org.apache.rocketmq.store.logfile.MappedFile;
import org.apache.rocketmq.store.queue.MultiDispatchUtils;
import org.apache.rocketmq.store.util.LibC;
Expand Down Expand Up @@ -130,7 +131,11 @@ protected PutMessageThreadLocal initialValue() {
return new PutMessageThreadLocal(defaultMessageStore.getMessageStoreConfig());
}
};
this.putMessageLock = messageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage() ? new PutMessageReentrantLock() : new PutMessageSpinLock();

PutMessageLock adaptiveBackOffSpinLock = new AdaptiveBackOffSpinLockImpl();

this.putMessageLock = messageStore.getMessageStoreConfig().getUseABSLock() ? adaptiveBackOffSpinLock :
messageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage() ? new PutMessageReentrantLock() : new PutMessageSpinLock();

this.flushDiskWatcher = new FlushDiskWatcher();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,17 @@ public class MessageStoreConfig {
*/
private String bottomMostCompressionTypeForConsumeQueueStore = "zstd";

/**
* Spin number in the retreat strategy of spin lock
* Default is 1000
*/
private int spinLockCollisionRetreatOptimalDegree = 1000;

/**
* Use AdaptiveBackOffLock
**/
private boolean useABSLock = false;

public int getBatchWriteKvCqSize() {
return batchWriteKvCqSize;
}
Expand Down Expand Up @@ -1908,4 +1919,20 @@ public String getBottomMostCompressionTypeForConsumeQueueStore() {
public void setBottomMostCompressionTypeForConsumeQueueStore(String bottomMostCompressionTypeForConsumeQueueStore) {
this.bottomMostCompressionTypeForConsumeQueueStore = bottomMostCompressionTypeForConsumeQueueStore;
}

public int getSpinLockCollisionRetreatOptimalDegree() {
return spinLockCollisionRetreatOptimalDegree;
}

public void setSpinLockCollisionRetreatOptimalDegree(int spinLockCollisionRetreatOptimalDegree) {
this.spinLockCollisionRetreatOptimalDegree = spinLockCollisionRetreatOptimalDegree;
}

public void setUseABSLock(boolean useABSLock) {
this.useABSLock = useABSLock;
}

public boolean getUseABSLock() {
return useABSLock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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 {
/**
* Configuration update
* @param messageStoreConfig
*/
default void update(MessageStoreConfig messageStoreConfig) {
}

/**
* Locking mechanism switching
*/
default void swap() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* 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);

// Used to determine the switchover between a mutex lock and a spin lock
private final static float SWAP_SPIN_LOCK_RATIO = 0.8f;

// It is used to adjust the spin number K of the escape spin lock
// When (retreat number / TPS) <= (1 / BASE_SWAP_ADAPTIVE_RATIO * SPIN_LOCK_ADAPTIVE_RATIO), K is decreased
private final static int SPIN_LOCK_ADAPTIVE_RATIO = 4;

// It is used to adjust the spin number K of the escape spin lock
// When (retreat number / TPS) >= (1 / BASE_SWAP_ADAPTIVE_RATIO), K is increased
private final static int BASE_SWAP_LOCK_RATIO = 320;

private final static String BACK_OFF_SPIN_LOCK = "SpinLock";

private final static String REENTRANT_LOCK = "ReentrantLock";

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_LOCK, new BackOffReentrantLock());
this.locks.put(BACK_OFF_SPIN_LOCK, 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(BACK_OFF_SPIN_LOCK);
}

@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;
// Avoid frequent adjustment of K, and make a reasonable range through experiments
// reasonable range : (retreat number / TPS) > (1 / BASE_SWAP_ADAPTIVE_RATIO * SPIN_LOCK_ADAPTIVE_RATIO) &&
// (retreat number / TPS) < (1 / BASE_SWAP_ADAPTIVE_RATIO)
if (lock.getNumberOfRetreat(slot) * BASE_SWAP_LOCK_RATIO >= tps) {
if (lock.isAdapt()) {
lock.adapt(true);
} else {
// It is used to switch between mutex lock and spin lock
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done~

if (this.state.compareAndSet(true, false)) {
// Ensures that no threads are in contention locks as well as in critical zones
int currentThreadNum;
do {
currentThreadNum = this.currentThreadNum.get();
} while (currentThreadNum != 0);

try {
if (this.adaptiveLock instanceof BackOffSpinLock) {
this.adaptiveLock = this.locks.get(REENTRANT_LOCK);
} else {
this.adaptiveLock = this.locks.get(BACK_OFF_SPIN_LOCK);
((BackOffSpinLock) this.adaptiveLock).adapt(false);
}
} catch (Exception e) {
//ignore
} finally {
this.state.compareAndSet(false, true);
}
}
}
}

public List<AdaptiveBackOffSpinLock> getLocks() {
return (List<AdaptiveBackOffSpinLock>) this.locks.values();
}

public void setLocks(Map<String, AdaptiveBackOffSpinLock> locks) {
this.locks = locks;
}

public boolean getState() {
return this.state.get();
}

public void setState(boolean state) {
this.state.set(state);
}

public AdaptiveBackOffSpinLock getAdaptiveLock() {
return adaptiveLock;
}

public List<AtomicInteger> getTpsTable() {
return tpsTable;
}

public void setSwapCriticalPoint(int swapCriticalPoint) {
this.swapCriticalPoint = swapCriticalPoint;
}

public int getSwapCriticalPoint() {
return swapCriticalPoint;
}

public boolean isOpen() {
return this.isOpen.get();
}

public void setOpen(boolean open) {
this.isOpen.set(open);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 java.util.concurrent.locks.ReentrantLock;

public class BackOffReentrantLock implements AdaptiveBackOffSpinLock {
private ReentrantLock putMessageNormalLock = new ReentrantLock(); // NonfairSync

@Override
public void lock() {
putMessageNormalLock.lock();
}

@Override
public void unlock() {
putMessageNormalLock.unlock();
}
}
Loading
Loading