use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class JRaftServer method applyOperation.
public void applyOperation(Node node, Message data, FailoverClosure closure) {
final Task task = new Task();
task.setDone(new NacosClosure(data, status -> {
NacosClosure.NacosStatus nacosStatus = (NacosClosure.NacosStatus) status;
closure.setThrowable(nacosStatus.getThrowable());
closure.setResponse(nacosStatus.getResponse());
closure.run(nacosStatus);
}));
// add request type field at the head of task data.
byte[] requestTypeFieldBytes = new byte[2];
requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
if (data instanceof ReadRequest) {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
} else {
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
}
byte[] dataBytes = data.toByteArray();
task.setData((ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes).put(dataBytes).position(0));
node.apply(task);
}
use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class NacosStateMachine method onApply.
@Override
public void onApply(Iterator iter) {
int index = 0;
int applied = 0;
Message message;
NacosClosure closure = null;
try {
while (iter.hasNext()) {
Status status = Status.OK();
try {
if (iter.done() != null) {
closure = (NacosClosure) iter.done();
message = closure.getMessage();
} else {
final ByteBuffer data = iter.getData();
message = ProtoMessageUtil.parse(data.array());
if (message instanceof ReadRequest) {
// 'iter.done() == null' means current node is follower, ignore read operation
applied++;
index++;
iter.next();
continue;
}
}
LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "receive log : {}", message);
if (message instanceof WriteRequest) {
Response response = processor.onApply((WriteRequest) message);
postProcessor(response, closure);
}
if (message instanceof ReadRequest) {
Response response = processor.onRequest((ReadRequest) message);
postProcessor(response, closure);
}
} catch (Throwable e) {
index++;
status.setError(RaftError.UNKNOWN, e.toString());
Optional.ofNullable(closure).ifPresent(closure1 -> closure1.setThrowable(e));
throw e;
} finally {
Optional.ofNullable(closure).ifPresent(closure1 -> closure1.run(status));
}
applied++;
index++;
iter.next();
}
} catch (Throwable t) {
Loggers.RAFT.error("processor : {}, stateMachine meet critical error: {}.", processor, t);
iter.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE, "StateMachine meet critical error: %s.", ExceptionUtil.getStackTrace(t)));
}
}
use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class ProtoMessageUtilTest method testConvertToReadRequest.
@Test
public void testConvertToReadRequest() {
ByteString data = ByteString.copyFrom("data".getBytes());
String group = "test";
GetRequest getRequest = GetRequest.newBuilder().setGroup(group).setData(data).putExtendInfo("k", "v").build();
ReadRequest readRequest = ProtoMessageUtil.convertToReadRequest(getRequest);
assertEquals(group, readRequest.getGroup());
assertEquals(data, readRequest.getData());
assertEquals(1, readRequest.getExtendInfoCount());
}
use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class ProtoMessageUtilTest method testParseReadRequest.
@Test
public void testParseReadRequest() {
String group = "test";
ByteString data = ByteString.copyFrom("data".getBytes());
ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build();
Object actual = ProtoMessageUtil.parse(testCase.toByteArray());
assertEquals(ReadRequest.class, testCase.getClass());
assertEquals(group, ((ReadRequest) actual).getGroup());
assertEquals(data, ((ReadRequest) actual).getData());
}
use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class StandalonePersistentServiceProcessor method get.
@Override
public Datum get(String key) throws NacosException {
final List<byte[]> keys = Collections.singletonList(ByteUtils.toBytes(key));
final ReadRequest req = ReadRequest.newBuilder().setGroup(Constants.NAMING_PERSISTENT_SERVICE_GROUP).setData(ByteString.copyFrom(serializer.serialize(keys))).build();
try {
final Response resp = onRequest(req);
if (resp.getSuccess()) {
BatchReadResponse response = serializer.deserialize(resp.getData().toByteArray(), BatchReadResponse.class);
final List<byte[]> rValues = response.getValues();
return rValues.isEmpty() ? null : serializer.deserialize(rValues.get(0), getDatumTypeFromKey(key));
}
throw new NacosException(ErrorCode.ProtoReadError.getCode(), resp.getErrMsg());
} catch (Throwable e) {
throw new NacosException(ErrorCode.ProtoReadError.getCode(), e.getMessage());
}
}
Aggregations