Search in sources :

Example 81 with Config

use of org.apache.heron.spi.common.Config in project heron by twitter.

the class HealthManagerTest method testInitialize.

@Test
public void testInitialize() throws Exception {
    String topologyName = "testTopology";
    Config config = Config.newBuilder().put(Key.SCHEDULER_IS_SERVICE, true).put(Key.TOPOLOGY_NAME, topologyName).put(Key.ENVIRON, "environ").put(Key.CLUSTER, "cluster").build();
    SchedulerStateManagerAdaptor adaptor = mock(SchedulerStateManagerAdaptor.class);
    SchedulerLocation schedulerLocation = SchedulerLocation.newBuilder().setTopologyName(topologyName).setHttpEndpoint("http://127.0.0.1").build();
    when(adaptor.getSchedulerLocation(anyString())).thenReturn(schedulerLocation);
    HealthManagerMetrics publishingMetrics = mock(HealthManagerMetrics.class);
    AbstractModule baseModule = HealthManager.buildBaseModule("127.0.0.1", TrackerMetricsProvider.class.getName(), publishingMetrics);
    HealthManager healthManager = new HealthManager(config, baseModule);
    Map<String, Object> policy = new HashMap<>();
    policy.put(PolicyConfigKey.HEALTH_POLICY_CLASS.key(), TestPolicy.class.getName());
    policy.put("test-config", "test-value");
    String[] policyIds = { "policy" };
    HealthPolicyConfigReader policyConfigProvider = mock(HealthPolicyConfigReader.class);
    when(policyConfigProvider.getPolicyIds()).thenReturn(Arrays.asList(policyIds));
    when(policyConfigProvider.getPolicyConfig("policy")).thenReturn(policy);
    HealthManager spyHealthMgr = spy(healthManager);
    doReturn(adaptor).when(spyHealthMgr).createStateMgrAdaptor();
    doReturn(policyConfigProvider).when(spyHealthMgr).createPolicyConfigReader();
    spyHealthMgr.initialize();
    List<IHealthPolicy> healthPolicies = spyHealthMgr.getHealthPolicies();
    assertEquals(1, healthPolicies.size());
    TestPolicy healthPolicy = (TestPolicy) healthPolicies.iterator().next();
    assertNotNull(healthPolicy.schedulerClient);
    assertEquals(healthPolicy.stateMgrAdaptor, adaptor);
    assertNotNull(healthPolicy.metricsProvider);
    assertEquals(healthPolicy.config.getConfig("test-config"), "test-value");
}
Also used : HashMap(java.util.HashMap) Config(org.apache.heron.spi.common.Config) SchedulerLocation(org.apache.heron.proto.scheduler.Scheduler.SchedulerLocation) Mockito.anyString(org.mockito.Mockito.anyString) TrackerMetricsProvider(org.apache.heron.healthmgr.sensors.TrackerMetricsProvider) SchedulerStateManagerAdaptor(org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor) AbstractModule(com.google.inject.AbstractModule) IHealthPolicy(com.microsoft.dhalion.api.IHealthPolicy) Test(org.junit.Test)

Example 82 with Config

use of org.apache.heron.spi.common.Config in project heron by twitter.

the class DownloadRunner method main.

