Search in sources :

Example 6 with SamzaException

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

the class CoordinatorStreamSystemConsumer method bootstrap.

/**
   * Read all messages from the earliest offset, all the way to the latest.
   * Currently, this method only pays attention to config messages.
   */
public void bootstrap() {
    synchronized (bootstrapLock) {
        // Make a copy so readers aren't affected while we modify the set.
        final LinkedHashSet<CoordinatorStreamMessage> bootstrappedMessages = new LinkedHashSet<>(bootstrappedStreamSet);
        log.info("Bootstrapping configuration from coordinator stream.");
        SystemStreamPartitionIterator iterator = new SystemStreamPartitionIterator(systemConsumer, coordinatorSystemStreamPartition);
        try {
            while (iterator.hasNext()) {
                IncomingMessageEnvelope envelope = iterator.next();
                Object[] keyArray = keySerde.fromBytes((byte[]) envelope.getKey()).toArray();
                Map<String, Object> valueMap = null;
                if (envelope.getMessage() != null) {
                    valueMap = messageSerde.fromBytes((byte[]) envelope.getMessage());
                }
                CoordinatorStreamMessage coordinatorStreamMessage = new CoordinatorStreamMessage(keyArray, valueMap);
                log.debug("Received coordinator stream message: {}", coordinatorStreamMessage);
                // Remove any existing entry. Set.add() does not add if the element already exists.
                if (bootstrappedMessages.remove(coordinatorStreamMessage)) {
                    log.debug("Removed duplicate message: {}", coordinatorStreamMessage);
                }
                bootstrappedMessages.add(coordinatorStreamMessage);
                if (SetConfig.TYPE.equals(coordinatorStreamMessage.getType())) {
                    String configKey = coordinatorStreamMessage.getKey();
                    if (coordinatorStreamMessage.isDelete()) {
                        configMap.remove(configKey);
                    } else {
                        String configValue = new SetConfig(coordinatorStreamMessage).getConfigValue();
                        configMap.put(configKey, configValue);
                    }
                }
            }
            bootstrappedStreamSet = Collections.unmodifiableSet(bootstrappedMessages);
            log.debug("Bootstrapped configuration: {}", configMap);
            isBootstrapped = true;
        } catch (Exception e) {
            throw new SamzaException(e);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) SystemStreamPartitionIterator(org.apache.samza.system.SystemStreamPartitionIterator) IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) SetConfig(org.apache.samza.coordinator.stream.messages.SetConfig) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException)

Example 7 with SamzaException

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

the class CoordinatorStreamSystemConsumer method register.

/**
   * Retrieves the oldest offset in the coordinator stream, and registers the
   * coordinator stream with the SystemConsumer using the earliest offset.
   */
