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_r395650523
 
 

 ##########
 File path: 
flink-tests/src/test/java/org/apache/flink/runtime/util/ExceptionUtilsITCases.java
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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.flink.runtime.util;
+
+import org.apache.flink.test.util.TestProcessBuilder;
+import org.apache.flink.test.util.TestProcessBuilder.TestProcess;
+import org.apache.flink.testutils.ClassLoaderUtils;
+import org.apache.flink.testutils.ClassLoaderUtils.ClassLoaderBuilder;
+import org.apache.flink.util.ExceptionUtils;
+
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.management.ManagementFactory;
+import java.lang.management.MemoryPoolMXBean;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Tests for {@link  ExceptionUtils} which require to spawn JVM process and 
set JVM memory args.
+ */
+public class ExceptionUtilsITCases {
+       private static final int DIRECT_MEMORY_SIZE = 1 << 20; // 128Mb
+       private static final int DIRECT_MEMORY_ALLOCATION_PAGE_SIZE = 1024; // 
1Kb
+       private static final int DIRECT_MEMORY_PAGE_NUMBER = DIRECT_MEMORY_SIZE 
/ DIRECT_MEMORY_ALLOCATION_PAGE_SIZE;
+       private static final long INITIAL_BIG_METASPACE_SIZE = 128 * (1 << 20); 
// 128Mb
+
+       @ClassRule
+       public static final TemporaryFolder TEMPORARY_FOLDER = new 
TemporaryFolder();
+
+       @Test
+       public void testIsDirectOutOfMemoryError() throws IOException, 
InterruptedException {
+               String className = DummyDirectAllocatingProgram.class.getName();
+               String out = run(className, Collections.emptyList(), 
DIRECT_MEMORY_SIZE, -1);
+               assertThat(out, is(""));
+       }
+
+       @Test
+       public void testIsMetaspaceOutOfMemoryError() throws IOException, 
InterruptedException {
+               String className = DummyClassLoadingProgram.class.getName();
+               // load only one class and record required Metaspace
+               String normalOut = run(className, 
getDummyClassLoadingProgramArgs(1), -1, INITIAL_BIG_METASPACE_SIZE);
+               long okMetaspace = Long.parseLong(normalOut);
+               // load more classes to cause 'OutOfMemoryError: Metaspace'
+               String oomOut = run(className, 
getDummyClassLoadingProgramArgs(1000), -1, okMetaspace);
+               assertThat(oomOut, is(""));
+       }
+
+       private static String run(
+                       String className,
+                       Iterable<String> args,
+                       long directMemorySize,
+                       long metaspaceSize) throws InterruptedException, 
IOException {
+               TestProcessBuilder taskManagerProcessBuilder = new 
TestProcessBuilder(className);
+               if (directMemorySize > 0) {
+                       
taskManagerProcessBuilder.addJvmArg(String.format("-XX:MaxDirectMemorySize=%d", 
directMemorySize));
+               }
+               if (metaspaceSize > 0) {
+                       
taskManagerProcessBuilder.addJvmArg("-XX:-UseCompressedOops");
+                       
taskManagerProcessBuilder.addJvmArg(String.format("-XX:MaxMetaspaceSize=%d", 
metaspaceSize));
+               }
+               for (String arg : args) {
+                       taskManagerProcessBuilder.addMainClassArg(arg);
+               }
+               TestProcess p = taskManagerProcessBuilder.start();
+               p.getProcess().waitFor();
+               assertThat(p.getErrorOutput().toString().trim(), is(""));
+               return p.getProcessOutput().toString().trim();
+       }
+
+       private static Collection<String> getDummyClassLoadingProgramArgs(int 
numberOfLoadedClasses) {
+               return Arrays.asList(
+                       Integer.toString(numberOfLoadedClasses),
+                       TEMPORARY_FOLDER.getRoot().getAbsolutePath());
+       }
+
+       /**
+        * Dummy java program to generate Direct OOM.
+        */
+       public static class DummyDirectAllocatingProgram {
+               private DummyDirectAllocatingProgram() {
+               }
+
+               public static void main(String[] args) {
+                       Collection<ByteBuffer> buffers = new ArrayList<>();
+                       try {
+                               for (int page = 0; page < 2 * 
DIRECT_MEMORY_PAGE_NUMBER; page++) {
+                                       
buffers.add(ByteBuffer.allocateDirect(DIRECT_MEMORY_ALLOCATION_PAGE_SIZE));
+                               }
+                       } catch (Throwable t) {
+                               if 
(!ExceptionUtils.isDirectOutOfMemoryError(t)) {
+                                       output("Wrong exception: " + t);
+                               }
+                               return;
+                       }
+                       output("buffers: " + buffers);
 
 Review comment:
   Can this call be moved inside of the `try` block directly after the for loop?

----------------------------------------------------------------
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