Tabnine Logo
SamzaContainer
Code IndexAdd Tabnine to your IDE (free)

How to use
SamzaContainer
in
org.apache.samza.container

Best Java code snippets using org.apache.samza.container.SamzaContainer (Showing top 20 results out of 315)

origin: apache/samza

/**
 * This method returns the number of containers in the job. It works by querying the server, and getting the job model.
 * Then it extracts the number of containers from the job model
 *
 * @return current number of containers in the job
 */
public int getCurrentNumContainers() {
 return SamzaContainer.readJobModel(coordinatorServerURL, defaultReadJobModelDelayMs).getContainers().values().size();
}
origin: apache/samza

@Override
public void onNewJobModel(String processorId, JobModel jobModel) {
 synchronized (lock) {
  if (state == State.IN_REBALANCE) {
   containerShutdownLatch = new CountDownLatch(1);
   container = createSamzaContainer(processorId, jobModel);
   container.setContainerListener(new ContainerListener());
   LOGGER.info("Starting the container: {} for the stream processor: {}.", container, processorId);
   containerExcecutorService.submit(container);
  } else {
   LOGGER.info("Ignoring onNewJobModel invocation since the current state is {} and not {}.", state, State.IN_REBALANCE);
  }
 }
}
origin: apache/samza

/**
 * Stops the {@link SamzaContainer}.
 * @return true if {@link SamzaContainer} had shutdown within task.shutdown.ms. false otherwise.
 */
private boolean stopSamzaContainer() {
 boolean hasContainerShutdown = true;
 if (container != null) {
  try {
   container.shutdown();
   LOGGER.info("Waiting {} ms for the container: {} to shutdown.", taskShutdownMs, container);
   hasContainerShutdown = containerShutdownLatch.await(taskShutdownMs, TimeUnit.MILLISECONDS);
  } catch (InterruptedException e) {
   LOGGER.error("Exception occurred when shutting down the container: {}.", container, e);
   hasContainerShutdown = false;
   if (containerException != null) {
    containerException = e;
   }
  }
  LOGGER.info(String.format("Shutdown status of container: %s for stream processor: %s is: %b.", container, processorId, hasContainerShutdown));
 }
 // We want to propagate TimeoutException when container shutdown times out. It is possible that the timeout exception
 // we propagate to the application runner maybe overwritten by container failure cause in case of interleaved execution.
 // It is acceptable since container exception is much more useful compared to timeout exception.
 // We can infer from the logs about the fact that container shutdown timed out or not for additional inference.
 if (!hasContainerShutdown && containerException == null) {
  containerException = new TimeoutException("Container shutdown timed out after " + taskShutdownMs + " ms.");
 }
 return hasContainerShutdown;
}
origin: apache/samza

@Test
public void testOnNewJobModelShouldResultInValidStateTransitions() throws Exception {
 JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
 ProcessorLifecycleListener lifecycleListener = Mockito.mock(ProcessorLifecycleListener.class);
 SamzaContainer mockSamzaContainer = Mockito.mock(SamzaContainer.class);
 MapConfig config = new MapConfig(ImmutableMap.of("task.shutdown.ms", "0"));
 StreamProcessor streamProcessor = new TestableStreamProcessor(config, new HashMap<>(), null,
   lifecycleListener, mockJobCoordinator, mockSamzaContainer);
 streamProcessor.state = State.IN_REBALANCE;
 Mockito.doNothing().when(mockSamzaContainer).run();
 streamProcessor.jobCoordinatorListener.onNewJobModel("TestProcessorId", new JobModel(new MapConfig(), new HashMap<>()));
 Mockito.verify(mockSamzaContainer, Mockito.times(1)).setContainerListener(any());
 Mockito.verify(mockSamzaContainer, Mockito.atMost(1)).run();
}
origin: org.apache.samza/samza-core_2.12

