org.apache.curator.test.InstanceSpec Java Examples

The following examples show how to use org.apache.curator.test.InstanceSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test
public void     testMissingServer() throws URISyntaxException
{
    RemoteInstanceRequestClientImpl         client = new RemoteInstanceRequestClientImpl(new RemoteConnectionConfiguration());
    try
    {
        // a non-existent port should generate an exception
        client.getWebResource(new URI("http://localhost:" + InstanceSpec.getRandomPort()), MediaType.WILDCARD_TYPE, Object.class);
    }
    catch ( Exception e )
    {
        Throwable cause = e.getCause();
        if ( cause == null )
        {
            cause = e;
        }
        Assert.assertTrue(cause instanceof ConnectException, cause.getClass().getName());
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }
}
 
Example #2
Source File: ZookeeperDiscoverySpiTestUtil.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @param instances Number of instances in cluster.
 * @param customProps Custom configuration properties for every server.
 * @return Test cluster.
 */
public static TestingCluster createTestingCluster(int instances, @Nullable Map<String,Object>[] customProps) {
    String tmpDir;

    tmpDir = System.getenv("TMPFS_ROOT") != null
        ? System.getenv("TMPFS_ROOT") : System.getProperty("java.io.tmpdir");

    List<InstanceSpec> specs = new ArrayList<>();

    for (int i = 0; i < instances; i++) {
        File file = new File(tmpDir, "apacheIgniteTestZk-" + i);

        if (file.isDirectory())
            deleteRecursively0(file);
        else {
            if (!file.mkdirs())
                throw new IgniteException("Failed to create directory for test Zookeeper server: " + file.getAbsolutePath());
        }

        Map<String,Object> props = customProps != null ? customProps[i] : null;

        specs.add(new InstanceSpec(file, -1, -1, -1, true, -1, -1, 500, props));
    }

    return new TestingCluster(specs);
}
 
Example #3
Source File: ZookeeperDiscoverySpiSslTestBase.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the connection string to pass to the ZooKeeper constructor
 *
 * @return connection string
 */
protected String getSslConnectString() {
    StringBuilder str = new StringBuilder();

    for (InstanceSpec spec : zkCluster.getInstances()) {
        if (str.length() > 0)
            str.append(",");

        Object secClientPort = spec.getCustomProperties().get(ZK_SECURE_CLIENT_PORT);

        if (secClientPort == null)
            throw new IllegalArgumentException("Security client port is not configured. [spec=" + spec + ']');

        str.append(spec.getHostname()).append(":").append(secClientPort);
    }

    return str.toString();
}
 
Example #4
Source File: KafkaEmbedded.java    From eagle with Apache License 2.0 6 votes vote down vote up
public KafkaEmbedded(Integer kafkaPort, Integer zookeeperPort) {
    try {
        zk = new ZookeeperEmbedded(zookeeperPort);
        zk.start();

        this.port = null != kafkaPort ? kafkaPort : InstanceSpec.getRandomPort();
        logDir = new File(System.getProperty("java.io.tmpdir"), "kafka/logs/kafka-test-" + kafkaPort);
        FileUtils.deleteQuietly(logDir);

        KafkaConfig config = buildKafkaConfig(zk.getConnectionString());
        kafka = new KafkaServerStartable(config);
        kafka.startup();
    } catch (Exception ex) {
        throw new RuntimeException("Could not start test broker", ex);
    }
}
 
Example #5
Source File: ExampleAppBase.java    From soabase with Apache License 2.0 6 votes vote down vote up
public static String[] setSystemAndAdjustArgs(String configFqpn)
{
    checkNullConsole();
    System.setProperty("dw.curator.connectionString", "localhost:2181");
    System.setProperty("dw.soa.instanceName", "instance-" + new Random().nextInt(10000));
    System.setProperty("dw.soa.discovery.type", "zookeeper");
    System.setProperty("dw.soa.discovery.bindAddress", "localhost");
    System.setProperty("dw.sql.mybatisConfigUrl", "example-mybatis.xml");
    System.setProperty("dw.soa.attributes.type", "sql");
    System.setProperty("dw.server.applicationConnectors[0].port", "" + InstanceSpec.getRandomPort());
    System.setProperty("dw.server.adminConnectors[0].port", "" + InstanceSpec.getRandomPort());
    return new String[]
    {
        "server",
        "|" + configFqpn
    };
}
 
