Skip to content

Commit

Permalink
Replace code duplicates with closeAllSuppress helper
Browse files Browse the repository at this point in the history
  • Loading branch information
losipiuk committed Jan 20, 2022
1 parent 7148d4c commit 569e4fd
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 138 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import static com.google.common.base.MoreObjects.toStringHelper;
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 @@ -138,15 +139,15 @@ public Page getNextPage()
return new Page(currentPageSize, blocks);
}
catch (TrinoException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw e;
}
catch (RcFileCorruptionException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw new TrinoException(HIVE_BAD_DATA, format("Corrupted RC file: %s", rcFileReader.getId()), e);
}
catch (IOException | RuntimeException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw new TrinoException(HIVE_CURSOR_ERROR, format("Failed to read RC file: %s", rcFileReader.getId()), e);
}
}
Expand Down Expand Up @@ -179,19 +180,6 @@ public long getSystemMemoryUsage()
return GUESSED_MEMORY_USAGE;
}

private void closeWithSuppression(Throwable throwable)
{
requireNonNull(throwable, "throwable is null");
try {
close();
}
catch (Exception e) {
if (e != throwable) {
throwable.addSuppressed(e);
}
}
}

private Block createBlock(int currentPageSize, int fieldId)
{
int hiveColumnIndex = hiveColumnIndexes[fieldId];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import io.trino.plugin.hive.metastore.Table;
import io.trino.spi.ErrorCodeSupplier;
import io.trino.spi.TrinoException;
import io.trino.spi.connector.RecordCursor;
import io.trino.spi.predicate.NullableValue;
import io.trino.spi.type.ArrayType;
import io.trino.spi.type.CharType;
Expand Down Expand Up @@ -164,7 +163,6 @@
import static java.math.BigDecimal.ROUND_UNNECESSARY;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import static org.apache.hadoop.hive.common.FileUtils.unescapePathName;
import static org.apache.hadoop.hive.metastore.api.hive_metastoreConstants.FILE_INPUT_FORMAT;
Expand Down Expand Up @@ -1024,21 +1022,6 @@ else if (type.equals(VarbinaryType.VARBINARY)) {
throw new TrinoException(NOT_SUPPORTED, format("Unsupported column type %s for prefilled column: %s", type.getDisplayName(), name));
}

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

public static List<HiveType> extractStructFieldTypes(HiveType hiveType)
{
return ((StructTypeInfo) hiveType.getTypeInfo()).getAllStructFieldTypeInfos().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.OptionalLong;

import static com.google.common.base.Throwables.throwIfInstanceOf;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.iceberg.IcebergErrorCode.ICEBERG_BAD_DATA;
import static io.trino.plugin.iceberg.IcebergUtil.deserializePartitionValue;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -153,15 +154,6 @@ public long getSystemMemoryUsage()

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);
}
}
closeAllSuppress(throwable, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.airlift.slice.Slices.utf8Slice;
import static io.trino.plugin.base.util.Closables.closeAllSuppress;
import static io.trino.plugin.raptor.legacy.RaptorColumnHandle.SHARD_UUID_COLUMN_TYPE;
import static io.trino.plugin.raptor.legacy.RaptorErrorCode.RAPTOR_ERROR;
import static io.trino.spi.type.BigintType.BIGINT;
Expand Down Expand Up @@ -104,7 +105,7 @@ public Page getNextPage()
page = recordReader.nextPage();
}
catch (IOException | RuntimeException e) {
closeWithSuppression(e);
closeAllSuppress(e, this);
throw handleException(e);
}

Expand Down Expand Up @@ -175,20 +176,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, long filePosition);
Expand Down

0 comments on commit 569e4fd

Please sign in to comment.