gowa commented on code in PR #4166:
URL: https://github.com/apache/cassandra/pull/4166#discussion_r2116264911


##########
conf/cassandra-env.sh:
##########
@@ -199,6 +199,45 @@ if [ "x$CASSANDRA_HEAPDUMP_DIR" = "x" ]; then
 fi
 JVM_OPTS="$JVM_OPTS -XX:HeapDumpPath=$CASSANDRA_HEAPDUMP_DIR/cassandra-`date 
+%s`-pid$$.hprof"
 
+# by default, enable cassandra heapdump files clean up, keeping 2 latest files
+# and 1 oldest heap dump file (this may help identify the earliest OOM issue)
+if [ "x$CASSANDRA_HEAPDUMP_CLEAN" = "x" ]; then
+    CASSANDRA_HEAPDUMP_CLEAN=1
+fi
+if [ "x$CASSANDRA_HEAPDUMP_KEEP_LAST_N_FILES" = "x" ]; then
+    CASSANDRA_HEAPDUMP_KEEP_LAST_N_FILES=2
+fi
+if [ "x$CASSANDRA_HEAPDUMP_KEEP_FIRST_N_FILES" = "x" ]; then
+    CASSANDRA_HEAPDUMP_KEEP_FIRST_N_FILES=1
+fi
+
+# this flag identifies that 'cassandra-env.sh' function
+# clean_heap_dump_files has been loaded and should be called.
+# this flag can be reset in bin/cassandra if -H option is passed.
+call_clean_heap_dump_files=1
+
+clean_heap_dump_files()
+{
+    if [ "x$CASSANDRA_HEAPDUMP_CLEAN" = "x1" ] && \
+           [ "$CASSANDRA_HEAPDUMP_KEEP_LAST_N_FILES" -ge 0 ] && \
+           [ "$CASSANDRA_HEAPDUMP_KEEP_FIRST_N_FILES" -ge 0 ] && \
+           [ -d "$CASSANDRA_HEAPDUMP_DIR" ]; then
+        # find heap dump files, take not more than 100 of them (in order not 
to overload xargs),
+        # sort by last modification date descending
+        # print those, that need to be removed
+        find "$CASSANDRA_HEAPDUMP_DIR" -name "cassandra-*-pid*.hprof" -type f 
| \
+        head -n 100 | \
+        xargs ls -t1 2>/dev/null | \
+        awk "BEGIN{ f=0; }{ files[f]=\$0; f+=1; }END{

Review Comment:
   Hi @smiklosovic ! Thanks for your review. Will try to address your comments 
properly.
   I decided to rely on `awk` because it is used already in this script and 
because the solution using `head/tail `is not portable (at least chatgpt 
reported that head's negative line numbers cannot be used on MacOS, while some 
switch on `uname` in the script above suggests it should be portable), so I 
decided to go with awk using its basics only.



-- 
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-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to