public static void main(String[] args) throws Exception {
 Thread.setDefaultUncaughtExceptionHandler(
   new SamzaUncaughtExceptionHandler(() -> {
    log.info("Exiting process now.");
    System.exit(1);
   }));
 String containerId = System.getenv(ShellCommandConfig.ENV_CONTAINER_ID());
 log.info(String.format("Got container ID: %s", containerId));
 System.out.println(String.format("Container ID: %s", containerId));
 String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL());
 log.info(String.format("Got coordinator URL: %s", coordinatorUrl));
 System.out.println(String.format("Coordinator URL: %s", coordinatorUrl));
 int delay = new Random().nextInt(SamzaContainer.DEFAULT_READ_JOBMODEL_DELAY_MS()) + 1;
 JobModel jobModel = SamzaContainer.readJobModel(coordinatorUrl, delay);
 Config config = jobModel.getConfig();
 JobConfig jobConfig = new JobConfig(config);
 if (jobConfig.getName().isEmpty()) {
  throw new SamzaException("can not find the job name");
 }
 String jobName = jobConfig.getName().get();
 String jobId = jobConfig.getJobId();
 MDC.put("containerName", "samza-container-" + containerId);
 MDC.put("jobName", jobName);
 MDC.put("jobId", jobId);
 ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc =
   ApplicationDescriptorUtil.getAppDescriptor(ApplicationUtil.fromConfig(config), config);
 run(appDesc, containerId, jobModel, config);
 System.exit(0);
}
origin: apache/samza

@Test
public void testStopShouldBeIdempotent() {
 JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
 ProcessorLifecycleListener lifecycleListener = Mockito.mock(ProcessorLifecycleListener.class);
 SamzaContainer mockSamzaContainer = Mockito.mock(SamzaContainer.class);
 MapConfig config = new MapConfig(ImmutableMap.of("task.shutdown.ms", "0"));
 StreamProcessor streamProcessor = PowerMockito.spy(new StreamProcessor("TestProcessorId", config, new HashMap<>(), null, lifecycleListener, mockJobCoordinator));
 Mockito.doNothing().when(mockJobCoordinator).stop();
 Mockito.doNothing().when(mockSamzaContainer).shutdown();
 Mockito.when(mockSamzaContainer.hasStopped()).thenReturn(false);
 Mockito.when(mockSamzaContainer.getStatus())
     .thenReturn(SamzaContainerStatus.STARTED)
     .thenReturn(SamzaContainerStatus.STOPPED);
 streamProcessor.state = State.RUNNING;
 streamProcessor.stop();
 assertEquals(State.STOPPING, streamProcessor.state);
}
origin: org.apache.samza/samza-core_2.11

@VisibleForTesting
SamzaContainer createSamzaContainer(String processorId, JobModel jobModel) {
 return SamzaContainer.apply(processorId, jobModel, ScalaJavaUtil.toScalaMap(this.customMetricsReporter),
   this.taskFactory, JobContextImpl.fromConfigWithDefaults(this.config),
   Option.apply(this.applicationDefinedContainerContextFactoryOptional.orElse(null)),
   Option.apply(this.applicationDefinedTaskContextFactoryOptional.orElse(null)));
}
origin: apache/samza

@Test
public void testCoordinatorFailureShouldStopTheStreamProcessor() {
 JobCoordinator mockJobCoordinator = Mockito.mock(JobCoordinator.class);
 ProcessorLifecycleListener lifecycleListener = Mockito.mock(ProcessorLifecycleListener.class);
 SamzaContainer mockSamzaContainer = Mockito.mock(SamzaContainer.class);
 MapConfig config = new MapConfig(ImmutableMap.of("task.shutdown.ms", "0"));
 StreamProcessor streamProcessor = new StreamProcessor("TestProcessorId", config, new HashMap<>(), null, lifecycleListener, mockJobCoordinator);
 Exception failureException = new Exception("dummy exception");
 streamProcessor.container = mockSamzaContainer;
 streamProcessor.state = State.RUNNING;
 streamProcessor.jobCoordinatorListener.onCoordinatorFailure(failureException);
 Mockito.doNothing().when(mockSamzaContainer).shutdown();
 Mockito.when(mockSamzaContainer.hasStopped()).thenReturn(false);
 assertEquals(State.STOPPED, streamProcessor.state);
 Mockito.verify(lifecycleListener).afterFailure(failureException);
 Mockito.verify(mockSamzaContainer).shutdown();
}
origin: org.apache.samza/samza-core_2.11

  .createInstance(new ProcessorContext() { }, config);
