Search in sources :

Example 1 with ReadRequest

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);
}
Also used : LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) FailoverClosure(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosure) RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) Random(java.util.Random) ThreadUtils(com.alibaba.nacos.common.utils.ThreadUtils) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MetricsMonitor(com.alibaba.nacos.core.monitor.MetricsMonitor) Map(java.util.Map) Endpoint(com.alipay.sofa.jraft.util.Endpoint) JRaftUtils(com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils) FailoverClosureImpl(com.alibaba.nacos.core.distributed.raft.utils.FailoverClosureImpl) RestResult(com.alibaba.nacos.common.model.RestResult) SerializeFactory(com.alibaba.nacos.consistency.SerializeFactory) PeerId(com.alipay.sofa.jraft.entity.PeerId) EnvUtil(com.alibaba.nacos.sys.env.EnvUtil) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtoMessageUtil(com.alibaba.nacos.consistency.ProtoMessageUtil) RpcServer(com.alipay.sofa.jraft.rpc.RpcServer) Collection(java.util.Collection) InternetAddressUtil(com.alibaba.nacos.common.utils.InternetAddressUtil) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) Objects(java.util.Objects) List(java.util.List) JRaftException(com.alibaba.nacos.core.distributed.raft.exception.JRaftException) CliOptions(com.alipay.sofa.jraft.option.CliOptions) RaftGroupService(com.alipay.sofa.jraft.RaftGroupService) JRaftConstants(com.alibaba.nacos.core.distributed.raft.utils.JRaftConstants) StringUtils(com.alibaba.nacos.common.utils.StringUtils) CollectionUtils(org.springframework.util.CollectionUtils) RpcProcessor(com.alipay.sofa.jraft.rpc.RpcProcessor) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) Optional(java.util.Optional) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) JustForTest(com.alibaba.nacos.common.JustForTest) Loggers(com.alibaba.nacos.core.utils.Loggers) HashSet(java.util.HashSet) BiConsumer(java.util.function.BiConsumer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) RaftError(com.alipay.sofa.jraft.error.RaftError) ConvertUtils(com.alibaba.nacos.common.utils.ConvertUtils) DuplicateRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.DuplicateRaftGroupException) NoLeaderException(com.alibaba.nacos.core.distributed.raft.exception.NoLeaderException) NoSuchRaftGroupException(com.alibaba.nacos.core.distributed.raft.exception.NoSuchRaftGroupException) Serializer(com.alibaba.nacos.consistency.Serializer) Executor(java.util.concurrent.Executor) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor) RaftServiceFactory(com.alipay.sofa.jraft.RaftServiceFactory) Status(com.alipay.sofa.jraft.Status) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CliClientServiceImpl(com.alipay.sofa.jraft.rpc.impl.cli.CliClientServiceImpl) RaftOptionsBuilder(com.alibaba.nacos.core.distributed.raft.utils.RaftOptionsBuilder) Response(com.alibaba.nacos.consistency.entity.Response) TimeUnit(java.util.concurrent.TimeUnit) RaftExecutor(com.alibaba.nacos.core.distributed.raft.utils.RaftExecutor) CliServiceImpl(com.alipay.sofa.jraft.core.CliServiceImpl) Task(com.alipay.sofa.jraft.entity.Task) Node(com.alipay.sofa.jraft.Node) Paths(java.nio.file.Paths) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback) CliService(com.alipay.sofa.jraft.CliService) Message(com.google.protobuf.Message) Collections(java.util.Collections) BytesUtil(com.alipay.sofa.jraft.util.BytesUtil) Task(com.alipay.sofa.jraft.entity.Task) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Example 2 with ReadRequest

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)));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Response(com.alibaba.nacos.consistency.entity.Response) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) Arrays(java.util.Arrays) LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) StateMachineAdapter(com.alipay.sofa.jraft.core.StateMachineAdapter) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) LeaderChangeContext(com.alipay.sofa.jraft.entity.LeaderChangeContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Loggers(com.alibaba.nacos.core.utils.Loggers) ArrayList(java.util.ArrayList) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Closure(com.alipay.sofa.jraft.Closure) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) RaftError(com.alipay.sofa.jraft.error.RaftError) Iterator(com.alipay.sofa.jraft.Iterator) JRaftUtils(com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) Writer(com.alibaba.nacos.consistency.snapshot.Writer) ExceptionUtil(com.alibaba.nacos.common.utils.ExceptionUtil) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtoMessageUtil(com.alibaba.nacos.consistency.ProtoMessageUtil) Collection(java.util.Collection) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor) Status(com.alipay.sofa.jraft.Status) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Response(com.alibaba.nacos.consistency.entity.Response) Objects(java.util.Objects) List(java.util.List) Node(com.alipay.sofa.jraft.Node) Reader(com.alibaba.nacos.consistency.snapshot.Reader) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) Message(com.google.protobuf.Message) Optional(java.util.Optional) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Collections(java.util.Collections) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteBuffer(java.nio.ByteBuffer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Example 3 with ReadRequest

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());
}
Also used : ByteString(com.google.protobuf.ByteString) GetRequest(com.alibaba.nacos.consistency.entity.GetRequest) ByteString(com.google.protobuf.ByteString) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) Test(org.junit.Test)

Example 4 with ReadRequest

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());
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) Test(org.junit.Test)

Example 5 with ReadRequest

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());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NacosException(com.alibaba.nacos.api.exception.NacosException) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Aggregations

ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)9 Response (com.alibaba.nacos.consistency.entity.Response)4 ByteString (com.google.protobuf.ByteString)3 Message (com.google.protobuf.Message)3 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 NacosException (com.alibaba.nacos.api.exception.NacosException)2 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)2 ProtoMessageUtil (com.alibaba.nacos.consistency.ProtoMessageUtil)2 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)2 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)2 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)2 JRaftUtils (com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils)2 Loggers (com.alibaba.nacos.core.utils.Loggers)2 Node (com.alipay.sofa.jraft.Node)2 RouteTable (com.alipay.sofa.jraft.RouteTable)2 Status (com.alipay.sofa.jraft.Status)2 Configuration (com.alipay.sofa.jraft.conf.Configuration)2