-
Notifications
You must be signed in to change notification settings - Fork 11.7k
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
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 c4bdf51
extract the adaptive lock
3424672656 12094b4
feat(): perfect the adaptive lock
3424672656 2b3c647
feat(): perfect the adaptive lock
3424672656 6d02e7e
Optimized code type
3424672656 2a5e23e
Optimized code type
3424672656 9714ea0
Optimized code type
3424672656 ff9f994
fix fail test
3424672656 439e2b8
Optimize the adaptive locking mechanism logic
1d00404
Optimize the adaptive locking mechanism logic
0904c50
feat:Adaptive locking mechanism adjustment
99e07d6
feat:Adaptive locking mechanism adjustment
ef10b0a
feat:Adaptive locking mechanism adjustment
8cfcf83
Merge remote-tracking branch 'origin/adaptive_lock' into adaptive_lock
a49deda
Optimize the adaptive locking mechanism logic
429f9e3
Optimize the adaptive locking mechanism logic
8b98e66
Optimize the adaptive locking mechanism logic
27a0a05
feat:Supports the hot activation of ABS locks
c8b4339
feat:Supports the hot activation of ABS locks
154fe75
feat:Supports the hot activation of ABS locks
8b9722d
Merge branch 'apache:develop' into adaptive_lock
3424672656 568f412
feat:Supports the hot activation of ABS locks
f674f1b
Optimize code style
61a9354
Optimize code style
c7abdd3
Optimize code style
8de385e
Optimize code style
70783b6
Optimize code style
56606d3
Optimize code style
993bcde
Updated the locking mechanism name
e44da59
Optimize the logic of switching to spin locks
fd4272a
Optimize the logic of switching to spin locks
efafc71
Optimize the logic of switching to spin locks
293212e
Optimize the logic of switching to spin locks
454bba6
Optimize the logic of switching to spin locks
ba0b4d0
Optimize the logic of switching to spin locks
e83b0a6
Optimize the logic of switching to spin locks
f257111
Optimize the logic of switching to spin locks
6b5a40b
delete unused import
1d7bac5
Optimize the logic of switching to spin locks
2174bd5
Revert "Optimize the logic of switching to spin locks"
b68b059
Optimize the logic of switching to spin locks
1b546f2
Optimize the logic of switching to spin locks
1ba5545
Merge branch 'apache:develop' into adaptive_lock
3424672656 ade9084
Optimize the logic of switching to spin locks
5557818
Merge remote-tracking branch 'origin/adaptive_lock' into adaptive_lock
1323ed0
Optimize the logic of switching to spin locks
02e6328
Optimize the logic of switching to spin locks
081b5e0
Optimize the logic of switching to spin locks
882f7ed
Optimize the logic of switching to spin locks
cb96e72
Optimized locking logic
c0751c7
Merge branch 'apache:develop' into adaptive_lock
3424672656 7b4401e
Optimized locking logic
52c359d
Merge remote-tracking branch 'origin/adaptive_lock' into adaptive_lock
3ed1e35
Optimized locking logic
c401dca
fix test
3c6bcef
fix test
0302a49
fix test
530cb62
fix test
cfc0240
Optimize code style
f526056
Optimize code style
447ad62
fix test
e886447
fix test
4b7fa65
Merge branch 'develop' of https://github.com/3424672656/rocketmq into…
d056c40
Merge branch 'develop' into adaptive_lock
39180a3
optimize client rebalancing logic
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
store/src/main/java/org/apache/rocketmq/store/lock/AdaptiveBackOffSpinLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} |
207 changes: 207 additions & 0 deletions
207
store/src/main/java/org/apache/rocketmq/store/lock/AdaptiveBackOffSpinLockImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
store/src/main/java/org/apache/rocketmq/store/lock/BackOffReentrantLock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done~