com.netflix.governator.InjectorBuilder Java Examples

The following examples show how to use com.netflix.governator.InjectorBuilder. 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: EmbeddedTitusRunner.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    LifecycleInjector injector = InjectorBuilder.fromModule(new EmbeddedCloudModule()).createInjector();
    SimulatedCloud cloud = injector.getInstance(SimulatedCloud.class);

    System.setProperty(DefaultTitusRuntime.FIT_ACTIVATION_PROPERTY, "true");
    EmbeddedTitusMaster.Builder builder = EmbeddedTitusMaster.aTitusMaster()
            .withSimulatedCloud(cloud)
            .withCassadraV3JobStore()
            .withApiPort(8080)
            .withGrpcPort(8090);

    EmbeddedTitusMaster titusMaster = builder.build();
    titusMaster.boot();
    System.out.println("TitusMaster started");
    Thread.sleep(24 * 60 * 60 * 1000L);
}
 
Example #2
Source File: HealthModuleTest.java    From runtime-health with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleInstancesOfHealthModuleInstalled() throws InterruptedException, ExecutionException {
    LifecycleInjector injector = InjectorBuilder.fromModules(new HealthModule() {
        @Override
        protected void configureHealth() {
            bindAdditionalHealthIndicator().toInstance(healthy);
        }
    }, new HealthModule() {
        @Override
        protected void configureHealth() {
            bindAdditionalHealthIndicator().toInstance(unhealthy);
        }
    }, new ArchaiusModule()).createInjector();
    HealthCheckAggregator aggregator = injector.getInstance(HealthCheckAggregator.class);
    assertNotNull(aggregator);
    HealthCheckStatus healthCheckStatus = aggregator.check().get();
    assertFalse(healthCheckStatus.isHealthy());
    assertEquals(2, healthCheckStatus.getHealthResults().size());
}
 
Example #3
Source File: EmbeddedTitusFederation.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
public EmbeddedTitusFederation boot() {
    logger.info("Starting Titus Federation");

    injector = InjectorBuilder.fromModules(
            new EmbeddedJettyModule(httpPort),
            new ArchaiusModule() {
                @Override
                protected void configureArchaius() {
                    // We can set some properties only after the gateway is started.ł
                    config.setProperty("titus.federation.cells", buildCellString());

                    bindApplicationConfigurationOverride().toInstance(config);
                }
            },
            new TitusFederationModule()
    ).createInjector();
    return this;
}
 
Example #4
Source File: ActivationProvisionListenerTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void testActivationLifecycle() throws Exception {
    LifecycleInjector injector = InjectorBuilder.fromModules(
            new ContainerEventBusModule(),
            new AbstractModule() {
                @Override
                protected void configure() {
                    bind(TitusRuntime.class).toInstance(TitusRuntimes.internal());
                    bind(ActivationProvisionListenerTest.class).toInstance(ActivationProvisionListenerTest.this);
                    bind(ServiceA.class).asEagerSingleton();
                }
            }).createInjector();

    ActivationLifecycle activationLifecycle = injector.getInstance(ActivationLifecycle.class);
    ServiceA serviceA = injector.getInstance(ServiceA.class);

    assertThat(serviceA.state).isEqualTo("NOT_READY");

    activationLifecycle.activate();
    assertThat(activationLifecycle.isActive(serviceA)).isTrue();
    assertThat(serviceA.state).isEqualTo("ACTIVATED");

    activationLifecycle.deactivate();
    assertThat(activationLifecycle.isActive(serviceA)).isFalse();
    assertThat(serviceA.state).isEqualTo("DEACTIVATED");
}
 
