Search in sources :

Example 6 with ReaderBuilder

use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.

the class FunctionRuntimeManagerTest method testProcessAssignmentUpdateAddFunctions.

@Test
public void testProcessAssignmentUpdateAddFunctions() throws Exception {
    WorkerConfig workerConfig = new WorkerConfig();
    workerConfig.setWorkerId("worker-1");
    workerConfig.setThreadContainerFactory(new WorkerConfig.ThreadContainerFactory().setThreadGroupName("test"));
    workerConfig.setPulsarServiceUrl("pulsar://localhost:6650");
    workerConfig.setStateStorageServiceUrl("foo");
    workerConfig.setFunctionAssignmentTopicName("assignments");
    PulsarClient pulsarClient = mock(PulsarClient.class);
    ReaderBuilder readerBuilder = mock(ReaderBuilder.class);
    doReturn(readerBuilder).when(pulsarClient).newReader();
    doReturn(readerBuilder).when(readerBuilder).topic(anyString());
    doReturn(readerBuilder).when(readerBuilder).startMessageId(any());
    doReturn(mock(Reader.class)).when(readerBuilder).create();
    // test new assignment add functions
    FunctionRuntimeManager functionRuntimeManager = spy(new FunctionRuntimeManager(workerConfig, pulsarClient, mock(Namespace.class), mock(MembershipManager.class)));
    Function.FunctionMetaData function1 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setTenant("test-tenant").setNamespace("test-namespace").setName("func-1")).build();
    Function.FunctionMetaData function2 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setTenant("test-tenant").setNamespace("test-namespace").setName("func-2")).build();
    Function.Assignment assignment1 = Function.Assignment.newBuilder().setWorkerId("worker-1").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()).build();
    Function.Assignment assignment2 = Function.Assignment.newBuilder().setWorkerId("worker-2").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build()).build();
    List<Function.Assignment> assignments = new LinkedList<>();
    assignments.add(assignment1);
    assignments.add(assignment2);
    Request.AssignmentsUpdate assignmentsUpdate = Request.AssignmentsUpdate.newBuilder().addAllAssignments(assignments).setVersion(1).build();
    functionRuntimeManager.processAssignmentUpdate(MessageId.earliest, assignmentsUpdate);
    verify(functionRuntimeManager, times(2)).setAssignment(any(Function.Assignment.class));
    verify(functionRuntimeManager, times(0)).deleteAssignment(any(Function.Assignment.class));
    Assert.assertEquals(functionRuntimeManager.workerIdToAssignments.size(), 2);
    Assert.assertEquals(functionRuntimeManager.workerIdToAssignments.get("worker-1").get("test-tenant/test-namespace/func-1:0"), assignment1);
    Assert.assertEquals(functionRuntimeManager.workerIdToAssignments.get("worker-2").get("test-tenant/test-namespace/func-2:0"), assignment2);
    verify(functionRuntimeManager, times(1)).insertStartAction(any(FunctionRuntimeInfo.class));
    verify(functionRuntimeManager).insertStartAction(argThat(new ArgumentMatcher<FunctionRuntimeInfo>() {

        @Override
        public boolean matches(Object o) {
            if (o instanceof FunctionRuntimeInfo) {
                FunctionRuntimeInfo functionRuntimeInfo = (FunctionRuntimeInfo) o;
                if (!functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().equals(function1)) {
                    return false;
                }
                return true;
            }
            return false;
        }
    }));
    verify(functionRuntimeManager, times(0)).insertStopAction(any(FunctionRuntimeInfo.class));
    Assert.assertEquals(functionRuntimeManager.actionQueue.size(), 1);
    Assert.assertTrue(functionRuntimeManager.actionQueue.contains(new FunctionAction().setAction(FunctionAction.Action.START).setFunctionRuntimeInfo(new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()))));
    Assert.assertEquals(functionRuntimeManager.functionRuntimeInfoMap.size(), 1);
    Assert.assertEquals(functionRuntimeManager.functionRuntimeInfoMap.get("test-tenant/test-namespace/func-1:0"), new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()));
}
Also used : Request(org.apache.pulsar.functions.proto.Request) Reader(org.apache.pulsar.client.api.Reader) LinkedList(java.util.LinkedList) ReaderBuilder(org.apache.pulsar.client.api.ReaderBuilder) Function(org.apache.pulsar.functions.proto.Function) ArgumentMatcher(org.mockito.ArgumentMatcher) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 7 with ReaderBuilder

use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.

the class MembershipManagerTest method testCheckFailuresSomeFailures.

