All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.transactions.log.EventBusPersistedLogger Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021 Couchbase, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.couchbase.transactions.log;

import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.cnc.Event;
import com.couchbase.client.core.cnc.EventBus;
import com.couchbase.transactions.config.MergedTransactionConfig;

import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.function.Consumer;

/**
 * Listens for events on the Couchbase event bus and logs some to the transactions persisted log.
 *
 * @author Graham Pople
 * @since 1.0.0
 */
@Stability.Internal
public class EventBusPersistedLogger implements Consumer, Runnable {
    private final PersistedLogWriter log;
    private final MergedTransactionConfig config;
    private final Thread thread;
    // 2048 arbitrarily chosen, expected to be ample
    private final ArrayBlockingQueue queue = new ArrayBlockingQueue<>(2048);
    private final SimpleEventBusLogger eventBusLogger;

    public EventBusPersistedLogger(EventBus eventBus, PersistedLogWriter log, MergedTransactionConfig config) {
        this.log = log;
        this.config = config;
        this.eventBusLogger = new SimpleEventBusLogger(eventBus);
        eventBus.subscribe(this);

        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void accept(Event event) {
        try {
            if (event instanceof TransactionEvent) {
                queue.add((TransactionEvent) event);
            }
        }
        catch(RuntimeException err) {
            eventBusLogger.debug("Failed to add event bus log event: " + err.toString());
        }
    }

    @Override
    public void run() {
        Long last = System.currentTimeMillis();
        int statsEventsWritten = 0;
        long statsTotalTimeMillis = 0;
        long statsMaxTimeMillis = 0;

        while (true) {
            try {
                TransactionEvent event = queue.take();

                Long now = System.currentTimeMillis();

                if (!event.success()) {
                    // Just to tie together these logs
                    String uuid = "[" + UUID.randomUUID().toString().substring(0, 5) + "] ";

                    log.write("", uuid + event.description()).block();
                    event.logs().forEach(l -> {
                        log.write("", uuid + l.toString());
                    });
                }
                else {
                    log.write("", event.description()).block();
                }

                long timeToLog = System.currentTimeMillis() - now;

                statsTotalTimeMillis += timeToLog;
                statsEventsWritten += 1;
                if (timeToLog > statsMaxTimeMillis) statsMaxTimeMillis = timeToLog;

                if (now - last > 60_000) {
                    long timePerEvent = statsEventsWritten > 0 ? (statsTotalTimeMillis / statsEventsWritten) : 0;
                    log.write("",
                            String.format("Event bus logger stats for last 60 seconds: events=%d, avg time to log event=%d millis, max time for event=%d millis",
                                    statsEventsWritten, timePerEvent, statsMaxTimeMillis));
                    statsEventsWritten = 0;
                    statsTotalTimeMillis = 0;
                    statsMaxTimeMillis = 0;
                }

            } catch (InterruptedException e) {
                eventBusLogger.debug("Failed to read event bus queue: " + e.toString());
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy