Search in sources :

Example 16 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class AbstractTask method closeStateManager.

/**
 * @throws ProcessorStateException if there is an error while closing the state manager
 * @param writeCheckpoint boolean indicating if a checkpoint file should be written
 */
// visible for testing
void closeStateManager(final boolean writeCheckpoint) throws ProcessorStateException {
    ProcessorStateException exception = null;
    log.trace("Closing state manager");
    try {
        stateMgr.close(writeCheckpoint ? recordCollectorOffsets() : null);
    } catch (final ProcessorStateException e) {
        exception = e;
    } finally {
        try {
            stateDirectory.unlock(id);
        } catch (IOException e) {
            if (exception == null) {
                exception = new ProcessorStateException(String.format("%sFailed to release state dir lock", logPrefix), e);
            }
        }
    }
    if (exception != null) {
        throw exception;
    }
}
Also used : IOException(java.io.IOException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

Example 17 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class GlobalStateManagerImpl method flush.

@Override
public void flush() {
    log.debug("Flushing all global globalStores registered in the state manager");
    for (StateStore store : this.globalStores.values()) {
        try {
            log.trace("Flushing global store={}", store.name());
            store.flush();
        } catch (Exception e) {
            throw new ProcessorStateException(String.format("Failed to flush global state store %s", store.name()), e);
        }
    }
}
Also used : StateStore(org.apache.kafka.streams.processor.StateStore) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) StreamsException(org.apache.kafka.streams.errors.StreamsException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException) InvalidOffsetException(org.apache.kafka.clients.consumer.InvalidOffsetException) LockException(org.apache.kafka.streams.errors.LockException)

Example 18 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.

the class StateDirectory method lock.

/**
 * Get the lock for the {@link TaskId}s directory if it is available
 * @param taskId
 * @return true if successful
 * @throws IOException
 */
synchronized boolean lock(final TaskId taskId) throws IOException {
    final File lockFile;
    // we already have the lock so bail out here
    final LockAndOwner lockAndOwner = locks.get(taskId);
    if (lockAndOwner != null && lockAndOwner.owningThread.equals(Thread.currentThread().getName())) {
        log.trace("{} Found cached state dir lock for task {}", logPrefix(), taskId);
        return true;
    } else if (lockAndOwner != null) {
        // another thread owns the lock
        return false;
    }
    try {
        lockFile = new File(directoryForTask(taskId), LOCK_FILE_NAME);
    } catch (ProcessorStateException e) {
        // has concurrently deleted the directory
        return false;
    }
    final FileChannel channel;
    try {
        channel = getOrCreateFileChannel(taskId, lockFile.toPath());
    } catch (NoSuchFileException e) {
        // file, in this case we will return immediately indicating locking failed.
        return false;
    }
    final FileLock lock = tryLock(channel);
    if (lock != null) {
        locks.put(taskId, new LockAndOwner(Thread.currentThread().getName(), lock));
        log.debug("{} Acquired state dir lock for task {}", logPrefix(), taskId);
    }
    return lock != null;
}
Also used : FileChannel(java.nio.channels.FileChannel) NoSuchFileException(java.nio.file.NoSuchFileException) FileLock(java.nio.channels.FileLock) File(java.io.File) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

Example 19 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class AbstractSegments method openExisting.

@Override
public void openExisting(final ProcessorContext context, final long streamTime) {
    try {
        final File dir = new File(context.stateDir(), name);
        if (dir.exists()) {
            final String[] list = dir.list();
            if (list != null) {
                Arrays.stream(list).map(segment -> segmentIdFromSegmentName(segment, dir)).sorted().filter(segmentId -> segmentId >= 0).forEach(segmentId -> getOrCreateSegment(segmentId, context));
            }
        } else {
            if (!dir.mkdir()) {
                throw new ProcessorStateException(String.format("dir %s doesn't exist and cannot be created for segments %s", dir, name));
            }
        }
    } catch (final Exception ex) {
    // ignore
    }
    final long minLiveSegment = segmentId(streamTime - retentionPeriod);
    cleanupEarlierThan(minLiveSegment);
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) NavigableMap(java.util.NavigableMap) File(java.io.File) ArrayList(java.util.ArrayList) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) List(java.util.List) TreeMap(java.util.TreeMap) Map(java.util.Map) ParseException(java.text.ParseException) SimpleTimeZone(java.util.SimpleTimeZone) File(java.io.File) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException) IOException(java.io.IOException) ParseException(java.text.ParseException)

Example 20 with ProcessorStateException

use of org.apache.kafka.streams.errors.ProcessorStateException in project kafka by apache.

the class AbstractRocksDBSegmentedBytesStore method restoreAllInternal.

// Visible for testing
void restoreAllInternal(final Collection<ConsumerRecord<byte[], byte[]>> records) {
    try {
        final Map<S, WriteBatch> writeBatchMap = getWriteBatches(records);
        for (final Map.Entry<S, WriteBatch> entry : writeBatchMap.entrySet()) {
            final S segment = entry.getKey();
            final WriteBatch batch = entry.getValue();
            segment.write(batch);
            batch.close();
        }
    } catch (final RocksDBException e) {
        throw new ProcessorStateException("Error restoring batch to store " + this.name, e);
    }
}
Also used : RocksDBException(org.rocksdb.RocksDBException) WriteBatch(org.rocksdb.WriteBatch) HashMap(java.util.HashMap) Map(java.util.Map) ProcessorStateException(org.apache.kafka.streams.errors.ProcessorStateException)

Aggregations

ProcessorStateException (org.apache.kafka.streams.errors.ProcessorStateException)68 Test (org.junit.Test)23 IOException (java.io.IOException)19 File (java.io.File)15 RocksDBException (org.rocksdb.RocksDBException)11 StreamsException (org.apache.kafka.streams.errors.StreamsException)7 StateStore (org.apache.kafka.streams.processor.StateStore)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 WriteBatch (org.rocksdb.WriteBatch)5 ParseException (java.text.ParseException)4 Map (java.util.Map)4 MetricName (org.apache.kafka.common.MetricName)4 TopicPartition (org.apache.kafka.common.TopicPartition)4 LockException (org.apache.kafka.streams.errors.LockException)4 MockKeyValueStore (org.apache.kafka.test.MockKeyValueStore)4 MockStateStore (org.apache.kafka.test.MockStateStore)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)3