Line data Source code
1 : // Copyright (C) 2018 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.logging; 16 : 17 : import com.google.common.base.MoreObjects; 18 : import com.google.common.collect.ImmutableList; 19 : import java.util.ArrayList; 20 : import java.util.List; 21 : 22 : /** 23 : * Thread-safe store for performance log records. 24 : * 25 : * <p>This class is intended to keep track of performance log records in {@link LoggingContext}. It 26 : * needs to be thread-safe because it gets shared between threads when the logging context is copied 27 : * to another thread (see {@link LoggingContextAwareRunnable} and {@link 28 : * LoggingContextAwareCallable}. In this case the logging contexts of both threads share the same 29 : * instance of this class. This is important since performance log records are processed only at the 30 : * end of a request and performance log records that are created in another thread should not get 31 : * lost. 32 : */ 33 148 : public class MutablePerformanceLogRecords { 34 148 : private final ArrayList<PerformanceLogRecord> performanceLogRecords = new ArrayList<>(); 35 : 36 : public synchronized void add(PerformanceLogRecord record) { 37 3 : performanceLogRecords.add(record); 38 3 : } 39 : 40 : public synchronized void set(List<PerformanceLogRecord> records) { 41 1 : performanceLogRecords.clear(); 42 1 : performanceLogRecords.addAll(records); 43 1 : } 44 : 45 : public synchronized ImmutableList<PerformanceLogRecord> list() { 46 98 : return ImmutableList.copyOf(performanceLogRecords); 47 : } 48 : 49 : public boolean isEmtpy() { 50 15 : return performanceLogRecords.isEmpty(); 51 : } 52 : 53 : @Override 54 : public String toString() { 55 0 : return MoreObjects.toStringHelper(this) 56 0 : .add("performanceLogRecords", performanceLogRecords) 57 0 : .toString(); 58 : } 59 : }