Search in sources :

Example 1 with Managed

use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.

the class JDBITest method setUp.

@Before
public void setUp() throws Exception {
    when(environment.healthChecks()).thenReturn(healthChecks);
    when(environment.lifecycle()).thenReturn(lifecycleEnvironment);
    when(environment.metrics()).thenReturn(metricRegistry);
    when(environment.getHealthCheckExecutorService()).thenReturn(Executors.newSingleThreadExecutor());
    this.dbi = factory.build(environment, hsqlConfig, "hsql");
    final ArgumentCaptor<Managed> managedCaptor = ArgumentCaptor.forClass(Managed.class);
    verify(lifecycleEnvironment).manage(managedCaptor.capture());
    managed.addAll(managedCaptor.getAllValues());
    for (Managed obj : managed) {
        obj.start();
    }
    try (Handle handle = dbi.open()) {
        handle.createCall("DROP TABLE people IF EXISTS").invoke();
        handle.createCall("CREATE TABLE people (name varchar(100) primary key, email varchar(100), age int, created_at timestamp)").invoke();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Coda Hale").bind(1, "chale@yammer-inc.com").bind(2, 30).bind(3, new Timestamp(1365465078000L)).execute();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Kris Gale").bind(1, "kgale@yammer-inc.com").bind(2, 32).bind(3, new Timestamp(1365465078000L)).execute();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Old Guy").bindNull(1, Types.VARCHAR).bind(2, 99).bind(3, new Timestamp(1365465078000L)).execute();
        handle.createStatement("INSERT INTO people VALUES (?, ?, ?, ?)").bind(0, "Alice Example").bind(1, "alice@example.org").bind(2, 99).bindNull(3, Types.TIMESTAMP).execute();
    }
}
Also used : Timestamp(java.sql.Timestamp) Managed(io.dropwizard.lifecycle.Managed) Handle(org.skife.jdbi.v2.Handle) Before(org.junit.Before)

Example 2 with Managed

use of io.dropwizard.lifecycle.Managed in project pinot by linkedin.

the class ThirdEyeAnomalyApplication method run.

