This commit is contained in:
eson 2022-06-02 01:55:54 +08:00
parent 02b4722799
commit 911a7883ce
4 changed files with 30 additions and 18 deletions

View File

@ -54,11 +54,16 @@ public class Server {
@Autowired @Autowired
public static Node node; public static Node node;
public static RaftClosure done;
public static Node GetNode() { public static Node GetNode() {
return node; return node;
} }
public static RaftClosure GetDone() {
return done;
}
public static void main(String[] args) { public static void main(String[] args) {
@ -92,12 +97,13 @@ public class Server {
RaftGroupService cluster = new RaftGroupService(groupId, serverId, nodeOptions); RaftGroupService cluster = new RaftGroupService(groupId, serverId, nodeOptions);
node = cluster.start(); node = cluster.start();
done = new RaftClosure();
System.setProperty("server.port", sprPort); System.setProperty("server.port", sprPort);
SpringApplication.run(Server.class, args); SpringApplication.run(Server.class, args);
Closure done = new RaftClosure();
node.shutdown(done); // node.shutdown(done);
} }
} }

View File

@ -20,34 +20,38 @@ import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import com.alipay.sofa.jraft.Node;
@Controller @Controller
public class TaskLog { public class TaskLog {
private static final Logger LOG = LoggerFactory.getLogger(TaskLog.class); private static final Logger LOG = LoggerFactory.getLogger(TaskLog.class);
private static Node node = Server.GetNode();
@GetMapping(path = "/test") @GetMapping(path = "/test")
public ResponseEntity<JsonObject> greeting() { public ResponseEntity<String> greeting() {
Task task = new Task(); Task task = new Task();
Closure done = new RaftClosure();
LOG.error(node.toString());
RaftClosure done = new RaftClosure();
task.setData(ByteBuffer.wrap("hello".getBytes())); task.setData(ByteBuffer.wrap("hello".getBytes()));
task.setDone( done); task.setDone(done);
LOG.info( done.toString()); Server.GetNode().apply(task);
LOG.info(Server.GetNode().toString());
JsonObject response = new JsonObject(); JsonObject response = new JsonObject();
if (Server.GetNode() == null) {
return new ResponseEntity<JsonObject>(response, HttpStatus.OK);
}
Server.GetNode()
.apply(task);
response.addProperty("status", "success"); response.addProperty("status", "success");
response.addProperty("apply", "hello"); response.addProperty("apply", "hello");
return new ResponseEntity<JsonObject>(response, HttpStatus.OK); return new ResponseEntity<String>(response.toString(), HttpStatus.OK);
} }
} }

View File

@ -16,6 +16,7 @@ public class RaftClosure implements Closure {
LOG.info("Task completed with status"+status.getCode()); LOG.info("Task completed with status"+status.getCode());
LOG.info("Task completed with "+status.getErrorMsg()); LOG.info("Task completed with "+status.getErrorMsg());
LOG.info("Task completed with "+status.getRaftError()); LOG.info("Task completed with "+status.getRaftError());
} }
// @Override // @Override

View File

@ -57,10 +57,11 @@ public class StateMachine extends StateMachineAdapter {
// This task is applied by this node, get value from closure to avoid additional // This task is applied by this node, get value from closure to avoid additional
// parsing. // parsing.
LOG.debug("done:%s",iter.getData().toString()); LOG.error("done:%1$s",iter.getData().toString());
} else { } else {
// Have to parse FetchAddRequest from this user log. // Have to parse FetchAddRequest from this user log.
LOG.debug("null:%s",iter.getData().toString()); LOG.error("null:%1$s",iter.getData().toString());
} }
iter.next(); iter.next();