Search in sources :

Example 6 with Response

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

Example 7 with Response

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

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

the class ServiceMetadataProcessorTest method testOnRequest.

@Test
public void testOnRequest() {
    Response response = serviceMetadataProcessor.onRequest(ReadRequest.getDefaultInstance());
    Assert.assertNull(response);
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) Test(org.junit.Test)

Example 9 with Response

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

Example 10 with Response

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

the class DistributedDatabaseOperateImpl method queryOne.

@Override
public <R> R queryOne(String sql, Class<R> cls) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}", sql);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_NO_MAPPER_NO_ARGS).sql(sql).className(cls.getCanonicalName()).build());
        final boolean blockRead = EmbeddedStorageContextUtils.containsExtendInfo(Constants.EXTEND_NEED_READ_UNTIL_HAVE_DATA);
        Response response = innerRead(ReadRequest.newBuilder().setGroup(group()).setData(ByteString.copyFrom(data)).build(), blockRead);
        if (response.getSuccess()) {
            return serializer.deserialize(response.getData().toByteArray(), cls);
        }
        throw new NJdbcException(response.getErrMsg(), response.getErrMsg());
    } catch (Exception e) {
        LogUtil.FATAL_LOG.error("An exception occurred during the query operation : {}", e.toString());
        throw new NacosRuntimeException(NacosException.SERVER_ERROR, e.toString());
    }
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) TimeoutException(java.util.concurrent.TimeoutException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) DataAccessException(org.springframework.dao.DataAccessException) NJdbcException(com.alibaba.nacos.config.server.exception.NJdbcException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) BadSqlGrammarException(org.springframework.jdbc.BadSqlGrammarException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)

Aggregations

Response (com.alibaba.nacos.consistency.entity.Response)19 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)10 NacosException (com.alibaba.nacos.api.exception.NacosException)9 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)7 NJdbcException (com.alibaba.nacos.config.server.exception.NJdbcException)7 TimeoutException (java.util.concurrent.TimeoutException)7 DataAccessException (org.springframework.dao.DataAccessException)7 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)7 BadSqlGrammarException (org.springframework.jdbc.BadSqlGrammarException)7 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)5 Test (org.junit.Test)5 ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)4 ArrayList (java.util.ArrayList)4 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)3 Status (com.alipay.sofa.jraft.Status)3 ByteString (com.google.protobuf.ByteString)3 CompletableFuture (java.util.concurrent.CompletableFuture)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