Search in sources :

Example 6 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class BaseDiscoveryTestSetup method setup.

protected void setup() throws Exception {
    config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setServicePortTls(nextFreePort());
    config.setBindOnLocalhost(true);
    config.setTlsEnabled(true);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    mockZookKeeper = createMockZooKeeper();
    service = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(service).getZooKeeperClientFactory();
    service.start();
}
Also used : ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService)

Example 7 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class DiscoveryServiceWebTest method testWebDiscoveryServiceStarter.

@Test
public void testWebDiscoveryServiceStarter() throws Exception {
    int port = nextFreePort();
    File testConfigFile = new File("tmp." + System.currentTimeMillis() + ".properties");
    if (testConfigFile.exists()) {
        testConfigFile.delete();
    }
    PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(testConfigFile)));
    printWriter.println("zookeeperServers=z1.pulsar.com,z2.pulsar.com,z3.pulsar.com");
    printWriter.println("globalZookeeperServers=z1.pulsar.com,z2.pulsar.com,z3.pulsar.com");
    printWriter.println("webServicePort=" + port);
    printWriter.close();
    testConfigFile.deleteOnExit();
    final ServiceConfig config = PulsarConfigurationLoader.create(testConfigFile.getAbsolutePath(), ServiceConfig.class);
    final ServerManager server = new ServerManager(config);
    DiscoveryServiceStarter.startWebService(server, config);
    assertTrue(server.isStarted());
    server.stop();
    testConfigFile.delete();
}
Also used : ServerManager(org.apache.pulsar.discovery.service.server.ServerManager) ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 8 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class DiscoveryServiceWebTest method testTlsEnable.

@Test
public void testTlsEnable() throws Exception {
    // 1. start server with tls enable
    int port = nextFreePort();
    int tlsPort = nextFreePort();
    ServiceConfig config = new ServiceConfig();
    config.setWebServicePort(port);
    config.setWebServicePortTls(tlsPort);
    config.setTlsEnabled(true);
    config.setTlsCertificateFilePath(TLS_SERVER_CERT_FILE_PATH);
    config.setTlsKeyFilePath(TLS_SERVER_KEY_FILE_PATH);
    ServerManager server = new ServerManager(config);
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Map<String, String> params = new TreeMap<>();
    params.put("zookeeperServers", "dummy-value");
    params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
    server.addServlet("/", DiscoveryServiceServlet.class, params);
    server.start();
    // 2. get ZookeeperCacheLoader to add more brokers
    final String redirect_broker_host = "broker-1";
    List<String> brokers = Lists.newArrayList(redirect_broker_host);
    brokers.stream().forEach(b -> {
        try {
            final String brokerUrl = b + ":" + port;
            final String brokerUrlTls = b + ":" + tlsPort;
            LoadReport report = new LoadReport("http://" + brokerUrl, "https://" + brokerUrlTls, null, null);
            String reportData = ObjectMapperFactory.getThreadLocal().writeValueAsString(report);
            ZkUtils.createFullPathOptimistic(mockZookKeeper, LOADBALANCE_BROKERS_ROOT + "/" + brokerUrl, reportData.getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
        // Ok
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        }
    });
    // 3. https request with tls enable at server side
    String serviceUrl = String.format("https://localhost:%s/", tlsPort);
    String requestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
    KeyManager[] keyManagers = null;
    TrustManager[] trustManagers = InsecureTrustManagerFactory.INSTANCE.getTrustManagers();
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(keyManagers, trustManagers, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
    try {
        InputStream response = new URL(requestUrl).openStream();
        fail("it should give unknown host exception as: discovery service redirects request to: " + redirect_broker_host);
    } catch (Exception e) {
        // 4. Verify: server accepts https request and redirected to one of the available broker host defined into
        // zk. and as broker-service is not up: it should give "UnknownHostException with host=broker-url"
        String host = e.getLocalizedMessage();
        assertEquals(e.getClass(), UnknownHostException.class);
        assertTrue(host.startsWith(redirect_broker_host));
    }
    server.stop();
}
Also used : ServerManager(org.apache.pulsar.discovery.service.server.ServerManager) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) TreeMap(java.util.TreeMap) URL(java.net.URL) KeeperException(org.apache.zookeeper.KeeperException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) UnknownHostException(java.net.UnknownHostException) TrustManager(javax.net.ssl.TrustManager) ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) LoadReport(org.apache.pulsar.policies.data.loadbalancer.LoadReport) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KeyManager(javax.net.ssl.KeyManager) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 9 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class DiscoveryServiceWebTest method testRiderectUrlWithServerStarted.

