Search in sources :

Example 86 with SamzaException

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

the class JobModelUtil method writeJobModel.

/**
 * Converts the JobModel into a byte array into {@link MetadataStore}.
 * @param jobModel the job model to store into {@link MetadataStore}.
 * @param jobModelVersion the job model version.
 * @param metadataStore the metadata store.
 */
public static void writeJobModel(JobModel jobModel, String jobModelVersion, MetadataStore metadataStore) {
    try {
        String jobModelSerializedAsString = MAPPER.writeValueAsString(jobModel);
        byte[] jobModelSerializedAsBytes = jobModelSerializedAsString.getBytes(UTF_8);
        String metadataStoreKey = getJobModelKey(jobModelVersion);
        metadataStore.put(metadataStoreKey, jobModelSerializedAsBytes);
        metadataStore.flush();
    } catch (Exception e) {
        throw new SamzaException(String.format("Exception occurred when storing JobModel: %s with version: %s.", jobModel, jobModelVersion), e);
    }
}
Also used : SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException)

Example 87 with SamzaException

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

the class MetricsSnapshotReporterFactory method getSystemStream.

protected SystemStream getSystemStream(String reporterName, Config config) {
    MetricsConfig metricsConfig = new MetricsConfig(config);
    String metricsSystemStreamName = metricsConfig.getMetricsSnapshotReporterStream(reporterName).orElseThrow(() -> new SamzaException("No metrics stream defined in config."));
    SystemStream systemStream = StreamUtil.getSystemStreamFromNames(metricsSystemStreamName);
    LOG.info("Got system stream {}.", systemStream);
    return systemStream;
}
Also used : SystemStream(org.apache.samza.system.SystemStream) SamzaException(org.apache.samza.SamzaException) MetricsConfig(org.apache.samza.config.MetricsConfig)

Example 88 with SamzaException

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

the class SendToTableWithUpdateOperatorImpl method handleMessageAsync.

@Override
protected CompletionStage<Collection<KV<K, UpdateMessage<U, V>>>> handleMessageAsync(KV<K, UpdateMessage<U, V>> message, MessageCollector collector, TaskCoordinator coordinator) {
    final UpdateOptions options = spec.getUpdateOptions();
    final CompletableFuture<Void> updateFuture = table.updateAsync(message.getKey(), message.getValue().getUpdate());
    return updateFuture.handle((result, ex) -> {
        if (ex == null) {
            // success, no need to Put a default value
            return false;
        } else if (ex.getCause() instanceof RecordNotFoundException && message.getValue().hasDefault()) {
            // to PUT a default record for the key and then apply the update.
            if (options == UpdateOptions.UPDATE_WITH_DEFAULTS) {
                return true;
            } else {
                throw new SamzaException("Put default failed for update as the Update options was set to " + options + ". Please use UpdateOptions.UPDATE_WITH_DEFAULTS instead.");
            }
        } else {
            throw new SamzaException("Update failed with exception: ", ex);
        }
    }).thenCompose(shouldPutDefault -> {
        if (shouldPutDefault) {
            final CompletableFuture<Void> putFuture = table.putAsync(message.getKey(), message.getValue().getDefault());
            return putFuture.exceptionally(ex -> {
                LOG.warn("PUT default failed due to an exception. Ignoring the exception and proceeding with update. " + "The exception encountered is: ", ex);
                return null;
            }).thenCompose(res -> table.updateAsync(message.getKey(), message.getValue().getUpdate())).exceptionally(ex -> {
                throw new SamzaException("Update after Put default failed with exception: ", ex);
            });
        } else {
            return CompletableFuture.completedFuture(null);
        }
    }).thenApply(result -> Collections.singleton(message));
}
Also used : Logger(org.slf4j.Logger) UpdateMessage(org.apache.samza.operators.UpdateMessage) SendToTableWithUpdateOperatorSpec(org.apache.samza.operators.spec.SendToTableWithUpdateOperatorSpec) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Context(org.apache.samza.context.Context) RecordNotFoundException(org.apache.samza.table.RecordNotFoundException) RemoteTable(org.apache.samza.table.remote.RemoteTable) CompletionStage(java.util.concurrent.CompletionStage) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) MessageCollector(org.apache.samza.task.MessageCollector) UpdateOptions(org.apache.samza.operators.UpdateOptions) CompactBatchProvider(org.apache.samza.table.batching.CompactBatchProvider) Collections(java.util.Collections) KV(org.apache.samza.operators.KV) RecordNotFoundException(org.apache.samza.table.RecordNotFoundException) CompletableFuture(java.util.concurrent.CompletableFuture) SamzaException(org.apache.samza.SamzaException) UpdateOptions(org.apache.samza.operators.UpdateOptions)

