TODO: 更新状态的 重新设计 避免自锁

This commit is contained in:
huangsimin 2022-07-21 16:32:48 +08:00
parent 08fbedeccc
commit 761a8dd664
4 changed files with 54 additions and 37 deletions

View File

@ -45,6 +45,7 @@ public class PacketsProcessor implements RpcProcessor<PacketsProcessor.PacketsRe
log.info("{}",request.packets.size()); log.info("{}",request.packets.size());
var resp = new ResponseSM(); var resp = new ResponseSM();
resp.setMsg(rpcCtx.getRemoteAddress()); resp.setMsg(rpcCtx.getRemoteAddress());
rpcCtx.sendResponse(resp); rpcCtx.sendResponse(resp);
} }

View File

@ -126,6 +126,7 @@ public class StateServerFactory {
int port = serverId.getPort(); int port = serverId.getPort();
NodeOptions nodeOptions = new NodeOptions(); NodeOptions nodeOptions = new NodeOptions();
nodeOptions.setElectionTimeoutMs(1000); nodeOptions.setElectionTimeoutMs(1000);
nodeOptions.setSnapshotLogIndexMargin(3600); nodeOptions.setSnapshotLogIndexMargin(3600);
nodeOptions.setInitialConf(conf); nodeOptions.setInitialConf(conf);
@ -142,6 +143,7 @@ public class StateServerFactory {
cluster = new RaftGroupService(groupId, serverId, nodeOptions); cluster = new RaftGroupService(groupId, serverId, nodeOptions);
Set<Class<?>> scans = new Reflections("com.yuandian.dataflow").getTypesAnnotatedWith(ProcessorRaft.class); Set<Class<?>> scans = new Reflections("com.yuandian.dataflow").getTypesAnnotatedWith(ProcessorRaft.class);
scans.forEach((pRaftClass)->{ scans.forEach((pRaftClass)->{
try { try {

View File

@ -6,13 +6,16 @@
*/ */
package com.yuandian.dataflow.statemachine.state; package com.yuandian.dataflow.statemachine.state;
import java.time.Instant;
import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.handler.codec.http.HttpContentEncoder.Result; import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.handler.codec.http.HttpContentEncoder.Result;
import com.alipay.sofa.jraft.Status;
import com.alipay.sofa.jraft.error.RemotingException; import com.alipay.sofa.jraft.error.RemotingException;
import com.google.protobuf.Any; import com.google.protobuf.Any;
import com.yuandian.dataflow.controller.PacketsProcessor.PacketsRequest; import com.yuandian.dataflow.controller.PacketsProcessor.PacketsRequest;
import com.yuandian.dataflow.proto.msgtype.BacktrackingFlowOuterClass; import com.yuandian.dataflow.proto.msgtype.BacktrackingFlowOuterClass;
import com.yuandian.dataflow.statemachine.StateServerFactory; import com.yuandian.dataflow.statemachine.StateServerFactory;
import com.yuandian.dataflow.statemachine.SyncClosure;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -43,44 +46,55 @@ public class StateFactory {
if (alivePeers != null) { if (alivePeers != null) {
var ss = StateServerFactory.getStateServer(); var ss = StateServerFactory.getStateServer();
StateServerFactory.getStateServer().useFsmStateAsync((state) -> { StateServerFactory.getStateServer().useFsmStateAsync((state) -> {
synchronized(alivePeers){ synchronized (alivePeers) {
alivePeers.forEach((peer) -> { alivePeers.forEach((peer) -> {
var ws = state.getWorkers().get(peer); WorkerState ws = state.getWorkers().get(peer);
if (ws != null) { if (ws != null) {
var cap = 100 - ws.getTaskQueueSize(); var cap = 100 - ws.getTaskQueueSize();
if (cap > 0) { if (cap > 0) {
log.debug("{}", cap); log.debug("{}", cap);
var request = new PacketsRequest(); var request = new PacketsRequest();
for(int i = 0; i < cap ; i++ ) { for (int i = 0; i < cap; i++) {
var p = Any.pack( var p = Any.pack(
BacktrackingFlowOuterClass.BacktrackingFlow.newBuilder() BacktrackingFlowOuterClass.BacktrackingFlow.newBuilder()
.setTableId(10086) .setTableId(10086)
.build() .build());
);
request.getPackets().add(p); request.getPackets().add(p);
} }
try { try {
var result = ss.getRpcClient().invokeSync(ws.getPeerId().getEndpoint(), request, 5000); var result = ss.getRpcClient().invokeSync(ws.getPeerId().getEndpoint(),
log.info("{}", result); request, 5000);
} catch (InterruptedException e) { log.info("{}", result);
// TODO Auto-generated catch block ws.setUpdateAt( Instant.now() );
e.printStackTrace(); ws.setTaskQueueSize(ws.getTaskQueueSize() - cap);
} catch (RemotingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemotingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} }
}
alivePeers.notifyAll(); ss.applyState(state, new SyncClosure<State>() {
}); public void run(Status status) {
} log.debug("{}", status);
};
} );
alivePeers.notifyAll();
});
}
return null; return null;
}); });
synchronized(alivePeers){ synchronized (alivePeers) {
alivePeers.wait(5000); alivePeers.wait(5000);
} }

View File

@ -20,11 +20,11 @@
</appender> </appender>
<!-- <root level="info"> <!-- <root level="debug">
<appender-ref ref="CONSOLE" /> <appender-ref ref="CONSOLE" />
</root> --> </root> -->
<logger name="com.yuandian.dataflow" level="debug"> <logger name="com.yuandian.dataflow" level="debug|info">
<appender-ref ref="CONSOLE" /> <appender-ref ref="CONSOLE"/>
</logger> </logger>
</configuration> </configuration>