You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by "smiklosovic (via GitHub)" <gi...@apache.org> on 2025/09/02 08:57:57 UTC
[PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
smiklosovic opened a new pull request, #4356:
URL: https://github.com/apache/cassandra/pull/4356
Thanks for sending a pull request! Here are some tips if you're new here:
* Ensure you have added or run the [appropriate tests](https://cassandra.apache.org/_/development/testing.html) for your PR.
* Be sure to keep the PR description updated to reflect all changes.
* Write your PR title to summarize what this PR proposes.
* If possible, provide a concise example to reproduce the issue for a faster review.
* Read our [contributor guidelines](https://cassandra.apache.org/_/development/index.html)
* If you're making a documentation change, see our [guide to documentation contribution](https://cassandra.apache.org/_/development/documentation.html)
Commit messages should follow the following format:
```
<One sentence description, usually Jira title or CHANGES.txt summary>
<Optional lengthier description (context on patch)>
patch by <Authors>; reviewed by <Reviewers> for CASSANDRA-#####
Co-authored-by: Name1 <email1>
Co-authored-by: Name2 <email2>
```
The [Cassandra Jira](https://issues.apache.org/jira/projects/CASSANDRA/issues/)
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348343931
##########
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;
Review Comment:
always returning false is not a standard behaviour for this method, so a clarifying comment would be nice to add
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369918474
##########
src/java/org/apache/cassandra/utils/logging/AbstractVirtualTableAppender.java:
##########
@@ -65,32 +64,41 @@ protected <T> T getVirtualTable(Class<T> vtableClass, String tableName)
if (!vt.getClass().equals(vtableClass))
throw new IllegalStateException(String.format("Virtual table %s.%s is not backed by an instance of %s but by %s",
- VIRTUAL_VIEWS,
+ keyspaceName,
tableName,
vtableClass.getName(),
vt.getClass().getName()));
return (T) vt;
}
+ public static <T> T getVirtualTable(Class<T> vtableClass, String tableName)
+ {
+ return getVirtualTable(vtableClass, VIRTUAL_VIEWS, tableName);
+ }
+
/**
* This method adds an event to virtual table, when present.
* When vtable is null, we will attempt to find it among registered ones. Then not found, we add it to internal
* buffer for later processing. This might happen e.g. for logging tables when log events
* were appended via logging framework sooner than registration of virtual tables was done so after they are registered,
* they would miss logging events happened before being so.
*
- * @param vtable vtable to append to
- * @param event event to append to
- * @param tableName table name of virtual table to append to
+ * @param vtableClass class of vtable to append to
+ * @param vtable vtable to append to
+ * @param event event to append to
+ * @param tableName table name of virtual table to append to
* @return vtable or when null, found vtable
*/
- protected AbstractLoggerVirtualTable<?> appendToVirtualTable(AbstractLoggerVirtualTable<?> vtable, LoggingEvent event, String tableName)
+ protected AbstractLoggerVirtualTable<?> appendToVirtualTable(Class<? extends AbstractLoggerVirtualTable<?>> vtableClass,
Review Comment:
should we use a generic to align classes for first 2 parameters and the result value?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369885197
##########
src/java/org/apache/cassandra/db/virtual/ExceptionsTable.java:
##########
@@ -0,0 +1,323 @@
+/*
+ * 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.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;
+import org.apache.cassandra.utils.Clock;
+import org.apache.cassandra.utils.logging.AbstractVirtualTableAppender;
+
+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_OCCURRENCE_COLUMN_NAME = "last_occurrence";
+
+ /**
+ * Buffer of uncaught exceptions which happened while virtual table was not initialized.
+ */
+ public static final List<ExceptionRow> preInitialisationBuffer = Collections.synchronizedList(new ArrayList<>());
+
+ @VisibleForTesting
+ public static volatile ExceptionsTable INSTANCE;
+
+ // 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_OCCURRENCE_COLUMN_NAME, TimestampType.instance)
+ .build());
+
+ this.buffer = new BoundedMap(maxSize);
+ }
+
+ public void flush()
+ {
+ for (ExceptionRow row : preInitialisationBuffer)
+ add(row.exceptionClass, row.exceptionLocation, row.message, row.stackTrace, row.occurrence.getTime());
+
+ preInitialisationBuffer.clear();
+ }
+
+ @Override
+ public DataSet data()
+ {
+ SimpleDataSet result = new SimpleDataSet(metadata());
+
+ synchronized (buffer)
+ {
+ for (Map.Entry<String, LinkedHashMap<String, ExceptionRow>> partition : buffer.entrySet())
+ {
+ 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_OCCURRENCE_COLUMN_NAME, row.occurrence);
+ }
+
+ @Override
+ public void truncate()
+ {
+ synchronized (buffer)
+ {
+ buffer.clear();
+ }
+ }
+
+ public static void persist(Throwable t)
+ {
+ if (INSTANCE == null)
+ INSTANCE = AbstractVirtualTableAppender.getVirtualTable(ExceptionsTable.class, EXCEPTIONS_TABLE_NAME);
+
+ Throwable toPersist = t;
+
+ while (toPersist.getCause() != null)
+ toPersist = toPersist.getCause();
+
+ List<String> stackTrace = ExceptionRow.extractStacktrace(toPersist.getStackTrace());
+ long now = Clock.Global.currentTimeMillis();
+
+ if (INSTANCE != null)
+ {
+ INSTANCE.add(toPersist.getClass().getName(),
+ stackTrace.get(0),
+ toPersist.getMessage(),
+ stackTrace,
+ now);
+ }
+ else
+ {
+ preInitialisationBuffer.add(new ExceptionRow(toPersist.getClass().getName(),
+ stackTrace.get(0),
+ 0,
+ toPersist.getMessage(),
+ stackTrace,
+ now));
+ }
+ }
+
+
+ /**
+ * @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 occurrenceTime time when given exception ocurred
+ */
+ private void add(String exceptionClass,
+ String exceptionLocation,
+ String message,
+ List<String> stackTrace,
+ long occurrenceTime)
+ {
+ synchronized (buffer)
+ {
+ Map<String, ExceptionRow> exceptionRowWithLocation = buffer.computeIfAbsent(exceptionClass, (classToAdd) -> new LinkedHashMap<>());
+ ExceptionRow exceptionRow = exceptionRowWithLocation.get(exceptionLocation);
+ if (exceptionRow == null)
+ {
+ // exception class and location can be null for value as we have it as part of keys already
+ exceptionRow = new ExceptionRow(null, null, 1, message, stackTrace, occurrenceTime);
+ exceptionRowWithLocation.put(exceptionLocation, exceptionRow);
+ // 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
+ {
+ exceptionRow.count += 1;
+ exceptionRow.message = message;
+ exceptionRow.stackTrace = stackTrace;
+ exceptionRow.occurrence = new Date(occurrenceTime);
+ }
+ }
+ }
+
+ // public to make warning go away about public preInitialisationBuffer exposing List<ExceptionRow>
+ // where ExceptionRow would have lower visibility than that.
+ public static final class ExceptionRow
+ {
+ final String exceptionClass;
+ final String exceptionLocation;
+ int count;
+ String message;
+ List<String> stackTrace;
+ Date occurrence;
+
+ /**
+ * @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 occurrenceTime time when given exception ocurred, in milliseconds from epoch
+ */
+ ExceptionRow(String exceptionClass,
+ String exceptionLocation,
+ int count,
+ String message,
+ List<String> stackTrace,
+ long occurrenceTime)
+ {
+ this.exceptionClass = exceptionClass;
+ this.exceptionLocation = exceptionLocation;
+ this.count = count;
+ this.stackTrace = stackTrace;
+ this.message = message;
+ this.occurrence = new Date(occurrenceTime);
+ }
+
+ public static List<String> extractStacktrace(StackTraceElement[] stackTraceArray)
+ {
+ List<String> result = new ArrayList<>(stackTraceArray.length);
+
+ 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 oldestLastOccurrence = Long.MAX_VALUE;
+ for (Map.Entry<String, LinkedHashMap<String, ExceptionRow>> entry : entrySet())
+ {
+ for (Map.Entry<String, ExceptionRow> entryInEntry : entry.getValue().entrySet())
+ {
+ long currentLastOccurrence = entryInEntry.getValue().occurrence.getTime();
+ if (currentLastOccurrence < oldestLastOccurrence)
+ {
+ oldestExceptionLocation = entryInEntry.getKey();
+ oldestExceptionClass = entry.getKey();
+ oldestLastOccurrence = currentLastOccurrence;
+ }
+ }
+ }
+
+ if (oldestLastOccurrence < Long.MAX_VALUE)
+ {
+ LinkedHashMap<String, ExceptionRow> aMap = get(oldestExceptionClass);
+ if (aMap.size() == 1)
+ remove(oldestExceptionClass);
+ else
+ aMap.remove(oldestExceptionLocation);
+ }
+ }
+
+ // always returning false as per method's contract saying that
+ // overrides might modify the map directly but in that case it must return false
+ return false;
+ }
+
+ private int computeSize()
+ {
+ int size = 0;
+
+ for (Map.Entry<String, LinkedHashMap<String, ExceptionRow>> entry : entrySet())
Review Comment:
nit: we can just iterate over values()
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369944693
##########
test/unit/org/apache/cassandra/db/virtual/ExceptionsTableTest.java:
##########
@@ -0,0 +1,295 @@
+/*
+ * 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.LinkedHashMap;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+import java.util.function.Consumer;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.junit.Test;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.UntypedResultSet;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.utils.JVMStabilityInspector;
+
+import static java.lang.String.format;
+import static java.lang.Thread.currentThread;
+import static java.util.stream.Collectors.toList;
+import static org.apache.cassandra.db.virtual.ExceptionsTable.EXCEPTIONS_TABLE_NAME;
+import static org.apache.cassandra.utils.logging.AbstractVirtualTableAppender.getVirtualTable;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ExceptionsTableTest extends CQLTester
+{
+ private static final String KS_NAME = "vts";
+
+ @Test
+ public void testExceptionInterceptionWhileVirtualTableIsNotRegistered()
+ {
+ doWithVTable(100, table ->
+ {
+ JVMStabilityInspector.uncaughtException(currentThread(), new MyUncaughtException("my exception"));
+ JVMStabilityInspector.uncaughtException(currentThread(), new MyUncaughtException2("my exception2"));
+
+ // register after treating exception, so it goes to pre-initialisation buffer first
+ VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+
+ // we have buffer populated with exceptions from time vtable was not initialized yet
+ assertEquals(2, ExceptionsTable.preInitialisationBuffer.size());
+
+ // flush buffer and populate table internally
+ table.flush();
+
+ assertTrue(ExceptionsTable.preInitialisationBuffer.isEmpty());
+
+ ExceptionsTable.BoundedMap buffer = table.buffer;
+ assertEquals(2, buffer.size());
+ });
+ }
+
+ @Test
+ public void testExceptionsTableWhileVirtualTableIsRegistered()
+ {
+ doWithVTable(100, table ->
+ {
+ // register before treating exception to avoid pre-initialisation buffer
+ VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+ ExceptionsTable.INSTANCE = getVirtualTable(ExceptionsTable.class, KS_NAME, EXCEPTIONS_TABLE_NAME);
+
+ JVMStabilityInspector.uncaughtException(currentThread(), new MyUncaughtException("my exception"));
+ JVMStabilityInspector.uncaughtException(currentThread(), new MyUncaughtException2("my exception2"));
+
+ assertTrue(ExceptionsTable.preInitialisationBuffer.isEmpty());
+ ExceptionsTable.BoundedMap buffer = table.buffer;
+ assertEquals(2, buffer.size());
+ });
+ }
+
+ @Test
+ public void testWrappedException()
+ {
+ doWithVTable(100, table ->
+ {
+ // register before treating exception to avoid pre-initialisation buffer
+ VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+ ExceptionsTable.INSTANCE = getVirtualTable(ExceptionsTable.class, KS_NAME, EXCEPTIONS_TABLE_NAME);
+
+ MyUncaughtException2 inner = new MyUncaughtException2("inner");
+ MyUncaughtException myUncaughtException = new MyUncaughtException("outer", inner);
+
+ JVMStabilityInspector.uncaughtException(currentThread(), myUncaughtException);
+
+ assertTrue(ExceptionsTable.preInitialisationBuffer.isEmpty());
+ ExceptionsTable.BoundedMap buffer = table.buffer;
+ assertEquals(1, buffer.size());
+
+ LinkedHashMap<String, ExceptionsTable.ExceptionRow> entry = buffer.get(MyUncaughtException2.class.getName());
+ assertNotNull(entry);
+ ExceptionsTable.ExceptionRow exceptionRow = entry.get(inner.getStackTrace()[0].toString());
+ assertNotNull(exceptionRow);
+ assertEquals("inner", exceptionRow.message);
+ assertEquals(1, exceptionRow.count);
+ });
+ }
+
+ @Test
+ public void testDeduplication()
+ {
+ doWithVTable(100, table ->
+ {
+ // register before treating exception to avoid pre-initialisation buffer
+ VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+ ExceptionsTable.INSTANCE = getVirtualTable(ExceptionsTable.class, KS_NAME, EXCEPTIONS_TABLE_NAME);
+
+ // same type of exception
+
+ MyUncaughtException exception1 = new MyUncaughtException("my exception");
+ MyUncaughtException exception2 = new MyUncaughtException("my exception");
+ MyUncaughtException2 exception3 = new MyUncaughtException2("my second uncaught exception");
+
+ String firstLocation = ExceptionsTable.ExceptionRow.extractStacktrace(exception1.getStackTrace()).get(0);
+ String secondLocation = ExceptionsTable.ExceptionRow.extractStacktrace(exception2.getStackTrace()).get(0);
+ String thirdLocation = ExceptionsTable.ExceptionRow.extractStacktrace(exception3.getStackTrace()).get(0);
+
+ JVMStabilityInspector.uncaughtException(currentThread(), exception1);
+ JVMStabilityInspector.uncaughtException(currentThread(), exception1);
+ JVMStabilityInspector.uncaughtException(currentThread(), exception2);
+ JVMStabilityInspector.uncaughtException(currentThread(), exception3);
+
+ ExceptionsTable.BoundedMap buffer = table.buffer;
+ assertEquals(2, buffer.size());
+
+ LinkedHashMap<String, ExceptionsTable.ExceptionRow> exceptionRow = buffer.get(MyUncaughtException.class.getName());
+ assertNotNull(exceptionRow);
+ ExceptionsTable.ExceptionRow firstLocationRow = exceptionRow.get(firstLocation);
+ assertEquals("my exception", firstLocationRow.message);
+ assertEquals(2, firstLocationRow.count);
+
+ ExceptionsTable.ExceptionRow secondLocationRow = exceptionRow.get(secondLocation);
+ assertEquals("my exception", secondLocationRow.message);
+ assertEquals(1, secondLocationRow.count);
+
+ LinkedHashMap<String, ExceptionsTable.ExceptionRow> exceptionRow2 = buffer.get(MyUncaughtException2.class.getName());
+
+ assertNotNull(exceptionRow2);
+ ExceptionsTable.ExceptionRow firstLocationRow2 = exceptionRow2.get(thirdLocation);
+ assertEquals("my second uncaught exception", firstLocationRow2.message);
+ assertEquals(1, firstLocationRow2.count);
+
+ List<UntypedResultSet.Row> rows = execute(format("SELECT * FROM %s.%s", KS_NAME, EXCEPTIONS_TABLE_NAME)).stream().collect(toList());
+ assertEquals(3, rows.size());
+
+ List<String> lastStacktraceException1 = rows.get(0).getList(ExceptionsTable.LAST_STACKTRACE_COLUMN_NAME, UTF8Type.instance);
+ List<String> lastStacktraceException2 = rows.get(1).getList(ExceptionsTable.LAST_STACKTRACE_COLUMN_NAME, UTF8Type.instance);
+
+ assertTrue(lastStacktraceException1.get(0).contains(ExceptionsTableTest.class.getName()));
+ assertTrue(lastStacktraceException2.get(0).contains(ExceptionsTableTest.class.getName()));
+ });
+ }
+
+ @Test
+ public void testRemovalOfEntryWithOldestOccurence()
+ {
+ doWithVTable(4, table ->
+ {
+ // register before treating exception to avoid pre-initialisation buffer
+ VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+ ExceptionsTable.INSTANCE = getVirtualTable(ExceptionsTable.class, KS_NAME, EXCEPTIONS_TABLE_NAME);
+
+ MyUncaughtException2 myException2 = new MyUncaughtException2("my exception2");
+
+ JVMStabilityInspector.uncaughtException(currentThread(), new MyUncaughtException("my exception"));
+ Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
+ JVMStabilityInspector.uncaughtException(currentThread(), myException2);
+ JVMStabilityInspector.uncaughtException(currentThread(), myException2);
+
+ JVMStabilityInspector.uncaughtException(currentThread(), new RuntimeException("some exception"));
+ JVMStabilityInspector.uncaughtException(currentThread(), new IllegalStateException("some illegal state exception"));
+
+ // we inserted 5th unique exception, so the oldest one, based on the occurence, will be removed
+ // the first one inserted was MyUncaughtException with "my exception" message so that one will not be there anymore
+ JVMStabilityInspector.uncaughtException(currentThread(), new IllegalArgumentException("some illegal state exception"));
+
+ assertEquals(4, table.buffer.size());
+ assertNull(table.buffer.get(MyUncaughtException.class.getName()));
+ assertEquals(4, execute(format("SELECT * FROM %s.%s", KS_NAME, EXCEPTIONS_TABLE_NAME)).stream().count());
+
+ JVMStabilityInspector.uncaughtException(currentThread(), new IllegalArgumentException("some illegal state exception"));
+
+ assertEquals(4, execute(format("SELECT * FROM %s.%s", KS_NAME, EXCEPTIONS_TABLE_NAME)).stream().count());
+ assertNull(table.buffer.get(MyUncaughtException2.class.getName()));
+ });
+ }
+
+ @Test
+ public void testSelection()
+ {
+ doWithVTable(4, table ->
+ {
+ // register before treating exception to avoid pre-initialisation buffer
+ VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+ ExceptionsTable.INSTANCE = getVirtualTable(ExceptionsTable.class, KS_NAME, EXCEPTIONS_TABLE_NAME);
+
+ MyUncaughtException myUncaughtException = new MyUncaughtException("my exception");
+
+ JVMStabilityInspector.uncaughtException(currentThread(), myUncaughtException);
+
+ assertFalse(execute(format("SELECT * FROM %s.%s WHERE exception_class = '%s'",
Review Comment:
maybe matchers are better here, they are more readable and print more clear error messages
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2371722246
##########
src/java/org/apache/cassandra/utils/logging/AbstractVirtualTableAppender.java:
##########
@@ -65,32 +64,41 @@ protected <T> T getVirtualTable(Class<T> vtableClass, String tableName)
if (!vt.getClass().equals(vtableClass))
throw new IllegalStateException(String.format("Virtual table %s.%s is not backed by an instance of %s but by %s",
- VIRTUAL_VIEWS,
+ keyspaceName,
tableName,
vtableClass.getName(),
vt.getClass().getName()));
return (T) vt;
}
+ public static <T> T getVirtualTable(Class<T> vtableClass, String tableName)
+ {
+ return getVirtualTable(vtableClass, VIRTUAL_VIEWS, tableName);
+ }
+
/**
* This method adds an event to virtual table, when present.
* When vtable is null, we will attempt to find it among registered ones. Then not found, we add it to internal
* buffer for later processing. This might happen e.g. for logging tables when log events
* were appended via logging framework sooner than registration of virtual tables was done so after they are registered,
* they would miss logging events happened before being so.
*
- * @param vtable vtable to append to
- * @param event event to append to
- * @param tableName table name of virtual table to append to
+ * @param vtableClass class of vtable to append to
+ * @param vtable vtable to append to
+ * @param event event to append to
+ * @param tableName table name of virtual table to append to
* @return vtable or when null, found vtable
*/
- protected AbstractLoggerVirtualTable<?> appendToVirtualTable(AbstractLoggerVirtualTable<?> vtable, LoggingEvent event, String tableName)
+ protected AbstractLoggerVirtualTable<?> appendToVirtualTable(Class<? extends AbstractLoggerVirtualTable<?>> vtableClass,
Review Comment:
i am not doing this, a lot of work, not related to this change anyway.
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348466413
##########
src/java/org/apache/cassandra/utils/JVMStabilityInspector.java:
##########
@@ -63,11 +66,52 @@ public final class JVMStabilityInspector
// It is used for unit test
public static OnKillHook killerHook;
- private JVMStabilityInspector() {}
+ @VisibleForTesting
+ public static volatile ExceptionsTable exceptionsTable;
+
+ private JVMStabilityInspector()
+ {
+ }
public static void uncaughtException(Thread thread, Throwable t)
{
- try { StorageMetrics.uncaughtExceptions.inc(); } catch (Throwable ignore) { /* might not be initialised */ }
+ try
+ {
+ StorageMetrics.uncaughtExceptions.inc();
+
+ if (exceptionsTable == null)
+ exceptionsTable = AbstractVirtualTableAppender.getVirtualTable(ExceptionsTable.class, EXCEPTIONS_TABLE_NAME);
+
+ Throwable toPersist = t;
+
+ while (toPersist.getCause() != null)
+ toPersist = toPersist.getCause();
+
+ List<String> stackTrace = ExceptionRow.extractStacktrace(toPersist.getStackTrace());
+ long now = Clock.Global.currentTimeMillis();
+
+ if (exceptionsTable != null)
+ {
+ exceptionsTable.add(toPersist.getClass().getName(),
+ stackTrace.get(0),
+ toPersist.getMessage(),
+ stackTrace,
+ now);
+ }
+ else
+ {
+ preInitialisationBuffer.add(new ExceptionRow(toPersist.getClass().getName(),
+ stackTrace.get(0),
+ 0,
+ toPersist.getMessage(),
+ stackTrace,
+ now));
+ }
Review Comment:
what's about moving/encapsulating of this logic into ExceptionsTable
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2354795124
##########
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>>
Review Comment:
yes, agree, sorry for the misguidance
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2352191442
##########
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>>
Review Comment:
@netudima I do not think I can do this, as afterNodeInsertion is internal method of HashMap, which is package protected, so we would need to implement custom java.util.BoundedList etc ... I would not complicate this further.
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2343925440
##########
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";
Review Comment:
no, as you see, `uncaught_exceptions` now.
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369835328
##########
src/java/org/apache/cassandra/db/virtual/ExceptionsTable.java:
##########
@@ -0,0 +1,323 @@
+/*
+ * 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.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;
+import org.apache.cassandra.utils.Clock;
+import org.apache.cassandra.utils.logging.AbstractVirtualTableAppender;
+
+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_OCCURRENCE_COLUMN_NAME = "last_occurrence";
+
+ /**
+ * Buffer of uncaught exceptions which happened while virtual table was not initialized.
+ */
+ public static final List<ExceptionRow> preInitialisationBuffer = Collections.synchronizedList(new ArrayList<>());
+
+ @VisibleForTesting
+ public static volatile ExceptionsTable INSTANCE;
+
+ // 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_OCCURRENCE_COLUMN_NAME, TimestampType.instance)
+ .build());
+
+ this.buffer = new BoundedMap(maxSize);
+ }
+
+ public void flush()
+ {
+ for (ExceptionRow row : preInitialisationBuffer)
+ add(row.exceptionClass, row.exceptionLocation, row.message, row.stackTrace, row.occurrence.getTime());
+
+ preInitialisationBuffer.clear();
+ }
+
+ @Override
+ public DataSet data()
+ {
+ SimpleDataSet result = new SimpleDataSet(metadata());
+
+ synchronized (buffer)
+ {
+ for (Map.Entry<String, LinkedHashMap<String, ExceptionRow>> partition : buffer.entrySet())
+ {
+ 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_OCCURRENCE_COLUMN_NAME, row.occurrence);
+ }
+
+ @Override
+ public void truncate()
+ {
+ synchronized (buffer)
+ {
+ buffer.clear();
+ }
+ }
+
+ public static void persist(Throwable t)
+ {
+ if (INSTANCE == null)
+ INSTANCE = AbstractVirtualTableAppender.getVirtualTable(ExceptionsTable.class, EXCEPTIONS_TABLE_NAME);
+
+ Throwable toPersist = t;
+
+ while (toPersist.getCause() != null)
+ toPersist = toPersist.getCause();
+
+ List<String> stackTrace = ExceptionRow.extractStacktrace(toPersist.getStackTrace());
+ long now = Clock.Global.currentTimeMillis();
+
+ if (INSTANCE != null)
+ {
+ INSTANCE.add(toPersist.getClass().getName(),
+ stackTrace.get(0),
+ toPersist.getMessage(),
+ stackTrace,
+ now);
+ }
+ else
+ {
+ preInitialisationBuffer.add(new ExceptionRow(toPersist.getClass().getName(),
+ stackTrace.get(0),
+ 0,
+ toPersist.getMessage(),
+ stackTrace,
+ now));
+ }
+ }
+
+
+ /**
+ * @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 occurrenceTime time when given exception ocurred
+ */
+ private void add(String exceptionClass,
+ String exceptionLocation,
+ String message,
+ List<String> stackTrace,
+ long occurrenceTime)
+ {
+ synchronized (buffer)
+ {
+ Map<String, ExceptionRow> exceptionRowWithLocation = buffer.computeIfAbsent(exceptionClass, (classToAdd) -> new LinkedHashMap<>());
+ ExceptionRow exceptionRow = exceptionRowWithLocation.get(exceptionLocation);
+ if (exceptionRow == null)
+ {
+ // exception class and location can be null for value as we have it as part of keys already
+ exceptionRow = new ExceptionRow(null, null, 1, message, stackTrace, occurrenceTime);
+ exceptionRowWithLocation.put(exceptionLocation, exceptionRow);
+ // 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
+ {
+ exceptionRow.count += 1;
+ exceptionRow.message = message;
+ exceptionRow.stackTrace = stackTrace;
+ exceptionRow.occurrence = new Date(occurrenceTime);
+ }
+ }
+ }
+
+ // public to make warning go away about public preInitialisationBuffer exposing List<ExceptionRow>
+ // where ExceptionRow would have lower visibility than that.
+ public static final class ExceptionRow
+ {
+ final String exceptionClass;
+ final String exceptionLocation;
+ int count;
+ String message;
+ List<String> stackTrace;
+ Date occurrence;
+
+ /**
+ * @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 occurrenceTime time when given exception ocurred, in milliseconds from epoch
+ */
+ ExceptionRow(String exceptionClass,
+ String exceptionLocation,
+ int count,
+ String message,
+ List<String> stackTrace,
+ long occurrenceTime)
+ {
+ this.exceptionClass = exceptionClass;
+ this.exceptionLocation = exceptionLocation;
+ this.count = count;
+ this.stackTrace = stackTrace;
+ this.message = message;
+ this.occurrence = new Date(occurrenceTime);
+ }
+
+ public static List<String> extractStacktrace(StackTraceElement[] stackTraceArray)
Review Comment:
Nit: probably the method is better to place in the upper class, it is not used in this one and declarations to use it are longer now..
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348408984
##########
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";
Review Comment:
typo: occurence -> occurrence
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348476090
##########
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<>();
Review Comment:
what is about to specify the result size?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348472173
##########
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()
Review Comment:
what is the purpose of synchronized on the method here?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348498715
##########
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>>
Review Comment:
it looks like we do not rely on LinkedHashMap logic, should we just use HashMap (with afterNodeInsertion override) instead or just invoke the cleanup logic manually? It would be more straightforward IMHO..
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348458293
##########
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);
+ }
+ }
Review Comment:
it looks a bit complicated, what's about something like this:
```
Map<String, ExceptionRow> exceptionRowWithLocation = buffer.computeIfAbsent(exceptionClass, (classToAdd) -> new LinkedHashMap<>());
ExceptionRow exceptionRow = exceptionRowWithLocation.get(exceptionLocation);
if (exceptionRow == null)
{
// exception class and location can be null for value as we have it as part of keys already
exceptionRow = new ExceptionRow(null, null, 1, message, stackTrace, occurenceTime);
exceptionRowWithLocation.put(exceptionLocation, exceptionRow);
// 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
{
exceptionRow.count += 1;
exceptionRow.message = message;
exceptionRow.stackTrace = stackTrace;
exceptionRow.occurence = new Date(occurenceTime);
}
```
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic merged PR #4356:
URL: https://github.com/apache/cassandra/pull/4356
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369918474
##########
src/java/org/apache/cassandra/utils/logging/AbstractVirtualTableAppender.java:
##########
@@ -65,32 +64,41 @@ protected <T> T getVirtualTable(Class<T> vtableClass, String tableName)
if (!vt.getClass().equals(vtableClass))
throw new IllegalStateException(String.format("Virtual table %s.%s is not backed by an instance of %s but by %s",
- VIRTUAL_VIEWS,
+ keyspaceName,
tableName,
vtableClass.getName(),
vt.getClass().getName()));
return (T) vt;
}
+ public static <T> T getVirtualTable(Class<T> vtableClass, String tableName)
+ {
+ return getVirtualTable(vtableClass, VIRTUAL_VIEWS, tableName);
+ }
+
/**
* This method adds an event to virtual table, when present.
* When vtable is null, we will attempt to find it among registered ones. Then not found, we add it to internal
* buffer for later processing. This might happen e.g. for logging tables when log events
* were appended via logging framework sooner than registration of virtual tables was done so after they are registered,
* they would miss logging events happened before being so.
*
- * @param vtable vtable to append to
- * @param event event to append to
- * @param tableName table name of virtual table to append to
+ * @param vtableClass class of vtable to append to
+ * @param vtable vtable to append to
+ * @param event event to append to
+ * @param tableName table name of virtual table to append to
* @return vtable or when null, found vtable
*/
- protected AbstractLoggerVirtualTable<?> appendToVirtualTable(AbstractLoggerVirtualTable<?> vtable, LoggingEvent event, String tableName)
+ protected AbstractLoggerVirtualTable<?> appendToVirtualTable(Class<? extends AbstractLoggerVirtualTable<?>> vtableClass,
Review Comment:
should we use a generic to align classes for first 2 parameters and the result value (avoid possible mistakes in usage)?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "Maxwell-Guo (via GitHub)" <gi...@apache.org>.
Maxwell-Guo commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2343687977
##########
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";
Review Comment:
so the table name is not exceptions now ?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "smiklosovic (via GitHub)" <gi...@apache.org>.
smiklosovic commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2352191442
##########
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>>
Review Comment:
@netudima I do not think I can do this, as `afterNodeInsertion` is internal method of `HashMap`, which is package protected, so we would need to implement custom java.util.BoundedList etc ... I would not complicate this further.
##########
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>>
Review Comment:
@netudima I do not think I can do this, as `afterNodeInsertion` is internal method of `HashMap`, which is package protected, so we would need to implement custom `java.util.BoundedList` etc ... I would not complicate this further.
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348331916
##########
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;
Review Comment:
it would be nice to mark immutable fields as final + we can use a lowest possible visibility scope (package private)
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2348341062
##########
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();
Review Comment:
foreach cycle would be more neat:
```
for(Map.Entry<String, LinkedHashMap<String, ExceptionRow>> partition : buffer.entrySet())
{
for (Map.Entry<String, ExceptionRow> entry : partition.getValue().entrySet())
populateRow(result, partition.getKey(), entry.getKey(), entry.getValue());
}
```
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369808101
##########
src/java/org/apache/cassandra/db/virtual/ExceptionsTable.java:
##########
@@ -0,0 +1,323 @@
+/*
+ * 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.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;
+import org.apache.cassandra.utils.Clock;
+import org.apache.cassandra.utils.logging.AbstractVirtualTableAppender;
+
+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_OCCURRENCE_COLUMN_NAME = "last_occurrence";
+
+ /**
+ * Buffer of uncaught exceptions which happened while virtual table was not initialized.
+ */
+ public static final List<ExceptionRow> preInitialisationBuffer = Collections.synchronizedList(new ArrayList<>());
+
+ @VisibleForTesting
+ public static volatile ExceptionsTable INSTANCE;
Review Comment:
Do we need to have it public? (tests are in the same package..)
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org
Re: [PR] CASSANDRA-20858 exceptions in vtables, rework to bound map [cassandra]
Posted by "netudima (via GitHub)" <gi...@apache.org>.
netudima commented on code in PR #4356:
URL: https://github.com/apache/cassandra/pull/4356#discussion_r2369803867
##########
src/java/org/apache/cassandra/db/virtual/ExceptionsTable.java:
##########
@@ -0,0 +1,323 @@
+/*
+ * 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.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;
+import org.apache.cassandra.utils.Clock;
+import org.apache.cassandra.utils.logging.AbstractVirtualTableAppender;
+
+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_OCCURRENCE_COLUMN_NAME = "last_occurrence";
+
+ /**
+ * Buffer of uncaught exceptions which happened while virtual table was not initialized.
+ */
+ public static final List<ExceptionRow> preInitialisationBuffer = Collections.synchronizedList(new ArrayList<>());
Review Comment:
should we make it package private and mark as VisibleForTesting?
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org