@Test
public void testCheckFailuresSomeFailures() throws Exception {
    workerConfig.setRescheduleTimeoutMs(30000);
    SchedulerManager schedulerManager = mock(SchedulerManager.class);
    PulsarClient pulsarClient = mock(PulsarClient.class);
    ReaderBuilder readerBuilder = mock(ReaderBuilder.class);
    doReturn(readerBuilder).when(pulsarClient).newReader();
    doReturn(readerBuilder).when(readerBuilder).topic(anyString());
    doReturn(readerBuilder).when(readerBuilder).startMessageId(any());
    doReturn(mock(Reader.class)).when(readerBuilder).create();
    FunctionRuntimeManager functionRuntimeManager = spy(new FunctionRuntimeManager(workerConfig, pulsarClient, mock(Namespace.class), mock(MembershipManager.class)));
    FunctionMetaDataManager functionMetaDataManager = mock(FunctionMetaDataManager.class);
    MembershipManager membershipManager = spy(new MembershipManager(workerConfig, mock(PulsarClient.class)));
    List<MembershipManager.WorkerInfo> workerInfoList = new LinkedList<>();
    workerInfoList.add(MembershipManager.WorkerInfo.of("worker-1", "host-1", 8000));
    Mockito.doReturn(workerInfoList).when(membershipManager).getCurrentMembership();
    Function.FunctionMetaData function1 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setTenant("test-tenant").setNamespace("test-namespace").setName("func-1")).build();
    Function.FunctionMetaData function2 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setTenant("test-tenant").setNamespace("test-namespace").setName("func-2")).build();
    List<Function.FunctionMetaData> metaDataList = new LinkedList<>();
    metaDataList.add(function1);
    metaDataList.add(function2);
    Mockito.doReturn(metaDataList).when(functionMetaDataManager).getAllFunctionMetaData();
    Function.Assignment assignment1 = Function.Assignment.newBuilder().setWorkerId("worker-1").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()).build();
    Function.Assignment assignment2 = Function.Assignment.newBuilder().setWorkerId("worker-2").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build()).build();
    // add existing assignments
    functionRuntimeManager.setAssignment(assignment1);
    functionRuntimeManager.setAssignment(assignment2);
    membershipManager.checkFailures(functionMetaDataManager, functionRuntimeManager, schedulerManager);
    verify(schedulerManager, times(0)).schedule();
    verify(functionRuntimeManager, times(0)).removeAssignments(any());
    Assert.assertEquals(membershipManager.unsignedFunctionDurations.size(), 1);
    Function.Instance instance = Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build();
    Assert.assertTrue(membershipManager.unsignedFunctionDurations.get(instance) != null);
    membershipManager.unsignedFunctionDurations.put(instance, membershipManager.unsignedFunctionDurations.get(instance) - 30001);
    membershipManager.checkFailures(functionMetaDataManager, functionRuntimeManager, schedulerManager);
    verify(functionRuntimeManager, times(1)).removeAssignments(argThat(new ArgumentMatcher<Collection<Function.Assignment>>() {

        @Override
        public boolean matches(Object o) {
            if (o instanceof Collection) {
                Collection<Function.Assignment> assignments = (Collection) o;
                if (!assignments.contains(assignment2)) {
                    return false;
                }
                return true;
            }
            return false;
        }
    }));
    verify(schedulerManager, times(1)).schedule();
}
Also used : Reader(org.apache.pulsar.client.api.Reader) LinkedList(java.util.LinkedList) ReaderBuilder(org.apache.pulsar.client.api.ReaderBuilder) Function(org.apache.pulsar.functions.proto.Function) ArgumentMatcher(org.mockito.ArgumentMatcher) Collection(java.util.Collection) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

PulsarClient (org.apache.pulsar.client.api.PulsarClient)7 Reader (org.apache.pulsar.client.api.Reader)7 ReaderBuilder (org.apache.pulsar.client.api.ReaderBuilder)7 LinkedList (java.util.LinkedList)6 Function (org.apache.pulsar.functions.proto.Function)6 Test (org.testng.annotations.Test)6 ArgumentMatcher (org.mockito.ArgumentMatcher)4 Request (org.apache.pulsar.functions.proto.Request)3 JCommander (com.beust.jcommander.JCommander)1 Parameter (com.beust.jcommander.Parameter)1 ParameterException (com.beust.jcommander.ParameterException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 Lists (com.google.common.collect.Lists)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 FileInputStream (java.io.FileInputStream)1 DecimalFormat (java.text.DecimalFormat)1 Collection (java.util.Collection)1 List (java.util.List)1 Properties (java.util.Properties)1