Search in sources :

Example 81 with SamzaException

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

the class LocalJobPlanner method createStreams.

/**
 * Create intermediate streams using {@link StreamManager}.
 * If {@link CoordinationUtils} is provided, this function will first invoke leader election, and the leader
 * will create the streams. All the runner processes will wait on the latch that is released after the leader finishes
 * stream creation.
 * @param planId a unique identifier representing the plan used for coordination purpose
 * @param intStreams list of intermediate {@link StreamSpec}s
 * @param streamManager the {@link StreamManager} used to create streams
 */
private void createStreams(String planId, List<StreamSpec> intStreams, StreamManager streamManager) {
    if (intStreams.isEmpty()) {
        LOG.info("Set of intermediate streams is empty. Nothing to create.");
        return;
    }
    LOG.info("A single processor must create the intermediate streams. Processor {} will attempt to acquire the lock.", processorId);
    // Refer SAMZA-1385 for more details
    if (coordinationUtils == null) {
        LOG.warn("Processor {} failed to create utils. Each processor will attempt to create streams.", processorId);
        // each application process will try creating the streams, which
        // requires stream creation to be idempotent
        streamManager.createStreams(intStreams);
        return;
    }
    // If BATCH, then need to create new intermediate streams every run.
    // planId does not change every run and hence, need to use runid
    // as the lockId to create a new lock with state each run
    // to create new streams each run.
    // If run.id is null, defaults to old behavior of using planId
    boolean isAppModeBatch = new ApplicationConfig(userConfig).getAppMode() == ApplicationConfig.ApplicationMode.BATCH;
    String lockId = planId;
    if (isAppModeBatch && runId != null) {
        lockId = runId;
    }
    try {
        checkAndCreateStreams(lockId, intStreams, streamManager);
    } catch (TimeoutException te) {
        throw new SamzaException(String.format("Processor {} failed to get the lock for stream initialization within timeout.", processorId), te);
    } finally {
        if (!isAppModeBatch && coordinationUtils != null) {
            coordinationUtils.close();
        }
    }
}
Also used : ApplicationConfig(org.apache.samza.config.ApplicationConfig) SamzaException(org.apache.samza.SamzaException) TimeoutException(java.util.concurrent.TimeoutException)

Example 82 with SamzaException

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

the class LocalJobPlanner method checkAndCreateStreams.