container.setContainerListener(
  new SamzaContainerListener() {
   @Override
container.run();
if (heartbeatMonitor != null) {
 heartbeatMonitor.stop();
origin: org.apache.samza/samza-core_2.11

public static void main(String[] args) throws Exception {
 Thread.setDefaultUncaughtExceptionHandler(
   new SamzaUncaughtExceptionHandler(() -> {
    log.info("Exiting process now.");
    System.exit(1);
   }));
 String containerId = System.getenv(ShellCommandConfig.ENV_CONTAINER_ID());
 log.info(String.format("Got container ID: %s", containerId));
 System.out.println(String.format("Container ID: %s", containerId));
 String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL());
 log.info(String.format("Got coordinator URL: %s", coordinatorUrl));
 System.out.println(String.format("Coordinator URL: %s", coordinatorUrl));
 int delay = new Random().nextInt(SamzaContainer.DEFAULT_READ_JOBMODEL_DELAY_MS()) + 1;
 JobModel jobModel = SamzaContainer.readJobModel(coordinatorUrl, delay);
 Config config = jobModel.getConfig();
 JobConfig jobConfig = new JobConfig(config);
 if (jobConfig.getName().isEmpty()) {
  throw new SamzaException("can not find the job name");
 }
 String jobName = jobConfig.getName().get();
 String jobId = jobConfig.getJobId();
 MDC.put("containerName", "samza-container-" + containerId);
 MDC.put("jobName", jobName);
 MDC.put("jobId", jobId);
 ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc =
   ApplicationDescriptorUtil.getAppDescriptor(ApplicationUtil.fromConfig(config), config);
 run(appDesc, containerId, jobModel, config);
 System.exit(0);
}
origin: apache/samza

Mockito.doNothing().when(mockSamzaContainer).shutdown();
Mockito.when(mockSamzaContainer.hasStopped()).thenReturn(false);
Mockito.when(mockSamzaContainer.getStatus())
    .thenReturn(SamzaContainerStatus.STARTED)
    .thenReturn(SamzaContainerStatus.STOPPED);
Mockito.verify(mockSamzaContainer, Mockito.times(1)).shutdown();
Mockito.verify(mockJobCoordinator, Mockito.times(1)).stop();
origin: org.apache.samza/samza-core_2.12

@VisibleForTesting
SamzaContainer createSamzaContainer(String processorId, JobModel jobModel) {
 return SamzaContainer.apply(processorId, jobModel, ScalaJavaUtil.toScalaMap(this.customMetricsReporter),
   this.taskFactory, JobContextImpl.fromConfigWithDefaults(this.config),
   Option.apply(this.applicationDefinedContainerContextFactoryOptional.orElse(null)),
   Option.apply(this.applicationDefinedTaskContextFactoryOptional.orElse(null)));
}
origin: org.apache.samza/samza-core

  .createInstance(new ProcessorContext() { }, config);
container.setContainerListener(
  new SamzaContainerListener() {
   @Override
container.run();
if (heartbeatMonitor != null) {
 heartbeatMonitor.stop();
origin: apache/samza

System.out.println(String.format("Coordinator URL: %s", coordinatorUrl));
int delay = new Random().nextInt(SamzaContainer.DEFAULT_READ_JOBMODEL_DELAY_MS()) + 1;
JobModel jobModel = SamzaContainer.readJobModel(coordinatorUrl, delay);
Config config = jobModel.getConfig();
JobConfig jobConfig = new JobConfig(config);
origin: org.apache.samza/samza-core_2.11

/**
 * Stops the {@link SamzaContainer}.
 * @return true if {@link SamzaContainer} had shutdown within task.shutdown.ms. false otherwise.
 */
private boolean stopSamzaContainer() {
 boolean hasContainerShutdown = true;
 if (container != null) {
  try {
   container.shutdown();
   LOGGER.info("Waiting {} ms for the container: {} to shutdown.", taskShutdownMs, container);
   hasContainerShutdown = containerShutdownLatch.await(taskShutdownMs, TimeUnit.MILLISECONDS);
  } catch (InterruptedException e) {
   LOGGER.error("Exception occurred when shutting down the container: {}.", container, e);
   hasContainerShutdown = false;
   if (containerException != null) {
    containerException = e;
   }
  }
  LOGGER.info(String.format("Shutdown status of container: %s for stream processor: %s is: %b.", container, processorId, hasContainerShutdown));
 }
 // We want to propagate TimeoutException when container shutdown times out. It is possible that the timeout exception
 // we propagate to the application runner maybe overwritten by container failure cause in case of interleaved execution.
 // It is acceptable since container exception is much more useful compared to timeout exception.
 // We can infer from the logs about the fact that container shutdown timed out or not for additional inference.
 if (!hasContainerShutdown && containerException == null) {
  containerException = new TimeoutException("Container shutdown timed out after " + taskShutdownMs + " ms.");
 }
 return hasContainerShutdown;
}
origin: org.apache.samza/samza-core

@Override
public void onNewJobModel(String processorId, JobModel jobModel) {
 synchronized (lock) {
  if (state == State.IN_REBALANCE) {
   containerShutdownLatch = new CountDownLatch(1);
   container = createSamzaContainer(processorId, jobModel);
   container.setContainerListener(new ContainerListener());
   LOGGER.info("Starting the container: {} for the stream processor: {}.", container, processorId);
   containerExcecutorService.submit(container);
  } else {
   LOGGER.info("Ignoring onNewJobModel invocation since the current state is {} and not {}.", state, State.IN_REBALANCE);
  }
 }
}
origin: org.apache.samza/samza-core_2.10

@VisibleForTesting
SamzaContainer createSamzaContainer(String processorId, JobModel jobModel) {
 return SamzaContainer.apply(processorId, jobModel, ScalaJavaUtil.toScalaMap(this.customMetricsReporter),
   this.taskFactory, JobContextImpl.fromConfigWithDefaults(this.config),
   Option.apply(this.applicationDefinedContainerContextFactoryOptional.orElse(null)),
   Option.apply(this.applicationDefinedTaskContextFactoryOptional.orElse(null)));
}
origin: apache/samza

/**
 * This method returns the number of tasks in the job. It works by querying the server, and getting the job model.
 * Then it extracts the number of tasks from the job model
 *
 * @return current number of tasks in the job
 */
public int getCurrentNumTasks() {
 int currentNumTasks = 0;
 for (ContainerModel containerModel : SamzaContainer.readJobModel(coordinatorServerURL, defaultReadJobModelDelayMs).getContainers().values()) {
  currentNumTasks += containerModel.getTasks().size();
 }
 return currentNumTasks;
}
origin: org.apache.samza/samza-core_2.12

  .createInstance(new ProcessorContext() { }, config);
container.setContainerListener(
  new SamzaContainerListener() {
   @Override
container.run();
if (heartbeatMonitor != null) {
 heartbeatMonitor.stop();
origin: org.apache.samza/samza-core

public static void main(String[] args) throws Exception {
 Thread.setDefaultUncaughtExceptionHandler(
   new SamzaUncaughtExceptionHandler(() -> {
    log.info("Exiting process now.");
    System.exit(1);
   }));
 String containerId = System.getenv(ShellCommandConfig.ENV_CONTAINER_ID());
 log.info(String.format("Got container ID: %s", containerId));
 System.out.println(String.format("Container ID: %s", containerId));
 String coordinatorUrl = System.getenv(ShellCommandConfig.ENV_COORDINATOR_URL());
 log.info(String.format("Got coordinator URL: %s", coordinatorUrl));
 System.out.println(String.format("Coordinator URL: %s", coordinatorUrl));
 int delay = new Random().nextInt(SamzaContainer.DEFAULT_READ_JOBMODEL_DELAY_MS()) + 1;
 JobModel jobModel = SamzaContainer.readJobModel(coordinatorUrl, delay);
 Config config = jobModel.getConfig();
 JobConfig jobConfig = new JobConfig(config);
 if (jobConfig.getName().isEmpty()) {
  throw new SamzaException("can not find the job name");
 }
 String jobName = jobConfig.getName().get();
 String jobId = jobConfig.getJobId();
 MDC.put("containerName", "samza-container-" + containerId);
 MDC.put("jobName", jobName);
 MDC.put("jobId", jobId);
 ApplicationDescriptorImpl<? extends ApplicationDescriptor> appDesc =
   ApplicationDescriptorUtil.getAppDescriptor(ApplicationUtil.fromConfig(config), config);
 run(appDesc, containerId, jobModel, config);
 System.exit(0);
}
org.apache.samza.containerSamzaContainer

Most used methods

  • readJobModel
  • run
  • setContainerListener
  • shutdown
  • DEFAULT_READ_JOBMODEL_DELAY_MS
  • apply
  • getStatus
  • hasStopped

Popular in Java

  • Running tasks concurrently on multiple threads
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • getSupportFragmentManager (FragmentActivity)
  • findViewById (Activity)
  • InputStream (java.io)
    A readable source of bytes.Most clients will use input streams that read data from the file system (
  • NumberFormat (java.text)
    The abstract base class for all number formats. This class provides the interface for formatting and
  • Timer (java.util)
    Timers schedule one-shot or recurring TimerTask for execution. Prefer java.util.concurrent.Scheduled
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • SSLHandshakeException (javax.net.ssl)
    The exception that is thrown when a handshake could not be completed successfully.
  • BoxLayout (javax.swing)
  • Top plugins for WebStorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now