完成状态机的读写
This commit is contained in:
parent
886a5ffe1a
commit
dce906a7eb
|
@ -33,11 +33,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
@SpringBootApplication
|
||||
@SpringBootConfiguration
|
||||
public class Server {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
|
||||
|
|
|
@ -1,14 +1,12 @@
|
|||
package com.yuandian.dataflow.controller;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.alibaba.nacos.api.naming.pojo.Cluster;
|
||||
import com.alibaba.nacos.common.remote.client.RpcClientFactory;
|
||||
import com.alipay.sofa.jraft.Closure;
|
||||
import com.alipay.sofa.jraft.entity.Task;
|
||||
import com.alipay.sofa.jraft.rpc.RpcClient;
|
||||
import com.alipay.sofa.jraft.rpc.impl.BoltRpcClient;
|
||||
import com.yuandian.dataflow.Server;
|
||||
import com.alipay.sofa.jraft.Status;
|
||||
import com.yuandian.dataflow.projo.Response;
|
||||
import com.yuandian.dataflow.statemachine.StateServerFactory;
|
||||
import com.yuandian.dataflow.statemachine.SyncDataClosure;
|
||||
|
@ -17,23 +15,6 @@ import com.yuandian.dataflow.statemachine.rpc.TaskState;
|
|||
import lombok.var;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils.Null;
|
||||
import org.apache.commons.logging.Log;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.alipay.sofa.jraft.Node;
|
||||
import com.alipay.sofa.jraft.Status;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class TaskLog {
|
||||
|
@ -41,23 +22,35 @@ public class TaskLog {
|
|||
|
||||
|
||||
@GetMapping(path = "/test")
|
||||
public ResponseEntity<Response> Processing() {
|
||||
public ResponseEntity<Response> Processing() throws InterruptedException {
|
||||
|
||||
// var state = StateServerFactory.getStateServer().getFsm().getTaskState();
|
||||
var c = new SyncDataClosure() {
|
||||
|
||||
|
||||
SyncDataClosure closure = new SyncDataClosure() {
|
||||
@Override
|
||||
public void run(Status status) {
|
||||
log.info(getTaskState().toString());
|
||||
synchronized(lockObject) {
|
||||
lockObject.notify();
|
||||
}
|
||||
}
|
||||
};
|
||||
StateServerFactory.getStateServer().readIndexState(true, c );
|
||||
|
||||
|
||||
};
|
||||
var state = new TaskState();
|
||||
state.setTaskQueueSize(1);
|
||||
closure.setTaskState(state);
|
||||
StateServerFactory.getStateServer().readIndexState(true, closure);
|
||||
|
||||
synchronized(closure.lockObject) {
|
||||
closure.lockObject.wait();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Response response = new Response();
|
||||
final Response response = new Response();
|
||||
response.Code = HttpStatus.OK;
|
||||
response.Message = "OK";
|
||||
response.Message = closure.getTaskState().toString();
|
||||
return new ResponseEntity<Response>(response, HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
|
|
@ -140,6 +140,8 @@ public class StateServerFactory {
|
|||
public void readIndexState(final boolean readOnlySafe, final SyncDataClosure closure) {
|
||||
|
||||
if(!readOnlySafe){
|
||||
|
||||
closure.setTaskState(getTaskState());
|
||||
closure.success(getTaskState());
|
||||
closure.run(Status.OK());
|
||||
return;
|
||||
|
@ -149,15 +151,22 @@ public class StateServerFactory {
|
|||
@Override
|
||||
public void run(Status status, long index, byte[] reqCtx) {
|
||||
if(status.isOk()){
|
||||
closure.success(getTaskState());
|
||||
closure.run(Status.OK());
|
||||
|
||||
if(closure.getTaskState() != null){
|
||||
applyState(closure.getTaskState(), closure);
|
||||
} else {
|
||||
closure.setTaskState(getTaskState());
|
||||
closure.success(getTaskState());
|
||||
closure.run(Status.OK());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
readIndexExecutor.execute(() -> {
|
||||
if(isLeader()){
|
||||
log.debug("Fail to get value with 'ReadIndex': {}, try to applying to the state machine.", status);
|
||||
applyState( getTaskState(), closure);
|
||||
log.info("Fail to get value with 'ReadIndex': {}, try to applying to the state machine.", status);
|
||||
applyState(getTaskState(), closure);
|
||||
}else {
|
||||
handlerNotLeaderError(closure);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,13 @@ public abstract class SyncDataClosure implements Closure {
|
|||
// 代表任务状态
|
||||
private TaskState taskState;
|
||||
|
||||
public Object lockObject = new Object();
|
||||
|
||||
public SyncDataClosure() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void failure(final String errorMsg, final PeerId redirect) {
|
||||
final SMResponse response = new SMResponse();
|
||||
response.setSuccess(false);
|
||||
|
@ -41,6 +47,4 @@ public abstract class SyncDataClosure implements Closure {
|
|||
setResponse(response);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user