Search in sources :

Example 36 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TestCoordinatorStreamSystemConsumer method testCoordinatorStreamSystemConsumer.

@Test
public void testCoordinatorStreamSystemConsumer() {
    Map<String, String> expectedConfig = new LinkedHashMap<String, String>();
    expectedConfig.put("job.id", "1234");
    SystemStream systemStream = new SystemStream("system", "stream");
    MockSystemConsumer systemConsumer = new MockSystemConsumer(new SystemStreamPartition(systemStream, new Partition(0)));
    CoordinatorStreamSystemConsumer consumer = new CoordinatorStreamSystemConsumer(systemStream, systemConsumer, new SinglePartitionWithoutOffsetsSystemAdmin());
    assertEquals(0, systemConsumer.getRegisterCount());
    consumer.register();
    assertEquals(1, systemConsumer.getRegisterCount());
    assertFalse(systemConsumer.isStarted());
    consumer.start();
    assertTrue(systemConsumer.isStarted());
    try {
        consumer.getConfig();
        fail("Should have failed when retrieving config before bootstrapping.");
    } catch (SamzaException e) {
    // Expected.
    }
    consumer.bootstrap();
    assertEquals(expectedConfig, consumer.getConfig());
    assertFalse(systemConsumer.isStopped());
    consumer.stop();
    assertTrue(systemConsumer.isStopped());
}
Also used : SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Partition(org.apache.samza.Partition) SystemStream(org.apache.samza.system.SystemStream) SinglePartitionWithoutOffsetsSystemAdmin(org.apache.samza.util.SinglePartitionWithoutOffsetsSystemAdmin) SamzaException(org.apache.samza.SamzaException) LinkedHashMap(java.util.LinkedHashMap) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) Test(org.junit.Test)

Example 37 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class ZkUtils method publishJobModel.

/**
   * Publishes new job model into ZK.
   * This call should FAIL if the node already exists.
   * @param jobModelVersion  version of the jobModeL to publish
   * @param jobModel jobModel to publish
   *
   */
public void publishJobModel(String jobModelVersion, JobModel jobModel) {
    try {
        ObjectMapper mmapper = SamzaObjectMapper.getObjectMapper();
        String jobModelStr = mmapper.writerWithDefaultPrettyPrinter().writeValueAsString(jobModel);
        LOG.info("jobModelAsString=" + jobModelStr);
        zkClient.createPersistent(keyBuilder.getJobModelPath(jobModelVersion), jobModelStr);
        LOG.info("wrote jobModel path =" + keyBuilder.getJobModelPath(jobModelVersion));
    } catch (Exception e) {
        LOG.error("JobModel publish failed for version=" + jobModelVersion, e);
        throw new SamzaException(e);
    }
}
Also used : SamzaException(org.apache.samza.SamzaException) SamzaObjectMapper(org.apache.samza.serializers.model.SamzaObjectMapper) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException)

Example 38 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class AbstractContainerAllocator method runStreamProcessor.

/**
   * Updates the request state and runs a container process on the specified host. Assumes a resource
   * is available on the preferred host, so the caller must verify that before invoking this method.
   *
   * @param request             the {@link SamzaResourceRequest} which is being handled.
   * @param preferredHost       the preferred host on which the StreamProcessor process should be run or
   *                            {@link ResourceRequestState#ANY_HOST} if there is no host preference.
   * @throws
   * SamzaException if there is no allocated resource in the specified host.
   */
protected void runStreamProcessor(SamzaResourceRequest request, String preferredHost) {
    CommandBuilder builder = getCommandBuilder(request.getContainerID());
    // Get the available resource
    SamzaResource resource = peekAllocatedResource(preferredHost);
    if (resource == null)
        throw new SamzaException("Expected resource was unavailable on host " + preferredHost);
    // Update state
    resourceRequestState.updateStateAfterAssignment(request, preferredHost, resource);
    String containerID = request.getContainerID();
    //run container on resource
    log.info("Found available resources on {}. Assigning request for container_id {} with " + "timestamp {} to resource {}", new Object[] { preferredHost, String.valueOf(containerID), request.getRequestTimestampMs(), resource.getResourceID() });
    try {
        //launches a StreamProcessor on the resource
        clusterResourceManager.launchStreamProcessor(resource, builder);
        if (state.neededContainers.decrementAndGet() == 0) {
            state.jobHealthy.set(true);
        }
        state.runningContainers.put(request.getContainerID(), resource);
    } catch (SamzaContainerLaunchException e) {
        log.warn(String.format("Got exception while starting resource %s. Requesting a new resource on any host", resource), e);
        resourceRequestState.releaseUnstartableContainer(resource);
        requestResource(containerID, ResourceRequestState.ANY_HOST);
    }
}
Also used : ShellCommandBuilder(org.apache.samza.job.ShellCommandBuilder) CommandBuilder(org.apache.samza.job.CommandBuilder) SamzaException(org.apache.samza.SamzaException)

Example 39 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class PartitionDescriptorUtil method getDescriptorMapFromJson.

public static Map<Partition, List<String>> getDescriptorMapFromJson(String json) {
    try {
        @SuppressWarnings("unchecked") Map<String, String> rawMap = new ObjectMapper().readValue(json, HashMap.class);
        Map<Partition, List<String>> descriptorMap = new HashMap<>();
        rawMap.forEach((key, value) -> descriptorMap.put(new Partition(Integer.valueOf(key)), getPathsFromString(value)));
        return descriptorMap;
    } catch (IOException | NumberFormatException e) {
        throw new SamzaException("Failed to convert json: " + json, e);
    }
}
Also used : Partition(org.apache.samza.Partition) HashMap(java.util.HashMap) List(java.util.List) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 40 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class PartitionDescriptorUtil method getJsonFromDescriptorMap.

public static String getJsonFromDescriptorMap(Map<Partition, List<String>> descriptorMap) {
    JSONObject out = new JSONObject();
    descriptorMap.forEach((partition, paths) -> {
        String descriptorStr = getStringFromPaths(paths);
        try {
            out.put(String.valueOf(partition.getPartitionId()), descriptorStr);
        } catch (JSONException e) {
            throw new SamzaException(String.format("Invalid description to encode. partition=%s, descriptor=%s", partition, descriptorStr), e);
        }
    });
    try {
        return out.toString(INDENT_FACTOR);
    } catch (JSONException e) {
        throw new SamzaException("Failed to generate json string.", e);
    }
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) JSONException(org.codehaus.jettison.json.JSONException) SamzaException(org.apache.samza.SamzaException)

Aggregations

SamzaException (org.apache.samza.SamzaException)58 IOException (java.io.IOException)15 HashMap (java.util.HashMap)9 Config (org.apache.samza.config.Config)6 Test (org.junit.Test)6 Partition (org.apache.samza.Partition)5 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)5 HashSet (java.util.HashSet)4 JobConfig (org.apache.samza.config.JobConfig)4 MapConfig (org.apache.samza.config.MapConfig)4 Map (java.util.Map)3 ExecutionPlan (org.apache.samza.execution.ExecutionPlan)3 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)3 SystemFactory (org.apache.samza.system.SystemFactory)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 ZkInterruptedException (org.I0Itec.zkclient.exception.ZkInterruptedException)2 Path (org.apache.hadoop.fs.Path)2