Search in sources :

Example 1 with SynchronousLogStream

use of io.camunda.zeebe.logstreams.util.SynchronousLogStream in project zeebe by zeebe-io.

the class TestStreams method writeBatch.

public long writeBatch(final String logName, final RecordToWrite[] recordToWrites) {
    final SynchronousLogStream logStream = getLogStream(logName);
    final LogStreamBatchWriter logStreamBatchWriter = logStream.newLogStreamBatchWriter();
    for (final RecordToWrite recordToWrite : recordToWrites) {
        logStreamBatchWriter.event().key(recordToWrite.getKey()).sourceIndex(recordToWrite.getSourceIndex()).metadataWriter(recordToWrite.getRecordMetadata()).valueWriter(recordToWrite.getUnifiedRecordValue()).done();
    }
    return logStreamBatchWriter.tryWrite();
}
Also used : SynchronousLogStream(io.camunda.zeebe.logstreams.util.SynchronousLogStream) LogStreamBatchWriter(io.camunda.zeebe.logstreams.log.LogStreamBatchWriter)

Example 2 with SynchronousLogStream

use of io.camunda.zeebe.logstreams.util.SynchronousLogStream in project zeebe by zeebe-io.

the class TestStreams method buildStreamProcessor.

private StreamProcessor buildStreamProcessor(final SynchronousLogStream stream, final ZeebeDbFactory zeebeDbFactory, final TypedRecordProcessorFactory factory, final boolean awaitOpening) {
    final var storage = createRuntimeFolder(stream);
    final var snapshot = storage.getParent().resolve(SNAPSHOT_FOLDER);
    final var recoveredLatch = new CountDownLatch(1);
    final var recoveredAwaiter = new StreamProcessorLifecycleAware() {

        @Override
        public void onRecovered(final ReadonlyProcessingContext context) {
            recoveredLatch.countDown();
        }
    };
    final TypedRecordProcessorFactory wrappedFactory = (ctx) -> factory.createProcessors(ctx).withListener(recoveredAwaiter);
    final ZeebeDb<?> zeebeDb;
    if (snapshotWasTaken) {
        zeebeDb = zeebeDbFactory.createDb(snapshot.toFile());
    } else {
        zeebeDb = zeebeDbFactory.createDb(storage.toFile());
    }
    final String logName = stream.getLogName();
    final StreamProcessor streamProcessor = StreamProcessor.builder().logStream(stream.getAsyncLogStream()).zeebeDb(zeebeDb).actorSchedulingService(actorScheduler).commandResponseWriter(mockCommandResponseWriter).listener(mockStreamProcessorListener).streamProcessorFactory(wrappedFactory).eventApplierFactory(eventApplierFactory).streamProcessorMode(streamProcessorMode).build();
    final var openFuture = streamProcessor.openAsync(false);
    if (awaitOpening) {
        // and recovery
        try {
            recoveredLatch.await(15, TimeUnit.SECONDS);
        } catch (final InterruptedException e) {
            Thread.interrupted();
        }
    }
    openFuture.join(15, TimeUnit.SECONDS);
    final LogContext context = logContextMap.get(logName);
    final ProcessorContext processorContext = ProcessorContext.createStreamContext(context, streamProcessor, zeebeDb, storage, snapshot);
    streamContextMap.put(logName, processorContext);
    closeables.manage(processorContext);
    return streamProcessor;
}
Also used : StreamProcessorListener(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorListener) UnpackedObject(io.camunda.zeebe.msgpack.UnpackedObject) Protocol(io.camunda.zeebe.protocol.Protocol) RecordType(io.camunda.zeebe.protocol.record.RecordType) AutoCloseableRule(io.camunda.zeebe.test.util.AutoCloseableRule) CommandResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter) LogStreamRecordWriter(io.camunda.zeebe.logstreams.log.LogStreamRecordWriter) LogStreamReader(io.camunda.zeebe.logstreams.log.LogStreamReader) RecordMetadata(io.camunda.zeebe.protocol.impl.record.RecordMetadata) Map(java.util.Map) Path(java.nio.file.Path) StreamProcessorMode(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorMode) TypedEventRegistry(io.camunda.zeebe.engine.processing.streamprocessor.TypedEventRegistry) LogStorage(io.camunda.zeebe.logstreams.storage.LogStorage) MutableZeebeState(io.camunda.zeebe.engine.state.mutable.MutableZeebeState) EventApplier(io.camunda.zeebe.engine.state.EventApplier) Loggers(io.camunda.zeebe.util.Loggers) TypedRecordProcessorFactory(io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory) UncheckedIOException(java.io.UncheckedIOException) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtil.doRepeatedly(io.camunda.zeebe.test.util.TestUtil.doRepeatedly) Stream(java.util.stream.Stream) CopiedRecord(io.camunda.zeebe.protocol.impl.record.CopiedRecord) EventAppliers(io.camunda.zeebe.engine.state.appliers.EventAppliers) Optional(java.util.Optional) LOG(io.camunda.zeebe.logstreams.impl.log.LogStorageAppender.LOG) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) LogStreamBatchWriter(io.camunda.zeebe.logstreams.log.LogStreamBatchWriter) ValueType(io.camunda.zeebe.protocol.record.ValueType) HashMap(java.util.HashMap) Function(java.util.function.Function) ZeebeDb(io.camunda.zeebe.db.ZeebeDb) SynchronousLogStream(io.camunda.zeebe.logstreams.util.SynchronousLogStream) StreamSupport(java.util.stream.StreamSupport) NoSuchElementException(java.util.NoSuchElementException) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) LoggedEvent(io.camunda.zeebe.logstreams.log.LoggedEvent) FileUtil(io.camunda.zeebe.util.FileUtil) Intent(io.camunda.zeebe.protocol.record.intent.Intent) StreamProcessor(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessor) Files(java.nio.file.Files) SyncLogStream(io.camunda.zeebe.logstreams.util.SyncLogStream) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) ZeebeDbFactory(io.camunda.zeebe.db.ZeebeDbFactory) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ReadonlyProcessingContext(io.camunda.zeebe.engine.processing.streamprocessor.ReadonlyProcessingContext) StreamProcessorLifecycleAware(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorLifecycleAware) TemporaryFolder(org.junit.rules.TemporaryFolder) ActorScheduler(io.camunda.zeebe.util.sched.ActorScheduler) StreamProcessorLifecycleAware(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorLifecycleAware) StreamProcessor(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessor) ReadonlyProcessingContext(io.camunda.zeebe.engine.processing.streamprocessor.ReadonlyProcessingContext) CountDownLatch(java.util.concurrent.CountDownLatch) TypedRecordProcessorFactory(io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory)

