Search in sources :

Example 86 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class ExampleMQProfileTest method testExample.

@Test
public void testExample() throws Exception {
    System.out.println(executeCommand("fabric:create -n --wait-for-provisioning"));
    // System.out.println(executeCommand("shell:info"));
    // System.out.println(executeCommand("fabric:info"));
    // System.out.println(executeCommand("fabric:profile-list"));
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext, FabricService.class);
    try {
        FabricService fabricService = fabricProxy.getService();
        CuratorFramework curator = fabricService.adapt(CuratorFramework.class);
        Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy, 2).withName("cnt").withProfiles("default").assertProvisioningResult().build();
        try {
            LinkedList<Container> containerList = new LinkedList<Container>(containers);
            Container broker = containerList.removeLast();
            setData(curator, ZkPath.CONTAINER_PROVISION_RESULT.getPath(broker.getId()), "changing");
            System.out.println(executeCommand("fabric:container-change-profile " + broker.getId() + " mq-default"));
            Provision.provisioningSuccess(Arrays.asList(new Container[] { broker }), PROVISION_TIMEOUT);
            System.out.println(executeCommand("fabric:cluster-list"));
            for (Container c : containerList) {
                setData(curator, ZkPath.CONTAINER_PROVISION_RESULT.getPath(c.getId()), "changing");
                System.out.println(executeCommand("fabric:container-change-profile " + c.getId() + " example-mq"));
            }
            Provision.provisioningSuccess(containerList, PROVISION_TIMEOUT);
            System.out.println(executeCommand("fabric:cluster-list"));
            Assert.assertTrue(Provision.waitForCondition(Arrays.asList(new Container[] { broker }), new ContainerCondition() {

                @Override
                public Boolean checkConditionOnContainer(final Container c) {
                    System.out.println(executeCommand("fabric:container-connect -u admin -p admin " + c.getId() + " activemq:bstat"));
                    String output = executeCommand("fabric:container-connect -u admin -p admin " + c.getId() + " activemq:query -QQueue=FABRIC.DEMO");
                    return output.contains("DequeueCount = ") && !output.contains("DequeueCount = 0");
                }
            }, 10000L));
        } finally {
            ContainerBuilder.destroy(containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Container(io.fabric8.api.Container) FabricService(io.fabric8.api.FabricService) ContainerProxy(io.fabric8.itests.paxexam.support.ContainerProxy) LinkedList(java.util.LinkedList) ContainerCondition(io.fabric8.itests.paxexam.support.ContainerCondition) Test(org.junit.Test)

Example 87 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class FabricMavenProxyTest method testUpload.

@Test
public void testUpload() throws Exception {
    String featureLocation = System.getProperty("feature.location");
    System.out.println("Testing with feature from:" + featureLocation);
    System.out.println(executeCommand("fabric:create -n --wait-for-provisioning"));
    // System.out.println(executeCommand("shell:info"));
    // System.out.println(executeCommand("fabric:info"));
    // System.out.println(executeCommand("fabric:profile-list"));
    ServiceProxy<FabricService> fabricProxy = ServiceProxy.createServiceProxy(bundleContext, FabricService.class);
    try {
        Set<ContainerProxy> containers = ContainerBuilder.create(fabricProxy, 2).withName("maven").withProfiles("fabric").waitForProvisioning(10 * 60 * 1000L).assertProvisioningResult().build();
        try {
            List<String> uploadUrls = new ArrayList<String>();
            ServiceProxy<CuratorFramework> curatorProxy = ServiceProxy.createServiceProxy(bundleContext, CuratorFramework.class);
            try {
                CuratorFramework curator = curatorProxy.getService();
                List<String> children = ZooKeeperUtils.getChildren(curator, ZkPath.MAVEN_PROXY.getPath("upload"));
                for (String child : children) {
                    String uploadeUrl = ZooKeeperUtils.getSubstitutedPath(curator, ZkPath.MAVEN_PROXY.getPath("upload") + "/" + child);
                    uploadUrls.add(uploadeUrl);
                }
            } finally {
                curatorProxy.close();
            }
            // Pick a random maven proxy from the list.
            Random random = new Random();
            int index = random.nextInt(uploadUrls.size());
            String targetUrl = uploadUrls.get(index);
            String uploadUrl = targetUrl + "itest/itest/1.0/itest-1.0-features.xml";
            System.out.println("Using URI: " + uploadUrl);
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "admin"));
            CloseableHttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
            HttpPut put = new HttpPut(uploadUrl);
            NFileEntity entity = new NFileEntity(new File(featureLocation), ContentType.TEXT_XML);
            put.setEntity(entity);
            HttpResponse response = client.execute(put);
            System.out.println("Response:" + response.getStatusLine());
            Assert.assertTrue(response.getStatusLine().getStatusCode() == 200 || response.getStatusLine().getStatusCode() == 202);
            System.out.println(executeCommand("fabric:profile-edit --repository mvn:itest/itest/1.0/xml/features default"));
            System.out.println(executeCommand("fabric:profile-edit --feature example-cbr default"));
            Provision.containerStatus(containers, PROVISION_TIMEOUT);
        } finally {
            ContainerBuilder.destroy(containers);
        }
    } finally {
        fabricProxy.close();
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) NFileEntity(org.apache.http.nio.entity.NFileEntity) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) CuratorFramework(org.apache.curator.framework.CuratorFramework) Random(java.util.Random) FabricService(io.fabric8.api.FabricService) File(java.io.File) ContainerProxy(io.fabric8.itests.paxexam.support.ContainerProxy) Test(org.junit.Test)

