Search in sources :

Example 56 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class Runner method main.

public static void main(String... argv) throws Exception {
    CommandLine cli = RunnerOptions.parse(new PosixParser(), argv);
    String zkQuorum = RunnerOptions.ZK_QUORUM.get(cli);
    String zkRoot = RunnerOptions.ZK_ROOT.get(cli);
    String script = RunnerOptions.SCRIPT.get(cli);
    String name = RunnerOptions.NAME.get(cli);
    String version = RunnerOptions.VERSION.get(cli);
    String containerId = RunnerOptions.CONTAINER_ID.get(cli);
    String hostname = RunnerOptions.HOSTNAME.get(cli);
    CuratorFramework client = null;
    LOG.info("Running script " + script);
    LOG.info("Local Directory Contents");
    for (File f : new File(".").listFiles()) {
        LOG.info("  " + f.getName());
    }
    try {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.newClient(zkQuorum, retryPolicy);
        client.start();
        MaaSConfig config = ConfigUtil.INSTANCE.read(client, zkRoot, new MaaSConfig(), MaaSConfig.class);
        JsonInstanceSerializer<ModelEndpoint> serializer = new JsonInstanceSerializer<>(ModelEndpoint.class);
        try {
            serviceDiscovery = ServiceDiscoveryBuilder.builder(ModelEndpoint.class).client(client).basePath(config.getServiceRoot()).serializer(serializer).build();
        } finally {
        }
        LOG.info("Created service @ " + config.getServiceRoot());
        serviceDiscovery.start();
        File cwd = new File(script).getParentFile();
        File scriptFile = new File(cwd, script);
        if (scriptFile.exists() && !scriptFile.canExecute()) {
            scriptFile.setExecutable(true);
        }
        final String cmd = scriptFile.getAbsolutePath();
        try {
            p = new ProcessBuilder(cmd).directory(cwd).start();
        } catch (Exception e) {
            LOG.info("Unable to execute " + cmd + " from " + new File(".").getAbsolutePath());
            LOG.error(e.getMessage(), e);
            throw new IllegalStateException(e.getMessage(), e);
        }
        try {
            LOG.info("Started " + cmd);
            Endpoint ep = readEndpoint(cwd);
            URL endpointUrl = correctLocalUrl(hostname, ep.getUrl());
            ep.setUrl(endpointUrl.toString());
            LOG.info("Read endpoint " + ep);
            ModelEndpoint endpoint = new ModelEndpoint();
            {
                endpoint.setName(name);
                endpoint.setContainerId(containerId);
                endpoint.setEndpoint(ep);
                endpoint.setVersion(version);
            }
            ;
            ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint>builder().address(endpointUrl.getHost()).id(containerId).name(name).port(endpointUrl.getPort()).registrationTimeUTC(System.currentTimeMillis()).serviceType(ServiceType.STATIC).payload(endpoint);
            final ServiceInstance<ModelEndpoint> instance = builder.build();
            try {
                LOG.info("Installing service instance: " + instance + " at " + serviceDiscovery);
                serviceDiscovery.registerService(instance);
                LOG.info("Installed instance " + name + ":" + version + "@" + endpointUrl);
            } catch (Throwable t) {
                LOG.error("Unable to install instance " + name + ":" + version + "@" + endpointUrl, t);
            }
            Runtime.getRuntime().addShutdownHook(new Thread() {

                @Override
                public void run() {
                    LOG.info("KILLING CONTAINER PROCESS...");
                    if (p != null) {
                        LOG.info("Process destroyed forcibly");
                        p.destroyForcibly();
                    }
                }
            });
        } finally {
            if (p.waitFor() != 0) {
                String stderr = Joiner.on("\n").join(IOUtils.readLines(p.getErrorStream()));
                String stdout = Joiner.on("\n").join(IOUtils.readLines(p.getInputStream()));
                throw new IllegalStateException("Unable to execute " + script + ".  Stderr is: " + stderr + "\nStdout is: " + stdout);
            }
        }
    } finally {
        if (serviceDiscovery != null) {
            CloseableUtils.closeQuietly(serviceDiscovery);
        }
        if (client != null) {
            CloseableUtils.closeQuietly(client);
        }
    }
}
Also used : ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) MaaSConfig(org.apache.metron.maas.config.MaaSConfig) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL) CuratorFramework(org.apache.curator.framework.CuratorFramework) Endpoint(org.apache.metron.maas.config.Endpoint) ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) JsonInstanceSerializer(org.apache.curator.x.discovery.details.JsonInstanceSerializer) File(java.io.File) RetryPolicy(org.apache.curator.RetryPolicy)

