use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class ProxyRolesEnforcementTest method testIncorrectRoles.
@Test
void testIncorrectRoles() throws Exception {
log.info("-- Starting {} test --", methodName);
// Step 1: Create Admin Client
createAdminClient();
final String proxyServiceUrl = "pulsar://localhost:" + servicePort;
// create a client which connects to proxy and pass authData
String namespaceName = "my-property/use/my-ns";
String topicName = "persistent://my-property/use/my-ns/my-topic1";
String subscriptionName = "my-subscriber-name";
String clientAuthParams = "authParam:client";
String proxyAuthParams = "authParam:proxy";
admin.properties().createProperty("my-property", new PropertyAdmin(Lists.newArrayList("appid1", "appid2"), Sets.newHashSet("use")));
admin.namespaces().createNamespace(namespaceName);
admin.namespaces().grantPermissionOnNamespace(namespaceName, "proxy", Sets.newHashSet(AuthAction.consume, AuthAction.produce));
admin.namespaces().grantPermissionOnNamespace(namespaceName, "client", Sets.newHashSet(AuthAction.consume, AuthAction.produce));
// Step 2: Try to use proxy Client as a normal Client - expect exception
PulsarClient proxyClient = createPulsarClient("pulsar://localhost:" + BROKER_PORT, proxyAuthParams);
boolean exceptionOccured = false;
try {
proxyClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
} catch (Exception ex) {
exceptionOccured = true;
}
Assert.assertTrue(exceptionOccured);
// Step 3: Run Pulsar Proxy and pass proxy params as client params - expect exception
ProxyConfiguration proxyConfig = new ProxyConfiguration();
proxyConfig.setAuthenticationEnabled(true);
proxyConfig.setServicePort(servicePort);
proxyConfig.setWebServicePort(webServicePort);
proxyConfig.setBrokerServiceURL("pulsar://localhost:" + BROKER_PORT);
proxyConfig.setBrokerClientAuthenticationPlugin(BasicAuthentication.class.getName());
proxyConfig.setBrokerClientAuthenticationParameters(proxyAuthParams);
Set<String> providers = new HashSet<>();
providers.add(BasicAuthenticationProvider.class.getName());
proxyConfig.setAuthenticationProviders(providers);
ProxyService proxyService = new ProxyService(proxyConfig);
proxyService.start();
proxyClient = createPulsarClient(proxyServiceUrl, proxyAuthParams);
exceptionOccured = false;
try {
proxyClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
} catch (Exception ex) {
exceptionOccured = true;
}
Assert.assertTrue(exceptionOccured);
// Step 4: Pass correct client params
proxyClient = createPulsarClient(proxyServiceUrl, clientAuthParams);
proxyClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
proxyClient.close();
proxyService.close();
}
use of org.apache.pulsar.client.api.PulsarClient 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);
}
use of org.apache.pulsar.client.api.PulsarClient 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()));
}
use of org.apache.pulsar.client.api.PulsarClient in project incubator-pulsar by apache.
the class MembershipManagerTest method testConsumerEventListener.
@Test
public void testConsumerEventListener() throws Exception {
PulsarClient mockClient = mock(PulsarClient.class);
Consumer mockConsumer = mock(Consumer.class);
AtomicReference<ConsumerEventListener> listenerHolder = new AtomicReference<>();
when(mockClient.subscribe(eq(workerConfig.getClusterCoordinationTopic()), eq(MembershipManager.COORDINATION_TOPIC_SUBSCRIPTION), any(ConsumerConfiguration.class))).thenAnswer(invocationOnMock -> {
ConsumerConfiguration conf = invocationOnMock.getArgumentAt(2, ConsumerConfiguration.class);
listenerHolder.set(conf.getConsumerEventListener());
return mockConsumer;
});
MembershipManager membershipManager = spy(new MembershipManager(workerConfig, mockClient));
assertFalse(membershipManager.isLeader());
verify(mockClient, times(1)).subscribe(eq(workerConfig.getClusterCoordinationTopic()), eq(MembershipManager.COORDINATION_TOPIC_SUBSCRIPTION), any(ConsumerConfiguration.class));
listenerHolder.get().becameActive(mockConsumer, 0);
assertTrue(membershipManager.isLeader());
listenerHolder.get().becameInactive(mockConsumer, 0);
assertFalse(membershipManager.isLeader());
}
use of org.apache.pulsar.client.api.PulsarClient 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();
}
Aggregations