use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class JRaftProtocolTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
raftProtocol = new JRaftProtocol(memberManager);
ReadRequest.Builder readRequestBuilder = ReadRequest.newBuilder();
readRequest = readRequestBuilder.build();
WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder();
writeRequest = writeRequestBuilder.build();
Field raftServerField = JRaftProtocol.class.getDeclaredField("raftServer");
raftServerField.setAccessible(true);
raftServerField.set(raftProtocol, serverMock);
Field jRaftMaintainServiceField = JRaftProtocol.class.getDeclaredField("jRaftMaintainService");
jRaftMaintainServiceField.setAccessible(true);
jRaftMaintainServiceField.set(raftProtocol, jRaftMaintainService);
when(serverMock.get(readRequest)).thenReturn(futureMock);
when(serverMock.commit(any(String.class), any(Message.class), any(CompletableFuture.class))).thenReturn(futureMock);
groupId = "test_group";
when(serverMock.findNodeByGroup(groupId)).thenReturn(nodeMock);
}
use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class JRaftServerTest method before.
@Before
public void before() throws NoSuchFieldException, IllegalAccessException {
initPeersAndConfiguration();
RaftConfig config = new RaftConfig();
Collection<Member> initEvent = Collections.singletonList(Member.builder().ip("1.1.1.1").port(7848).build());
config.setMembers("1.1.1.1:7848", ProtocolManager.toCPMembersInfo(initEvent));
server = new JRaftServer() {
@Override
boolean peerChange(JRaftMaintainService maintainService, Set<String> newPeers) {
return super.peerChange(maintainService, newPeers);
}
};
server.init(config);
Map<String, JRaftServer.RaftGroupTuple> map = new HashMap<>();
map.put("test_nacos", new JRaftServer.RaftGroupTuple(node, requestProcessor, raftGroupService, nacosStateMachine));
server.mockMultiRaftGroup(map);
mockcliClientService();
mockcliService();
setLeaderAs(peerId1);
// Inject the mocked cliClientServiceMock into server.
Field cliClientServiceField = JRaftServer.class.getDeclaredField("cliClientService");
cliClientServiceField.setAccessible(true);
cliClientServiceField.set(server, cliClientServiceMock);
// Inject the mocked cliServiceMock into server.
Field cliServiceField = JRaftServer.class.getDeclaredField("cliService");
cliServiceField.setAccessible(true);
cliServiceField.set(server, cliServiceMock);
// currently useless
ReadRequest.Builder readRequestBuilder = ReadRequest.newBuilder();
readRequest = readRequestBuilder.build();
when(mockProcessor4CP.loadSnapshotOperate()).thenReturn(Collections.emptyList());
when(mockProcessor4CP.group()).thenReturn(groupId);
when(future.completeExceptionally(any(IllegalArgumentException.class))).thenReturn(true);
Field isStartedField = JRaftServer.class.getDeclaredField("isStarted");
isStartedField.setAccessible(true);
isStartedField.set(server, true);
}
use of com.alibaba.nacos.consistency.entity.ReadRequest 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());
}
}
use of com.alibaba.nacos.consistency.entity.ReadRequest in project nacos by alibaba.
the class ProtoMessageUtilTest method testParseReadRequestWithRequestTypeField.
@Test
public void testParseReadRequestWithRequestTypeField() {
String group = "test";
ByteString data = ByteString.copyFrom("data".getBytes());
ReadRequest testCase = ReadRequest.newBuilder().setGroup(group).setData(data).build();
byte[] requestTypeFieldBytes = new byte[2];
requestTypeFieldBytes[0] = ProtoMessageUtil.REQUEST_TYPE_FIELD_TAG;
requestTypeFieldBytes[1] = ProtoMessageUtil.REQUEST_TYPE_READ;
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(ReadRequest.class, testCase.getClass());
assertEquals(group, ((ReadRequest) actual).getGroup());
assertEquals(data, ((ReadRequest) actual).getData());
}
Aggregations