Example #6
Source File: ZookeeperRemoteAliasServiceTest.java    From knox with Apache License 2.0 6 votes vote down vote up
private static void configureAndStartZKCluster() throws Exception {
  // Configure security for the ZK cluster instances
  Map<String, Object> customInstanceSpecProps = new HashMap<>();
  customInstanceSpecProps.put("authProvider.1",
      "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
  customInstanceSpecProps.put("requireClientAuthScheme", "sasl");
  customInstanceSpecProps.put("admin.enableServer", false);

  // Define the test cluster
  List<InstanceSpec> instanceSpecs = new ArrayList<>();
  for (int i = 0; i < 1; i++) {
    InstanceSpec is = new InstanceSpec(null, -1, -1, -1, false, (i + 1), -1,
        -1, customInstanceSpecProps);
    instanceSpecs.add(is);
  }
  zkNodes = new TestingCluster(instanceSpecs);

  // Start the cluster
  zkNodes.start();
}
 
Example #7
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "fail", expectedExceptions = {Exception.class})
public void testHttpsFail(HttpsConfiguration httpsConf) throws Exception
{
    int port = InstanceSpec.getRandomPort();
    JettyServer server = new JettyServer(port, httpsConf);
    server.start();

    RemoteInstanceRequestHttpsClientImpl client = null;
    try
    {
        client = new RemoteInstanceRequestHttpsClientImpl(new RemoteConnectionConfiguration(), httpsConf);
        client.getWebResource(new URI("https://localhost:" + port + "/test"), MediaType.TEXT_PLAIN_TYPE, String.class);
    }
    finally
    {
        client.close();
        server.stop();
    }
}
 
Example #8
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "pass")
public void testHttpsPass(HttpsConfiguration httpsConf) throws Exception
{
    int port = InstanceSpec.getRandomPort();
    JettyServer server = new JettyServer(port, httpsConf);
    server.start();

    RemoteInstanceRequestHttpsClientImpl client = null;
    try
    {
        client = new RemoteInstanceRequestHttpsClientImpl(new RemoteConnectionConfiguration(), httpsConf);
        client.getWebResource(new URI("https://localhost:" + port + "/test"), MediaType.TEXT_PLAIN_TYPE, String.class);
    }
    finally
    {
        client.close();
        server.stop();
    }
}
 
Example #9
Source File: TestRemoteInstanceRequestClient.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
@Test
public void     testConnectionTimeout() throws Exception
{
    int             port = InstanceSpec.getRandomPort();

    RemoteInstanceRequestClientImpl client = null;
    ServerSocket                    server = new ServerSocket(port, 0);
    try
    {
        client = new RemoteInstanceRequestClientImpl(new RemoteConnectionConfiguration());
        client.getWebResource(new URI("http://localhost:" + port), MediaType.WILDCARD_TYPE, Object.class);
    }
    catch ( Exception e )
    {
        Throwable cause = e.getCause();
        Assert.assertTrue(cause instanceof SocketTimeoutException);
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
        server.close();
    }
}
 
Example #10
Source File: TestStringsWithRestEasy.java    From curator with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    RestEasyApplication.singletonsRef.set(new RestEasySingletons());

    ResteasyProviderFactory.setInstance(new ResteasyProviderFactory());

    HttpServletDispatcher   dispatcher = new HttpServletDispatcher();

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.getInitParams().put("javax.ws.rs.Application", RestEasyApplication.class.getName());
    root.addServlet(new ServletHolder(dispatcher), "/*");
    root.addEventListener(new ResteasyBootstrap());
    server.start();
}
 
Example #11
Source File: ZooKeeperRegistryTest.java    From light-4j with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception
{
    int port = 9000;
    clientUrl = new URLImpl(Constants.PROTOCOL_LIGHT, "127.0.0.1", 0, service);
    clientUrl.addParameter("group", "aaa");

    serviceUrl = new URLImpl(Constants.PROTOCOL_LIGHT, "127.0.0.1", 8001, service);
    serviceUrl.addParameter("group", "aaa");

    InstanceSpec spec = new InstanceSpec(null, port, -1, -1, true, 1,-1, -1,new HashMap<>());
    zookeeper = new TestingServer(spec, true);

    client = (ZooKeeperClient)SingletonServiceFactory.getBean(ZooKeeperClient.class);
    registry = (ZooKeeperRegistry)SingletonServiceFactory.getBean(Registry.class);
    System.out.println("client = " + client + " registry = " + registry);
}
 
