Skip to content

Commit

Permalink
Fix switch manager dead lock until timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
KomachiSion committed Aug 15, 2024
1 parent f804248 commit 298fe23
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
Expand All @@ -68,7 +69,9 @@ public class SwitchManager extends RequestProcessor4CP {

private final ProtocolManager protocolManager;

private final ReentrantReadWriteLock lock;
private final ReentrantReadWriteLock raftLock;

private final ReentrantLock requestLock;

private final Serializer serializer;

Expand All @@ -79,9 +82,10 @@ public class SwitchManager extends RequestProcessor4CP {
public SwitchManager(SwitchDomain switchDomain, ProtocolManager protocolManager) {
this.switchDomain = switchDomain;
this.protocolManager = protocolManager;
this.lock = new ReentrantReadWriteLock();
this.raftLock = new ReentrantReadWriteLock();
this.requestLock = new ReentrantLock();
this.serializer = SerializeFactory.getSerializer("JSON");
this.snapshotOperation = new SwitchDomainSnapshotOperation(this.lock, this, this.serializer);
this.snapshotOperation = new SwitchDomainSnapshotOperation(this.raftLock, this, this.serializer);
this.dataFile = Paths.get(UtilsAndCommons.DATA_BASE_DIR, "data", KeyBuilder.getSwitchDomainKey()).toFile();
try {
DiskUtils.forceMkdir(this.dataFile.getParent());
Expand All @@ -101,7 +105,7 @@ public SwitchManager(SwitchDomain switchDomain, ProtocolManager protocolManager)
*/
public void update(String entry, String value, boolean debug) throws Exception {

this.lock.writeLock().lock();
this.requestLock.lock();
try {

SwitchDomain tempSwitchDomain = this.switchDomain.clone();
Expand Down Expand Up @@ -313,7 +317,7 @@ public void update(String entry, String value, boolean debug) throws Exception {
}

} finally {
this.lock.writeLock().unlock();
this.requestLock.unlock();
}

}
Expand Down Expand Up @@ -389,7 +393,7 @@ public List<SnapshotOperation> loadSnapshotOperate() {
* @param snapshotPath snapshot dir
*/
public void loadSnapshot(String snapshotPath) {
this.lock.writeLock().lock();
this.raftLock.writeLock().lock();
try {
File srcDir = Paths.get(snapshotPath).toFile();
// If snapshot path is non-exist, means snapshot is empty
Expand All @@ -413,7 +417,7 @@ public void loadSnapshot(String snapshotPath) {
} catch (IOException e) {
throw new NacosRuntimeException(ErrorCode.IOCopyDirError.getCode(), e);
} finally {
this.lock.writeLock().unlock();
this.raftLock.writeLock().unlock();
}
}

Expand All @@ -423,21 +427,21 @@ public void loadSnapshot(String snapshotPath) {
* @param backupPath snapshot dir
*/
public void dumpSnapshot(String backupPath) {
this.lock.writeLock().lock();
this.raftLock.writeLock().lock();
try {
File srcDir = Paths.get(this.dataFile.getParent()).toFile();
File descDir = Paths.get(backupPath).toFile();
DiskUtils.copyDirectory(srcDir, descDir);
} catch (IOException e) {
throw new NacosRuntimeException(ErrorCode.IOCopyDirError.getCode(), e);
} finally {
this.lock.writeLock().unlock();
this.raftLock.writeLock().unlock();
}
}

@Override
public Response onRequest(ReadRequest request) {
this.lock.readLock().lock();
this.raftLock.readLock().lock();
try {
final List<byte[]> keys = serializer.deserialize(request.getData().toByteArray(),
TypeUtils.parameterize(List.class, byte[].class));
Expand All @@ -453,13 +457,13 @@ public Response onRequest(ReadRequest request) {
Loggers.RAFT.warn("On read switch domain failed, ", e);
return Response.newBuilder().setSuccess(false).setErrMsg(e.getMessage()).build();
} finally {
this.lock.readLock().unlock();
this.raftLock.readLock().unlock();
}
}

@Override
public Response onApply(WriteRequest log) {
this.lock.writeLock().lock();
this.raftLock.writeLock().lock();
try {
BatchWriteRequest bwRequest = serializer.deserialize(log.getData().toByteArray(), BatchWriteRequest.class);
if (isNotSwitchDomainKey(bwRequest.getKeys())) {
Expand All @@ -479,7 +483,7 @@ public Response onApply(WriteRequest log) {
Loggers.RAFT.warn("On apply switch domain failed, ", e);
return Response.newBuilder().setSuccess(false).setErrMsg(e.getMessage()).build();
} finally {
this.lock.writeLock().unlock();
this.raftLock.writeLock().unlock();
}
}

Expand Down

0 comments on commit 298fe23

Please sign in to comment.