use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.
the class FunctionRuntimeManagerTest method testProcessAssignmentUpdateDeleteFunctions.
@Test
public void testProcessAssignmentUpdateDeleteFunctions() 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");
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 delete 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();
// add existing assignments
functionRuntimeManager.setAssignment(assignment1);
functionRuntimeManager.setAssignment(assignment2);
reset(functionRuntimeManager);
List<Function.Assignment> assignments = new LinkedList<>();
assignments.add(assignment2);
Request.AssignmentsUpdate assignmentsUpdate = Request.AssignmentsUpdate.newBuilder().addAllAssignments(assignments).setVersion(1).build();
functionRuntimeManager.functionRuntimeInfoMap.put("test-tenant/test-namespace/func-1:0", new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()));
functionRuntimeManager.processAssignmentUpdate(MessageId.earliest, assignmentsUpdate);
verify(functionRuntimeManager, times(0)).setAssignment(any(Function.Assignment.class));
verify(functionRuntimeManager, times(1)).deleteAssignment(any(Function.Assignment.class));
Assert.assertEquals(functionRuntimeManager.workerIdToAssignments.size(), 1);
Assert.assertEquals(functionRuntimeManager.workerIdToAssignments.get("worker-2").get("test-tenant/test-namespace/func-2:0"), assignment2);
verify(functionRuntimeManager, times(0)).insertStartAction(any(FunctionRuntimeInfo.class));
verify(functionRuntimeManager, times(1)).insertStopAction(any(FunctionRuntimeInfo.class));
verify(functionRuntimeManager).insertStopAction(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;
}
}));
Assert.assertEquals(functionRuntimeManager.actionQueue.size(), 1);
Assert.assertTrue(functionRuntimeManager.actionQueue.contains(new FunctionAction().setAction(FunctionAction.Action.STOP).setFunctionRuntimeInfo(new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()))));
Assert.assertEquals(functionRuntimeManager.functionRuntimeInfoMap.size(), 0);
}
use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.
the class MembershipManagerTest method testCheckFailuresSomeUnassigned.
@Test
public void testCheckFailuresSomeUnassigned() 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.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));
workerInfoList.add(MembershipManager.WorkerInfo.of("worker-2", "host-2", 8001));
Mockito.doReturn(workerInfoList).when(membershipManager).getCurrentMembership();
Function.FunctionMetaData function1 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setParallelism(1).setTenant("test-tenant").setNamespace("test-namespace").setName("func-1")).build();
Function.FunctionMetaData function2 = Function.FunctionMetaData.newBuilder().setFunctionConfig(Function.FunctionConfig.newBuilder().setParallelism(1).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();
// add existing assignments
functionRuntimeManager.setAssignment(assignment1);
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(schedulerManager, times(1)).schedule();
verify(functionRuntimeManager, times(0)).removeAssignments(any());
}
use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.
the class MembershipManagerTest method testCheckFailuresNoFailures.
@Test
public void testCheckFailuresNoFailures() throws Exception {
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));
workerInfoList.add(MembershipManager.WorkerInfo.of("worker-2", "host-2", 8001));
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(), 0);
}
use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.
the class PerformanceReader method main.
public static void main(String[] args) throws Exception {
final Arguments arguments = new Arguments();
JCommander jc = new JCommander(arguments);
jc.setProgramName("pulsar-perf-reader");
try {
jc.parse(args);
} catch (ParameterException e) {
System.out.println(e.getMessage());
jc.usage();
System.exit(-1);
}
if (arguments.help) {
jc.usage();
System.exit(-1);
}
if (arguments.topic.size() != 1) {
System.out.println("Only one topic name is allowed");
jc.usage();
System.exit(-1);
}
if (arguments.confFile != null) {
Properties prop = new Properties(System.getProperties());
prop.load(new FileInputStream(arguments.confFile));
if (arguments.serviceURL == null) {
arguments.serviceURL = prop.getProperty("brokerServiceUrl");
}
if (arguments.serviceURL == null) {
arguments.serviceURL = prop.getProperty("webServiceUrl");
}
// fallback to previous-version serviceUrl property to maintain backward-compatibility
if (arguments.serviceURL == null) {
arguments.serviceURL = prop.getProperty("serviceUrl", "http://localhost:8080/");
}
if (arguments.authPluginClassName == null) {
arguments.authPluginClassName = prop.getProperty("authPlugin", null);
}
if (arguments.authParams == null) {
arguments.authParams = prop.getProperty("authParams", null);
}
if (arguments.useTls == false) {
arguments.useTls = Boolean.parseBoolean(prop.getProperty("useTls"));
}
if (isBlank(arguments.tlsTrustCertsFilePath)) {
arguments.tlsTrustCertsFilePath = prop.getProperty("tlsTrustCertsFilePath", "");
}
}
// Dump config variables
ObjectMapper m = new ObjectMapper();
ObjectWriter w = m.writerWithDefaultPrettyPrinter();
log.info("Starting Pulsar performance reader with config: {}", w.writeValueAsString(arguments));
final TopicName prefixTopicName = TopicName.get(arguments.topic.get(0));
final RateLimiter limiter = arguments.rate > 0 ? RateLimiter.create(arguments.rate) : null;
ReaderListener<byte[]> listener = (reader, msg) -> {
messagesReceived.increment();
bytesReceived.add(msg.getData().length);
if (limiter != null) {
limiter.acquire();
}
};
ClientBuilder clientBuilder = //
PulsarClient.builder().serviceUrl(//
arguments.serviceURL).connectionsPerBroker(//
arguments.maxConnections).statsInterval(arguments.statsIntervalSeconds, //
TimeUnit.SECONDS).ioThreads(//
Runtime.getRuntime().availableProcessors()).enableTls(//
arguments.useTls).tlsTrustCertsFilePath(arguments.tlsTrustCertsFilePath);
if (isNotBlank(arguments.authPluginClassName)) {
clientBuilder.authentication(arguments.authPluginClassName, arguments.authParams);
}
PulsarClient pulsarClient = clientBuilder.build();
List<CompletableFuture<Reader<byte[]>>> futures = Lists.newArrayList();
MessageId startMessageId;
if ("earliest".equals(arguments.startMessageId)) {
startMessageId = MessageId.earliest;
} else if ("latest".equals(arguments.startMessageId)) {
startMessageId = MessageId.latest;
} else {
String[] parts = arguments.startMessageId.split(":");
startMessageId = new MessageIdImpl(Long.parseLong(parts[0]), Long.parseLong(parts[1]), -1);
}
ReaderBuilder<byte[]> readerBuilder = //
pulsarClient.newReader().readerListener(//
listener).receiverQueueSize(//
arguments.receiverQueueSize).startMessageId(startMessageId);
for (int i = 0; i < arguments.numTopics; i++) {
final TopicName topicName = (arguments.numTopics == 1) ? prefixTopicName : TopicName.get(String.format("%s-%d", prefixTopicName, i));
futures.add(readerBuilder.clone().topic(topicName.toString()).createAsync());
}
FutureUtil.waitForAll(futures).get();
log.info("Start reading from {} topics", arguments.numTopics);
long oldTime = System.nanoTime();
while (true) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
break;
}
long now = System.nanoTime();
double elapsed = (now - oldTime) / 1e9;
double rate = messagesReceived.sumThenReset() / elapsed;
double throughput = bytesReceived.sumThenReset() / elapsed * 8 / 1024 / 1024;
log.info("Read throughput: {} msg/s -- {} Mbit/s", dec.format(rate), dec.format(throughput));
oldTime = now;
}
pulsarClient.close();
}
use of org.apache.pulsar.client.api.ReaderBuilder in project incubator-pulsar by apache.
the class FunctionRuntimeManagerTest method testProcessAssignmentUpdateModifyFunctions.
@Test
public void testProcessAssignmentUpdateModifyFunctions() 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");
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 update 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();
// add existing assignments
functionRuntimeManager.setAssignment(assignment1);
functionRuntimeManager.setAssignment(assignment2);
reset(functionRuntimeManager);
Function.Assignment assignment3 = Function.Assignment.newBuilder().setWorkerId("worker-1").setInstance(Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build()).build();
List<Function.Assignment> assignments = new LinkedList<>();
assignments.add(assignment1);
assignments.add(assignment3);
Request.AssignmentsUpdate assignmentsUpdate = Request.AssignmentsUpdate.newBuilder().addAllAssignments(assignments).setVersion(1).build();
functionRuntimeManager.functionRuntimeInfoMap.put("test-tenant/test-namespace/func-1:0", new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function1).setInstanceId(0).build()));
functionRuntimeManager.functionRuntimeInfoMap.put("test-tenant/test-namespace/func-2:0", new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build()));
functionRuntimeManager.processAssignmentUpdate(MessageId.earliest, assignmentsUpdate);
verify(functionRuntimeManager, times(1)).insertStopAction(any(FunctionRuntimeInfo.class));
verify(functionRuntimeManager).insertStopAction(argThat(new ArgumentMatcher<FunctionRuntimeInfo>() {
@Override
public boolean matches(Object o) {
if (o instanceof FunctionRuntimeInfo) {
FunctionRuntimeInfo functionRuntimeInfo = (FunctionRuntimeInfo) o;
if (!functionRuntimeInfo.getFunctionInstance().getFunctionMetaData().equals(function2)) {
return false;
}
return true;
}
return false;
}
}));
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(function2)) {
return false;
}
return true;
}
return false;
}
}));
Assert.assertEquals(functionRuntimeManager.actionQueue.size(), 2);
Assert.assertTrue(functionRuntimeManager.actionQueue.contains(new FunctionAction().setAction(FunctionAction.Action.START).setFunctionRuntimeInfo(new FunctionRuntimeInfo().setFunctionInstance(Function.Instance.newBuilder().setFunctionMetaData(function2).setInstanceId(0).build()))));
Assert.assertEquals(functionRuntimeManager.functionRuntimeInfoMap.size(), 2);
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-1").get("test-tenant/test-namespace/func-2:0"), assignment3);
}
Aggregations