Search in sources :

Example 1 with LogStreamBatchWriter

use of io.camunda.zeebe.logstreams.log.LogStreamBatchWriter 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 LogStreamBatchWriter

use of io.camunda.zeebe.logstreams.log.LogStreamBatchWriter in project zeebe by camunda-cloud.

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 3 with LogStreamBatchWriter

use of io.camunda.zeebe.logstreams.log.LogStreamBatchWriter in project zeebe by zeebe-io.

the class TestStreams method buildStreamProcessor.

public StreamProcessor buildStreamProcessor(final SynchronousLogStream stream, final ZeebeDbFactory zeebeDbFactory, final TypedRecordProcessorFactory factory, final Function<LogStreamBatchWriter, TypedStreamWriter> streamWriterFactory, 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 var builder = StreamProcessor.builder().logStream(stream.getAsyncLogStream()).zeebeDb(zeebeDb).actorSchedulingService(actorScheduler).commandResponseWriter(mockCommandResponseWriter).listener(mockStreamProcessorListener).streamProcessorFactory(wrappedFactory).eventApplierFactory(eventApplierFactory).streamProcessorMode(streamProcessorMode);
    if (streamWriterFactory != null) {
        builder.typedStreamWriterFactory(streamWriterFactory);
    }
    final StreamProcessor streamProcessor = builder.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) Loggers(io.camunda.zeebe.engine.Loggers) Map(java.util.Map) Path(java.nio.file.Path) StreamProcessorMode(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorMode) ListLogStorage(io.camunda.zeebe.logstreams.util.ListLogStorage) 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) 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) 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) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) 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) Logger(org.slf4j.Logger) 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 4 with LogStreamBatchWriter

use of io.camunda.zeebe.logstreams.log.LogStreamBatchWriter in project zeebe by camunda-cloud.

the class TestStreams method setupBatchWriter.

public LogStreamBatchWriter setupBatchWriter(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;
}
Also used : SynchronousLogStream(io.camunda.zeebe.logstreams.util.SynchronousLogStream) LogStreamBatchWriter(io.camunda.zeebe.logstreams.log.LogStreamBatchWriter)

Example 5 with LogStreamBatchWriter

use of io.camunda.zeebe.logstreams.log.LogStreamBatchWriter in project zeebe by camunda-cloud.

the class TestStreams method buildStreamProcessor.

public StreamProcessor buildStreamProcessor(final SynchronousLogStream stream, final ZeebeDbFactory zeebeDbFactory, final TypedRecordProcessorFactory factory, final Function<LogStreamBatchWriter, TypedStreamWriter> streamWriterFactory, 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 var builder = StreamProcessor.builder().logStream(stream.getAsyncLogStream()).zeebeDb(zeebeDb).actorSchedulingService(actorScheduler).commandResponseWriter(mockCommandResponseWriter).listener(mockStreamProcessorListener).streamProcessorFactory(wrappedFactory).eventApplierFactory(eventApplierFactory).streamProcessorMode(streamProcessorMode);
    if (streamWriterFactory != null) {
        builder.typedStreamWriterFactory(streamWriterFactory);
    }
    final StreamProcessor streamProcessor = builder.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) Loggers(io.camunda.zeebe.engine.Loggers) Map(java.util.Map) Path(java.nio.file.Path) StreamProcessorMode(io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorMode) ListLogStorage(io.camunda.zeebe.logstreams.util.ListLogStorage) 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) 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) 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) TypedStreamWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter) 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) Logger(org.slf4j.Logger) 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)

Aggregations

LogStreamBatchWriter (io.camunda.zeebe.logstreams.log.LogStreamBatchWriter)7 SynchronousLogStream (io.camunda.zeebe.logstreams.util.SynchronousLogStream)7 ZeebeDb (io.camunda.zeebe.db.ZeebeDb)2 ZeebeDbFactory (io.camunda.zeebe.db.ZeebeDbFactory)2 Loggers (io.camunda.zeebe.engine.Loggers)2 ReadonlyProcessingContext (io.camunda.zeebe.engine.processing.streamprocessor.ReadonlyProcessingContext)2 StreamProcessor (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessor)2 StreamProcessorLifecycleAware (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorLifecycleAware)2 StreamProcessorListener (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorListener)2 StreamProcessorMode (io.camunda.zeebe.engine.processing.streamprocessor.StreamProcessorMode)2 TypedEventRegistry (io.camunda.zeebe.engine.processing.streamprocessor.TypedEventRegistry)2 TypedRecordProcessorFactory (io.camunda.zeebe.engine.processing.streamprocessor.TypedRecordProcessorFactory)2 CommandResponseWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.CommandResponseWriter)2 TypedStreamWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedStreamWriter)2 EventApplier (io.camunda.zeebe.engine.state.EventApplier)2 EventAppliers (io.camunda.zeebe.engine.state.appliers.EventAppliers)2 MutableZeebeState (io.camunda.zeebe.engine.state.mutable.MutableZeebeState)2 LogStreamReader (io.camunda.zeebe.logstreams.log.LogStreamReader)2 LogStreamRecordWriter (io.camunda.zeebe.logstreams.log.LogStreamRecordWriter)2 LoggedEvent (io.camunda.zeebe.logstreams.log.LoggedEvent)2