Search in sources :

Example 1 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class DiagnosticsUtil method buildDiagnosticsManager.

/**
 * Create a {@link DiagnosticsManager} for the given jobName, jobId, containerId, and execEnvContainerId, if
 * diagnostics is enabled.
 * @param executionEnvContainerId ID assigned to the container by the cluster manager (e.g. YARN)
 * @param samzaEpochId ID assigned to the job deployment attempt by the cluster manager
 */
public static Optional<DiagnosticsManager> buildDiagnosticsManager(String jobName, String jobId, JobModel jobModel, String containerId, Optional<String> executionEnvContainerId, Optional<String> samzaEpochId, Config config) {
    JobConfig jobConfig = new JobConfig(config);
    MetricsConfig metricsConfig = new MetricsConfig(config);
    Optional<DiagnosticsManager> diagnosticsManagerOptional = Optional.empty();
    if (jobConfig.getDiagnosticsEnabled()) {
        ClusterManagerConfig clusterManagerConfig = new ClusterManagerConfig(config);
        int containerMemoryMb = clusterManagerConfig.getContainerMemoryMb();
        int containerNumCores = clusterManagerConfig.getNumCores();
        long maxHeapSizeBytes = Runtime.getRuntime().maxMemory();
        int containerThreadPoolSize = jobConfig.getThreadPoolSize();
        String taskClassVersion = Util.getTaskClassVersion(config);
        String samzaVersion = Util.getSamzaVersion();
        String hostName = Util.getLocalHost().getHostName();
        Optional<String> diagnosticsReporterStreamName = metricsConfig.getMetricsSnapshotReporterStream(MetricsConfig.METRICS_SNAPSHOT_REPORTER_NAME_FOR_DIAGNOSTICS);
        if (!diagnosticsReporterStreamName.isPresent()) {
            throw new ConfigException("Missing required config: " + String.format(MetricsConfig.METRICS_SNAPSHOT_REPORTER_STREAM, MetricsConfig.METRICS_SNAPSHOT_REPORTER_NAME_FOR_DIAGNOSTICS));
        }
        SystemStream diagnosticsSystemStream = StreamUtil.getSystemStreamFromNames(diagnosticsReporterStreamName.get());
        // Create a SystemProducer for DiagnosticsManager. This producer is used by the DiagnosticsManager
        // to write to the same stream as the MetricsSnapshotReporter called `diagnosticsreporter`.
        Optional<String> diagnosticsSystemFactoryName = new SystemConfig(config).getSystemFactory(diagnosticsSystemStream.getSystem());
        if (!diagnosticsSystemFactoryName.isPresent()) {
            throw new SamzaException("Missing factory in config for system " + diagnosticsSystemStream.getSystem());
        }
        SystemFactory systemFactory = ReflectionUtil.getObj(diagnosticsSystemFactoryName.get(), SystemFactory.class);
        SystemProducer systemProducer = systemFactory.getProducer(diagnosticsSystemStream.getSystem(), config, new MetricsRegistryMap(), DiagnosticsUtil.class.getSimpleName());
        DiagnosticsManager diagnosticsManager = new DiagnosticsManager(jobName, jobId, jobModel.getContainers(), containerMemoryMb, containerNumCores, new StorageConfig(config).getNumPersistentStores(), maxHeapSizeBytes, containerThreadPoolSize, containerId, executionEnvContainerId.orElse(""), samzaEpochId.orElse(""), taskClassVersion, samzaVersion, hostName, diagnosticsSystemStream, systemProducer, Duration.ofMillis(new TaskConfig(config).getShutdownMs()), jobConfig.getAutosizingEnabled(), config);
        diagnosticsManagerOptional = Optional.of(diagnosticsManager);
    }
    return diagnosticsManagerOptional;
}
Also used : SystemConfig(org.apache.samza.config.SystemConfig) SystemFactory(org.apache.samza.system.SystemFactory) DiagnosticsManager(org.apache.samza.diagnostics.DiagnosticsManager) StorageConfig(org.apache.samza.config.StorageConfig) SystemStream(org.apache.samza.system.SystemStream) SystemProducer(org.apache.samza.system.SystemProducer) ConfigException(org.apache.samza.config.ConfigException) TaskConfig(org.apache.samza.config.TaskConfig) SamzaException(org.apache.samza.SamzaException) JobConfig(org.apache.samza.config.JobConfig) MetricsConfig(org.apache.samza.config.MetricsConfig) ClusterManagerConfig(org.apache.samza.config.ClusterManagerConfig) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap)

Example 2 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class TestInMemorySystem method produceMessages.

private void produceMessages(Object... events) {
    SystemProducer systemProducer = systemFactory.getProducer(SYSTEM_NAME, config, mockRegistry);
    Stream.of(events).forEach(event -> systemProducer.send(SOURCE, new OutgoingMessageEnvelope(SYSTEM_STREAM, event)));
}
Also used : SystemProducer(org.apache.samza.system.SystemProducer) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope)

Example 3 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class TestDiagnosticsUtil method testBuildDiagnosticsManager.