Example 88 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testDiscoveryOnRestartCurator.

@Test
public void testDiscoveryOnRestartCurator() throws Exception {
    underTest = new ActiveMQServiceFactory();
    underTest.curator = curator;
    Properties props = new Properties();
    props.put("config", "amq.xml");
    props.put("broker-name", "amq");
    props.put("group", "amq");
    props.put("connectors", "openwire");
    props.put("openwire-port", "0");
    props.put("container.ip", "localhost");
    underTest.updated("b", props);
    final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
    OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {

        @Override
        public String getProperty(String s) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
            return null;
        }

        @Override
        public Bundle installBundle(String s) throws BundleException {
            return null;
        }

        @Override
        public Bundle getBundle(long l) {
            return null;
        }

        @Override
        public Bundle[] getBundles() {
            return new Bundle[0];
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void removeServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void addBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void removeBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void addFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public void removeFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?> getServiceReference(String s) {
            return null;
        }

        @Override
        public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
            return null;
        }

        @Override
        public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public <S> S getService(ServiceReference<S> serviceReference) {
            return (S) curatorFrameworkAtomicReference.get();
        }

        @Override
        public boolean ungetService(ServiceReference<?> serviceReference) {
            return false;
        }

        @Override
        public File getDataFile(String s) {
            return null;
        }

        @Override
        public Filter createFilter(String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public Bundle getBundle(String s) {
            return null;
        }
    });
    final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
    osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {

        @Override
        public void onServiceAdd(DiscoveryEvent discoveryEvent) {
            discoveryEvents.offer(discoveryEvent);
        }

        @Override
        public void onServiceRemove(DiscoveryEvent discoveryEvent) {
        }
    });
    // will call into dummy bundle and get curator
    osgiFabricDiscoveryAgent.addingService(null);
    osgiFabricDiscoveryAgent.setGroupName("amq");
    osgiFabricDiscoveryAgent.start();
    DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("event: " + event);
    assertNotNull("got added service", event);
    underTest.deleted("b");
    // swap curator ref
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
    curator = builder.build();
    LOG.debug("Starting new curator " + curator);
    curator.start();
    curatorFrameworkAtomicReference.get().close();
    curatorFrameworkAtomicReference.set(curator);
    // will call into dummy bundle and get new curator ref
    osgiFabricDiscoveryAgent.addingService(null);
    // start broker again
    underTest.curator = curator;
    underTest.updated("b", props);
    event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("new event: " + event);
    assertNotNull("got newly added service", event);
    underTest.deleted("b");
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) DiscoveryEvent(org.apache.activemq.command.DiscoveryEvent) Properties(java.util.Properties) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryNTimes(org.apache.curator.retry.RetryNTimes) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) OsgiFabricDiscoveryAgent(io.fabric8.mq.fabric.discovery.OsgiFabricDiscoveryAgent) Collection(java.util.Collection) File(java.io.File) DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener) Test(org.junit.Test)

