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

Open
wants to merge 65 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 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:/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
14 changes: 12 additions & 2 deletions 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,9 @@
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.AdaptiveBackOffLock;
import org.apache.rocketmq.store.lock.AdaptiveBackOffLockImpl;
import org.apache.rocketmq.store.lock.CollisionRetreatLock;
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 @@ -93,7 +96,7 @@ public class CommitLog implements Swappable {

private volatile long beginTimeInLock = 0;

protected final PutMessageLock putMessageLock;
protected final AdaptiveBackOffLock putMessageLock;

protected final TopicQueueLock topicQueueLock;

Expand Down Expand Up @@ -130,7 +133,8 @@ protected PutMessageThreadLocal initialValue() {
return new PutMessageThreadLocal(defaultMessageStore.getMessageStoreConfig());
}
};
this.putMessageLock = messageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage() ? new PutMessageReentrantLock() : new PutMessageSpinLock();
this.putMessageLock = messageStore.getMessageStoreConfig().getUseABSLock() ? new AdaptiveBackOffLockImpl() :
messageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage() ? new PutMessageReentrantLock() : new CollisionRetreatLock();

this.flushDiskWatcher = new FlushDiskWatcher();

Expand Down Expand Up @@ -948,6 +952,9 @@ public CompletableFuture<PutMessageResult> asyncPutMessage(final MessageExtBroke
currOffset = mappedFile.getFileFromOffset() + mappedFile.getWrotePosition();
}

this.putMessageLock.isOpen(this.defaultMessageStore.getMessageStoreConfig().getUseABSLock(),
this.defaultMessageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage());

int needAckNums = this.defaultMessageStore.getMessageStoreConfig().getInSyncReplicas();
boolean needHandleHA = needHandleHA(msg);

Expand Down Expand Up @@ -1113,6 +1120,9 @@ public CompletableFuture<PutMessageResult> asyncPutMessages(final MessageExtBatc
currOffset = mappedFile.getFileFromOffset() + mappedFile.getWrotePosition();
}

this.putMessageLock.isOpen(this.defaultMessageStore.getMessageStoreConfig().getUseABSLock(),
this.defaultMessageStore.getMessageStoreConfig().isUseReentrantLockWhenPutMessage());

int needAckNums = this.defaultMessageStore.getMessageStoreConfig().getInSyncReplicas();
boolean needHandleHA = needHandleHA(messageExtBatch);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package org.apache.rocketmq.store;

import org.apache.rocketmq.store.lock.AdaptiveBackOffLock;

import java.util.concurrent.locks.ReentrantLock;

/**
* Exclusive lock implementation to put message
*/
public class PutMessageReentrantLock implements PutMessageLock {
public class PutMessageReentrantLock implements PutMessageLock, AdaptiveBackOffLock {
private ReentrantLock putMessageNormalLock = new ReentrantLock(); // NonfairSync

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ public class MessageStoreConfig {

private int batchWriteKvCqSize = 16;

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

/**
* Use AdaptiveBackOffLock
**/
private boolean isUseABSLock = true;

public int getBatchWriteKvCqSize() {
return batchWriteKvCqSize;
Expand Down Expand Up @@ -1885,4 +1895,19 @@ public void setTransferMetadataJsonToRocksdb(boolean transferMetadataJsonToRocks
this.transferMetadataJsonToRocksdb = transferMetadataJsonToRocksdb;
}

public int getSpinLockCollisionRetreatOptimalDegree() {
return spinLockCollisionRetreatOptimalDegree;
}

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

public boolean getUseABSLock() {
return isUseABSLock;
}

public void setUseABSLock(boolean useABSLock) {
this.isUseABSLock = 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.config.MessageStoreConfig;

public interface AdaptiveBackOffLock {

void lock();

void unlock();

default void update(MessageStoreConfig messageStoreConfig) {
}

default void swap() {
}

default void isOpen(boolean open, boolean isUseReentrantLock) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* 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.PutMessageReentrantLock;
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.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

public class AdaptiveBackOffLockImpl implements AdaptiveBackOffLock {
private AdaptiveBackOffLock adaptiveLock;

//state
private AtomicBoolean state = new AtomicBoolean(true);

private Map<String, AdaptiveBackOffLock> locks;

private final List<AtomicInteger> tpsTable;

private int tpsSwapCriticalPoint;

private AtomicInteger currentThreadNum = new AtomicInteger(0);

private AtomicBoolean isOpen = new AtomicBoolean(true);

public AdaptiveBackOffLockImpl() {
this.locks = new HashMap<>();
this.locks.put("Reentrant", new PutMessageReentrantLock());
this.locks.put("Collision", new CollisionRetreatLock());

this.tpsTable = new ArrayList<>(2);
this.tpsTable.add(new AtomicInteger(0));
this.tpsTable.add(new AtomicInteger(0));

adaptiveLock = this.locks.get("Collision");
}

@Override
public void lock() {
tpsTable.get(LocalTime.now().getSecond() % 2).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 = LocalTime.now().getSecond() % 2 - 1 >= 0 ? 0 : 1;
int tps = this.tpsTable.get(slot).get() + 1;
this.tpsTable.get(slot).set(-1);
if (tps == 0) {
return;
}

if (this.adaptiveLock instanceof CollisionRetreatLock) {
CollisionRetreatLock lock = (CollisionRetreatLock) this.adaptiveLock;
int base = Math.min(200 + tps / 200, 500);
if (lock.getNumberOfRetreat(slot) * base >= tps) {
if (lock.isAdapt()) {
lock.adapt(true);
} else {
this.tpsSwapCriticalPoint = tps;
needSwap = true;
}
} else if (lock.getNumberOfRetreat(slot) * base * 3 / 2 <= tps) {
lock.adapt(false);
}
lock.setNumberOfRetreat(slot, 0);
} else {
if (tps <= this.tpsSwapCriticalPoint * 4 / 5) {
needSwap = true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Too many magic numbers, it should be optimized.

}

if (needSwap) {
if (this.state.compareAndSet(true, false)) {
int currentThreadNum;
do {
currentThreadNum = this.currentThreadNum.get();
} while (currentThreadNum != 0);

try {
if (this.adaptiveLock instanceof CollisionRetreatLock) {
this.adaptiveLock = this.locks.get("Reentrant");
} else {
this.adaptiveLock = this.locks.get("Collision");
((CollisionRetreatLock) this.adaptiveLock).adapt(false);
}
} catch (Exception e) {
//ignore
} finally {
this.state.compareAndSet(false, true);
}
}
}
}

@Override
public void isOpen(boolean open, boolean isUseReentrantLock) {
if (open != isOpen.get()) {
isOpen.set(open);
boolean success = false;
do {
success = this.state.compareAndSet(true, false);
} while (!success);

int currentThreadNum;
do {
currentThreadNum = this.currentThreadNum.get();
} while (currentThreadNum != 0);

if (open) {
adaptiveLock = this.locks.get("Collision");
} else {
adaptiveLock = !isUseReentrantLock ? this.locks.get("Collision") : this.locks.get("Reentrant");
}
state.set(true);
}
}

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

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

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

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

public AdaptiveBackOffLock getAdaptiveLock() {
return adaptiveLock;
}

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

public void setTpsSwapCriticalPoint(int tpsSwapCriticalPoint) {
this.tpsSwapCriticalPoint = tpsSwapCriticalPoint;
}

public int getTpsSwapCriticalPoint() {
return tpsSwapCriticalPoint;
}

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

public void setOpen(boolean open) {
this.isOpen.set(open);
}
}
Loading
Loading