Search in sources :

Example 1 with MethodWriterBuilder

use of net.openhft.chronicle.bytes.MethodWriterBuilder in project Chronicle-Queue by OpenHFT.

the class ChronicleReader method execute.

public void execute() {
    long lastObservedTailIndex;
    long highestReachedIndex = 0L;
    boolean isFirstIteration = true;
    boolean retryLastOperation;
    boolean queueHasBeenModified;
    final AtomicLong matchCounter = new AtomicLong(0L);
    do {
        try (final ChronicleQueue queue = createQueue();
            final QueueEntryHandler messageConverter = entryHandlerFactory.get()) {
            final ExcerptTailer tailer = queue.createTailer();
            MessageHistory.set(new VanillaMessageHistory());
            tlTailer = ThreadLocal.withInitial(queue::createTailer);
            do {
                if (highestReachedIndex != 0L) {
                    tailer.moveToIndex(highestReachedIndex);
                }
                final Bytes textConversionTarget = Bytes.elasticByteBuffer();
                try {
                    moveToSpecifiedPosition(queue, tailer, isFirstIteration);
                    lastObservedTailIndex = tailer.index();
                    Consumer<String> messageConsumer = text -> applyFiltersAndLog(text, tailer.index(), matchCounter);
                    BooleanSupplier readOne;
                    if (methodReaderInterface == null) {
                        readOne = () -> readOne(messageConverter, tailer, messageConsumer);
                    } else {
                        // TODO: consider unifying this with messageConverter
                        Bytes<ByteBuffer> bytes = Bytes.elasticHeapByteBuffer(256);
                        Wire wire = wireType.apply(bytes);
                        if (wire instanceof TextWire)
                            ((TextWire) wire).useTextDocuments();
                        MethodWriterBuilder<?> mwb = wire.methodWriterBuilder(methodReaderInterface);
                        if (showMessageHistory)
                            mwb.updateInterceptor((methodName, t) -> {
                                MessageHistory messageHistory = MessageHistory.get();
                                // this is an attempt to recognise that no MH was read and instead the method reader called reset(...) on it
                                if (messageHistory.sources() != 1 || messageHistory.timings() != 1)
                                    bytes.append(messageHistory + System.lineSeparator());
                                return true;
                            });
                        MethodReader methodReader = tailer.methodReader(mwb.build());
                        readOne = () -> {
                            boolean found = methodReader.readOne();
                            if (found)
                                messageConsumer.accept(bytes.toString());
                            bytes.clear();
                            return found;
                        };
                    }
                    while (!Thread.currentThread().isInterrupted()) {
                        boolean found = readOne.getAsBoolean();
                        if (!found) {
                            if (tailInputSource) {
                                pauser.pause();
                            }
                            break;
                        } else {
                            if (matchLimitReached(matchCounter.get())) {
                                break;
                            }
                        }
                        pauser.reset();
                    }
                } finally {
                    textConversionTarget.releaseLast();
                    highestReachedIndex = tailer.index();
                    isFirstIteration = false;
                }
                queueHasBeenModified = queueHasBeenModifiedSinceLastCheck(lastObservedTailIndex);
                retryLastOperation = false;
                if (!running || matchLimitReached(matchCounter.get()))
                    return;
            } while (tailerDirection != BACKWARD && (tailInputSource || queueHasBeenModified));
        } catch (final RuntimeException e) {
            if (e.getCause() != null && e.getCause() instanceof DateTimeParseException) {
                // ignore this error - due to a race condition between
                // the reader creating a Queue (with default roll-cycle due to no files on disk)
                // and the writer appending to the Queue with a non-default roll-cycle
                retryLastOperation = true;
            } else {
                throw e;
            }
        }
    } while (retryLastOperation);
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) FORWARD(net.openhft.chronicle.queue.TailerDirection.FORWARD) BACKWARD(net.openhft.chronicle.queue.TailerDirection.BACKWARD) TailerDirection(net.openhft.chronicle.queue.TailerDirection) NotComparableException(net.openhft.chronicle.queue.impl.single.NotComparableException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Jvm(net.openhft.chronicle.core.Jvm) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) BooleanSupplier(java.util.function.BooleanSupplier) Bytes(net.openhft.chronicle.bytes.Bytes) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) net.openhft.chronicle.wire(net.openhft.chronicle.wire) MethodWriterBuilder(net.openhft.chronicle.bytes.MethodWriterBuilder) ParseException(java.text.ParseException) Path(java.nio.file.Path) Pauser(net.openhft.chronicle.threads.Pauser) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Files(java.nio.file.Files) NO_OP(net.openhft.chronicle.queue.impl.StoreFileListener.NO_OP) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) Consumer(java.util.function.Consumer) SingleChronicleQueueBuilder(net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder) Nullable(org.jetbrains.annotations.Nullable) AtomicLong(java.util.concurrent.atomic.AtomicLong) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) ToolsUtil(net.openhft.chronicle.queue.util.ToolsUtil) Pattern(java.util.regex.Pattern) NotNull(org.jetbrains.annotations.NotNull) BinarySearch(net.openhft.chronicle.queue.impl.single.BinarySearch) BinarySearchComparator(net.openhft.chronicle.queue.reader.comparator.BinarySearchComparator) ByteBuffer(java.nio.ByteBuffer) ExcerptTailer(net.openhft.chronicle.queue.ExcerptTailer) Bytes(net.openhft.chronicle.bytes.Bytes) DateTimeParseException(java.time.format.DateTimeParseException) AtomicLong(java.util.concurrent.atomic.AtomicLong) SingleChronicleQueue(net.openhft.chronicle.queue.impl.single.SingleChronicleQueue) ChronicleQueue(net.openhft.chronicle.queue.ChronicleQueue) BooleanSupplier(java.util.function.BooleanSupplier) MethodReader(net.openhft.chronicle.bytes.MethodReader)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ParseException (java.text.ParseException)1 DateTimeParseException (java.time.format.DateTimeParseException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 BooleanSupplier (java.util.function.BooleanSupplier)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Supplier (java.util.function.Supplier)1 Pattern (java.util.regex.Pattern)1 Bytes (net.openhft.chronicle.bytes.Bytes)1 MethodReader (net.openhft.chronicle.bytes.MethodReader)1 MethodWriterBuilder (net.openhft.chronicle.bytes.MethodWriterBuilder)1 Jvm (net.openhft.chronicle.core.Jvm)1 ChronicleQueue (net.openhft.chronicle.queue.ChronicleQueue)1 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)1 TailerDirection (net.openhft.chronicle.queue.TailerDirection)1