Example 89 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class FabricMQGateway method createListener.

protected GatewayServiceTreeCache createListener() {
    String zkPath = getZooKeeperPath();
    // TODO we should discover the broker group configuration here using the same
    // mq-create / mq-client profiles so that we only listen to a subset of the available brokers here?
    ServiceMap serviceMap = new ServiceMap();
    VertxService vertxService = getVertxService();
    Vertx vertx = vertxService.getVertx();
    CuratorFramework curator = getCurator();
    LoadBalancer pathLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LoadBalancer serviceLoadBalancer = LoadBalancers.createLoadBalancer(loadBalancerType, stickyLoadBalancerCacheSize);
    LOG.info("activating MQ mapping ZooKeeper path: " + zkPath + " host: " + host + " with load balancer: " + pathLoadBalancer);
    List<TcpGateway> gateways = new ArrayList<TcpGateway>();
    addGateway(gateways, vertx, serviceMap, "tcp", isOpenWireEnabled(), getOpenWirePort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "stomp", isStompEnabled(), getStompPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "amqp", isAmqpEnabled(), getAmqpPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "mqtt", isMqttEnabled(), getMqttPort(), pathLoadBalancer, serviceLoadBalancer);
    addGateway(gateways, vertx, serviceMap, "ws", isWebsocketEnabled(), getWebsocketPort(), pathLoadBalancer, serviceLoadBalancer);
    if (gateways.isEmpty()) {
        return null;
    }
    return new GatewayServiceTreeCache(curator, zkPath, serviceMap, gateways);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) TcpGateway(io.fabric8.gateway.handlers.tcp.TcpGateway) VertxService(io.fabric8.gateway.fabric.support.vertx.VertxService) ArrayList(java.util.ArrayList) LoadBalancer(io.fabric8.gateway.loadbalancer.LoadBalancer) Vertx(org.vertx.java.core.Vertx)

Example 90 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class GatewayServiceTreeCacheTest method treeCacheEvent.

@Test
public void treeCacheEvent() throws Exception {
    String zkPath = "/fabric/registry/clusters/test";
    ServiceMap serviceMap = new ServiceMap();
    CuratorFramework curator = mock(CuratorFramework.class);
    GatewayServiceTreeCache cache = new GatewayServiceTreeCache(curator, zkPath, serviceMap);
    String path = "/fabric/registry/clusters/test/default/0000001";
    // Add container1 - master
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container1
    // Update container2 - master
    // Add container1 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container2", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container1")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container2
    // Update container1 - master
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_UPDATED, data("test", "container1", "service1", "service2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
    // Remove container2
    // Add container2 - slave
    cache.treeCacheEvent(event(path, CHILD_REMOVED, data("test", "container2")));
    cache.treeCacheEvent(event(path, CHILD_ADDED, data("test", "container2")));
    assertEquals(1, serviceMap.getServices("default").size());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceMap(io.fabric8.gateway.ServiceMap) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)924 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)271 Test (org.junit.Test)199 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)124 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)114 KeeperException (org.apache.zookeeper.KeeperException)93 IOException (java.io.IOException)79 ConnectionState (org.apache.curator.framework.state.ConnectionState)71 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)58 ExecutorService (java.util.concurrent.ExecutorService)55 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)53 ArrayList (java.util.ArrayList)51 RetryNTimes (org.apache.curator.retry.RetryNTimes)51 RetryPolicy (org.apache.curator.RetryPolicy)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)38 Cleanup (lombok.Cleanup)37 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)37 Stat (org.apache.zookeeper.data.Stat)36