tillrohrmann commented on a change in pull request #11408: 
[FLINK-15989][FLINK-16225] Improve direct and metaspace out-of-memory error 
handling
URL: https://github.com/apache/flink/pull/11408#discussion_r396347458
 
 

 ##########
 File path: flink-core/src/main/java/org/apache/flink/util/ExceptionUtils.java
 ##########
 @@ -109,6 +131,59 @@ public static boolean 
isJvmFatalOrOutOfMemoryError(Throwable t) {
                return isJvmFatalError(t) || t instanceof OutOfMemoryError;
        }
 
+       /**
+        * Generates new {@link OutOfMemoryError} with more detailed message.
+        *
+        * <p>This method improves error message for direct and metaspace 
{@link OutOfMemoryError}.
+        * It adds description of possible causes and ways of resolution.
+        *
+        * @param exception The exception to enrich.
+        * @return either enriched exception if needed or the original one.
+        */
+       public static Throwable enrichTaskManagerOutOfMemoryError(Throwable 
exception) {
+               if (isMetaspaceOutOfMemoryError(exception)) {
+                       return changeOutOfMemoryErrorMessage(exception, 
TM_METASPACE_OOM_ERROR_MESSAGE);
+               } else if (isDirectOutOfMemoryError(exception)) {
+                       return changeOutOfMemoryErrorMessage(exception, 
TM_DIRECT_OOM_ERROR_MESSAGE);
+               }
+               return exception;
+       }
+
+       private static OutOfMemoryError changeOutOfMemoryErrorMessage(Throwable 
exception, String newMessage) {
+               Preconditions.checkArgument(exception instanceof 
OutOfMemoryError);
+               if (exception.getMessage().equals(newMessage)) {
+                       return (OutOfMemoryError) exception;
+               }
+               OutOfMemoryError newError = new OutOfMemoryError(newMessage);
+               newError.initCause(exception.getCause());
+               newError.setStackTrace(exception.getStackTrace());
+               return newError;
+       }
+
+       /**
+        * Checks whether the given exception indicates a JVM metaspace 
out-of-memory error.
+        *
+        * @param t The exception to check.
+        * @return True, if the exception is the metaspace {@link 
OutOfMemoryError}, false otherwise.
+        */
+       public static boolean isMetaspaceOutOfMemoryError(Throwable t) {
+               return isOutOfMemoryErrorWithMessageStartingWith(t, 
"Metaspace");
+       }
+
+       /**
+        * Checks whether the given exception indicates a JVM direct 
out-of-memory error.
+        *
+        * @param t The exception to check.
+        * @return True, if the exception is the direct {@link 
OutOfMemoryError}, false otherwise.
+        */
+       public static boolean isDirectOutOfMemoryError(Throwable t) {
+               return isOutOfMemoryErrorWithMessageStartingWith(t, "Direct 
buffer memory");
+       }
+
+       private static boolean 
isOutOfMemoryErrorWithMessageStartingWith(Throwable t, String prefix) {
+               return t.getClass() == OutOfMemoryError.class && t.getMessage() 
!= null && t.getMessage().startsWith(prefix);
 
 Review comment:
   Maybe add a comment stating that we check for class equality in order to not 
catch subclasses.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to