smiklosovic commented on code in PR #4356: URL: https://github.com/apache/cassandra/pull/4356#discussion_r2323174176
########## src/java/org/apache/cassandra/db/virtual/ExceptionsTable.java: ########## @@ -0,0 +1,301 @@ +/* + * 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 org.apache.cassandra.db.virtual; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import com.google.common.annotations.VisibleForTesting; + +import org.apache.cassandra.db.DecoratedKey; +import org.apache.cassandra.db.marshal.Int32Type; +import org.apache.cassandra.db.marshal.ListType; +import org.apache.cassandra.db.marshal.TimestampType; +import org.apache.cassandra.db.marshal.UTF8Type; +import org.apache.cassandra.dht.LocalPartitioner; +import org.apache.cassandra.schema.TableMetadata; + +public class ExceptionsTable extends AbstractMutableVirtualTable +{ + public static final String EXCEPTIONS_TABLE_NAME = "uncaught_exceptions"; + public static final String EXCEPTION_CLASS_COLUMN_NAME = "exception_class"; + public static final String EXCEPTION_LOCATION_COLUMN_NAME = "exception_location"; + public static final String COUNT_COLUMN_NAME = "count"; + public static final String LAST_MESSAGE_COLUMN_NAME = "last_message"; + public static final String LAST_STACKTRACE_COLUMN_NAME = "last_stacktrace"; + public static final String LAST_OCCURENCE_COLUMN_NAME = "last_occurence"; + + /** + * Buffer of uncaught exceptions which happened while virtual table was not initialized. + */ + public static final List<ExceptionRow> preInitialisationBuffer = Collections.synchronizedList(new ArrayList<>()); + + // please be sure operations on this structure are thread-safe + @VisibleForTesting + final BoundedMap buffer; + + ExceptionsTable(String keyspace) + { + // for starters capped to 1k, I do not think we need to make this configurable (yet). + this(keyspace, 1000); + } + + ExceptionsTable(String keyspace, int maxSize) + { + super(TableMetadata.builder(keyspace, EXCEPTIONS_TABLE_NAME) + .comment("View into uncaught exceptions") + .kind(TableMetadata.Kind.VIRTUAL) + .partitioner(new LocalPartitioner(UTF8Type.instance)) + .addPartitionKeyColumn(EXCEPTION_CLASS_COLUMN_NAME, UTF8Type.instance) + .addClusteringColumn(EXCEPTION_LOCATION_COLUMN_NAME, UTF8Type.instance) + .addRegularColumn(COUNT_COLUMN_NAME, Int32Type.instance) + .addRegularColumn(LAST_MESSAGE_COLUMN_NAME, UTF8Type.instance) + .addRegularColumn(LAST_STACKTRACE_COLUMN_NAME, ListType.getInstance(UTF8Type.instance, false)) + .addRegularColumn(LAST_OCCURENCE_COLUMN_NAME, TimestampType.instance) + .build()); + + this.buffer = new BoundedMap(maxSize); + } + + /** + * @param exceptionClass exception class of uncaught exception + * @param exceptionLocation location where that exception was thrown + * @param message message of given exception + * @param stackTrace whole stacktrace of given exception + * @param occurenceTime time when given exception ocurred + */ + public void add(String exceptionClass, + String exceptionLocation, + String message, + List<String> stackTrace, + long occurenceTime) + { + synchronized (buffer) + { + ExceptionRow mergeInto = null; + + LinkedHashMap<String, ExceptionRow> exceptionRowWithLocation = buffer.get(exceptionClass); + + if (exceptionRowWithLocation != null) + { + ExceptionRow exceptionRow = exceptionRowWithLocation.get(exceptionLocation); + if (exceptionRow != null) + mergeInto = exceptionRow; + } + + if (mergeInto == null) + { + if (exceptionRowWithLocation == null) + { + // exception class and location can be null for value as we have it as part of keys already + LinkedHashMap<String, ExceptionRow> newLocation = new LinkedHashMap<>(); + newLocation.put(exceptionLocation, new ExceptionRow(null, null, 1, message, stackTrace, occurenceTime)); + buffer.put(exceptionClass, newLocation); + } + else + { + exceptionRowWithLocation.put(exceptionLocation, new ExceptionRow(null, null, 1, message, stackTrace, occurenceTime)); + // not important, can be null + // we need to do this, because if we add into a map which is + // a value of some buffer key, we might exceed the number + // of overall entries in the buffer + buffer.removeEldestEntry(null); + } + } + else + { + mergeInto.count += 1; + mergeInto.message = message; + mergeInto.stackTrace = stackTrace; + mergeInto.occurence = new Date(occurenceTime); + } + } + } + + public synchronized void flush() + { + for (ExceptionRow row : preInitialisationBuffer) + add(row.exceptionClass, row.exceptionLocation, row.message, row.stackTrace, row.occurence.getTime()); + + preInitialisationBuffer.clear(); + } + + @Override + public DataSet data() + { + SimpleDataSet result = new SimpleDataSet(metadata()); + + synchronized (buffer) + { + Iterator<Map.Entry<String, LinkedHashMap<String, ExceptionRow>>> iterator = buffer.entrySet().iterator(); + while (iterator.hasNext()) + { + Map.Entry<String, LinkedHashMap<String, ExceptionRow>> partition = iterator.next(); + for (Map.Entry<String, ExceptionRow> entry : partition.getValue().entrySet()) + populateRow(result, partition.getKey(), entry.getKey(), entry.getValue()); + } + } + + return result; + } + + @Override + public DataSet data(DecoratedKey partitionKey) + { + SimpleDataSet result = new SimpleDataSet(metadata()); + + synchronized (buffer) + { + String exceptionClass = UTF8Type.instance.getSerializer().deserialize(partitionKey.getKey()); + LinkedHashMap<String, ExceptionRow> partition = buffer.get(exceptionClass); + + if (partition != null) + { + for (Map.Entry<String, ExceptionRow> row : partition.entrySet()) + populateRow(result, exceptionClass, row.getKey(), row.getValue()); + } + } + + return result; + } + + private void populateRow(SimpleDataSet result, String exceptionClass, String exceptionLocation, ExceptionRow row) + { + result.row(exceptionClass, exceptionLocation) + .column(COUNT_COLUMN_NAME, row.count) + .column(LAST_MESSAGE_COLUMN_NAME, row.message) + .column(LAST_STACKTRACE_COLUMN_NAME, row.stackTrace) + .column(LAST_OCCURENCE_COLUMN_NAME, row.occurence); + } + + @Override + public void truncate() + { + synchronized (buffer) + { + buffer.clear(); + } + } + + public static class ExceptionRow + { + public String exceptionClass; + public String exceptionLocation; + public int count; + public String message; + public List<String> stackTrace; + public Date occurence; + + /** + * @param exceptionClass exception class of uncaught exception + * @param exceptionLocation location where that exception was thrown + * @param message message of given exception + * @param stackTrace whole stacktrace of given exception + * @param occurenceTime time when given exception ocurred, in milliseconds from epoch + */ + public ExceptionRow(String exceptionClass, + String exceptionLocation, + int count, + String message, + List<String> stackTrace, + long occurenceTime) + { + this.exceptionClass = exceptionClass; + this.exceptionLocation = exceptionLocation; + this.count = count; + this.stackTrace = stackTrace; + this.message = message; + this.occurence = new Date(occurenceTime); + } + + public static List<String> extractStacktrace(StackTraceElement[] stackTraceArray) + { + List<String> result = new ArrayList<>(); + + for (StackTraceElement element : stackTraceArray) + result.add(element.toString()); + + return result; + } + } + + @VisibleForTesting + static class BoundedMap extends LinkedHashMap<String, LinkedHashMap<String, ExceptionRow>> + { + private final int maxSize; + + public BoundedMap(int maxSize) + { + if (maxSize <= 0) + throw new IllegalArgumentException("maxSize has to be bigger than 0"); + + this.maxSize = maxSize; + } + + @Override + protected boolean removeEldestEntry(Map.Entry<String, LinkedHashMap<String, ExceptionRow>> eldest) + { + if (computeSize() > maxSize) + { + String oldestExceptionClass = null; + String oldestExceptionLocation = null; + long oldestLastOccurence = Long.MAX_VALUE; + for (Map.Entry<String, LinkedHashMap<String, ExceptionRow>> entry : entrySet()) + { + for (Map.Entry<String, ExceptionRow> entryInEntry : entry.getValue().entrySet()) + { + long currentLastOccurence = entryInEntry.getValue().occurence.getTime(); + if (currentLastOccurence < oldestLastOccurence) + { + oldestExceptionLocation = entryInEntry.getKey(); + oldestExceptionClass = entry.getKey(); + oldestLastOccurence = currentLastOccurence; + } + } + } + + if (oldestLastOccurence < Long.MAX_VALUE) + { + LinkedHashMap<String, ExceptionRow> aMap = get(oldestExceptionClass); + if (aMap.size() == 1) + remove(oldestExceptionClass); + else + aMap.remove(oldestExceptionLocation); + } + } + + return false; + } + + private int computeSize() Review Comment: i -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

