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);
}
}
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;
}
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));
}
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);
}
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);
}
}
Aggregations