@Test
public void testRiderectUrlWithServerStarted() throws Exception {
    // 1. start server
    int port = nextFreePort();
    ServiceConfig config = new ServiceConfig();
    config.setWebServicePort(port);
    ServerManager server = new ServerManager(config);
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Map<String, String> params = new TreeMap<>();
    params.put("zookeeperServers", "dummy-value");
    params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
    server.addServlet("/", DiscoveryServiceServlet.class, params);
    server.start();
    // 2. create znode for each broker
    List<String> brokers = Lists.newArrayList("broker-1", "broker-2", "broker-3");
    brokers.stream().forEach(b -> {
        try {
            final String broker = b + ":15000";
            LoadReport report = new LoadReport("http://" + broker, null, null, null);
            String reportData = ObjectMapperFactory.getThreadLocal().writeValueAsString(report);
            ZkUtils.createFullPathOptimistic(mockZookKeeper, LOADBALANCE_BROKERS_ROOT + "/" + broker, reportData.getBytes(ZookeeperClientFactoryImpl.ENCODING_SCHEME), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException ne) {
        // Ok
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            fail("failed while creating broker znodes");
        }
    });
    String serviceUrl = server.getServiceUri().toString();
    String requestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
    /**
     * 3. verify : every time when vip receives a request: it redirects to above brokers sequentially and client
     * must get unknown host exception with above brokers in a sequential manner.
     */
    assertEquals(brokers, validateRequest(brokers, HttpMethod.PUT, requestUrl, new BundlesData(1)), "redirection failed");
    assertEquals(brokers, validateRequest(brokers, HttpMethod.GET, requestUrl, null), "redirection failed");
    assertEquals(brokers, validateRequest(brokers, HttpMethod.POST, requestUrl, new BundlesData(1)), "redirection failed");
    server.stop();
}
Also used : ServerManager(org.apache.pulsar.discovery.service.server.ServerManager) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) TreeMap(java.util.TreeMap) ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) LoadReport(org.apache.pulsar.policies.data.loadbalancer.LoadReport) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 10 with ServiceConfig

use of org.apache.pulsar.discovery.service.server.ServiceConfig in project incubator-pulsar by apache.

the class BrokerServiceLookupTest method testDiscoveryLookup.

/**
 * Discovery-Service lookup over binary-protocol 1. Start discovery service 2. start broker 3. Create
 * Producer/Consumer: by calling Discovery service for partitionedMetadata and topic lookup
 *
 * @throws Exception
 */
@Test
public void testDiscoveryLookup() throws Exception {
    // (1) start discovery service
    ServiceConfig config = new ServiceConfig();
    config.setServicePort(nextFreePort());
    config.setBindOnLocalhost(true);
    DiscoveryService discoveryService = spy(new DiscoveryService(config));
    doReturn(mockZooKeeperClientFactory).when(discoveryService).getZooKeeperClientFactory();
    discoveryService.start();
    // (2) lookup using discovery service
    final String discoverySvcUrl = discoveryService.getServiceUrl();
    PulsarClient pulsarClient2 = PulsarClient.builder().serviceUrl(discoverySvcUrl).build();
    Consumer<byte[]> consumer = pulsarClient2.newConsumer().topic("persistent://my-property2/use2/my-ns/my-topic1").subscriptionName("my-subscriber-name").subscribe();
    Producer<byte[]> producer = pulsarClient2.newProducer().topic("persistent://my-property2/use2/my-ns/my-topic1").create();
    for (int i = 0; i < 10; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message<byte[]> msg = null;
    Set<String> messageSet = Sets.newHashSet();
    for (int i = 0; i < 10; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    producer.close();
}
Also used : ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) DiscoveryService(org.apache.pulsar.discovery.service.DiscoveryService) Test(org.testng.annotations.Test)

Aggregations

ServiceConfig (org.apache.pulsar.discovery.service.server.ServiceConfig)10 Test (org.testng.annotations.Test)9 DiscoveryService (org.apache.pulsar.discovery.service.DiscoveryService)6 HashMap (java.util.HashMap)4 ServerManager (org.apache.pulsar.discovery.service.server.ServerManager)4 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Cleanup (lombok.Cleanup)2 BundlesData (org.apache.pulsar.common.policies.data.BundlesData)2 LoadReport (org.apache.pulsar.policies.data.loadbalancer.LoadReport)2 KeeperException (org.apache.zookeeper.KeeperException)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 SecureRandom (java.security.SecureRandom)1