Search in sources :

Example 11 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, Object[] args, Class<R> cls) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryOne info : sql : {}, args : {}", sql, args);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_ONE_NO_MAPPER_WITH_ARGS).sql(sql).args(args).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)

Example 12 with Response

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

the class DistributedDatabaseOperateImpl method queryMany.

@Override
public List<Map<String, Object>> queryMany(String sql, Object[] args) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryMany info : sql : {}, args : {}", sql, args);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_MANY_WITH_LIST_WITH_ARGS).sql(sql).args(args).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(), List.class);
        }
        throw new NJdbcException(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)

Example 13 with Response

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

the class DistributedDatabaseOperateImpl method queryMany.

@Override
public <R> List<R> queryMany(String sql, Object[] args, Class<R> rClass) {
    try {
        LoggerUtils.printIfDebugEnabled(LogUtil.DEFAULT_LOG, "queryMany info : sql : {}, args : {}", sql, args);
        byte[] data = serializer.serialize(SelectRequest.builder().queryType(QueryType.QUERY_MANY_NO_MAPPER_WITH_ARGS).sql(sql).args(args).className(rClass.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(), List.class);
        }
        throw new NJdbcException(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)

Example 14 with Response

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

the class AbstractProcessorTest method testErrorThroughRpc.

@Test
public void testErrorThroughRpc() {
    final AtomicReference<Response> reference = new AtomicReference<>();
    RpcContext context = new RpcContext() {

        @Override
        public void sendResponse(Object responseObj) {
            reference.set((Response) responseObj);
        }

        @Override
        public Connection getConnection() {
            return null;
        }

        @Override
        public String getRemoteAddress() {
            return null;
        }
    };
    AbstractProcessor processor = new NacosLogProcessor(server, SerializeFactory.getDefault());
    processor.execute(server, context, WriteRequest.newBuilder().build(), new JRaftServer.RaftGroupTuple());
    Response response = reference.get();
    Assert.assertNotNull(response);
    Assert.assertEquals("Error message transmission", response.getErrMsg());
    Assert.assertFalse(response.getSuccess());
}
Also used : Response(com.alibaba.nacos.consistency.entity.Response) RpcContext(com.alipay.sofa.jraft.rpc.RpcContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) JRaftServer(com.alibaba.nacos.core.distributed.raft.JRaftServer) Test(org.junit.Test)

Example 15 with Response

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

the class PersistentServiceProcessor method get.

@Override
public Datum get(String key) throws NacosException {
    final List<byte[]> keys = new ArrayList<>(1);
    keys.add(ByteUtils.toBytes(key));
    final ReadRequest req = ReadRequest.newBuilder().setGroup(Constants.NAMING_PERSISTENT_SERVICE_GROUP).setData(ByteString.copyFrom(serializer.serialize(keys))).build();
    try {
        Response resp = protocol.getData(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) ArrayList(java.util.ArrayList) NacosException(com.alibaba.nacos.api.exception.NacosException) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

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