public void register() {
    if (isStarted) {
        log.info("Coordinator stream partition {} has already been registered. Skipping.", coordinatorSystemStreamPartition);
        return;
    }
    log.debug("Attempting to register: {}", coordinatorSystemStreamPartition);
    Set<String> streamNames = new HashSet<String>();
    String streamName = coordinatorSystemStreamPartition.getStream();
    streamNames.add(streamName);
    Map<String, SystemStreamMetadata> systemStreamMetadataMap = systemAdmin.getSystemStreamMetadata(streamNames);
    log.info(String.format("Got metadata %s", systemStreamMetadataMap.toString()));
    if (systemStreamMetadataMap == null) {
        throw new SamzaException("Received a null systemStreamMetadataMap from the systemAdmin. This is illegal.");
    }
    SystemStreamMetadata systemStreamMetadata = systemStreamMetadataMap.get(streamName);
    if (systemStreamMetadata == null) {
        throw new SamzaException("Expected " + streamName + " to be in system stream metadata.");
    }
    SystemStreamPartitionMetadata systemStreamPartitionMetadata = systemStreamMetadata.getSystemStreamPartitionMetadata().get(coordinatorSystemStreamPartition.getPartition());
    if (systemStreamPartitionMetadata == null) {
        throw new SamzaException("Expected metadata for " + coordinatorSystemStreamPartition + " to exist.");
    }
    String startingOffset = systemStreamPartitionMetadata.getOldestOffset();
    log.debug("Registering {} with offset {}", coordinatorSystemStreamPartition, startingOffset);
    systemConsumer.register(coordinatorSystemStreamPartition, startingOffset);
}
Also used : SystemStreamMetadata(org.apache.samza.system.SystemStreamMetadata) SystemStreamPartitionMetadata(org.apache.samza.system.SystemStreamMetadata.SystemStreamPartitionMetadata) SamzaException(org.apache.samza.SamzaException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 8 with SamzaException

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

the class MetricsReporterLoader method getMetricsReporters.

public static Map<String, MetricsReporter> getMetricsReporters(MetricsConfig config, String containerName) {
    Map<String, MetricsReporter> metricsReporters = new HashMap<>();
    for (String metricsReporterName : JavaConverters.seqAsJavaListConverter(config.getMetricReporterNames()).asJava()) {
        String metricsFactoryClass = config.getMetricsFactoryClass(metricsReporterName).get();
        if (metricsFactoryClass == null) {
            throw new SamzaException(String.format("Metrics reporter %s missing .class config", metricsReporterName));
        }
        MetricsReporterFactory metricsReporterFactory = Util.getObj(metricsFactoryClass);
        metricsReporters.put(metricsReporterName, metricsReporterFactory.getMetricsReporter(metricsReporterName, containerName, config));
    }
    return metricsReporters;
}
Also used : HashMap(java.util.HashMap) MetricsReporter(org.apache.samza.metrics.MetricsReporter) MetricsReporterFactory(org.apache.samza.metrics.MetricsReporterFactory) SamzaException(org.apache.samza.SamzaException)

Example 9 with SamzaException

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

the class ZkUtils method getJobModel.

/**
   * get the job model from ZK by version
   * @param jobModelVersion jobModel version to get
   * @return job model for this version
   */
public JobModel getJobModel(String jobModelVersion) {
    LOG.info("read the model ver=" + jobModelVersion + " from " + keyBuilder.getJobModelPath(jobModelVersion));
    Object data = zkClient.readData(keyBuilder.getJobModelPath(jobModelVersion));
    ObjectMapper mmapper = SamzaObjectMapper.getObjectMapper();
    JobModel jm;
    try {
        jm = mmapper.readValue((String) data, JobModel.class);
    } catch (IOException e) {
        throw new SamzaException("failed to read JobModel from ZK", e);
    }
    return jm;
}
Also used : JobModel(org.apache.samza.job.model.JobModel) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException) SamzaObjectMapper(org.apache.samza.serializers.model.SamzaObjectMapper) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 10 with SamzaException

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

the class ZkUtils method publishJobModelVersion.

/**
   * publish the version number of the next JobModel
   * @param oldVersion - used to validate, that no one has changed the version in the meanwhile.
   * @param newVersion - new version.
   */
public void publishJobModelVersion(String oldVersion, String newVersion) {
    Stat stat = new Stat();
    String currentVersion = zkClient.<String>readData(keyBuilder.getJobModelVersionPath(), stat);
    LOG.info("publishing new version: " + newVersion + "; oldVersion = " + oldVersion + "(" + stat.getVersion() + ")");
    if (currentVersion != null && !currentVersion.equals(oldVersion)) {
        throw new SamzaException("Someone changed JobModelVersion while the leader was generating one: expected" + oldVersion + ", got " + currentVersion);
    }
    // data version is the ZK version of the data from the ZK.
    int dataVersion = stat.getVersion();
    try {
        stat = zkClient.writeDataReturnStat(keyBuilder.getJobModelVersionPath(), newVersion, dataVersion);
    } catch (Exception e) {
        String msg = "publish job model version failed for new version = " + newVersion + "; old version = " + oldVersion;
        LOG.error(msg, e);
        throw new SamzaException(msg, e);
    }
    LOG.info("published new version: " + newVersion + "; expected data version = " + (dataVersion + 1) + "(actual data version after update = " + stat.getVersion() + ")");
}
Also used : Stat(org.apache.zookeeper.data.Stat) SamzaException(org.apache.samza.SamzaException) ZkInterruptedException(org.I0Itec.zkclient.exception.ZkInterruptedException) IOException(java.io.IOException) 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