Example #12
Source File: ZookeeperLocalCluster.java    From hadoop-mini-clusters with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws Exception {
    LOG.info("ZOOKEEPER: Starting Zookeeper on port: {}", port);
    InstanceSpec spec = new InstanceSpec(new File(tempDir), port, electionPort,
            quorumPort, deleteDataDirectoryOnClose, serverId, tickTime, maxClientCnxns, customProperties);
    testingServer = new TestingServer(spec, true);
}
 
Example #13
Source File: TestMapsWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new MapDiscoveryContext(new MockServiceDiscovery<Map<String, String>>(), new RandomStrategy<Map<String, String>>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<Map<String, String>>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<Map<String, String>>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(MapDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example #14
Source File: TestObjectPayloadWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new ServiceDetailsDiscoveryContext(new MockServiceDiscovery<ServiceDetails>(), new RandomStrategy<ServiceDetails>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<ServiceDetails>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<ServiceDetails>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(ServiceDetailsDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example #15
Source File: TestStringsWithJersey.java    From curator with Apache License 2.0 5 votes vote down vote up
@BeforeMethod
public void         setup() throws Exception
{
    context = new StringDiscoveryContext(new MockServiceDiscovery<String>(), new RandomStrategy<String>(), 1000);
    serviceNamesMarshaller = new JsonServiceNamesMarshaller();
    serviceInstanceMarshaller = new JsonServiceInstanceMarshaller<String>(context);
    serviceInstancesMarshaller = new JsonServiceInstancesMarshaller<String>(context);

    Application                                     application = new DefaultResourceConfig()
    {
        @Override
        public Set<Class<?>> getClasses()
        {
            Set<Class<?>>       classes = Sets.newHashSet();
            classes.add(StringDiscoveryResource.class);
            return classes;
        }

        @Override
        public Set<Object> getSingletons()
        {
            Set<Object>     singletons = Sets.newHashSet();
            singletons.add(context);
            singletons.add(serviceNamesMarshaller);
            singletons.add(serviceInstanceMarshaller);
            singletons.add(serviceInstancesMarshaller);
            return singletons;
        }
    };
    ServletContainer        container = new ServletContainer(application);

    port = InstanceSpec.getRandomPort();
    server = new Server(port);
    Context root = new Context(server, "/", Context.SESSIONS);
    root.addServlet(new ServletHolder(container), "/*");
    server.start();
}
 
Example #16
Source File: ZooKeeperConfigurationMonitorTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private static void configureAndStartZKCluster() throws Exception {
    // Configure security for the ZK cluster instances
    Map<String, Object> customInstanceSpecProps = new HashMap<>();
    customInstanceSpecProps.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
    customInstanceSpecProps.put("requireClientAuthScheme", "sasl");
    customInstanceSpecProps.put("admin.enableServer", false);

    // Define the test cluster
    List<InstanceSpec> instanceSpecs = new ArrayList<>();
    for (int i = 0 ; i < 1 ; i++) {
        InstanceSpec is = new InstanceSpec(null, -1, -1, -1, false, (i+1), -1, -1, customInstanceSpecProps);
        instanceSpecs.add(is);
    }
    zkCluster = new TestingCluster(instanceSpecs);

    // Start the cluster
    zkCluster.start();

    // Create the client for the test cluster
    client = CuratorFrameworkFactory.builder()
                                    .connectString(zkCluster.getConnectString())
                                    .retryPolicy(new ExponentialBackoffRetry(100, 3))
                                    .build();
    assertNotNull(client);
    client.start();

    boolean connected = client.blockUntilConnected(10, TimeUnit.SECONDS);
    assertTrue(connected);

    // Create the knox config paths with an ACL for the sasl user configured for the client
    List<ACL> acls = new ArrayList<>();
    acls.add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.ANYONE_ID_UNSAFE));

    client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(acls).forPath(PATH_KNOX_DESCRIPTORS);
    assertNotNull("Failed to create node:" + PATH_KNOX_DESCRIPTORS,
                  client.checkExists().forPath(PATH_KNOX_DESCRIPTORS));
    client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(acls).forPath(PATH_KNOX_PROVIDERS);
    assertNotNull("Failed to create node:" + PATH_KNOX_PROVIDERS,
                  client.checkExists().forPath(PATH_KNOX_PROVIDERS));
}
 
Example #17
Source File: TestReconfiguration.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd() throws Exception
{
    try ( CuratorFramework client = newClient())
    {
        client.start();

        QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble());
        assertConfig(oldConfig, cluster.getInstances());

        CountDownLatch latch = setChangeWaiter(client);
        try ( TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false)) )
        {
            newCluster.start();

            client.reconfig().joining(toReconfigSpec(newCluster.getInstances())).fromConfig(oldConfig.getVersion()).forEnsemble();

            Assert.assertTrue(timing.awaitLatch(latch));

            byte[] newConfigData = client.getConfig().forEnsemble();
            QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
            List<InstanceSpec> newInstances = Lists.newArrayList(cluster.getInstances());
            newInstances.addAll(newCluster.getInstances());
            assertConfig(newConfig, newInstances);
            Assert.assertEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
        }
    }
}
 