Example 57 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class StellarMaaSIntegrationTest method setup.

@BeforeAll
public static void setup() throws Exception {
    UnitTestHelper.setJavaLoggingLevel(WebApplicationImpl.class, Level.WARNING);
    MockDGAModel.start(8282);
    testZkServer = new TestingServer(true);
    zookeeperUrl = testZkServer.getConnectString();
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
    client.start();
    context = new Context.Builder().with(Context.Capabilities.ZOOKEEPER_CLIENT, () -> client).build();
    MaaSConfig config = ConfigUtil.INSTANCE.read(client, "/metron/maas/config", new MaaSConfig(), MaaSConfig.class);
    discoverer = new ServiceDiscoverer(client, config.getServiceRoot());
    discoverer.start();
    endpointUrl = new URL("http://localhost:8282");
    ModelEndpoint endpoint = new ModelEndpoint();
    {
        endpoint.setName("dga");
        endpoint.setContainerId("0");
        Endpoint ep = new Endpoint();
        ep.setUrl(endpointUrl.toString());
        endpoint.setEndpoint(ep);
        endpoint.setVersion("1.0");
    }
    ;
    ServiceInstanceBuilder<ModelEndpoint> builder = ServiceInstance.<ModelEndpoint>builder().address(endpointUrl.getHost()).id("0").name("dga").port(endpointUrl.getPort()).registrationTimeUTC(System.currentTimeMillis()).serviceType(ServiceType.STATIC).payload(endpoint);
    final ServiceInstance<ModelEndpoint> instance = builder.build();
    discoverer.getServiceDiscovery().registerService(instance);
    // wait til the endpoint is installed...
    for (int i = 0; i < 10; ++i) {
        try {
            Object o = discoverer.getEndpoint("dga");
            if (o != null) {
                break;
            }
        } catch (Exception e) {
        }
        Thread.sleep(1000);
    }
}
Also used : TestingServer(org.apache.curator.test.TestingServer) Context(org.apache.metron.stellar.dsl.Context) ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) MaaSConfig(org.apache.metron.maas.config.MaaSConfig) URL(java.net.URL) Endpoint(org.apache.metron.maas.config.Endpoint) ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) Endpoint(org.apache.metron.maas.config.Endpoint) ModelEndpoint(org.apache.metron.maas.config.ModelEndpoint) RetryPolicy(org.apache.curator.RetryPolicy) ServiceDiscoverer(org.apache.metron.maas.discovery.ServiceDiscoverer) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 58 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class ProfileBuilderBolt method setupZookeeper.

/**
 * Setup connectivity to Zookeeper which provides the necessary configuration for the bolt.
 */