Example 89 with SamzaException

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

the class TimeSeriesKeySerde method fromBytes.

@Override
public TimeSeriesKey<K> fromBytes(byte[] timeSeriesKeyBytes) {
    // First obtain the key bytes, and deserialize them. Later de-serialize the timestamp and sequence number
    ByteBuffer buf = ByteBuffer.wrap(timeSeriesKeyBytes);
    int keySize = timeSeriesKeyBytes.length - TIMESTAMP_SIZE - SEQNUM_SIZE;
    K key = null;
    if (keySize != 0) {
        byte[] keyBytes = new byte[keySize];
        buf.get(keyBytes);
        key = keySerde.fromBytes(keyBytes);
    }
    long timeStamp = buf.getLong();
    long seqNum = buf.getLong();
    long version = seqNum & ~SEQUENCE_NUM_MASK;
    if (version != TimeSeriesKey.VERSION) {
        throw new SamzaException(String.format("Invalid version detected in TimeSeriesKey. " + "Expected Version: %s Actual Version: %s Sequence number: %s", TimeSeriesKey.VERSION, version, seqNum));
    }
    return new TimeSeriesKey(key, timeStamp, seqNum);
}
Also used : ByteBuffer(java.nio.ByteBuffer) SamzaException(org.apache.samza.SamzaException)

Example 90 with SamzaException

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

the class MockCoordinatorStreamWrappedConsumer method convertConfigToCoordinatorMessage.

private void convertConfigToCoordinatorMessage(Config config) {
    try {
        for (Map.Entry<String, String> configPair : config.entrySet()) {
            byte[] keyBytes;
            byte[] messgeBytes;
            if (configPair.getKey().startsWith(CHANGELOGPREFIX)) {
                String[] changelogInfo = configPair.getKey().split(":");
                String changeLogPartition = configPair.getValue();
                SetChangelogMapping changelogMapping = new SetChangelogMapping(changelogInfo[1], changelogInfo[2], Integer.parseInt(changeLogPartition));
                keyBytes = MAPPER.writeValueAsString(changelogMapping.getKeyArray()).getBytes("UTF-8");
                messgeBytes = MAPPER.writeValueAsString(changelogMapping.getMessageMap()).getBytes("UTF-8");
            } else {
                SetConfig setConfig = new SetConfig("source", configPair.getKey(), configPair.getValue());
                keyBytes = MAPPER.writeValueAsString(setConfig.getKeyArray()).getBytes("UTF-8");
                messgeBytes = MAPPER.writeValueAsString(setConfig.getMessageMap()).getBytes("UTF-8");
            }
            // The ssp here is the coordinator ssp (which is always fixed) and not the task ssp.
            put(systemStreamPartition, new IncomingMessageEnvelope(systemStreamPartition, "", keyBytes, messgeBytes));
        }
        setIsAtHead(systemStreamPartition, true);
    } catch (Exception e) {
        throw new SamzaException(e);
    }
}
Also used : IncomingMessageEnvelope(org.apache.samza.system.IncomingMessageEnvelope) SetChangelogMapping(org.apache.samza.coordinator.stream.messages.SetChangelogMapping) SetConfig(org.apache.samza.coordinator.stream.messages.SetConfig) BlockingEnvelopeMap(org.apache.samza.util.BlockingEnvelopeMap) Map(java.util.Map) SamzaException(org.apache.samza.SamzaException) IOException(java.io.IOException) SamzaException(org.apache.samza.SamzaException)

Aggregations

SamzaException (org.apache.samza.SamzaException)256 IOException (java.io.IOException)61 HashMap (java.util.HashMap)57 Test (org.junit.Test)40 Map (java.util.Map)38 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)34 ArrayList (java.util.ArrayList)30 List (java.util.List)27 File (java.io.File)26 JobConfig (org.apache.samza.config.JobConfig)26 Config (org.apache.samza.config.Config)25 VisibleForTesting (com.google.common.annotations.VisibleForTesting)24 SystemStream (org.apache.samza.system.SystemStream)24 CompletableFuture (java.util.concurrent.CompletableFuture)23 Logger (org.slf4j.Logger)21 LoggerFactory (org.slf4j.LoggerFactory)21 Set (java.util.Set)20 Collections (java.util.Collections)19 MapConfig (org.apache.samza.config.MapConfig)18 TaskName (org.apache.samza.container.TaskName)18