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

com.twitter.distributedlog.stats.ReadAheadExceptionsLogger Maven / Gradle / Ivy

The newest version!
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.twitter.distributedlog.stats;

import org.apache.bookkeeper.stats.StatsLogger;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Stats logger to log exceptions happened in {@link com.twitter.distributedlog.readahead.ReadAheadWorker}.
 * They are counters of exceptions happened on each read ahead phase:
 * `scope`/exceptions/`phase`/`code`. `scope` is the current scope of
 * stats logger, `phase` is the read ahead phase, while `code` is the exception code. Check
 * {@link com.twitter.distributedlog.readahead.ReadAheadPhase} for details about phases and
 * {@link BKExceptionStatsLogger} for details about `code`.
 */
public class ReadAheadExceptionsLogger {

    private final StatsLogger statsLogger;
    private StatsLogger parentExceptionStatsLogger;
    private final ConcurrentMap exceptionStatsLoggers =
            new ConcurrentHashMap();

    public ReadAheadExceptionsLogger(StatsLogger statsLogger) {
        this.statsLogger = statsLogger;
    }

    public BKExceptionStatsLogger getBKExceptionStatsLogger(String phase) {
        // initialize the parent exception stats logger lazily
        if (null == parentExceptionStatsLogger) {
            parentExceptionStatsLogger = statsLogger.scope("exceptions");
        }
        BKExceptionStatsLogger exceptionStatsLogger = exceptionStatsLoggers.get(phase);
        if (null == exceptionStatsLogger) {
            exceptionStatsLogger = new BKExceptionStatsLogger(parentExceptionStatsLogger.scope(phase));
            BKExceptionStatsLogger oldExceptionStatsLogger =
                    exceptionStatsLoggers.putIfAbsent(phase, exceptionStatsLogger);
            if (null != oldExceptionStatsLogger) {
                exceptionStatsLogger = oldExceptionStatsLogger;
            }
        }
        return exceptionStatsLogger;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy