Search in sources :

Example 6 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest 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 7 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class ProtoMessageUtilTest method testParseWriteRequest.

@Test
public void testParseWriteRequest() {
    String group = "test";
    ByteString data = ByteString.copyFrom("data".getBytes());
    WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build();
    Object actual = ProtoMessageUtil.parse(testCase.toByteArray());
    assertEquals(WriteRequest.class, testCase.getClass());
    assertEquals(group, ((WriteRequest) actual).getGroup());
    assertEquals(data, ((WriteRequest) actual).getData());
}
Also used : ByteString(com.google.protobuf.ByteString) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 8 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class ProtoMessageUtilTest method testParseWriteRequestWithRequestTypeField.

@Test
public void testParseWriteRequestWithRequestTypeField() {
    String group = "test";
    ByteString data = ByteString.copyFrom("data".getBytes());
    WriteRequest testCase = WriteRequest.newBuilder().setGroup(group).setData(data).build();
    byte[] requestTypeFieldBytes = new byte[2];
    requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
    requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_WRITE;
    byte[] dataBytes = testCase.toByteArray();
    ByteBuffer byteBuffer = (ByteBuffer) ByteBuffer.allocate(requestTypeFieldBytes.length + dataBytes.length).put(requestTypeFieldBytes).put(dataBytes).position(0);
    Object actual = ProtoMessageUtil.parse(byteBuffer.array());
    assertEquals(WriteRequest.class, testCase.getClass());
    assertEquals(group, ((WriteRequest) actual).getGroup());
    assertEquals(data, ((WriteRequest) actual).getData());
}
Also used : ByteString(com.google.protobuf.ByteString) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 9 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class JRaftServerTest method testCommit.

@Test
public void testCommit() throws NoSuchFieldException, IllegalAccessException, TimeoutException, InterruptedException {
    WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder();
    WriteRequest writeRequest = writeRequestBuilder.build();
    // No group is set, and make sure that an IllegalArgumentException will be thrown.
    CompletableFuture<Response> future = server.commit(groupId, writeRequest, this.future);
    verify(future).completeExceptionally(any(IllegalArgumentException.class));
    // Set an group.
    Collection<RequestProcessor4CP> processors = Collections.singletonList(mockProcessor4CP);
    server.createMultiRaftGroup(processors);
    Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
    cliClientServiceField.setAccessible(true);
    cliClientServiceField.set(server, cliClientServiceMock);
    // Make the node leader and verify the invokeToLeader is never called.
    NodeImpl node = (NodeImpl) server.findNodeByGroup(groupId);
    Field stateField = NodeImpl.class.getDeclaredField("state");
    stateField.setAccessible(true);
    stateField.set(node, State.STATE_LEADER);
    server.commit(groupId, writeRequest, future);
    verify(cliClientServiceMock, never()).getRpcClient();
    // make the node follower and verify the invokeToLeader is called.
    node = (NodeImpl) server.findNodeByGroup(groupId);
    stateField.setAccessible(true);
    stateField.set(node, State.STATE_FOLLOWER);
    RouteTable.getInstance().updateLeader(groupId, peerId1);
    server.commit(groupId, writeRequest, future);
    verify(cliClientServiceMock).getRpcClient();
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) NodeImpl(com.alipay.sofa.jraft.core.NodeImpl) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) Test(org.junit.Test)

Example 10 with WriteRequest

use of com.alibaba.nacos.consistency.entity.WriteRequest in project nacos by alibaba.

the class ServiceMetadataProcessorTest method testOnApply.

@Test
public void testOnApply() throws NoSuchFieldException, IllegalAccessException {
    WriteRequest defaultInstance = WriteRequest.getDefaultInstance();
    Class<WriteRequest> writeRequestClass = WriteRequest.class;
    Field operation = writeRequestClass.getDeclaredField("operation_");
    operation.setAccessible(true);
    operation.set(defaultInstance, "ADD");
    MetadataOperation<ServiceMetadata> metadataOperation = new MetadataOperation<>();
    ServiceMetadata serviceMetadata = new ServiceMetadata();
    metadataOperation.setMetadata(serviceMetadata);
    metadataOperation.setServiceName("nacos");
    metadataOperation.setNamespace("namespace");
    metadataOperation.setGroup("group");
    Serializer aDefault = SerializeFactory.getDefault();
    ByteString bytes = ByteString.copyFrom(aDefault.serialize(metadataOperation));
    Field data = writeRequestClass.getDeclaredField("data_");
    data.setAccessible(true);
    data.set(defaultInstance, bytes);
    // ADD
    Response addResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Service service = Service.newService(metadataOperation.getNamespace(), metadataOperation.getGroup(), metadataOperation.getServiceName(), metadataOperation.getMetadata().isEphemeral());
    Service singleton = ServiceManager.getInstance().getSingleton(service);
    namingMetadataManager.updateServiceMetadata(singleton, metadataOperation.getMetadata());
    Assert.assertTrue(addResponse.getSuccess());
    verify(namingMetadataManager).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context).getBean(DoubleWriteEventListener.class);
    // CHANGE
    operation.set(defaultInstance, "CHANGE");
    Response changeResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(changeResponse.getSuccess());
    verify(namingMetadataManager, times(2)).getServiceMetadata(service);
    verify(namingMetadataManager).updateServiceMetadata(service, serviceMetadata);
    verify(context, times(2)).getBean(DoubleWriteEventListener.class);
    // DELETE
    operation.set(defaultInstance, "DELETE");
    Response deleteResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertTrue(deleteResponse.getSuccess());
    verify(namingMetadataManager).removeServiceMetadata(service);
    verify(serviceStorage).removeData(service);
    // VERIFY
    operation.set(defaultInstance, "VERIFY");
    Response otherResponse = serviceMetadataProcessor.onApply(defaultInstance);
    Assert.assertFalse(otherResponse.getSuccess());
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Field(java.lang.reflect.Field) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteString(com.google.protobuf.ByteString) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Serializer(com.alibaba.nacos.consistency.Serializer) Test(org.junit.Test)

Aggregations

WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)21 NacosException (com.alibaba.nacos.api.exception.NacosException)7 Test (org.junit.Test)7 Response (com.alibaba.nacos.consistency.entity.Response)5 ByteString (com.google.protobuf.ByteString)5 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)3 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)3 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)3 Field (java.lang.reflect.Field)3 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)2 ExceptionUtil (com.alibaba.nacos.common.utils.ExceptionUtil)2 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)2 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)2 Serializer (com.alibaba.nacos.consistency.Serializer)2 Log (com.alibaba.nacos.consistency.entity.Log)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)2 SnapshotOperation (com.alibaba.nacos.consistency.snapshot.SnapshotOperation)2 Datum (com.alibaba.nacos.naming.consistency.Datum)2 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)2 ByteBuffer (java.nio.ByteBuffer)2