Search in sources :

Example 6 with PulsarClient

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

the class ProxyConnectionThrottlingTest method testInboundConnection.

@Test
public void testInboundConnection() throws Exception {
    LOG.info("Creating producer 1");
    PulsarClient client1 = PulsarClient.builder().serviceUrl("pulsar://localhost:" + proxyConfig.getServicePort()).build();
    Producer<byte[]> producer1 = client1.newProducer().topic("persistent://sample/test/local/producer-topic-1").create();
    LOG.info("Creating producer 2");
    PulsarClient client2 = PulsarClient.builder().serviceUrl("pulsar://localhost:" + proxyConfig.getServicePort()).build();
    Producer<byte[]> producer2;
    Assert.assertEquals(ProxyConnection.rejectedConnections.get(), 0.0d);
    try {
        producer2 = client2.newProducer().topic("persistent://sample/test/local/producer-topic-1").create();
        producer2.send("Message 1".getBytes());
        Assert.fail("Should have failed since max num of connections is 2 and the first producer used them all up - one for discovery and other for producing.");
    } catch (Exception ex) {
    // OK
    }
    Assert.assertEquals(ProxyConnection.rejectedConnections.get(), 1.0d);
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest) Test(org.testng.annotations.Test)

Example 7 with PulsarClient

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

the class ProxyForwardAuthDataTest method testForwardAuthData.

@Test
void testForwardAuthData() 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: Run Pulsar Proxy without forwarding authData - 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);
    try (ProxyService proxyService = new ProxyService(proxyConfig);
        PulsarClient proxyClient = createPulsarClient(proxyServiceUrl, clientAuthParams)) {
        proxyService.start();
        proxyClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
        Assert.fail("Shouldn't be able to subscribe, auth required");
    } catch (PulsarClientException.AuthorizationException e) {
    // expected behaviour
    }
    // Step 3: Create proxy with forwardAuthData enabled
    proxyConfig.setForwardAuthorizationCredentials(true);
    try (ProxyService proxyService = new ProxyService(proxyConfig);
        PulsarClient proxyClient = createPulsarClient(proxyServiceUrl, clientAuthParams)) {
        proxyService.start();
        proxyClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe().close();
    }
}
Also used : BasicAuthenticationProvider(org.apache.pulsar.proxy.server.ProxyRolesEnforcementTest.BasicAuthenticationProvider) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) BasicAuthentication(org.apache.pulsar.proxy.server.ProxyRolesEnforcementTest.BasicAuthentication) PulsarClient(org.apache.pulsar.client.api.PulsarClient) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 8 with PulsarClient

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

the class ProxyLookupThrottlingTest method testLookup.

@Test
public void testLookup() throws Exception {
    PulsarClient client = PulsarClient.builder().serviceUrl("pulsar://localhost:" + proxyConfig.getServicePort()).connectionsPerBroker(5).ioThreads(5).build();
    assertTrue(proxyService.getLookupRequestSemaphore().tryAcquire());
    assertTrue(proxyService.getLookupRequestSemaphore().tryAcquire());
    Producer<byte[]> producer1 = client.newProducer().topic("persistent://sample/test/local/producer-topic").create();
    assertTrue(proxyService.getLookupRequestSemaphore().tryAcquire());
    try {
        Producer<byte[]> producer2 = client.newProducer().topic("persistent://sample/test/local/producer-topic").create();
        Assert.fail("Should have failed since can't acquire LookupRequestSemaphore");
    } catch (Exception ex) {
    // Ignore
    }
    Assert.assertEquals(LookupProxyHandler.rejectedPartitionsMetadataRequests.get(), 1.0d);
    proxyService.getLookupRequestSemaphore().release();
    try {
        Producer<byte[]> producer3 = client.newProducer().topic("persistent://sample/test/local/producer-topic").create();
    } catch (Exception ex) {
        Assert.fail("Should not have failed since can acquire LookupRequestSemaphore");
    }
    Assert.assertEquals(LookupProxyHandler.rejectedPartitionsMetadataRequests.get(), 1.0d);
    client.close();
}
Also used : PulsarClient(org.apache.pulsar.client.api.PulsarClient) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest) Test(org.testng.annotations.Test)

Example 9 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient 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);
}
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 10 with PulsarClient

use of org.apache.pulsar.client.api.PulsarClient 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());
}
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) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

PulsarClient (org.apache.pulsar.client.api.PulsarClient)86 Test (org.testng.annotations.Test)69 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)27 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)14 IOException (java.io.IOException)12 PersistentTopicStats (org.apache.pulsar.common.policies.data.PersistentTopicStats)12 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)12 URL (java.net.URL)9 LinkedList (java.util.LinkedList)9 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)9 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)9 Function (org.apache.pulsar.functions.proto.Function)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 Map (java.util.Map)7 Producer (org.apache.pulsar.client.api.Producer)7 Reader (org.apache.pulsar.client.api.Reader)7 ReaderBuilder (org.apache.pulsar.client.api.ReaderBuilder)7 URI (java.net.URI)6 HashMap (java.util.HashMap)6 CompletableFuture (java.util.concurrent.CompletableFuture)6