@Override
public void run(final ThirdEyeAnomalyConfiguration config, final Environment environment) throws Exception {
    LOG.info("Starting ThirdeyeAnomalyApplication : Scheduler {} Worker {}", config.isScheduler(), config.isWorker());
    super.initDAOs();
    ThirdEyeCacheRegistry.initializeCaches(config);
    environment.lifecycle().manage(new Managed() {

        @Override
        public void start() throws Exception {
            if (config.isWorker()) {
                anomalyFunctionFactory = new AnomalyFunctionFactory(config.getFunctionConfigPath());
                alertFilterFactory = new AlertFilterFactory(config.getAlertFilterConfigPath());
                taskDriver = new TaskDriver(config, anomalyFunctionFactory, alertFilterFactory);
                taskDriver.start();
            }
            if (config.isScheduler()) {
                detectionJobScheduler = new DetectionJobScheduler();
                alertFilterFactory = new AlertFilterFactory(config.getAlertFilterConfigPath());
                alertFilterAutotuneFactory = new AlertFilterAutotuneFactory(config.getFilterAutotuneConfigPath());
                detectionJobScheduler.start();
                environment.jersey().register(new DetectionJobResource(detectionJobScheduler, alertFilterFactory, alertFilterAutotuneFactory));
                environment.jersey().register(new AnomalyFunctionResource(config.getFunctionConfigPath()));
            }
            if (config.isMonitor()) {
                monitorJobScheduler = new MonitorJobScheduler(config.getMonitorConfiguration());
                monitorJobScheduler.start();
            }
            if (config.isAlert()) {
                alertJobScheduler = new AlertJobScheduler();
                alertJobScheduler.start();
                // start alert scheduler v2
                alertJobSchedulerV2 = new AlertJobSchedulerV2();
                alertJobSchedulerV2.start();
                environment.jersey().register(new AlertJobResource(alertJobScheduler, emailConfigurationDAO));
            }
            if (config.isMerger()) {
                // anomalyFunctionFactory might have initiated if current machine is also a worker
                if (anomalyFunctionFactory == null) {
                    anomalyFunctionFactory = new AnomalyFunctionFactory(config.getFunctionConfigPath());
                }
                ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
                anomalyMergeExecutor = new AnomalyMergeExecutor(executorService, anomalyFunctionFactory);
                anomalyMergeExecutor.start();
            }
            if (config.isAutoload()) {
                autoLoadPinotMetricsService = new AutoLoadPinotMetricsService(config);
                autoLoadPinotMetricsService.start();
            }
            if (config.isDataCompleteness()) {
                dataCompletenessScheduler = new DataCompletenessScheduler();
                dataCompletenessScheduler.start();
            }
        }

        @Override
        public void stop() throws Exception {
            if (config.isWorker()) {
                taskDriver.stop();
            }
            if (config.isScheduler()) {
                detectionJobScheduler.shutdown();
            }
            if (config.isMonitor()) {
                monitorJobScheduler.stop();
            }
            if (config.isAlert()) {
                alertJobScheduler.shutdown();
                alertJobSchedulerV2.shutdown();
            }
            if (config.isMerger()) {
                anomalyMergeExecutor.stop();
            }
            if (config.isAutoload()) {
                autoLoadPinotMetricsService.shutdown();
            }
            if (config.isDataCompleteness()) {
                dataCompletenessScheduler.shutdown();
            }
        }
    });
}
Also used : DataCompletenessScheduler(com.linkedin.thirdeye.completeness.checker.DataCompletenessScheduler) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AlertFilterFactory(com.linkedin.thirdeye.detector.email.filter.AlertFilterFactory) TaskDriver(com.linkedin.thirdeye.anomaly.task.TaskDriver) MonitorJobScheduler(com.linkedin.thirdeye.anomaly.monitor.MonitorJobScheduler) AlertJobScheduler(com.linkedin.thirdeye.anomaly.alert.AlertJobScheduler) AnomalyMergeExecutor(com.linkedin.thirdeye.anomaly.merge.AnomalyMergeExecutor) AnomalyFunctionResource(com.linkedin.thirdeye.dashboard.resources.AnomalyFunctionResource) AlertJobResource(com.linkedin.thirdeye.anomaly.alert.AlertJobResource) DetectionJobResource(com.linkedin.thirdeye.anomaly.detection.DetectionJobResource) DetectionJobScheduler(com.linkedin.thirdeye.anomaly.detection.DetectionJobScheduler) AutoLoadPinotMetricsService(com.linkedin.thirdeye.autoload.pinot.metrics.AutoLoadPinotMetricsService) AnomalyFunctionFactory(com.linkedin.thirdeye.detector.function.AnomalyFunctionFactory) Managed(io.dropwizard.lifecycle.Managed) AlertFilterAutotuneFactory(com.linkedin.thirdeye.anomalydetection.alertFilterAutotune.AlertFilterAutotuneFactory) AlertJobSchedulerV2(com.linkedin.thirdeye.anomaly.alert.v2.AlertJobSchedulerV2)

Example 3 with Managed

use of io.dropwizard.lifecycle.Managed in project ratelimitj by mokies.

the class RateLimitApplication method run.

@Override
public void run(Configuration configuration, Environment environment) {
    environment.jersey().register(new LoginResource());
    environment.jersey().register(new UserResource());
    environment.jersey().register(new TrekResource());
    environment.jersey().register(new AuthDynamicFeature(new OAuthCredentialAuthFilter.Builder<PrincipalImpl>().setAuthenticator(new TestOAuthAuthenticator()).setPrefix("Bearer").buildAuthFilter()));
    environment.jersey().register(RolesAllowedDynamicFeature.class);
    environment.jersey().register(new AuthValueFactoryProvider.Binder<>(PrincipalImpl.class));
    // TODO move this cleanup into the tests
    environment.lifecycle().manage(new Managed() {

        @Override
        public void start() {
        }

        @Override
        public void stop() {
            flushRedis();
        }

        private void flushRedis() {
            try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
                connection.sync().flushdb();
            }
            redisClient.shutdownAsync();
        }
    });
}
Also used : UserResource(es.moki.ratelimij.dropwizard.component.app.api.UserResource) TestOAuthAuthenticator(es.moki.ratelimij.dropwizard.component.app.auth.TestOAuthAuthenticator) AuthValueFactoryProvider(io.dropwizard.auth.AuthValueFactoryProvider) LoginResource(es.moki.ratelimij.dropwizard.component.app.api.LoginResource) TrekResource(es.moki.ratelimij.dropwizard.component.app.api.TrekResource) StatefulRedisConnection(io.lettuce.core.api.StatefulRedisConnection) OAuthCredentialAuthFilter(io.dropwizard.auth.oauth.OAuthCredentialAuthFilter) AuthDynamicFeature(io.dropwizard.auth.AuthDynamicFeature) PrincipalImpl(io.dropwizard.auth.PrincipalImpl) Managed(io.dropwizard.lifecycle.Managed)

