Search in sources :

Example 6 with ReadRequest

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);
}
Also used : Field(java.lang.reflect.Field) CompletableFuture(java.util.concurrent.CompletableFuture) Message(com.google.protobuf.Message) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) Before(org.junit.Before)

Example 7 with ReadRequest

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);
}
Also used : HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Member(com.alibaba.nacos.core.cluster.Member) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) Before(org.junit.Before)

Example 8 with ReadRequest

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

Example 9 with ReadRequest

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());
}
Also used : ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) ByteBuffer(java.nio.ByteBuffer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) Test(org.junit.Test)

Aggregations

ReadRequest (com.alibaba.nacos.consistency.entity.ReadRequest)9 Response (com.alibaba.nacos.consistency.entity.Response)4 ByteString (com.google.protobuf.ByteString)3 Message (com.google.protobuf.Message)3 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 NacosException (com.alibaba.nacos.api.exception.NacosException)2 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)2 ProtoMessageUtil (com.alibaba.nacos.consistency.ProtoMessageUtil)2 RequestProcessor (com.alibaba.nacos.consistency.RequestProcessor)2 RequestProcessor4CP (com.alibaba.nacos.consistency.cp.RequestProcessor4CP)2 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)2 JRaftUtils (com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils)2 Loggers (com.alibaba.nacos.core.utils.Loggers)2 Node (com.alipay.sofa.jraft.Node)2 RouteTable (com.alipay.sofa.jraft.RouteTable)2 Status (com.alipay.sofa.jraft.Status)2 Configuration (com.alipay.sofa.jraft.conf.Configuration)2