Example 3 with SynchronousLogStream

use of io.camunda.zeebe.logstreams.util.SynchronousLogStream in project zeebe by camunda.

the class StreamProcessorRule method printAllRecords.

public void printAllRecords() {
    int partitionId = startPartitionId;
    for (int i = 0; i < partitionCount; i++) {
        final SynchronousLogStream logStream = streams.getLogStream(getLogName(partitionId++));
        LogStreamPrinter.printRecords(logStream);
    }
}
Also used : SynchronousLogStream(io.camunda.zeebe.logstreams.util.SynchronousLogStream)

Example 4 with SynchronousLogStream

use of io.camunda.zeebe.logstreams.util.SynchronousLogStream in project zeebe by camunda.

the class TestStreams method buildStreamProcessor.

private StreamProcessor buildStreamProcessor(final SynchronousLogStream stream, final ZeebeDbFactory zeebeDbFactory, final TypedRecordProcessorFactory factory, final boolean awaitOpening) {
    final var storage = createRuntimeFolder(stream);
    final var snapshot = storage.getParent().resolve(SNAPSHOT_FOLDER);
    final var recoveredLatch = new CountDownLatch(1);
    final var recoveredAwaiter = new StreamProcessorLifecycleAware() {

        @Override
        public void onRecovered(final ReadonlyProcessingContext context) {
            recoveredLatch.countDown();
        }
    };
    final TypedRecordProcessorFactory wrappedFactory = (ctx) -> factory.createProcessors(ctx).withListener(recoveredAwaiter);
    final ZeebeDb<?> zeebeDb;
    if (snapshotWasTaken) {
        zeebeDb = zeebeDbFactory.createDb(snapshot.toFile());
    } else {
        zeebeDb = zeebeDbFactory.createDb(storage.toFile());
    }
    final String logName = stream.getLogName();
    final StreamProcessor streamProcessor = StreamProcessor.builder().logStream(stream.getAsyncLogStream()).zeebeDb(zeebeDb).actorSchedulingService(actorScheduler).commandResponseWriter(mockCommandResponseWriter).listener(mockStreamProcessorListener).streamProcessorFactory(wrappedFactory).eventApplierFactory(eventApplierFactory).streamProcessorMode(streamProcessorMode).build();
    final var openFuture = streamProcessor.openAsync(false);
    if (awaitOpening) {
        // and recovery
        try {
            recoveredLatch.await(15, TimeUnit.SECONDS);
        } catch (final InterruptedException e) {
            Thread.interrupted();
        }
    }
    openFuture.join(15, TimeUnit.SECONDS);
    final LogContext context = logContextMap.get(logName);
    final ProcessorContext processorContext = ProcessorContext.createStreamContext(context, streamProcessor, zeebeDb, storage, snapshot);
    streamContextMap.put(logName, processorContext);
    closeables.manage(processorContext);
    return streamProcessor;
}
Also used : StreamProcessorListener(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorListener) UnpackedObject(io.camunda.zeebe.msgpack.UnpackedObject) Protocol(io.camunda.zeebe.protocol.Protocol) RecordType(io.camunda.zeebe.protocol.record.RecordType) AutoCloseableRule(io.camunda.zeebe.test.util.AutoCloseableRule) CommandResponseWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter) LogStreamRecordWriter(io.camunda.zeebe.logstreams.log.LogStreamRecordWriter) LogStreamReader(io.camunda.zeebe.logstreams.log.LogStreamReader) RecordMetadata(io.camunda.zeebe.protocol.impl.record.RecordMetadata) Map(java.util.Map) Path(java.nio.file.Path) StreamProcessorMode(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorMode) TypedEventRegistry(io.camunda.zeebe.engine.processing.streamprocessor.TypedEventRegistry) LogStorage(io.camunda.zeebe.logstreams.storage.LogStorage) MutableZeebeState(io.camunda.zeebe.engine.state.mutable.MutableZeebeState) EventApplier(io.camunda.zeebe.engine.state.EventApplier) Loggers(io.camunda.zeebe.util.Loggers) TypedRecordProcessorFactory(io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory) UncheckedIOException(java.io.UncheckedIOException) CountDownLatch(java.util.concurrent.CountDownLatch) TestUtil.doRepeatedly(io.camunda.zeebe.test.util.TestUtil.doRepeatedly) Stream(java.util.stream.Stream) CopiedRecord(io.camunda.zeebe.protocol.impl.record.CopiedRecord) EventAppliers(io.camunda.zeebe.engine.state.appliers.EventAppliers) Optional(java.util.Optional) LOG(io.camunda.zeebe.logstreams.impl.log.LogStorageAppender.LOG) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) LogStreamBatchWriter(io.camunda.zeebe.logstreams.log.LogStreamBatchWriter) ValueType(io.camunda.zeebe.protocol.record.ValueType) HashMap(java.util.HashMap) Function(java.util.function.Function) ZeebeDb(io.camunda.zeebe.db.ZeebeDb) SynchronousLogStream(io.camunda.zeebe.logstreams.util.SynchronousLogStream) StreamSupport(java.util.stream.StreamSupport) NoSuchElementException(java.util.NoSuchElementException) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) LoggedEvent(io.camunda.zeebe.logstreams.log.LoggedEvent) FileUtil(io.camunda.zeebe.util.FileUtil) Intent(io.camunda.zeebe.protocol.record.intent.Intent) StreamProcessor(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessor) Files(java.nio.file.Files) SyncLogStream(io.camunda.zeebe.logstreams.util.SyncLogStream) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) ZeebeDbFactory(io.camunda.zeebe.db.ZeebeDbFactory) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ReadonlyProcessingContext(io.camunda.zeebe.engine.processing.streamprocessor.ReadonlyProcessingContext) StreamProcessorLifecycleAware(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorLifecycleAware) TemporaryFolder(org.junit.rules.TemporaryFolder) ActorScheduler(io.camunda.zeebe.util.sched.ActorScheduler) StreamProcessorLifecycleAware(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorLifecycleAware) StreamProcessor(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessor) ReadonlyProcessingContext(io.camunda.zeebe.engine.processing.streamprocessor.ReadonlyProcessingContext) CountDownLatch(java.util.concurrent.CountDownLatch) TypedRecordProcessorFactory(io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory)