Example #18
Source File: TestReconfiguration.java    From curator with Apache License 2.0 5 votes vote down vote up
private void assertConfig(QuorumVerifier config, Collection<InstanceSpec> instances)
{
    for ( InstanceSpec instance : instances )
    {
        QuorumPeer.QuorumServer quorumServer = config.getAllMembers().get((long)instance.getServerId());
        Assert.assertNotNull(quorumServer, String.format("Looking for %s - found %s", instance.getServerId(), config.getAllMembers()));
        Assert.assertEquals(quorumServer.clientAddr.getPort(), instance.getPort());
    }
}
 
Example #19
Source File: SharedCacheCoordinatorTest.java    From datawave with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    InstanceSpec spec = new InstanceSpec(null, -1, -1, -1, true, -1);
    testingZooKeeperServer = new TestingZooKeeperServer(new QuorumConfigBuilder(spec));
    testingZooKeeperServer.start();
    
    cacheCoordinator = new SharedCacheCoordinator("CredentialsCacheBeanTest", spec.getConnectString(), 30, 300, 10);
    
    curatorClient = CuratorFrameworkFactory.builder().namespace("CredentialsCacheBeanTest").retryPolicy(new BoundedExponentialBackoffRetry(100, 200, 3))
                    .connectionTimeoutMs(200).sessionTimeoutMs(100).connectString(spec.getConnectString()).build();
    Whitebox.setInternalState(cacheCoordinator, CuratorFramework.class, curatorClient);
    
    cacheCoordinator.start();
}
 
Example #20
Source File: TestReconfiguration.java    From curator with Apache License 2.0 5 votes vote down vote up
private List<String> toReconfigSpec(Collection<InstanceSpec> instances) throws Exception
{
    String localhost = new InetSocketAddress((InetAddress)null, 0).getAddress().getHostAddress();
    List<String> specs = Lists.newArrayList();
    for ( InstanceSpec instance : instances ) {
        specs.add("server." + instance.getServerId() + "=" + localhost + ":" + instance.getElectionPort() + ":" + instance.getQuorumPort() + ";" + instance.getPort());
    }
    return specs;
}
 