// takes topology package URI and extracts it to a directory
public static void main(String[] args) throws Exception {
    CommandLineParser parser = new DefaultParser();
    Options slaManagerCliOptions = constructCliOptions();
    // parse the help options first.
    Options helpOptions = constructHelpOptions();
    CommandLine cmd = parser.parse(helpOptions, args, true);
    if (cmd.hasOption("h")) {
        usage(slaManagerCliOptions);
        return;
    }
    try {
        cmd = parser.parse(slaManagerCliOptions, args);
    } catch (ParseException e) {
        usage(slaManagerCliOptions);
        throw new RuntimeException("Error parsing command line options: ", e);
    }
    DownloaderMode mode = DownloaderMode.cluster;
    if (cmd.hasOption(CliArgs.MODE.text)) {
        mode = DownloaderMode.valueOf(cmd.getOptionValue(CliArgs.MODE.text, null));
    }
    Config config;
    switch(mode) {
        case cluster:
            config = Config.toClusterMode(Config.newBuilder().putAll(ConfigLoader.loadClusterConfig()).build());
            break;
        case local:
            if (!cmd.hasOption(CliArgs.HERON_HOME.text) || !cmd.hasOption(CliArgs.CONFIG_PATH.text)) {
                throw new IllegalArgumentException("Missing heron_home or config_path argument");
            }
            String heronHome = cmd.getOptionValue(CliArgs.HERON_HOME.text, null);
            String configPath = cmd.getOptionValue(CliArgs.CONFIG_PATH.text, null);
            config = Config.toLocalMode(Config.newBuilder().putAll(ConfigLoader.loadConfig(heronHome, configPath, null, null)).build());
            break;
        default:
            throw new IllegalArgumentException("Invalid mode: " + cmd.getOptionValue(CliArgs.MODE.text));
    }
    String uri = cmd.getOptionValue(CliArgs.TOPOLOGY_PACKAGE_URI.text, null);
    String destination = cmd.getOptionValue(CliArgs.EXTRACT_DESTINATION.text, null);
    // make it compatible with old param format
    if (uri == null && destination == null) {
        String[] leftOverArgs = cmd.getArgs();
        if (leftOverArgs.length != 2) {
            System.err.println("Usage: downloader <topology-package-uri> <extract-destination>");
            return;
        }
        uri = leftOverArgs[0];
        destination = leftOverArgs[1];
    }
    final URI topologyLocation = new URI(uri);
    final Path topologyDestination = Paths.get(destination);
    final File file = topologyDestination.toFile();
    if (!file.exists()) {
        file.mkdirs();
    }
    Class clazz = Registry.UriToClass(config, topologyLocation);
    final Downloader downloader = Registry.getDownloader(clazz, topologyLocation);
    downloader.download(topologyLocation, topologyDestination);
}
Also used : Path(java.nio.file.Path) Options(org.apache.commons.cli.Options) Config(org.apache.heron.spi.common.Config) URI(java.net.URI) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) File(java.io.File) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 83 with Config

use of org.apache.heron.spi.common.Config in project heron by twitter.

the class HealthManager method main.

public static void main(String[] args) throws Exception {
    CommandLineParser parser = new DefaultParser();
    Options slaManagerCliOptions = constructCliOptions();
    // parse the help options first.
    Options helpOptions = constructHelpOptions();
    CommandLine cmd = parser.parse(helpOptions, args, true);
    if (cmd.hasOption("h")) {
        usage(slaManagerCliOptions);
        return;
    }
    try {
        cmd = parser.parse(slaManagerCliOptions, args);
    } catch (ParseException e) {
        usage(slaManagerCliOptions);
        throw new RuntimeException("Error parsing command line options: ", e);
    }
    HealthManagerMode mode = HealthManagerMode.cluster;
    if (hasOption(cmd, CliArgs.MODE)) {
        mode = HealthManagerMode.valueOf(getOptionValue(cmd, CliArgs.MODE));
    }
    Config config;
    switch(mode) {
        case cluster:
            config = Config.toClusterMode(Config.newBuilder().putAll(ConfigLoader.loadClusterConfig()).putAll(commandLineConfigs(cmd)).build());
            break;
        case local:
            if (!hasOption(cmd, CliArgs.HERON_HOME) || !hasOption(cmd, CliArgs.CONFIG_PATH)) {
                throw new IllegalArgumentException("Missing heron_home or config_path argument");
            }
            String heronHome = getOptionValue(cmd, CliArgs.HERON_HOME);
            String configPath = getOptionValue(cmd, CliArgs.CONFIG_PATH);
            config = Config.toLocalMode(Config.newBuilder().putAll(ConfigLoader.loadConfig(heronHome, configPath, null, null)).putAll(commandLineConfigs(cmd)).build());
            break;
        default:
            throw new IllegalArgumentException("Invalid mode: " + getOptionValue(cmd, CliArgs.MODE));
    }
    setupLogging(cmd, config);
    LOG.fine(Arrays.toString(cmd.getOptions()));
    // Add the SystemConfig into SingletonRegistry
    SystemConfig systemConfig = SystemConfig.newBuilder(true).putAll(Context.systemFile(config), true).putAll(Context.overrideFile(config), true).build();
    SingletonRegistry.INSTANCE.registerSingleton(SystemConfig.HERON_SYSTEM_CONFIG, systemConfig);
    LOG.info("Static Heron config loaded successfully ");
    LOG.fine(config.toString());
    // load the default config value and override with any command line values
    String metricSourceClassName = config.getStringValue(PolicyConfigKey.METRIC_SOURCE_TYPE.key());
    metricSourceClassName = getOptionValue(cmd, CliArgs.METRIC_SOURCE_TYPE, metricSourceClassName);
    String metricsUrl = config.getStringValue(PolicyConfigKey.METRIC_SOURCE_URL.key());
    metricsUrl = getOptionValue(cmd, CliArgs.METRIC_SOURCE_URL, metricsUrl);
    // metrics reporting thread
    HealthManagerMetrics publishingMetrics = new HealthManagerMetrics(Integer.valueOf(getOptionValue(cmd, CliArgs.METRICSMGR_PORT)));
    AbstractModule module = buildBaseModule(metricsUrl, metricSourceClassName, publishingMetrics);
    HealthManager healthManager = new HealthManager(config, module);
    LOG.info("Initializing health manager");
    healthManager.initialize();
    LOG.info("Starting Health Manager");
    PoliciesExecutor policyExecutor = new PoliciesExecutor(healthManager.healthPolicies);
    ScheduledFuture<?> future = policyExecutor.start();
    LOG.info("Starting Health Manager metric posting thread");
    new Thread(publishingMetrics).start();
    try {
        future.get();
    } finally {
        policyExecutor.destroy();
        publishingMetrics.close();
    }
}
Also used : Options(org.apache.commons.cli.Options) SystemConfig(org.apache.heron.common.config.SystemConfig) SystemConfig(org.apache.heron.common.config.SystemConfig) Config(org.apache.heron.spi.common.Config) AbstractModule(com.google.inject.AbstractModule) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) PoliciesExecutor(com.microsoft.dhalion.policy.PoliciesExecutor) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 84 with Config

