You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2020/09/03 06:33:06 UTC

[GitHub] [druid] jihoonson commented on a change in pull request #10247: Remove CloseQuietly and migrate its usages to other methods.

jihoonson commented on a change in pull request #10247:
URL: https://github.com/apache/druid/pull/10247#discussion_r482719520



##########
File path: core/src/test/java/org/apache/druid/utils/CloseableUtilsTest.java
##########
@@ -0,0 +1,415 @@
+/*
+ * 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.druid.utils;
+
+import com.google.common.base.Throwables;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.internal.matchers.ThrowableCauseMatcher;
+import org.junit.internal.matchers.ThrowableMessageMatcher;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+
+public class CloseableUtilsTest
+{
+  private final TestCloseable quietCloseable = new TestCloseable(null);
+  private final TestCloseable quietCloseable2 = new TestCloseable(null);
+  private final TestCloseable ioExceptionCloseable = new TestCloseable(new IOException());
+  private final TestCloseable runtimeExceptionCloseable = new TestCloseable(new IllegalArgumentException());
+
+  // For closeAndSuppressException tests.
+  private final AtomicLong chomped = new AtomicLong();
+  private final Consumer<Exception> chomper = e -> chomped.incrementAndGet();
+
+  @Test
+  public void test_closeAll_array_quiet() throws IOException
+  {
+    CloseableUtils.closeAll(quietCloseable, null, quietCloseable2);
+    assertClosed(quietCloseable, quietCloseable2);
+  }
+
+  @Test
+  public void test_closeAll_list_quiet() throws IOException
+  {
+    CloseableUtils.closeAll(Arrays.asList(quietCloseable, null, quietCloseable2));
+    assertClosed(quietCloseable, quietCloseable2);
+  }
+
+  @Test
+  public void test_closeAll_array_loud()
+  {
+    Exception e = null;
+    try {
+      CloseableUtils.closeAll(quietCloseable, null, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable);
+    }
+    catch (Exception e2) {
+      e = e2;
+    }
+
+    assertClosed(quietCloseable, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable);
+
+    // First exception
+    Assert.assertThat(e, CoreMatchers.instanceOf(IOException.class));
+
+    // Second exception
+    Assert.assertEquals(1, e.getSuppressed().length);
+    Assert.assertThat(e.getSuppressed()[0], CoreMatchers.instanceOf(RuntimeException.class));

Review comment:
       Hmm, why not `IllegalArgumentException`?

##########
File path: processing/src/main/java/org/apache/druid/query/groupby/strategy/GroupByStrategyV2.java
##########
@@ -329,9 +329,8 @@ public boolean doMergeResults(final GroupByQuery query)
           finalResultSupplier
       );
     }
-    catch (Exception ex) {
-      CloseQuietly.close(resultSupplier);
-      throw ex;
+    catch (Throwable e) {

Review comment:
       Why catching `Throwable`s vs catching only `Exception`s? It doesn't seem useful to do anything when you see any `Error` here.

##########
File path: processing/src/main/java/org/apache/druid/segment/data/BlockLayoutColumnarDoublesSupplier.java
##########
@@ -168,7 +168,7 @@ public void get(final double[] out, final int[] indexes, final int length)
 
     protected void loadBuffer(int bufferNum)
     {
-      CloseQuietly.close(holder);
+      CloseableUtils.closeAndWrapExceptions(holder);

Review comment:
       This seems fine because `ResourceHolder.close()` doesn't throw `IOException`, but this code seems confusing to me since, until looking at the code closely, I wasn't sure if it's fine to not execute the below lines when this line throws an exception. How about adding a comment here, such as "this line should never throw any checked exceptions"?

##########
File path: core/src/main/java/org/apache/druid/java/util/common/guava/ParallelMergeCombiningSequence.java
##########
@@ -1376,6 +1382,6 @@ long getTotalCpuTimeNanos()
   {
     Closer closer = Closer.create();
     closer.registerAll(cursors);
-    CloseQuietly.close(closer);
+    CloseableUtils.closeAndWrapExceptions(closer);

Review comment:
       I agree that it would be better to suppress IOException. We shouldn't throw IOException immediately especially at [Line 650](https://github.com/apache/druid/pull/10247/files#diff-84792f9d3cefe47cbb471669dce2a276R650-R651) since the terminal resultBatch must be added to the outputQueue after closing all cursors.

##########
File path: processing/src/main/java/org/apache/druid/segment/QueryableIndexIndexableAdapter.java
##########
@@ -280,7 +281,7 @@ public boolean hasTimeAndDimsChangedSinceMark()
     @Override
     public void close()
     {
-      CloseQuietly.close(closer);
+      CloseableUtils.closeAndWrapExceptions(closer);

Review comment:
       nit: you can modify the signature of this method to throw IOExceptoin, but this should also be fine.

##########
File path: core/src/test/java/org/apache/druid/utils/CloseableUtilsTest.java
##########
@@ -0,0 +1,415 @@
+/*
+ * 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.druid.utils;
+
+import com.google.common.base.Throwables;
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.internal.matchers.ThrowableCauseMatcher;
+import org.junit.internal.matchers.ThrowableMessageMatcher;
+
+import javax.annotation.Nullable;
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+
+public class CloseableUtilsTest
+{
+  private final TestCloseable quietCloseable = new TestCloseable(null);
+  private final TestCloseable quietCloseable2 = new TestCloseable(null);
+  private final TestCloseable ioExceptionCloseable = new TestCloseable(new IOException());
+  private final TestCloseable runtimeExceptionCloseable = new TestCloseable(new IllegalArgumentException());
+
+  // For closeAndSuppressException tests.
+  private final AtomicLong chomped = new AtomicLong();
+  private final Consumer<Exception> chomper = e -> chomped.incrementAndGet();
+
+  @Test
+  public void test_closeAll_array_quiet() throws IOException
+  {
+    CloseableUtils.closeAll(quietCloseable, null, quietCloseable2);
+    assertClosed(quietCloseable, quietCloseable2);
+  }
+
+  @Test
+  public void test_closeAll_list_quiet() throws IOException
+  {
+    CloseableUtils.closeAll(Arrays.asList(quietCloseable, null, quietCloseable2));
+    assertClosed(quietCloseable, quietCloseable2);
+  }
+
+  @Test
+  public void test_closeAll_array_loud()
+  {
+    Exception e = null;
+    try {
+      CloseableUtils.closeAll(quietCloseable, null, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable);
+    }
+    catch (Exception e2) {
+      e = e2;
+    }
+
+    assertClosed(quietCloseable, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable);
+
+    // First exception
+    Assert.assertThat(e, CoreMatchers.instanceOf(IOException.class));
+
+    // Second exception
+    Assert.assertEquals(1, e.getSuppressed().length);
+    Assert.assertThat(e.getSuppressed()[0], CoreMatchers.instanceOf(RuntimeException.class));
+  }
+
+  @Test
+  public void test_closeAll_list_loud()
+  {
+    Exception e = null;
+    try {
+      CloseableUtils.closeAll(
+          Arrays.asList(
+              quietCloseable,
+              null,
+              ioExceptionCloseable,
+              quietCloseable2,
+              runtimeExceptionCloseable
+          )
+      );
+    }
+    catch (Exception e2) {
+      e = e2;
+    }
+
+    assertClosed(quietCloseable, ioExceptionCloseable, quietCloseable2, runtimeExceptionCloseable);
+
+    // First exception
+    Assert.assertThat(e, CoreMatchers.instanceOf(IOException.class));
+
+    // Second exception
+    Assert.assertEquals(1, e.getSuppressed().length);
+    Assert.assertThat(e.getSuppressed()[0], CoreMatchers.instanceOf(RuntimeException.class));

Review comment:
       Same here.

##########
File path: processing/src/main/java/org/apache/druid/segment/data/GenericIndexed.java
##########
@@ -530,13 +530,13 @@ public void close()
         headerOut.writeInt(Ints.checkedCast(valuesOut.size()));
 
         if (prevVal instanceof Closeable) {
-          CloseQuietly.close((Closeable) prevVal);
+          CloseableUtils.closeAndWrapExceptions((Closeable) prevVal);

Review comment:
       This changes the behavior, but any callers of `fromIterableVersionOne()` doesn't seem creating `GenericIndexed` from `Closeable` types. How about removing this?

##########
File path: processing/src/main/java/org/apache/druid/segment/data/BlockLayoutColumnarDoublesSupplier.java
##########
@@ -168,7 +168,7 @@ public void get(final double[] out, final int[] indexes, final int length)
 
     protected void loadBuffer(int bufferNum)
     {
-      CloseQuietly.close(holder);
+      CloseableUtils.closeAndWrapExceptions(holder);

Review comment:
       Same comment for other places where close `ResourceHolder`s.




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org