Search in sources :

Example 1 with RecordNotFoundException

use of org.apache.samza.table.RecordNotFoundException 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)

Aggregations

Collection (java.util.Collection)1 Collections (java.util.Collections)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 SamzaException (org.apache.samza.SamzaException)1 Context (org.apache.samza.context.Context)1 KV (org.apache.samza.operators.KV)1 UpdateMessage (org.apache.samza.operators.UpdateMessage)1 UpdateOptions (org.apache.samza.operators.UpdateOptions)1 OperatorSpec (org.apache.samza.operators.spec.OperatorSpec)1 SendToTableWithUpdateOperatorSpec (org.apache.samza.operators.spec.SendToTableWithUpdateOperatorSpec)1 ReadWriteUpdateTable (org.apache.samza.table.ReadWriteUpdateTable)1 RecordNotFoundException (org.apache.samza.table.RecordNotFoundException)1 CompactBatchProvider (org.apache.samza.table.batching.CompactBatchProvider)1 RemoteTable (org.apache.samza.table.remote.RemoteTable)1 MessageCollector (org.apache.samza.task.MessageCollector)1 TaskCoordinator (org.apache.samza.task.TaskCoordinator)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1