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

Reduce redundant code in procedure declarations #18569

Merged
merged 6 commits into from
Aug 8, 2023
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
Expand Up @@ -15,6 +15,7 @@

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import com.google.inject.Provider;
import io.trino.FullConnectorSession;
import io.trino.annotation.UsedByGeneratedCode;
import io.trino.dispatcher.DispatchManager;
Expand Down Expand Up @@ -44,6 +45,7 @@
import static java.util.Objects.requireNonNull;

public class KillQueryProcedure
implements Provider<Procedure>
{
private static final MethodHandle KILL_QUERY = methodHandle(KillQueryProcedure.class, "killQuery", String.class, String.class, ConnectorSession.class);

Expand Down Expand Up @@ -88,7 +90,8 @@ public void killQuery(String queryId, String message, ConnectorSession session)
}
}

public Procedure getProcedure()
@Override
public Procedure get()
{
return new Procedure(
"runtime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.multibindings.ProvidesIntoSet;
import io.trino.connector.system.jdbc.AttributeJdbcTable;
import io.trino.connector.system.jdbc.CatalogJdbcTable;
import io.trino.connector.system.jdbc.ColumnJdbcTable;
Expand Down Expand Up @@ -72,7 +71,8 @@ public void configure(Binder binder)
globalTableBinder.addBinding().to(TableTypeJdbcTable.class).in(Scopes.SINGLETON);
globalTableBinder.addBinding().to(UdtJdbcTable.class).in(Scopes.SINGLETON);

Multibinder.newSetBinder(binder, Procedure.class);
Multibinder<Procedure> procedures = Multibinder.newSetBinder(binder, Procedure.class);
procedures.addBinding().toProvider(KillQueryProcedure.class).in(Scopes.SINGLETON);

binder.bind(KillQueryProcedure.class).in(Scopes.SINGLETON);

Expand All @@ -82,10 +82,4 @@ public void configure(Binder binder)
tableFunctions.addBinding().toProvider(ExcludeColumns.class).in(Scopes.SINGLETON);
tableFunctions.addBinding().toProvider(Sequence.class).in(Scopes.SINGLETON);
}

@ProvidesIntoSet
public static Procedure getKillQueryProcedure(KillQueryProcedure procedure)
{
return procedure.getProcedure();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import jakarta.annotation.Nullable;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
Expand All @@ -32,6 +31,7 @@

import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.trino.util.Reflection.methodHandle;
import static java.util.Objects.requireNonNull;

public class DecoratingListeningExecutorService
Expand All @@ -49,14 +49,9 @@ public class DecoratingListeningExecutorService
catch (NoSuchMethodException e) {
closeMethod = null;
}
try {
CLOSE_METHOD = closeMethod != null
? MethodHandles.lookup().unreflect(closeMethod)
: null;
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
CLOSE_METHOD = closeMethod != null
? methodHandle(closeMethod)
: null;
}

private final ListeningExecutorService delegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import io.trino.sql.gen.CallSiteBinder;

import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.List;

import static com.google.common.collect.ImmutableList.toImmutableList;
Expand All @@ -59,7 +58,7 @@
import static io.trino.util.CompilerUtils.defineClass;
import static io.trino.util.CompilerUtils.makeClassName;
import static io.trino.util.Failures.checkCondition;
import static java.lang.invoke.MethodHandles.lookup;
import static io.trino.util.Reflection.methodHandle;
import static java.util.Collections.nCopies;

public final class ArrayConstructor
Expand Down Expand Up @@ -102,14 +101,7 @@ protected SpecializedSqlScalarFunction specialize(BoundSignature boundSignature)
}
ImmutableList<Class<?>> stackTypes = builder.build();
Class<?> clazz = generateArrayConstructor(stackTypes, type);
MethodHandle methodHandle;
try {
Method method = clazz.getMethod("arrayConstructor", stackTypes.toArray(new Class<?>[stackTypes.size()]));
methodHandle = lookup().unreflect(method);
}
catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
MethodHandle methodHandle = methodHandle(clazz, "arrayConstructor", stackTypes.toArray(new Class<?>[stackTypes.size()]));
return new ChoicesSpecializedSqlScalarFunction(
boundSignature,
FAIL_ON_NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 io.trino.spi.TrinoException;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;

import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;

/**
* @apiNote This mirrors {@code io.trino.util.Reflection}.
*/
public final class Reflection
{
private Reflection() {}

/**
* Returns a MethodHandle corresponding to the specified method.
* <p>
* Warning: The way Oracle JVM implements producing MethodHandle for a method involves creating
* JNI global weak references. G1 processes such references serially. As a result, calling this
* method in a tight loop can create significant GC pressure and significantly increase
* application pause time.
*/
public static MethodHandle methodHandle(Class<?> clazz, String name, Class<?>... parameterTypes)
{
try {
return MethodHandles.lookup().unreflect(clazz.getMethod(name, parameterTypes));
}
catch (IllegalAccessException | NoSuchMethodException e) {
throw new TrinoException(GENERIC_INTERNAL_ERROR, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,13 @@
import java.lang.invoke.MethodHandle;
import java.util.Optional;

import static java.lang.invoke.MethodHandles.lookup;
import static io.trino.plugin.base.util.Reflection.methodHandle;
import static java.util.Objects.requireNonNull;

public class FlushJdbcMetadataCacheProcedure
implements Provider<Procedure>
{
private static final MethodHandle FLUSH_JDBC_METADATA_CACHE;

static {
try {
FLUSH_JDBC_METADATA_CACHE = lookup().unreflect(FlushJdbcMetadataCacheProcedure.class.getMethod("flushMetadataCache"));
}
catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
private static final MethodHandle FLUSH_JDBC_METADATA_CACHE = methodHandle(FlushJdbcMetadataCacheProcedure.class, "flushMetadataCache");

private final CachingJdbcClient cachingJdbcClient;
private final Optional<CachingIdentifierMapping> cachingIdentifierMapping;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,16 @@
import java.util.List;

import static io.trino.plugin.base.util.Procedures.checkProcedureArgument;
import static io.trino.plugin.base.util.Reflection.methodHandle;
import static io.trino.spi.StandardErrorCode.INVALID_PROCEDURE_ARGUMENT;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.lang.String.format;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Objects.requireNonNull;

public class DropExtendedStatsProcedure
implements Provider<Procedure>
{
private static final MethodHandle PROCEDURE_METHOD;

static {
try {
PROCEDURE_METHOD = lookup().unreflect(DropExtendedStatsProcedure.class.getMethod("dropStats", ConnectorSession.class, ConnectorAccessControl.class, String.class, String.class));
}
catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
private static final MethodHandle PROCEDURE_METHOD = methodHandle(DropExtendedStatsProcedure.class, "dropStats", ConnectorSession.class, ConnectorAccessControl.class, String.class, String.class);

private final DeltaLakeMetadataFactory metadataFactory;
private final ExtendedStatisticsAccess statsAccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import java.lang.invoke.MethodHandle;
import java.util.Optional;

import static io.trino.plugin.base.util.Reflection.methodHandle;
import static io.trino.spi.StandardErrorCode.INVALID_PROCEDURE_ARGUMENT;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Objects.requireNonNull;

public class FlushMetadataCacheProcedure
Expand All @@ -40,16 +40,7 @@ public class FlushMetadataCacheProcedure
private static final String PARAM_SCHEMA_NAME = "SCHEMA_NAME";
private static final String PARAM_TABLE_NAME = "TABLE_NAME";

private static final MethodHandle FLUSH_METADATA_CACHE;

static {
try {
FLUSH_METADATA_CACHE = lookup().unreflect(FlushMetadataCacheProcedure.class.getMethod("flushMetadataCache", String.class, String.class));
}
catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
private static final MethodHandle FLUSH_METADATA_CACHE = methodHandle(FlushMetadataCacheProcedure.class, "flushMetadataCache", String.class, String.class);

private final Optional<CachingHiveMetastore> cachingHiveMetastore;
private final TransactionLogAccess transactionLogAccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static io.trino.plugin.base.util.Procedures.checkProcedureArgument;
import static io.trino.plugin.base.util.Reflection.methodHandle;
import static io.trino.plugin.deltalake.DeltaLakeErrorCode.DELTA_LAKE_FILESYSTEM_ERROR;
import static io.trino.plugin.deltalake.DeltaLakeErrorCode.DELTA_LAKE_INVALID_TABLE;
import static io.trino.plugin.deltalake.DeltaLakeMetadata.buildTable;
Expand All @@ -49,13 +50,12 @@
import static io.trino.spi.StandardErrorCode.PERMISSION_DENIED;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.lang.String.format;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Objects.requireNonNull;

public class RegisterTableProcedure
implements Provider<Procedure>
{
private static final MethodHandle REGISTER_TABLE;
private static final MethodHandle REGISTER_TABLE = methodHandle(RegisterTableProcedure.class, "registerTable", ConnectorSession.class, String.class, String.class, String.class);

private static final String PROCEDURE_NAME = "register_table";
private static final String SYSTEM_SCHEMA = "system";
Expand All @@ -64,15 +64,6 @@ public class RegisterTableProcedure
private static final String TABLE_NAME = "TABLE_NAME";
private static final String TABLE_LOCATION = "TABLE_LOCATION";

static {
try {
REGISTER_TABLE = lookup().unreflect(RegisterTableProcedure.class.getMethod("registerTable", ConnectorSession.class, String.class, String.class, String.class));
}
catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}

private final DeltaLakeMetadataFactory metadataFactory;
private final TransactionLogAccess transactionLogAccess;
private final CachingExtendedStatisticsAccess statisticsAccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,21 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static io.trino.plugin.base.util.Procedures.checkProcedureArgument;
import static io.trino.plugin.base.util.Reflection.methodHandle;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Objects.requireNonNull;

public class UnregisterTableProcedure
implements Provider<Procedure>
{
private static final MethodHandle UNREGISTER_TABLE;
private static final MethodHandle UNREGISTER_TABLE = methodHandle(UnregisterTableProcedure.class, "unregisterTable", ConnectorAccessControl.class, ConnectorSession.class, String.class, String.class);

private static final String PROCEDURE_NAME = "unregister_table";
private static final String SYSTEM_SCHEMA = "system";

private static final String SCHEMA_NAME = "SCHEMA_NAME";
private static final String TABLE_NAME = "TABLE_NAME";

static {
try {
UNREGISTER_TABLE = lookup().unreflect(UnregisterTableProcedure.class.getMethod("unregisterTable", ConnectorAccessControl.class, ConnectorSession.class, String.class, String.class));
}
catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}

private final DeltaLakeMetadataFactory metadataFactory;
private final TransactionLogAccess transactionLogAccess;
private final CachingExtendedStatisticsAccess statisticsAccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.plugin.base.util.Procedures.checkProcedureArgument;
import static io.trino.plugin.base.util.Reflection.methodHandle;
import static io.trino.plugin.deltalake.DeltaLakeMetadata.MAX_WRITER_VERSION;
import static io.trino.plugin.deltalake.DeltaLakeMetadata.checkValidTableHandle;
import static io.trino.plugin.deltalake.DeltaLakeSessionProperties.getVacuumMinRetention;
Expand All @@ -65,7 +66,6 @@
import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED;
import static io.trino.spi.type.VarcharType.VARCHAR;
import static java.lang.String.format;
import static java.lang.invoke.MethodHandles.lookup;
import static java.util.Comparator.naturalOrder;
import static java.util.Objects.requireNonNull;

Expand All @@ -75,16 +75,7 @@ public class VacuumProcedure
private static final Logger log = Logger.get(VacuumProcedure.class);
private static final int DELETE_BATCH_SIZE = 1000;

private static final MethodHandle VACUUM;

static {
try {
VACUUM = lookup().unreflect(VacuumProcedure.class.getMethod("vacuum", ConnectorSession.class, ConnectorAccessControl.class, String.class, String.class, String.class));
}
catch (ReflectiveOperationException e) {
throw new AssertionError(e);
}
}
private static final MethodHandle VACUUM = methodHandle(VacuumProcedure.class, "vacuum", ConnectorSession.class, ConnectorAccessControl.class, String.class, String.class, String.class);

private final CatalogName catalogName;
private final TrinoFileSystemFactory fileSystemFactory;
Expand Down
Loading