Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace code duplicates with closeAllSuppress helper #10682

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed 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 io.trino.plugin.base.util;

import static java.util.Objects.requireNonNull;

public final class Closables
{
private Closables() {}

public static <T extends Throwable> T closeAllSuppress(T rootCause, AutoCloseable... closeables)
{
requireNonNull(rootCause, "rootCause is null");
requireNonNull(closeables, "closeables is null");
for (AutoCloseable closeable : closeables) {
try {
if (closeable != null) {
closeable.close();
}
}
catch (Throwable e) {
// Self-suppression not permitted
if (rootCause != e) {
rootCause.addSuppressed(e);
}
}
}
return rootCause;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Licensed 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 io.trino.plugin.base.util;

import org.testng.annotations.Test;

import java.util.Optional;

import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.Assert.assertTrue;

public class TestClosables
{
@Test
public void testCloseAllSuppressNonThrowing()
{
RuntimeException rootException = new RuntimeException("root");
TestClosable closable = new TestClosable(Optional.empty());
closeAllSuppress(rootException, closable);
assertTrue(closable.isClosed());
assertThat(rootException.getSuppressed()).isEmpty();
}

@Test
public void testCloseAllSuppressThrowingOther()
{
RuntimeException rootException = new RuntimeException("root");
RuntimeException closeException = new RuntimeException("close");
TestClosable closable = new TestClosable(Optional.of(closeException));
closeAllSuppress(rootException, closable);
assertTrue(closable.isClosed());
assertThat(rootException.getSuppressed()).containsExactly(closeException);
}

@Test
public void testCloseAllSuppressThrowingRoot()
{
RuntimeException rootException = new RuntimeException("root");
TestClosable closable = new TestClosable(Optional.of(rootException));
closeAllSuppress(rootException, closable);
assertTrue(closable.isClosed());
assertThat(rootException.getSuppressed()).isEmpty();
}

@Test
public void testCloseAllSuppressNullClosable()
{
RuntimeException rootException = new RuntimeException("root");
closeAllSuppress(rootException, (AutoCloseable) null);
assertThat(rootException.getSuppressed()).isEmpty();
}

@Test
public void testCloseAllSuppressMultipleClosables()
{
RuntimeException rootException = new RuntimeException("root");
RuntimeException closeException1 = new RuntimeException("close");
RuntimeException closeException2 = new RuntimeException("close2");
TestClosable closable1 = new TestClosable(Optional.of(closeException1));
TestClosable closable2 = new TestClosable(Optional.of(closeException2));
TestClosable closable3 = new TestClosable(Optional.empty()); // non throwing
TestClosable closable4 = new TestClosable(Optional.of(rootException)); // throwing root
closeAllSuppress(rootException, closable1, closable2, closable3, closable4, null);
assertTrue(closable1.isClosed());
assertTrue(closable2.isClosed());
assertTrue(closable3.isClosed());
assertTrue(closable4.isClosed());
assertThat(rootException.getSuppressed()).containsExactly(closeException1, closeException2);
}

private static class TestClosable
implements AutoCloseable
{
private final Optional<Exception> closeException;
private boolean closed;

public TestClosable(Optional<Exception> closeException)
{
this.closeException = requireNonNull(closeException, "closeException is null");
}

@Override
public void close()
throws Exception
{
closed = true;
if (closeException.isPresent()) {
throw closeException.get();
}
}

public boolean isClosed()
{
return closed;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
import static io.trino.plugin.jdbc.JdbcErrorCode.JDBC_NON_TRANSIENT_ERROR;
import static io.trino.plugin.jdbc.JdbcWriteSessionProperties.getWriteBatchSize;
Expand Down Expand Up @@ -68,7 +69,7 @@ public JdbcPageSink(ConnectorSession session, JdbcOutputTableHandle handle, Jdbc
connection.setAutoCommit(false);
}
catch (SQLException e) {
closeWithSuppression(connection, e);
closeAllSuppress(e, connection);
throw new TrinoException(JDBC_ERROR, e);
}

Expand Down Expand Up @@ -102,7 +103,7 @@ public JdbcPageSink(ConnectorSession session, JdbcOutputTableHandle handle, Jdbc
statement = connection.prepareStatement(jdbcClient.buildInsertSql(handle, columnWriters));
}
catch (SQLException e) {
closeWithSuppression(connection, e);
closeAllSuppress(e, connection);
throw new TrinoException(JDBC_ERROR, e);
}

Expand Down Expand Up @@ -212,18 +213,4 @@ public void abort()
throw new TrinoException(JDBC_ERROR, e);
}
}

@SuppressWarnings("ObjectEquality")
private static void closeWithSuppression(Connection connection, Throwable throwable)
{
try {
connection.close();
}
catch (Throwable t) {
// Self-suppression not permitted
if (throwable != t) {
throwable.addSuppressed(t);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.trino.plugin.base.type.TrinoTimestampEncoderFactory.createTimestampEncoder;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.hive.HiveColumnHandle.ColumnType.REGULAR;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_BAD_DATA;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_CURSOR_ERROR;
import static io.trino.plugin.hive.util.HiveUtil.closeWithSuppression;
import static io.trino.plugin.hive.util.HiveUtil.getDeserializer;
import static io.trino.plugin.hive.util.HiveUtil.getTableObjectInspector;
import static io.trino.plugin.hive.util.HiveUtil.isStructuralType;
Expand Down Expand Up @@ -226,7 +226,7 @@ public boolean advanceNextPosition()
return true;
}
catch (IOException | SerDeException | RuntimeException e) {
closeWithSuppression(this, e);
closeAllSuppress(e, this);
if (e instanceof TextLineLengthLimitExceededException) {
throw new TrinoException(HIVE_BAD_DATA, "Line too long in text file: " + path, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.hive.HiveColumnHandle.isRowIdColumnHandle;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_CURSOR_ERROR;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_INVALID_BUCKET_FILES;
Expand Down Expand Up @@ -243,11 +244,11 @@ public Page getNextPage()
return page;
}
catch (TrinoException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw e;
}
catch (RuntimeException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw new TrinoException(HIVE_CURSOR_ERROR, e);
}
}
Expand Down Expand Up @@ -281,20 +282,6 @@ public Metrics getMetrics()
return delegate.getMetrics();
}

protected void closeWithSuppression(Throwable throwable)
{
requireNonNull(throwable, "throwable is null");
try {
close();
}
catch (RuntimeException e) {
// Self-suppression not permitted
if (throwable != e) {
throwable.addSuppressed(e);
}
}
}

public ConnectorPageSource getPageSource()
{
return delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static io.trino.orc.OrcReader.MAX_BATCH_SIZE;
import static io.trino.orc.OrcReader.createOrcReader;
import static io.trino.orc.OrcReader.fullyProjectedLayout;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_CANNOT_OPEN_SPLIT;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_MISSING_DATA;
import static io.trino.plugin.hive.acid.AcidSchema.ACID_COLUMN_BUCKET;
Expand Down Expand Up @@ -190,7 +191,7 @@ public Page getNextPage()
return page;
}
catch (IOException | RuntimeException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw handleException(orcDataSource.getId(), e);
}
}
Expand Down Expand Up @@ -227,20 +228,6 @@ public long getSystemMemoryUsage()
return systemMemoryContext.getBytes();
}

private void closeWithSuppression(Throwable throwable)
{
requireNonNull(throwable, "throwable is null");
try {
close();
}
catch (RuntimeException e) {
// Self-suppression not permitted
if (throwable != e) {
throwable.addSuppressed(e);
}
}
}

private static String openError(Throwable t, Path path)
{
return format("Error opening Hive delete delta file %s: %s", path, t.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_BAD_DATA;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_CURSOR_ERROR;
import static io.trino.plugin.hive.HiveUpdatablePageSource.BUCKET_CHANNEL;
Expand Down Expand Up @@ -150,7 +151,7 @@ public Page getNextPage()
}
}
catch (IOException | RuntimeException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw handleException(orcDataSource.getId(), e);
}

Expand Down Expand Up @@ -244,20 +245,6 @@ public long getSystemMemoryUsage()
return systemMemoryContext.getBytes();
}

private void closeWithSuppression(Throwable throwable)
{
requireNonNull(throwable, "throwable is null");
try {
close();
}
catch (RuntimeException e) {
// Self-suppression not permitted
if (throwable != e) {
throwable.addSuppressed(e);
}
}
}

public interface ColumnAdaptation
{
Block block(Page sourcePage, MaskDeletedRowsFunction maskDeletedRowsFunction, long filePosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_BAD_DATA;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_CURSOR_ERROR;
import static java.lang.String.format;
Expand Down Expand Up @@ -157,29 +158,15 @@ public Page getNextPage()
return new Page(batchSize, blocks);
}
catch (TrinoException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw e;
}
catch (RuntimeException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw new TrinoException(HIVE_CURSOR_ERROR, e);
}
}

private void closeWithSuppression(Throwable throwable)
{
requireNonNull(throwable, "throwable is null");
try {
close();
}
catch (RuntimeException e) {
// Self-suppression not permitted
if (e != throwable) {
throwable.addSuppressed(e);
}
}
}

@Override
public void close()
{
Expand Down
Loading