private void checkAndCreateStreams(String lockId, List<StreamSpec> intStreams, StreamManager streamManager) throws TimeoutException {
    MetadataStore metadataStore = getMetadataStore();
    DistributedLock distributedLock = coordinationUtils.getLock(lockId);
    if (distributedLock == null || metadataStore == null) {
        LOG.warn("Processor {} failed to create utils. Each processor will attempt to create streams.", processorId);
        // each application process will try creating the streams, which requires stream creation to be idempotent
        streamManager.createStreams(intStreams);
        return;
    }
    // Start timer for timeout
    long startTime = System.currentTimeMillis();
    long lockTimeout = TimeUnit.MILLISECONDS.convert(CoordinationConstants.LOCK_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    // does not die of timeout exception and comes back and checks for state and proceeds
    while ((System.currentTimeMillis() - startTime) < lockTimeout) {
        if (metadataStore.get(String.format(STREAM_CREATED_STATE_KEY, lockId)) != null) {
            LOG.info("Processor {} found streams created state data. They must've been created by another processor.", processorId);
            break;
        }
        try {
            if (distributedLock.lock(Duration.ofMillis(10000))) {
                LOG.info("lock acquired for streams creation by Processor " + processorId);
                streamManager.createStreams(intStreams);
                String streamCreatedMessage = "Streams created by processor " + processorId;
                metadataStore.put(String.format(STREAM_CREATED_STATE_KEY, lockId), streamCreatedMessage.getBytes("UTF-8"));
                metadataStore.flush();
                distributedLock.unlock();
                break;
            } else {
                LOG.info("Processor {} failed to get the lock for stream initialization. Will try again until time out", processorId);
            }
        } catch (UnsupportedEncodingException e) {
            String msg = String.format("Processor {} failed to encode string for stream initialization", processorId);
            throw new SamzaException(msg, e);
        }
    }
    if ((System.currentTimeMillis() - startTime) >= lockTimeout) {
        throw new TimeoutException(String.format("Processor {} failed to get the lock for stream initialization within {} milliseconds.", processorId, CoordinationConstants.LOCK_TIMEOUT_MS));
    }
}
Also used : MetadataStore(org.apache.samza.metadatastore.MetadataStore) DistributedLock(org.apache.samza.coordinator.DistributedLock) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SamzaException(org.apache.samza.SamzaException) TimeoutException(java.util.concurrent.TimeoutException)

Example 83 with SamzaException

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

the class OperatorImpl method onMessageAsync.

public final CompletionStage<Void> onMessageAsync(M message, MessageCollector collector, TaskCoordinator coordinator) {
    this.numMessage.inc();
    long startNs = this.highResClock.nanoTime();
    CompletionStage<Collection<RM>> completableResultsFuture;
    try {
        completableResultsFuture = handleMessageAsync(message, collector, coordinator);
    } catch (ClassCastException e) {
        String actualType = e.getMessage().replaceFirst(" cannot be cast to .*", "");
        String expectedType = e.getMessage().replaceFirst(".* cannot be cast to ", "");
        throw new SamzaException(String.format("Error applying operator %s (created at %s) to its input message. " + "Expected input message to be of type %s, but found it to be of type %s. " + "Are Serdes for the inputs to this operator configured correctly?", getOpImplId(), getOperatorSpec().getSourceLocation(), expectedType, actualType), e);
    }
    CompletionStage<Void> result = completableResultsFuture.thenCompose(results -> {
        long endNs = this.highResClock.nanoTime();
        this.handleMessageNs.update(endNs - startNs);
        return CompletableFuture.allOf(results.stream().flatMap(r -> this.registeredOperators.stream().map(op -> op.onMessageAsync(r, collector, coordinator))).toArray(CompletableFuture[]::new));
    });
    WatermarkFunction watermarkFn = getOperatorSpec().getWatermarkFn();
    if (watermarkFn != null) {
        // check whether there is new watermark emitted from the user function
        Long outputWm = watermarkFn.getOutputWatermark();
        return result.thenCompose(ignored -> propagateWatermark(outputWm, collector, coordinator));
    }
    return result;
}
Also used : ScheduledFunction(org.apache.samza.operators.functions.ScheduledFunction) MetricsConfig(org.apache.samza.config.MetricsConfig) LoggerFactory(org.slf4j.LoggerFactory) JobConfig(org.apache.samza.config.JobConfig) CompletableFuture(java.util.concurrent.CompletableFuture) TaskModel(org.apache.samza.job.model.TaskModel) SystemStreamPartition(org.apache.samza.system.SystemStreamPartition) TaskContext(org.apache.samza.context.TaskContext) WatermarkFunction(org.apache.samza.operators.functions.WatermarkFunction) Counter(org.apache.samza.metrics.Counter) OperatorSpec(org.apache.samza.operators.spec.OperatorSpec) CallbackScheduler(org.apache.samza.scheduler.CallbackScheduler) MessageCollector(org.apache.samza.task.MessageCollector) SystemStream(org.apache.samza.system.SystemStream) WatermarkMessage(org.apache.samza.system.WatermarkMessage) HighResolutionClock(org.apache.samza.util.HighResolutionClock) LinkedHashSet(java.util.LinkedHashSet) TaskName(org.apache.samza.container.TaskName) Logger(org.slf4j.Logger) Timer(org.apache.samza.metrics.Timer) Collection(java.util.Collection) ContainerContext(org.apache.samza.context.ContainerContext) Set(java.util.Set) Scheduler(org.apache.samza.operators.Scheduler) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) SamzaException(org.apache.samza.SamzaException) TaskCoordinator(org.apache.samza.task.TaskCoordinator) Context(org.apache.samza.context.Context) CompletionStage(java.util.concurrent.CompletionStage) EndOfStreamMessage(org.apache.samza.system.EndOfStreamMessage) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Config(org.apache.samza.config.Config) Collections(java.util.Collections) InternalTaskContext(org.apache.samza.context.InternalTaskContext) CompletableFuture(java.util.concurrent.CompletableFuture) WatermarkFunction(org.apache.samza.operators.functions.WatermarkFunction) Collection(java.util.Collection) SamzaException(org.apache.samza.SamzaException)

Example 84 with SamzaException

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

the class MetricsSnapshotReporter method stop.

@Override
public void stop() {
    // Scheduling an event with 0 delay to ensure flushing of metrics one last time before shutdown
    this.executor.schedule(this, 0, TimeUnit.SECONDS);
    LOG.info("Stopping reporter timer.");
    // Allow the scheduled task above to finish, and block for termination (for max 60 seconds)
    this.executor.shutdown();
    try {
        this.executor.awaitTermination(60, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        throw new SamzaException(e);
    }
    LOG.info("Stopping producer.");
    this.producer.stop();
    if (!this.executor.isTerminated()) {
        LOG.warn("Unable to shutdown reporter timer.");
    }
}
Also used : SamzaException(org.apache.samza.SamzaException)

Example 85 with SamzaException

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

the class JobCoordinatorMetadataManager method writeJobCoordinatorMetadata.

/**
 * Persist the {@link JobCoordinatorMetadata} in metadata store. The job coordinator metadata is associated
 * with the cluster type specified at the creation of the manager.
 *
 * @param metadata metadata to be persisted
 *
 * @throws SamzaException in case of exception encountered during the writes to underlying metadata store
 */
public void writeJobCoordinatorMetadata(JobCoordinatorMetadata metadata) {
    Preconditions.checkNotNull(metadata, "Job coordinator metadata cannot be null");
    try {
        String metadataValueString = metadataMapper.writeValueAsString(metadata);
        metadataStore.put(clusterType.name(), valueSerde.toBytes(metadataValueString));
        LOG.info("Successfully written job coordinator metadata: {} for cluster {}.", metadata, clusterType);
    } catch (Exception e) {
        metrics.incrementMetadataWriteFailedCount();
        LOG.error("Failed to write the job coordinator metadata to metadata store due to ", e);
        throw new SamzaException("Failed to write the job coordinator metadata.", e);
    }
}
Also used : SamzaException(org.apache.samza.SamzaException) 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