Example #5
Source File: ZuulBootstrap.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public void start() {
    Server server;
    try {
        // Load sample rules. You may also manage rules in Sentinel dashboard.
        new GatewayRuleConfig().doInit();

        ConfigurationManager.loadCascadedPropertiesFromResources("application");
        Injector injector = InjectorBuilder.fromModule(new ZuulModule()).createInjector();
        injector.getInstance(FiltersRegisteringService.class);
        BaseServerStartup serverStartup = injector.getInstance(BaseServerStartup.class);
        server = serverStartup.server();
        server.start(true);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: ActivationProvisionListenerTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void testServiceReordering() throws Exception {
    LifecycleInjector injector = InjectorBuilder.fromModules(
            new ContainerEventBusModule(),
            new AbstractModule() {
                @Override
                protected void configure() {
                    bind(TitusRuntime.class).toInstance(TitusRuntimes.internal());
                    bind(ActivationProvisionListenerTest.class).toInstance(ActivationProvisionListenerTest.this);
                    bind(ServiceB.class).asEagerSingleton();
                    bind(ServiceA.class).asEagerSingleton();
                }
            }).createInjector();

    ActivationLifecycle activationLifecycle = injector.getInstance(ActivationLifecycle.class);
    activationLifecycle.activate();

    assertThat(activationTrace).containsExactlyInAnyOrder(
            Pair.of("serviceA", "ACTIVATED"),
            Pair.of("serviceB", "ACTIVATED")
    );
}
 
Example #7
Source File: ActivationProvisionListenerTest.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeactivationOrderIsReverseOfActivation() throws Exception {
    LifecycleInjector injector = InjectorBuilder.fromModules(
            new ContainerEventBusModule(),
            new AbstractModule() {
                @Override
                protected void configure() {
                    bind(TitusRuntime.class).toInstance(TitusRuntimes.internal());
                    bind(ActivationProvisionListenerTest.class).toInstance(ActivationProvisionListenerTest.this);
                    bind(ServiceB.class).asEagerSingleton();
                    bind(ServiceA.class).asEagerSingleton();
                }
            }).createInjector();

    ActivationLifecycle activationLifecycle = injector.getInstance(ActivationLifecycle.class);
    activationLifecycle.activate();
    activationLifecycle.deactivate();

    assertThat(activationTrace).hasSize(4);
    String firstActivated = activationTrace.get(0).getLeft();
    String secondActivated = activationTrace.get(1).getLeft();
    assertThat(activationTrace.subList(2, 4)).containsExactly(
            Pair.of(secondActivated, "DEACTIVATED"),
            Pair.of(firstActivated, "DEACTIVATED")
    );
}
 
Example #8
Source File: ActivationProvisionListenerTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Test
public void testActivateOnlyOnce() throws Exception {
    LifecycleInjector injector = InjectorBuilder.fromModules(
            new ContainerEventBusModule(),
            new AbstractModule() {
                @Override
                protected void configure() {
                    bind(TitusRuntime.class).toInstance(TitusRuntimes.internal());
                    bind(ActivationProvisionListenerTest.class).toInstance(ActivationProvisionListenerTest.this);
                    bind(ServiceA.class);
                }
            }).createInjector();

    ActivationLifecycle activationLifecycle = injector.getInstance(ActivationLifecycle.class);
    ServiceA serviceA = injector.getInstance(ServiceA.class);
    ServiceA otherServiceA = injector.getInstance(ServiceA.class);

    assertThat(serviceA).isSameAs(otherServiceA);
    assertThat(serviceA.state).isEqualTo("NOT_READY");

    activationLifecycle.activate();
    assertThat(activationLifecycle.isActive(serviceA)).isTrue();
    assertThat(serviceA.state).isEqualTo("ACTIVATED");
    assertThat(activationTrace).hasSize(1);

    activationLifecycle.deactivate();
    assertThat(activationLifecycle.isActive(serviceA)).isFalse();
    assertThat(serviceA.state).isEqualTo("DEACTIVATED");
    assertThat(activationTrace).hasSize(2);
}
 
Example #9
Source File: Bootstrap.java    From zuul with Apache License 2.0 5 votes vote down vote up
public void start() {
    System.out.println("Zuul Sample: starting up.");
    long startTime = System.currentTimeMillis();
    int exitCode = 0;

    Server server = null;

    try {
        Injector injector = InjectorBuilder.fromModule(new ZuulSampleModule()).createInjector();
        BaseServerStartup serverStartup = injector.getInstance(BaseServerStartup.class);
        server = serverStartup.server();

        long startupDuration = System.currentTimeMillis() - startTime;
        System.out.println("Zuul Sample: finished startup. Duration = " + startupDuration + " ms");

        server.start(true);
    }
    catch (Throwable t) {
        t.printStackTrace();
        System.err.println("###############");
        System.err.println("Zuul Sample: initialization failed. Forcing shutdown now.");
        System.err.println("###############");
        exitCode = 1;
    }
    finally {
        // server shutdown
        if (server != null) server.stop();

        System.exit(exitCode);
    }
}
 
Example #10
Source File: EurekaHealthStatusBridgeModuleTest.java    From runtime-health with Apache License 2.0 5 votes vote down vote up
@Test
public void testHealthCheckHandlerRegistered() {
    InjectorBuilder.fromModules(new EurekaHealthStatusBridgeModule(), new AbstractModule() {
        @Override
        protected void configure() {
            bind(ApplicationInfoManager.class).toInstance(infoManager);
            bind(EurekaClient.class).toInstance(eurekaClient);
            bind(HealthCheckAggregator.class).toInstance(healthCheckAggregator);
        }
    }).createInjector();
    Mockito.verify(eurekaClient, Mockito.times(1)).registerHealthCheck(Mockito.any(HealthCheckHandler.class));
}
 
Example #11
Source File: HealthModuleTest.java    From runtime-health with Apache License 2.0 5 votes vote down vote up
@Test
public void testConfiguringIndicatorsByExtendingHealthModule() throws InterruptedException, ExecutionException {
    LifecycleInjector injector = InjectorBuilder.fromModules(new HealthModule() {
        @Override
        protected void configureHealth() {
            bindAdditionalHealthIndicator().toInstance(healthy);
        }
    }, new ArchaiusModule()).createInjector();
    HealthCheckAggregator aggregator = injector.getInstance(HealthCheckAggregator.class);
    assertNotNull(aggregator);
    HealthCheckStatus healthCheckStatus = aggregator.check().get();
    assertTrue(healthCheckStatus.isHealthy());
    assertEquals(1, healthCheckStatus.getHealthResults().size());
}
 
Example #12
Source File: HealthModuleTest.java    From runtime-health with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoIndicators() throws InterruptedException, ExecutionException {
    LifecycleInjector injector = InjectorBuilder.fromModules(new HealthModule(), new ArchaiusModule()).createInjector();
    HealthCheckAggregator aggregator = injector.getInstance(HealthCheckAggregator.class);
    assertNotNull(aggregator);
    HealthCheckStatus healthCheckStatus = aggregator.check().get();
    assertTrue(healthCheckStatus.isHealthy());
    assertEquals(0, healthCheckStatus.getHealthResults().size());
}
 
Example #13
Source File: TitusFederationMain.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    InjectorBuilder.fromModules(
            new TitusFederationModule(),
            new Archaius2JettyModule(),
            new ArchaiusModule() {
                @Override
                protected void configureArchaius() {
                    bindApplicationConfigurationOverrideResource("laptop");
                }
            })
            .createInjector()
            .awaitTermination();
}
 
Example #14
Source File: RemoteConnectorUtil.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
static LifecycleInjector createSimulatedCloudGrpcServer(SimulatedCloud cloud, int grpcPort) {
    return InjectorBuilder.fromModules(
            new AbstractModule() {
                @Override
                protected void configure() {
                    bind(SimulatedCloud.class).toInstance(cloud);
                    bind(SimulatedCloudConfiguration.class).toInstance(newConfiguration(grpcPort));
                }
            },
            new SimulatedCloudEndpointModule()
    ).createInjector();
}
 
Example #15
Source File: SimulatedCloudRunner.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    LifecycleInjector injector = InjectorBuilder.fromModule(new EmbeddedCloudModule()).createInjector();

    SimulatedCloud simulatedCloud = injector.getInstance(SimulatedCloud.class);

    simulatedCloud.createAgentInstanceGroups(
            SimulatedAgentGroupDescriptor.awsInstanceGroup("critical1", AwsInstanceType.M4_4XLarge, 5),
            SimulatedAgentGroupDescriptor.awsInstanceGroup("flex1", AwsInstanceType.R4_8XLarge, 5),
            SimulatedAgentGroupDescriptor.awsInstanceGroup("flexGpu", AwsInstanceType.G2_8XLarge, 5)
    );

    injector.awaitTermination();
}
 
