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