private void setupZookeeper() {
    try {
        if (zookeeperClient == null) {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            zookeeperClient = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
        }
        zookeeperClient.start();
        // this is temporary to ensure that any validation passes. the individual bolt
        // will reinitialize stellar to dynamically pull from zookeeper.
        ConfigurationsUtils.setupStellarStatically(zookeeperClient);
        if (zookeeperCache == null) {
            ConfigurationsUpdater<ProfilerConfigurations> updater = createUpdater();
            SimpleEventListener listener = new SimpleEventListener.Builder().with(updater::update, TreeCacheEvent.Type.NODE_ADDED, TreeCacheEvent.Type.NODE_UPDATED).with(updater::delete, TreeCacheEvent.Type.NODE_REMOVED).build();
            zookeeperCache = new ZKCache.Builder().withClient(zookeeperClient).withListener(listener).withRoot(Constants.ZOOKEEPER_TOPOLOGY_ROOT).build();
            updater.forceUpdate(zookeeperClient);
            zookeeperCache.start();
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : SimpleEventListener(org.apache.metron.zookeeper.SimpleEventListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ZKCache(org.apache.metron.zookeeper.ZKCache) RetryPolicy(org.apache.curator.RetryPolicy) ProfilerConfigurations(org.apache.metron.common.configuration.profiler.ProfilerConfigurations)

Example 59 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class FluxTopologyComponent method start.

@Override
public void start() throws UnableToStartException {
    try {
        stormCluster = new LocalCluster();
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        try (CuratorFramework client = CuratorFrameworkFactory.newClient(getZookeeperConnectString(), retryPolicy)) {
            client.start();
            String root = "/storm/leader-lock";
            Stat exists = client.checkExists().forPath(root);
            if (exists == null) {
                client.create().creatingParentsIfNeeded().forPath(root);
            }
        } catch (Exception e) {
            LOG.error("Unable to create leaderlock", e);
        } finally {
        }
    } catch (Exception e) {
        throw new UnableToStartException("Unable to start flux topology: " + getTopologyLocation(), e);
    }
}
Also used : LocalCluster(org.apache.storm.LocalCluster) CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) UnableToStartException(org.apache.metron.integration.UnableToStartException) RetryPolicy(org.apache.curator.RetryPolicy) UnableToStartException(org.apache.metron.integration.UnableToStartException) TException(org.apache.storm.thrift.TException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TProtocolException(org.apache.storm.thrift.protocol.TProtocolException)

Example 60 with RetryPolicy

use of org.apache.curator.RetryPolicy in project metron by apache.

the class ConfiguredBolt method prepCache.

/**
 * Prepares the cache that will be used during Metron's interaction with ZooKeeper.
 */
protected void prepCache() {
    try {
        if (client == null) {
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
            client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
        }
        client.start();
        // this is temporary to ensure that any validation passes.
        // The individual bolt will reinitialize stellar to dynamically pull from
        // zookeeper.
        ConfigurationsUtils.setupStellarStatically(client);
        if (cache == null) {
            ConfigurationsUpdater<CONFIG_T> updater = createUpdater();
            SimpleEventListener listener = new SimpleEventListener.Builder().with(updater::update, TreeCacheEvent.Type.NODE_ADDED, TreeCacheEvent.Type.NODE_UPDATED).with(updater::delete, TreeCacheEvent.Type.NODE_REMOVED).build();
            cache = new ZKCache.Builder().withClient(client).withListener(listener).withRoot(Constants.ZOOKEEPER_TOPOLOGY_ROOT).build();
            updater.forceUpdate(client);
            cache.start();
        }
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
Also used : SimpleEventListener(org.apache.metron.zookeeper.SimpleEventListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ZKCache(org.apache.metron.zookeeper.ZKCache) RetryPolicy(org.apache.curator.RetryPolicy)

Aggregations

RetryPolicy (org.apache.curator.RetryPolicy)69 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)47 CuratorFramework (org.apache.curator.framework.CuratorFramework)42 RetryNTimes (org.apache.curator.retry.RetryNTimes)12 IOException (java.io.IOException)10 ACLProvider (org.apache.curator.framework.api.ACLProvider)7 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)6 TestingServer (org.apache.curator.test.TestingServer)5 DefaultZooKeeperClient (com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient)4 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)4 SimpleEventListener (org.apache.metron.zookeeper.SimpleEventListener)4 ZKCache (org.apache.metron.zookeeper.ZKCache)4 ACL (org.apache.zookeeper.data.ACL)4 Before (org.junit.Before)4 DefaultServiceManager (com.bonree.brfs.common.service.impl.DefaultServiceManager)3 AuthInfo (org.apache.curator.framework.AuthInfo)3 PathChildrenCache (org.apache.curator.framework.recipes.cache.PathChildrenCache)3 Test (org.junit.Test)3 Service (com.bonree.brfs.common.service.Service)2 ServiceManager (com.bonree.brfs.common.service.ServiceManager)2