use of org.apache.heron.spi.common.Config in project heron by twitter.

the class AuroraLauncherTest method testLaunch.

@Test
public void testLaunch() throws Exception {
    Config config = Config.newBuilder().build();
    AuroraLauncher launcher = Mockito.spy(AuroraLauncher.class);
    launcher.initialize(config, config);
    LauncherUtils mockLauncherUtils = Mockito.mock(LauncherUtils.class);
    PowerMockito.spy(LauncherUtils.class);
    PowerMockito.doReturn(mockLauncherUtils).when(LauncherUtils.class, "getInstance");
    // Failed to schedule
    Mockito.when(mockLauncherUtils.onScheduleAsLibrary(Mockito.any(Config.class), Mockito.any(Config.class), Mockito.any(IScheduler.class), Mockito.any(PackingPlan.class))).thenReturn(false);
    Assert.assertFalse(launcher.launch(Mockito.mock(PackingPlan.class)));
    Mockito.verify(mockLauncherUtils).onScheduleAsLibrary(Mockito.any(Config.class), Mockito.any(Config.class), Mockito.any(IScheduler.class), Mockito.any(PackingPlan.class));
    // Happy path
    Mockito.when(mockLauncherUtils.onScheduleAsLibrary(Mockito.any(Config.class), Mockito.any(Config.class), Mockito.any(IScheduler.class), Mockito.any(PackingPlan.class))).thenReturn(true);
    Assert.assertTrue(launcher.launch(Mockito.mock(PackingPlan.class)));
    Mockito.verify(mockLauncherUtils, Mockito.times(2)).onScheduleAsLibrary(Mockito.any(Config.class), Mockito.any(Config.class), Mockito.any(IScheduler.class), Mockito.any(PackingPlan.class));
    launcher.close();
}
Also used : Config(org.apache.heron.spi.common.Config) PackingPlan(org.apache.heron.spi.packing.PackingPlan) LauncherUtils(org.apache.heron.scheduler.utils.LauncherUtils) IScheduler(org.apache.heron.spi.scheduler.IScheduler) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 85 with Config

use of org.apache.heron.spi.common.Config in project heron by twitter.

the class KubernetesContextTest method createVolumeConfigs.

/**
 * Generate <code>Volume</code> Configs for testing.
 * @param testCases Test case container.
 *                  Input: [0] Config, [1] Boolean to indicate Manager/Executor.
 *                  Output: [0] expectedKeys, [1] expectedOptionsKeys, [2] expectedOptionsValues.
 * @param prefix Configuration prefix key to use in lookup.
 */