Example #21
Source File: ZooKeeperTestingClusterManager.java    From helios with Apache License 2.0 5 votes vote down vote up
private List<InstanceSpec> createPeers(final int numPeers) {
  final ImmutableList.Builder<InstanceSpec> peers = ImmutableList.builder();

  for (int i = 0; i < numPeers; i++) {
    final int port = temporaryPorts.localPort("zk-client" + i);
    final int electionPort = temporaryPorts.localPort("zk-elect" + i);
    final int quorumPort = temporaryPorts.localPort("zk-quorum" + i);

    final Path peerDir = peerDir(i);
    try {
      Files.createDirectory(peerDir);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    final InstanceSpec spec = new InstanceSpec(
        peerDir.toFile(),
        port,
        electionPort,
        quorumPort,
        true,
        i);

    peers.add(spec);
  }

  return peers.build();
}
 
Example #22
Source File: ZooKeeperTestingClusterManager.java    From helios with Apache License 2.0 5 votes vote down vote up
private List<InetSocketAddress> allocateAddresses(final List<InstanceSpec> peers) {
  return ImmutableList.copyOf(transform(
      peers, new Function<InstanceSpec, InetSocketAddress>() {
        @Override
        public InetSocketAddress apply(@Nullable final InstanceSpec spec) {
          return new InetSocketAddress("127.0.0.1", spec.getPort());
        }
      }));
}
 
Example #23
Source File: TestWithCluster.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void     testSessionSurvives() throws Exception
{
    Timing              timing = new Timing();

    CuratorFramework    client = null;
    TestingCluster      cluster = new TestingCluster(3);
    cluster.start();
    try
    {
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
        client.start();

        client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
        Assert.assertNotNull(client.checkExists().forPath("/temp"));

        for ( InstanceSpec spec : cluster.getInstances() )
        {
            cluster.killServer(spec);
            timing.forWaiting().sleepABit();
            cluster.restartServer(spec);
            timing.sleepABit();
        }

        timing.sleepABit();
        Assert.assertNotNull(client.checkExists().forPath("/temp"));
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
 
Example #24
Source File: EphemeralKafkaBrokerTest.java    From kafka-junit with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartAndStop() throws Exception {
    int kafkaPort = InstanceSpec.getRandomPort();
    int zkPort = InstanceSpec.getRandomPort();
    final EphemeralKafkaBroker broker = EphemeralKafkaBroker.create(kafkaPort, zkPort);
    CompletableFuture<Void> res = broker.start();
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        //Ignore
    }

    assertThat(broker.isRunning()).isTrue();
    assertThat(broker.getKafkaPort().get()).isEqualTo(kafkaPort);
    assertThat(broker.getZookeeperPort().get()).isEqualTo(zkPort);
    assertThat(broker.getBrokerList().isPresent()).isTrue();
    assertThat(broker.getZookeeperConnectString().isPresent()).isTrue();
    assertThat(broker.getLogDir().isPresent()).isTrue();

    Path logDir = Paths.get(broker.getLogDir().get());
    assertThat(Files.exists(logDir)).isTrue();

    broker.stop();
    assertThat(res.isDone()).isTrue();
    assertThat(broker.isRunning()).isFalse();
    assertThat(broker.getBrokerList().isPresent()).isFalse();
    assertThat(broker.getZookeeperConnectString().isPresent()).isFalse();
    assertThat(Files.exists(logDir)).isFalse();
}
 
Example #25
Source File: EphemeralKafkaCluster.java    From kafka-junit with Apache License 2.0 5 votes vote down vote up
private EphemeralKafkaBroker addBroker(Properties overrideBrokerProperties) throws Exception {
    final int brokerPort = InstanceSpec.getRandomPort();
    Properties brokerConfigProperties = new Properties();
    brokerConfigProperties.setProperty(KafkaConfig.BrokerIdProp(), brokers.size() + "");
    brokerConfigProperties.setProperty(KafkaConfig.ZkConnectProp(), zookeeper.getConnectString());
    brokerConfigProperties.setProperty(KafkaConfig.ControlledShutdownEnableProp(), false + "");
    brokerConfigProperties.setProperty(KafkaConfig.ControlledShutdownMaxRetriesProp(), "1");
    brokerConfigProperties.setProperty(KafkaConfig.DeleteTopicEnableProp(), true + "");
    brokerConfigProperties.setProperty(KafkaConfig.PortProp(), "" + brokerPort);
    brokerConfigProperties.setProperty(KafkaConfig.SslEnabledProtocolsProp(), false + "");
    brokerConfigProperties.setProperty(KafkaConfig.AutoCreateTopicsEnableProp(), true + "");
    brokerConfigProperties.setProperty(KafkaConfig.ReplicaSocketTimeoutMsProp(), "300");
    brokerConfigProperties.setProperty(KafkaConfig.ReplicaFetchWaitMaxMsProp(), "100");
    brokerConfigProperties.setProperty(KafkaConfig.ControllerSocketTimeoutMsProp(), "10");

    brokerConfigProperties.setProperty(KafkaConfig.OffsetsTopicReplicationFactorProp(), numBroker + "");
    brokerConfigProperties.setProperty(KafkaConfig.LeaderImbalanceCheckIntervalSecondsProp(), 1 + "");
    brokerConfigProperties.setProperty(KafkaConfig.ZkSessionTimeoutMsProp(), 200 + "");
    brokerConfigProperties.setProperty(KafkaConfig.GroupInitialRebalanceDelayMsDoc(), 200 + "");
    brokerConfigProperties.setProperty(KafkaConfig.AdvertisedHostNameProp(), "localhost");
    brokerConfigProperties.setProperty(KafkaConfig.AdvertisedPortProp(), brokerPort + "");
    brokerConfigProperties.setProperty(KafkaConfig.AdvertisedListenersProp(), "PLAINTEXT://localhost:" + brokerPort);
    brokerConfigProperties.setProperty(KafkaConfig.HostNameProp(), "localhost");
    brokerConfigProperties.setProperty(KafkaConfig.MinInSyncReplicasProp(), Math.max(1, numBroker - 1) + "");
    if(!overrideBrokerProperties.isEmpty()){
        overrideBrokerProperties.forEach((k, v) -> brokerConfigProperties.put(k, v));
    }
    final EphemeralKafkaBroker broker = new EphemeralKafkaBroker(zookeeper, brokerPort, brokerConfigProperties);
    broker.start().get();
    brokers.add(broker);
    return broker;
}
 
Example #26
Source File: TestAbstractZooKeeperConfigurationProvider.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    // start the instance without the admin server!
    InstanceSpec serverSpec = new InstanceSpec(null, -1, -1, -1, true, -1, -1, -1, Collections.singletonMap("zookeeper.admin.enableServer", "false"));
    zkServer = new TestingServer(serverSpec, true);
    client = CuratorFrameworkFactory
            .newClient("localhost:" + zkServer.getPort(),
                    new ExponentialBackoffRetry(1000, 3));
    client.start();

    EnsurePath ensurePath = new EnsurePath(AGENT_PATH);
    ensurePath.ensure(client.getZookeeperClient());
    doSetUp();
}
 
Example #27
Source File: KafkaTestClusterTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Test a cluster instance with listeners on specified ports, where a port is duplicated.
 */
@Test
void testListenerWithSpecificPortRepeated() throws Exception {
    // Explicitly define our port
    final int port1 = InstanceSpec.getRandomPort();
    final int port2 = InstanceSpec.getRandomPort();
    final int port3 = InstanceSpec.getRandomPort();

    // Create plain listeners using the same port.
    final BrokerListener plainListener1 = new PlainListener()
        .onPorts(port1, port2);

    final BrokerListener plainListener2 = new PlainListener()
        .onPorts(port3, port1);

    final List<BrokerListener> listeners = new ArrayList<>();
    listeners.add(plainListener1);
    listeners.add(plainListener2);

    // Define how many brokers.
    final int numberOfBrokers = 2;

    // Speed up shutdown in our tests
    final Properties overrideProperties = getDefaultBrokerOverrideProperties();

    // Create our test server instance
    try (final KafkaTestCluster kafkaTestCluster = new KafkaTestCluster(numberOfBrokers, overrideProperties, listeners)) {

        // Start broker, this should throw an exception
        Assertions.assertThrows(RuntimeException.class, kafkaTestCluster::start);
    }
}
 
Example #28
Source File: StartStopWithoutInitialQuorumTest.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(CentralDogmaBuilder builder) {
    // Set up a cluster of two replicas where the second replica is always unavailable,
    final int quorumPort = InstanceSpec.getRandomPort();
    final int electionPort = InstanceSpec.getRandomPort();
    final int clientPort = InstanceSpec.getRandomPort();

    builder.replication(new ZooKeeperReplicationConfig(
            1, ImmutableMap.of(1, new ZooKeeperAddress("127.0.0.1",
                                                       quorumPort, electionPort, clientPort),
                               2, new ZooKeeperAddress("127.0.0.1", 1, 1, 1))));
}
 
Example #29
Source File: ZooKeeperCommandExecutorTest.java    From centraldogma with Apache License 2.0 5 votes vote down vote up
Replica(InstanceSpec spec, Map<Integer, ZooKeeperAddress> servers,
        Function<Command<?>, CompletableFuture<?>> delegate, boolean start) throws Exception {
    this.delegate = delegate;

    dataDir = spec.getDataDirectory();
    meterRegistry = PrometheusMeterRegistries.newRegistry();

    final int id = spec.getServerId();
    final ZooKeeperReplicationConfig zkCfg = new ZooKeeperReplicationConfig(id, servers);

    rm = new ZooKeeperCommandExecutor(zkCfg, dataDir, new AbstractCommandExecutor(null, null) {
        @Override
        public int replicaId() {
            return id;
        }

        @Override
        protected void doStart(@Nullable Runnable onTakeLeadership,
                               @Nullable Runnable onReleaseLeadership) {}

        @Override
        protected void doStop(@Nullable Runnable onReleaseLeadership) {}

        @Override
        @SuppressWarnings("unchecked")
        protected <T> CompletableFuture<T> doExecute(Command<T> command) {
            return (CompletableFuture<T>) delegate.apply(command);
        }
    }, meterRegistry, null, null);

    startFuture = start ? rm.start() : null;
}
 
Example #30
Source File: AbstractListener.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Internal method to get the next assigned port.  If called more times than configured ports,
 * this method will generate a random port to be used.
 *
 * @return next configured port to use.
 */
public int getNextPort() {
    if (ports == null || ports.length == 0 || portIndex >= ports.length) {
        // Return random Port
        return InstanceSpec.getRandomPort();
    }
    return ports[portIndex++];
}