Search in sources :

Example 1 with LockException

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

the class ProcessorStateManagerTest method shouldThrowLockExceptionIfFailedToLockStateDirectory.

@Test
public void shouldThrowLockExceptionIfFailedToLockStateDirectory() throws Exception {
    final File taskDirectory = stateDirectory.directoryForTask(taskId);
    final FileChannel channel = FileChannel.open(new File(taskDirectory, StateDirectory.LOCK_FILE_NAME).toPath(), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
    // lock the task directory
    final FileLock lock = channel.lock();
    try {
        new ProcessorStateManager(taskId, noPartitions, false, stateDirectory, Collections.<String, String>emptyMap(), changelogReader);
        fail("Should have thrown LockException");
    } catch (final LockException e) {
    // pass
    } finally {
        lock.release();
        channel.close();
    }
}
Also used : LockException(org.apache.kafka.streams.errors.LockException) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) File(java.io.File) Test(org.junit.Test)

Example 2 with LockException

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

the class GlobalStateManagerImpl method initialize.

@Override
public Set<String> initialize(final InternalProcessorContext processorContext) {
    try {
        if (!stateDirectory.lockGlobalState(MAX_LOCK_ATTEMPTS)) {
            throw new LockException(String.format("Failed to lock the global state directory: %s", baseDir));
        }
    } catch (IOException e) {
        throw new LockException(String.format("Failed to lock the global state directory: %s", baseDir));
    }
    try {
        this.checkpointableOffsets.putAll(checkpoint.read());
    } catch (IOException e) {
        try {
            stateDirectory.unlockGlobalState();
        } catch (IOException e1) {
            log.error("failed to unlock the global state directory", e);
        }
        throw new StreamsException("Failed to read checkpoints for global state stores", e);
    }
    final List<StateStore> stateStores = topology.globalStateStores();
    for (final StateStore stateStore : stateStores) {
        globalStoreNames.add(stateStore.name());
        stateStore.init(processorContext, stateStore);
    }
    return Collections.unmodifiableSet(globalStoreNames);
}
Also used : LockException(org.apache.kafka.streams.errors.LockException) StreamsException(org.apache.kafka.streams.errors.StreamsException) StateStore(org.apache.kafka.streams.processor.StateStore) IOException(java.io.IOException)

Aggregations

LockException (org.apache.kafka.streams.errors.LockException)2 File (java.io.File)1 IOException (java.io.IOException)1 FileChannel (java.nio.channels.FileChannel)1 FileLock (java.nio.channels.FileLock)1 StreamsException (org.apache.kafka.streams.errors.StreamsException)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 Test (org.junit.Test)1