private void createVolumeConfigs(List<TestTuple<Pair<Config, Boolean>, Object[]>> testCases, String prefix) {
    final String keyPattern = prefix + "%%s.%%s";
    final String keyExecutor = String.format(keyPattern, KubernetesConstants.EXECUTOR_NAME);
    final String keyManager = String.format(keyPattern, KubernetesConstants.MANAGER_NAME);
    final String volumeNameOne = "volume-name-one";
    final String volumeNameTwo = "volume-name-two";
    final String claimName = "OnDeMaNd";
    final String storageClassField = VolumeConfigKeys.storageClassName.name();
    final String pathField = VolumeConfigKeys.path.name();
    final String claimNameField = VolumeConfigKeys.claimName.name();
    final String expectedStorageClass = "expected-storage-class";
    final String expectedPath = "/path/for/volume/expected";
    // Create test cases for Executor/Manager on even/odd indices respectively.
    for (int idx = 0; idx < 2; ++idx) {
        // Manager case is default.
        boolean isExecutor = false;
        String key = keyManager;
        String description = KubernetesConstants.MANAGER_NAME;
        // Executor case.
        if (idx % 2 == 0) {
            isExecutor = true;
            key = keyExecutor;
            description = KubernetesConstants.EXECUTOR_NAME;
        }
        final String storageClassKeyOne = String.format(key, volumeNameOne, storageClassField);
        final String storageClassKeyTwo = String.format(key, volumeNameTwo, storageClassField);
        final String pathKeyOne = String.format(key, volumeNameOne, pathField);
        final String pathKeyTwo = String.format(key, volumeNameTwo, pathField);
        final String claimNameKeyOne = String.format(key, volumeNameOne, claimNameField);
        final String claimNameKeyTwo = String.format(key, volumeNameTwo, claimNameField);
        final Config configPVC = Config.newBuilder().put(pathKeyOne, expectedPath).put(pathKeyTwo, expectedPath).put(claimNameKeyOne, claimName).put(claimNameKeyTwo, claimName).put(storageClassKeyOne, expectedStorageClass).put(storageClassKeyTwo, expectedStorageClass).build();
        final List<String> expectedKeys = Arrays.asList(volumeNameOne, volumeNameTwo);
        final List<VolumeConfigKeys> expectedOptionsKeys = Arrays.asList(VolumeConfigKeys.path, VolumeConfigKeys.storageClassName, VolumeConfigKeys.claimName);
        final List<String> expectedOptionsValues = Arrays.asList(expectedPath, expectedStorageClass, claimName);
        testCases.add(new TestTuple<>(description, new Pair<>(configPVC, isExecutor), new Object[] { expectedKeys, expectedOptionsKeys, expectedOptionsValues }));
        final Config configPVCDisabled = Config.newBuilder().put(KubernetesContext.KUBERNETES_VOLUME_FROM_CLI_DISABLED, "true").put(pathKeyOne, expectedPath).put(pathKeyTwo, expectedPath).put(claimNameKeyOne, claimName).put(claimNameKeyTwo, claimName).put(storageClassKeyOne, expectedStorageClass).put(storageClassKeyTwo, expectedStorageClass).build();
        testCases.add(new TestTuple<>(description + " Disabled should not error", new Pair<>(configPVCDisabled, !isExecutor), new Object[] { new LinkedList<String>(), new LinkedList<VolumeConfigKeys>(), new LinkedList<String>() }));
    }
}
Also used : VolumeConfigKeys(org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys) Config(org.apache.heron.spi.common.Config) LinkedList(java.util.LinkedList) Pair(org.apache.heron.common.basics.Pair)

Aggregations

Config (org.apache.heron.spi.common.Config)140 Test (org.junit.Test)75 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 PackingPlan (org.apache.heron.spi.packing.PackingPlan)22 HashMap (java.util.HashMap)18 SchedulerStateManagerAdaptor (org.apache.heron.spi.statemgr.SchedulerStateManagerAdaptor)18 Pair (org.apache.heron.common.basics.Pair)16 TopologyAPI (org.apache.heron.api.generated.TopologyAPI)15 IOException (java.io.IOException)11 LauncherUtils (org.apache.heron.scheduler.utils.LauncherUtils)11 Map (java.util.Map)10 V1Volume (io.kubernetes.client.openapi.models.V1Volume)9 URI (java.net.URI)9 IScheduler (org.apache.heron.spi.scheduler.IScheduler)9 IStateManager (org.apache.heron.spi.statemgr.IStateManager)9 Before (org.junit.Before)9 File (java.io.File)7 LinkedList (java.util.LinkedList)7 Resource (org.apache.heron.spi.packing.Resource)7 CommandLine (org.apache.commons.cli.CommandLine)6