Example #16
Source File: EmbeddedTitusStackRunner.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    CommandLineFacade cliFacade = buildCliFacade(args);

    EmbeddedTitusMaster.Builder masterBuilder = EmbeddedTitusMaster.aTitusMaster()
            .withProperty("titus.scheduler.globalTaskLaunchingConstraintEvaluatorEnabled", "false")
            .withApiPort(8080)
            .withGrpcPort(7104)
            .withEnableDisruptionBudget(true);

    String cloudSimulatorHost = cliFacade.getString("H");
    if (cloudSimulatorHost != null) {
        int port = Preconditions.checkNotNull(cliFacade.getInt("p"), "cloud simulator port not provided (-p option) ");
        masterBuilder.withRemoteCloud(cloudSimulatorHost, port);
    } else {
        InjectorBuilder.fromModule(new EmbeddedCloudModule())
                .overrideWith(new AbstractModule() {
                    @Override
                    protected void configure() {
                        bind(SimulatedCloudConfiguration.class).toInstance(new SimulatedCloudConfiguration() {
                            @Override
                            public int getGrpcPort() {
                                return 8093;
                            }
                        });
                    }
                })
                .createInjector();
        masterBuilder.withRemoteCloud("localhost", 8093);
    }

    EmbeddedTitusMaster titusMaster = masterBuilder.build();

    boolean federationEnabled = cliFacade.isEnabled("f");

    EmbeddedTitusCell cell = EmbeddedTitusCell.aTitusCell()
            .withMaster(titusMaster)
            .withGateway(
                    EmbeddedTitusGateway.aDefaultTitusGateway()
                            .withMasterEndpoint("localhost", 8090, 8080)
                            .withHttpPort(8081)
                            .withGrpcPort(8091)
                            // Binds to the default TaskRelocation service configuration
                            // TODO Make it configurable via command line parameter
                            .withProperty("titus.relocation.grpcClient.grpcPort", "7105")
                            .withProperty("titus.feature.mergingTaskMigrationPlanInGatewayEnabled", "true")
                            .build(),
                    !federationEnabled
            )
            .build();

    cell.boot();

    if (federationEnabled) {
        EmbeddedTitusFederation stack = EmbeddedTitusFederation.aDefaultTitusFederation()
                .withCell(".*", cell)
                .withHttpPort(8082)
                .withGrpcPort(8092)
                .build();

        stack.boot();
    }

    System.out.println("TitusStack started");
    Thread.sleep(24 * 60 * 60 * 1000L);
}
 
