josh-mckenzie commented on code in PR #1778:
URL: https://github.com/apache/cassandra/pull/1778#discussion_r943644100


##########
src/java/org/apache/cassandra/config/DatabaseDescriptor.java:
##########
@@ -4381,4 +4397,68 @@ public static void 
setMinTrackedPartitionTombstoneCount(long value)
     {
         conf.min_tracked_partition_tombstone_count = value;
     }
+
+    public static boolean getDumpHeapOnUncaughtException()
+    {
+        return conf.dump_heap_on_uncaught_exception;
+    }
+
+    /**
+     * @return Whether the path exists (be it created now or already prior)
+     */
+    private static boolean maybeCreateHeapDumpPath()
+    {
+        if (!conf.dump_heap_on_uncaught_exception)
+            return false;
+
+        Path heap_dump_path = getHeapDumpPath();
+        if (heap_dump_path == null)
+        {
+            logger.warn("Neither -XX:HeapDumpPath nor 
cassandra.yaml:heap_dump_path are set; unable to create a directory to hold the 
output.");
+            return false;
+        }
+        if (PathUtils.exists(Paths.get(conf.heap_dump_path)))
+            return true;
+        return 
PathUtils.createDirectoryIfNotExists(Paths.get(conf.heap_dump_path));
+    }
+
+    /**
+     * As this is at its heart a debug operation (getting a one-shot heapdump 
from an uncaught exception), we support
+     * both the more evolved cassandra.yaml approach but also the -XX param to 
override it on a one-off basis so you don't
+     * have to change the full config of a node or a cluster in order to get a 
heap dump from a single node that's
+     * misbehaving.
+     * @return the absolute path of the -XX param if provided, else the 
heap_dump_path in cassandra.yaml
+     */
+    public static Path getHeapDumpPath()
+    {
+        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
+        Optional<String> pathArg = 
runtimeMxBean.getInputArguments().stream().filter(s -> 
s.startsWith("-XX:HeapDumpPath=")).findFirst();
+
+        if (pathArg.isPresent())
+        {
+            Pattern HEAP_DUMP_PATH_SPLITTER = Pattern.compile("HeapDumpPath=");
+            String fullHeapPathString = 
HEAP_DUMP_PATH_SPLITTER.split(pathArg.get())[1];
+            Path absolutePath = Paths.get(fullHeapPathString).toAbsolutePath();
+            Path basePath = fullHeapPathString.endsWith(".hprof") ? 
absolutePath.subpath(0, absolutePath.getNameCount() - 1) : absolutePath;
+            return Paths.get("/").resolve(basePath);
+        }
+        if (conf.heap_dump_path == null)
+            throw new ConfigurationException("Attempted to get heap dump path 
without -XX:HeapDumpPath or cassandra.yaml:heap_dump_path set.");
+        return Paths.get(conf.heap_dump_path);
+    }
+
+    public static void setDumpHeapOnUncaughtException(boolean enabled)
+    {
+        conf.dump_heap_on_uncaught_exception = enabled;
+        boolean pathExists = maybeCreateHeapDumpPath();
+
+        // In the good case, we're either disabling or we're enabled and the 
path exists on disk just fine.
+        if (!enabled || pathExists)
+            logger.info("Setting dump_heap_on_uncaught_exception to {}", 
enabled);
+        else
+        {
+            logger.error("Attempted to enable heap dump but cannot create the 
requested path. Disabling.");
+            conf.dump_heap_on_uncaught_exception = false;
+        }

Review Comment:
   _**Thank you**_. This code was aesthetically irritating the hell out of me 
but I kept bulldozing forward on other stuff and just walked away from it 
making me feel... annoyed... without going deep enough to figure out why.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to