Example 4 with Managed

use of io.dropwizard.lifecycle.Managed in project ratelimitj by mokies.

the class RateLimitBundle method run.

@Override
public void run(final Configuration configuration, final Environment environment) {
    environment.jersey().register(new RateLimitingFactoryProvider.Binder(requestRateLimiterFactory));
    environment.jersey().register(new RateLimited429EnforcerFeature());
    environment.lifecycle().manage(new Managed() {

        @Override
        public void start() {
        }

        @Override
        public void stop() throws Exception {
            requestRateLimiterFactory.close();
        }
    });
}
Also used : RateLimited429EnforcerFeature(es.moki.ratelimij.dropwizard.filter.RateLimited429EnforcerFeature) Managed(io.dropwizard.lifecycle.Managed)

Example 5 with Managed

use of io.dropwizard.lifecycle.Managed in project dropwizard by dropwizard.

the class JerseyClientBuilder method build.

private Client build(String name, ExecutorService threadPool, ObjectMapper objectMapper, Validator validator) {
    if (!configuration.isGzipEnabled()) {
        apacheHttpClientBuilder.disableContentCompression(true);
    }
    final Client client = ClientBuilder.newClient(buildConfig(name, threadPool, objectMapper, validator));
    client.register(new JerseyIgnoreRequestUserAgentHeaderFilter());
    // Tie the client to server lifecycle
    if (environment != null) {
        environment.lifecycle().manage(new Managed() {

            @Override
            public void start() throws Exception {
            }

            @Override
            public void stop() throws Exception {
                client.close();
            }
        });
    }
    if (configuration.isGzipEnabled()) {
        client.register(new GZipDecoder());
        client.register(new ConfiguredGZipEncoder(configuration.isGzipEnabledForRequests()));
    }
    return client;
}
Also used : ConfiguredGZipEncoder(io.dropwizard.jersey.gzip.ConfiguredGZipEncoder) GZipDecoder(io.dropwizard.jersey.gzip.GZipDecoder) Client(javax.ws.rs.client.Client) Managed(io.dropwizard.lifecycle.Managed)

Aggregations

Managed (io.dropwizard.lifecycle.Managed)9 MetricRegistry (com.codahale.metrics.MetricRegistry)2 Timestamp (java.sql.Timestamp)2 Before (org.junit.Before)2 Test (org.junit.jupiter.api.Test)2 Handle (org.skife.jdbi.v2.Handle)2 JmxReporter (com.codahale.metrics.JmxReporter)1 Injector (com.google.inject.Injector)1 AlertJobResource (com.linkedin.thirdeye.anomaly.alert.AlertJobResource)1 AlertJobScheduler (com.linkedin.thirdeye.anomaly.alert.AlertJobScheduler)1 AlertJobSchedulerV2 (com.linkedin.thirdeye.anomaly.alert.v2.AlertJobSchedulerV2)1 DetectionJobResource (com.linkedin.thirdeye.anomaly.detection.DetectionJobResource)1 DetectionJobScheduler (com.linkedin.thirdeye.anomaly.detection.DetectionJobScheduler)1 AnomalyMergeExecutor (com.linkedin.thirdeye.anomaly.merge.AnomalyMergeExecutor)1 MonitorJobScheduler (com.linkedin.thirdeye.anomaly.monitor.MonitorJobScheduler)1 TaskDriver (com.linkedin.thirdeye.anomaly.task.TaskDriver)1 AlertFilterAutotuneFactory (com.linkedin.thirdeye.anomalydetection.alertFilterAutotune.AlertFilterAutotuneFactory)1 AutoLoadPinotMetricsService (com.linkedin.thirdeye.autoload.pinot.metrics.AutoLoadPinotMetricsService)1 DataCompletenessScheduler (com.linkedin.thirdeye.completeness.checker.DataCompletenessScheduler)1 AnomalyFunctionResource (com.linkedin.thirdeye.dashboard.resources.AnomalyFunctionResource)1