Example #17
Source File: ArchaiusHealthStatusFilterModuleTest.java    From runtime-health with Apache License 2.0 4 votes vote down vote up
@Before
public void init() {
    injector = InjectorBuilder.fromModules(new ArchaiusModule(), new ArchaiusHealthStatusFilterModule()).createInjector();
    config = injector.getInstance(Key.get(SettableConfig.class, RuntimeLayer.class));
    matcher = injector.getInstance(IndicatorMatcher.class);
}
 
Example #18
Source File: EmbeddedTitusGateway.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
public EmbeddedTitusGateway boot() {
    Stopwatch timer = Stopwatch.createStarted();
    logger.info("Starting Titus Gateway");

    injector = InjectorBuilder.fromModules(
            newJettyModule(),
            new ArchaiusModule() {
                @Override
                protected void configureArchaius() {
                    config.setProperty("titus.masterClient.masterIp", masterGrpcHost);

                    if (embeddedTitusMaster == null) {
                        config.setProperty("titus.masterClient.masterGrpcPort", masterGrpcPort);
                        config.setProperty("titus.masterClient.masterHttpPort", masterHttpPort);
                    } else {
                        // In the embedded mode, master cannot run jetty, so we set only GRPC port.
                        config.setProperty("titus.masterClient.masterGrpcPort", embeddedTitusMaster.getGrpcPort());
                        config.setProperty("titus.masterClient.masterHttpPort", "0");
                    }

                    bindApplicationConfigurationOverride().toInstance(config);
                }
            },
            Modules.override(new TitusGatewayModule(enableREST, false)).with(new AbstractModule() {
                @Override
                protected void configure() {
                    if (store != null) {
                        bind(JobStore.class).toInstance(store);
                    }

                    bind(new TypeLiteral<AdmissionValidator<JobDescriptor>>() {
                    }).toInstance(validator);
                }

                @Provides
                @Singleton
                public AdmissionSanitizer<JobDescriptor> getJobSanitizer(TitusValidatorConfiguration configuration) {
                    return new AggregatingSanitizer(configuration, Collections.singletonList(jobSanitizer));
                }
            })
    ).createInjector();

    logger.info("Embedded TitusGateway started in {}ms", timer.elapsed(TimeUnit.MILLISECONDS));

    return this;
}