@Test
public void testBuildDiagnosticsManager() {
    Config config = new MapConfig(buildTestConfigs());
    JobModel mockJobModel = mock(JobModel.class);
    SystemFactory systemFactory = mock(SystemFactory.class);
    SystemProducer mockProducer = mock(SystemProducer.class);
    when(systemFactory.getProducer(anyString(), any(Config.class), any(MetricsRegistry.class), anyString())).thenReturn(mockProducer);
    PowerMockito.mockStatic(ReflectionUtil.class);
    when(ReflectionUtil.getObj(SYSTEM_FACTORY, SystemFactory.class)).thenReturn(systemFactory);
    Optional<DiagnosticsManager> diagnosticsManager = DiagnosticsUtil.buildDiagnosticsManager(JOB_NAME, JOB_ID, mockJobModel, CONTAINER_ID, Optional.of(ENV_ID), Optional.of(SAMZA_EPOCH_ID), config);
    Assert.assertTrue(diagnosticsManager.isPresent());
}
Also used : SystemFactory(org.apache.samza.system.SystemFactory) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) DiagnosticsManager(org.apache.samza.diagnostics.DiagnosticsManager) MetricsConfig(org.apache.samza.config.MetricsConfig) JobConfig(org.apache.samza.config.JobConfig) SystemConfig(org.apache.samza.config.SystemConfig) Config(org.apache.samza.config.Config) MapConfig(org.apache.samza.config.MapConfig) SystemProducer(org.apache.samza.system.SystemProducer) JobModel(org.apache.samza.job.model.JobModel) MapConfig(org.apache.samza.config.MapConfig) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class MetricsSnapshotReporterFactory method getMetricsReporter.

@Override
public MetricsReporter getMetricsReporter(String reporterName, String containerName, Config config) {
    LOG.info("Creating new metrics snapshot reporter.");
    MetricsRegistryMap registry = new MetricsRegistryMap();
    SystemStream systemStream = getSystemStream(reporterName, config);
    SystemProducer producer = getProducer(reporterName, config, registry);
    Duration reportingInterval = Duration.ofSeconds(getReportingInterval(reporterName, config));
    String jobName = getJobName(config);
    String jobId = getJobId(config);
    Serde<MetricsSnapshot> serde = getSerde(reporterName, config);
    Optional<Pattern> blacklist = getBlacklist(reporterName, config);
    MetricsSnapshotReporter reporter = new MetricsSnapshotReporter(producer, systemStream, reportingInterval, jobName, jobId, containerName, Util.getTaskClassVersion(config), Util.getSamzaVersion(), Util.getLocalHost().getHostName(), serde, blacklist, SystemClock.instance());
    reporter.register(this.getClass().getSimpleName(), registry);
    return reporter;
}
Also used : Pattern(java.util.regex.Pattern) SystemStream(org.apache.samza.system.SystemStream) SystemProducer(org.apache.samza.system.SystemProducer) Duration(java.time.Duration) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap)

Example 5 with SystemProducer

use of org.apache.samza.system.SystemProducer in project samza by apache.

the class TestDiagnosticsManager method testDiagnosticsManagerStop.

@Test
public void testDiagnosticsManagerStop() throws InterruptedException {
    SystemProducer mockSystemProducer = Mockito.mock(SystemProducer.class);
    Mockito.when(mockExecutorService.isTerminated()).thenReturn(true);
    Duration terminationDuration = Duration.ofSeconds(1);
    DiagnosticsManager diagnosticsManager = new DiagnosticsManager(JOB_NAME, JOB_ID, containerModels, CONTAINER_MB, CONTAINER_NUM_CORES, NUM_PERSISTENT_STORES, MAX_HEAP_SIZE, CONTAINER_THREAD_POOL_SIZE, "0", EXECUTION_ENV_CONTAINER_ID, SAMZA_EPOCH_ID, TASK_CLASS_VERSION, SAMZA_VERSION, HOSTNAME, diagnosticsSystemStream, mockSystemProducer, terminationDuration, mockExecutorService, AUTOSIZING_ENABLED, config, this.clock);
    diagnosticsManager.stop();
    Mockito.verify(mockExecutorService, Mockito.times(1)).shutdown();
    Mockito.verify(mockExecutorService, Mockito.times(1)).awaitTermination(terminationDuration.toMillis(), TimeUnit.MILLISECONDS);
    Mockito.verify(mockExecutorService, Mockito.never()).shutdownNow();
    Mockito.verify(mockSystemProducer, Mockito.times(1)).stop();
}
Also used : SystemProducer(org.apache.samza.system.SystemProducer) Duration(java.time.Duration) Test(org.junit.Test)

Aggregations

SystemProducer (org.apache.samza.system.SystemProducer)14 OutgoingMessageEnvelope (org.apache.samza.system.OutgoingMessageEnvelope)7 Test (org.junit.Test)7 Duration (java.time.Duration)4 Config (org.apache.samza.config.Config)4 SystemFactory (org.apache.samza.system.SystemFactory)4 SystemStream (org.apache.samza.system.SystemStream)4 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 SamzaException (org.apache.samza.SamzaException)3 MapConfig (org.apache.samza.config.MapConfig)3 SystemConfig (org.apache.samza.config.SystemConfig)3 HashMap (java.util.HashMap)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 JobConfig (org.apache.samza.config.JobConfig)2 MetricsConfig (org.apache.samza.config.MetricsConfig)2 DiagnosticsManager (org.apache.samza.diagnostics.DiagnosticsManager)2 MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)2