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());
}
}
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());
}
}
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());
}
}
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());
}
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());
}
}
Aggregations