Skip to content

Commit d7d2e9a

Browse files
committed
feat(multi-raft): update conflict code
1.update conflict code
1 parent 6460a7f commit d7d2e9a

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

src/main/java/io/openmessaging/storage/dledger/DLedgerRpcNettyService.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@
4141
import io.openmessaging.storage.dledger.protocol.VoteResponse;
4242
import io.openmessaging.storage.dledger.utils.DLedgerUtils;
4343

44-
import java.util.concurrent.*;
44+
import java.util.concurrent.CompletableFuture;
45+
import java.util.concurrent.ExecutorService;
46+
import java.util.concurrent.Executors;
47+
import java.util.concurrent.ThreadFactory;
4548
import java.util.concurrent.atomic.AtomicInteger;
4649

50+
import org.apache.rocketmq.remoting.ChannelEventListener;
4751
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
4852
import org.apache.rocketmq.remoting.netty.NettyRemotingClient;
4953
import org.apache.rocketmq.remoting.netty.NettyRemotingServer;
@@ -65,13 +69,9 @@ public class DLedgerRpcNettyService extends DLedgerRpcService {
6569
private NettyRemotingServer remotingServer;
6670
private NettyRemotingClient remotingClient;
6771

68-
// selfId -> remotingClient
69-
private ConcurrentHashMap<String, NettyRemotingClient> clientMap;
70-
7172
private DLedgerProxy dLedgerProxy;
7273
private MemberState memberState;
7374

74-
7575
private ExecutorService futureExecutor = Executors.newFixedThreadPool(4, new ThreadFactory() {
7676
private AtomicInteger threadIndex = new AtomicInteger(0);
7777

@@ -100,8 +100,15 @@ public Thread newThread(Runnable r) {
100100
});
101101

102102
public DLedgerRpcNettyService(DLedgerProxy dLedgerProxy) {
103+
this(dLedgerProxy, null, null, null);
104+
}
105+
106+
public DLedgerRpcNettyService(DLedgerProxy dLedgerProxy, NettyServerConfig nettyServerConfig, NettyClientConfig nettyClientConfig) {
107+
this(dLedgerProxy, nettyServerConfig, nettyClientConfig, null);
108+
}
109+
110+
public DLedgerRpcNettyService(DLedgerProxy dLedgerProxy, NettyServerConfig nettyServerConfig, NettyClientConfig nettyClientConfig, ChannelEventListener channelEventListener) {
103111
this.dLedgerProxy = dLedgerProxy;
104-
this.clientMap = new ConcurrentHashMap<>();
105112
DLedgerProxyConfig dLedgerProxyConfig = this.dLedgerProxy.getConfigManager().getdLedgerProxyConfig();
106113
NettyRequestProcessor protocolProcessor = new NettyRequestProcessor() {
107114
@Override
@@ -114,20 +121,25 @@ public boolean rejectRequest() {
114121
return false;
115122
}
116123
};
124+
117125
//register remoting server(We will only listen to one port. Limit in the configuration file)
118126
//check if the config has more than one port to bind
119127
if (!checkOnePort(dLedgerProxyConfig)) {
120128
logger.error("Bind the port error, because of more than one port in config");
121129
throw new RuntimeException("Bind the port error, because of more than one port in config");
122130
}
123131
DLedgerConfig dLedgerConfig = dLedgerProxyConfig.getConfigs().get(0);
124-
NettyRemotingServer nettyRemotingServer = registerRemotingServer(dLedgerConfig.getSelfAddress(), protocolProcessor);
125-
this.remotingServer = nettyRemotingServer;
132+
String address = dLedgerConfig.getSelfAddress();
133+
if (nettyServerConfig == null) {
134+
nettyServerConfig = new NettyServerConfig();
135+
}
136+
nettyServerConfig.setListenPort(Integer.valueOf(address.split(":")[1]));
137+
this.remotingServer = registerRemotingServer(nettyServerConfig, channelEventListener, protocolProcessor);
126138
//start the remoting client
127-
for (DLedgerConfig config : dLedgerProxyConfig.getConfigs()) {
128-
this.clientMap.put(config.getSelfId(), new NettyRemotingClient(new NettyClientConfig(), null));
139+
if (nettyClientConfig == null) {
140+
nettyClientConfig = new NettyClientConfig();
129141
}
130-
this.remotingClient = new NettyRemotingClient(new NettyClientConfig(), null);
142+
this.remotingClient = new NettyRemotingClient(nettyClientConfig, null);
131143
}
132144

133145
private boolean checkOnePort(DLedgerProxyConfig dLedgerProxyConfig) {
@@ -156,10 +168,8 @@ private void registerProcessor(NettyRemotingServer remotingServer, NettyRequestP
156168
remotingServer.registerProcessor(DLedgerRequestCode.LEADERSHIP_TRANSFER.getCode(), protocolProcessor, null);
157169
}
158170

159-
private NettyRemotingServer registerRemotingServer(String address, NettyRequestProcessor protocolProcessor) {
160-
NettyServerConfig nettyServerConfig = new NettyServerConfig();
161-
nettyServerConfig.setListenPort(Integer.valueOf(address.split(":")[1]));
162-
NettyRemotingServer remotingServer = new NettyRemotingServer(nettyServerConfig, null);
171+
private NettyRemotingServer registerRemotingServer(NettyServerConfig nettyServerConfig, ChannelEventListener channelEventListener, NettyRequestProcessor protocolProcessor) {
172+
NettyRemotingServer remotingServer = new NettyRemotingServer(nettyServerConfig, channelEventListener);
163173
registerProcessor(remotingServer, protocolProcessor);
164174
return remotingServer;
165175
}
@@ -514,4 +524,8 @@ public DLedgerProxy getdLedgerProxy() {
514524
public void setdLedgerProxy(DLedgerProxy dLedgerProxy) {
515525
this.dLedgerProxy = dLedgerProxy;
516526
}
527+
528+
public NettyRemotingServer getRemotingServer() {
529+
return remotingServer;
530+
}
517531
}

src/main/java/io/openmessaging/storage/dledger/dledger/DLedgerManager.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Iterator;
2727
import java.util.List;
2828
import java.util.Map;
29-
import java.util.HashMap;
3029
import java.util.concurrent.ConcurrentHashMap;
3130

3231
public class DLedgerManager {
@@ -53,7 +52,7 @@ public DLedgerServer getDLedgerServer(final String selfId) {
5352
return this.servers.get(selfId);
5453
}
5554

56-
public DLedgerServer getDLedgerServer(final String groupId, final String selfId){
55+
public DLedgerServer getDLedgerServer(final String groupId, final String selfId) {
5756
DLedgerServer dLedgerServer = this.servers.get(selfId);
5857
return dLedgerServer.getdLedgerConfig().getGroup().equals(groupId) ? dLedgerServer : null;
5958
}

src/main/java/io/openmessaging/storage/dledger/dledger/DLedgerProxy.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package io.openmessaging.storage.dledger.dledger;
1818

19-
import io.openmessaging.storage.dledger.*;
19+
import io.openmessaging.storage.dledger.AppendFuture;
20+
import io.openmessaging.storage.dledger.DLedgerConfig;
21+
import io.openmessaging.storage.dledger.DLedgerRpcNettyService;
22+
import io.openmessaging.storage.dledger.DLedgerRpcService;
23+
import io.openmessaging.storage.dledger.DLedgerServer;
2024
import io.openmessaging.storage.dledger.exception.DLedgerException;
2125
import io.openmessaging.storage.dledger.protocol.AppendEntryRequest;
2226
import io.openmessaging.storage.dledger.protocol.AppendEntryResponse;
@@ -37,6 +41,10 @@
3741
import io.openmessaging.storage.dledger.protocol.VoteRequest;
3842
import io.openmessaging.storage.dledger.protocol.VoteResponse;
3943
import io.openmessaging.storage.dledger.utils.PreConditions;
44+
import org.apache.rocketmq.remoting.ChannelEventListener;
45+
import org.apache.rocketmq.remoting.netty.NettyClientConfig;
46+
import org.apache.rocketmq.remoting.netty.NettyRemotingServer;
47+
import org.apache.rocketmq.remoting.netty.NettyServerConfig;
4048
import org.slf4j.Logger;
4149
import org.slf4j.LoggerFactory;
4250

@@ -55,10 +63,19 @@ public class DLedgerProxy implements DLedgerProtocolHandler {
5563

5664
private DLedgerRpcService dLedgerRpcService;
5765

58-
public DLedgerProxy(final DLedgerProxyConfig dLedgerProxyConfig) {
66+
public DLedgerProxy(DLedgerProxyConfig dLedgerProxyConfig) {
67+
this(dLedgerProxyConfig, null, null, null);
68+
}
69+
70+
public DLedgerProxy(DLedgerProxyConfig dLedgerProxyConfig, NettyServerConfig nettyServerConfig, NettyClientConfig nettyClientConfig) {
71+
this(dLedgerProxyConfig, nettyServerConfig, nettyClientConfig, null);
72+
}
73+
74+
75+
public DLedgerProxy(DLedgerProxyConfig dLedgerProxyConfig, NettyServerConfig nettyServerConfig, NettyClientConfig nettyClientConfig, ChannelEventListener channelEventListener) {
5976
try {
6077
this.configManager = new ConfigManager(dLedgerProxyConfig);
61-
this.dLedgerRpcService = new DLedgerRpcNettyService(this);
78+
this.dLedgerRpcService = new DLedgerRpcNettyService(this, nettyServerConfig, nettyClientConfig, channelEventListener);
6279
this.dLedgerManager = new DLedgerManager(dLedgerProxyConfig, this.dLedgerRpcService);
6380
} catch (Exception e) {
6481
logger.error("[Proxy][DLedgerProxy] fail to construct", e);
@@ -253,4 +270,11 @@ public void shutdown() {
253270
this.dLedgerRpcService.shutdown();
254271
}
255272

273+
public NettyRemotingServer getRemotingServer() {
274+
if (this.dLedgerRpcService instanceof DLedgerRpcNettyService) {
275+
return ((DLedgerRpcNettyService) this.dLedgerRpcService).getRemotingServer();
276+
}
277+
return null;
278+
}
279+
256280
}

0 commit comments

Comments
 (0)