Example 5 with SynchronousLogStream

use of io.camunda.zeebe.logstreams.util.SynchronousLogStream in project zeebe by camunda.

the class TestStreams method events.

public Stream<LoggedEvent> events(final String logName) {
    final SynchronousLogStream logStream = getLogStream(logName);
    final LogStreamReader reader = logStream.newLogStreamReader();
    closeables.manage(reader);
    reader.seekToFirstEvent();
    final Iterable<LoggedEvent> iterable = () -> reader;
    return StreamSupport.stream(iterable.spliterator(), false);
}
Also used : LogStreamReader(io.camunda.zeebe.logstreams.log.LogStreamReader) SynchronousLogStream(io.camunda.zeebe.logstreams.util.SynchronousLogStream) LoggedEvent(io.camunda.zeebe.logstreams.log.LoggedEvent)

Aggregations

SynchronousLogStream (io.camunda.zeebe.logstreams.util.SynchronousLogStream)28 LogStreamBatchWriter (io.camunda.zeebe.logstreams.log.LogStreamBatchWriter)10 LogStreamReader (io.camunda.zeebe.logstreams.log.LogStreamReader)8 LoggedEvent (io.camunda.zeebe.logstreams.log.LoggedEvent)8 ZeebeDb (io.camunda.zeebe.db.ZeebeDb)5 ZeebeDbFactory (io.camunda.zeebe.db.ZeebeDbFactory)5 ReadonlyProcessingContext (io.camunda.zeebe.engine.processing.streamprocessor.ReadonlyProcessingContext)5 StreamProcessor (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessor)5 StreamProcessorLifecycleAware (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorLifecycleAware)5 StreamProcessorListener (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorListener)5 StreamProcessorMode (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorMode)5 TypedEventRegistry (io.camunda.zeebe.engine.processing.streamprocessor.TypedEventRegistry)5 TypedRecordProcessorFactory (io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory)5 CommandResponseWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter)5 EventApplier (io.camunda.zeebe.engine.state.EventApplier)5 EventAppliers (io.camunda.zeebe.engine.state.appliers.EventAppliers)5 MutableZeebeState (io.camunda.zeebe.engine.state.mutable.MutableZeebeState)5 LogStreamRecordWriter (io.camunda.zeebe.logstreams.log.LogStreamRecordWriter)5 LogStorage (io.camunda.zeebe.logstreams.storage.LogStorage)5 SyncLogStream (io.camunda.zeebe.logstreams.util.SyncLogStream)5