-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Changes from 58 commits
ac6b13d
c4bdf51
12094b4
2b3c647
6d02e7e
2a5e23e
9714ea0
ff9f994
439e2b8
1d00404
0904c50
99e07d6
ef10b0a
8cfcf83
a49deda
429f9e3
8b98e66
27a0a05
c8b4339
154fe75
8b9722d
568f412
f674f1b
61a9354
c7abdd3
8de385e
70783b6
56606d3
993bcde
e44da59
fd4272a
efafc71
293212e
454bba6
ba0b4d0
e83b0a6
f257111
6b5a40b
1d7bac5
2174bd5
b68b059
1b546f2
1ba5545
ade9084
5557818
1323ed0
02e6328
081b5e0
882f7ed
cb96e72
c0751c7
7b4401e
52c359d
3ed1e35
c401dca
3c6bcef
0302a49
530cb62
cfc0240
f526056
447ad62
e886447
4b7fa65
d056c40
39180a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PutMessageLock class includes methods for locking and unlocking. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done~ |
||
|
||
void unlock(); | ||
|
||
/** | ||
* 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,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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These strings use class constants. |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done~ |
||
if (this.state.compareAndSet(true, false)) { | ||
int currentThreadNum; | ||
do { | ||
currentThreadNum = this.currentThreadNum.get(); | ||
} while (currentThreadNum != 0); | ||
|
||
try { | ||
if (this.adaptiveLock instanceof BackOffSpinLock) { | ||
this.adaptiveLock = this.locks.get("Reentrant"); | ||
} else { | ||
this.adaptiveLock = this.locks.get("Spin"); | ||
((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(); | ||
} | ||
} |
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.
PutMessageLock adaptiveBackOffSpinLock = new AdaptiveBackOffSpinLockImpl() may be better
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~