Skip to content

Commit

Permalink
Move ThriftHiveMetastore alternate calls to ThriftHiveMetastoreClient
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Aug 13, 2022
1 parent 47391c7 commit 280fee3
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 280 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import static java.lang.Math.toIntExact;
import static java.util.Collections.list;
Expand All @@ -57,6 +58,11 @@ public class DefaultThriftMetastoreClientFactory
private final HiveMetastoreAuthentication metastoreAuthentication;
private final String hostname;

private final MetastoreSupportsDateStatistics metastoreSupportsDateStatistics = new MetastoreSupportsDateStatistics();
private final AtomicInteger chosenGetTableAlternative = new AtomicInteger(Integer.MAX_VALUE);
private final AtomicInteger chosenTableParamAlternative = new AtomicInteger(Integer.MAX_VALUE);
private final AtomicInteger chosenGetAllViewsAlternative = new AtomicInteger(Integer.MAX_VALUE);

public DefaultThriftMetastoreClientFactory(
Optional<SSLContext> sslContext,
Optional<HostAndPort> socksProxy,
Expand Down Expand Up @@ -101,7 +107,11 @@ protected ThriftMetastoreClient create(TTransport transport, String hostname)
{
return new ThriftHiveMetastoreClient(
transport,
hostname);
hostname,
metastoreSupportsDateStatistics,
chosenGetTableAlternative,
chosenTableParamAlternative,
chosenGetAllViewsAlternative);
}

private TTransport createTransport(HostAndPort address, Optional<String> delegationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ public List<String> getAllTables(String databaseName)
}

@Override
public List<String> getTableNamesByFilter(String databaseName, String filter)
public List<String> getAllViews(String databaseName)
throws TException
{
return runWithHandle(() -> delegate.getTableNamesByFilter(databaseName, filter));
return runWithHandle(() -> delegate.getAllViews(databaseName));
}

@Override
public List<String> getTableNamesByType(String databaseName, String tableType)
public List<String> getTablesWithParameter(String databaseName, String parameterKey, String parameterValue)
throws TException
{
return runWithHandle(() -> delegate.getTableNamesByType(databaseName, tableType));
return runWithHandle(() -> delegate.getTablesWithParameter(databaseName, parameterKey, parameterValue));
}

@Override
Expand Down Expand Up @@ -153,13 +153,6 @@ public Table getTable(String databaseName, String tableName)
return runWithHandle(() -> delegate.getTable(databaseName, tableName));
}

@Override
public Table getTableWithCapabilities(String databaseName, String tableName)
throws TException
{
return runWithHandle(() -> delegate.getTableWithCapabilities(databaseName, tableName));
}

@Override
public List<FieldSchema> getFields(String databaseName, String tableName)
throws TException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.hive.metastore.thrift;

import io.airlift.units.Duration;

import javax.annotation.concurrent.ThreadSafe;

import java.util.concurrent.atomic.AtomicReference;

import static io.trino.plugin.hive.metastore.thrift.MetastoreSupportsDateStatistics.DateStatisticsSupport.NOT_SUPPORTED;
import static io.trino.plugin.hive.metastore.thrift.MetastoreSupportsDateStatistics.DateStatisticsSupport.SUPPORTED;
import static io.trino.plugin.hive.metastore.thrift.MetastoreSupportsDateStatistics.DateStatisticsSupport.UNKNOWN;
import static java.util.concurrent.TimeUnit.SECONDS;

@ThreadSafe
class MetastoreSupportsDateStatistics
{
private static final int MAX_SET_DATE_STATISTICS_ATTEMPTS = 100;

public enum DateStatisticsSupport {
SUPPORTED, NOT_SUPPORTED, UNKNOWN
}

private final AtomicReference<DateStatisticsSupport> supported = new AtomicReference<>(UNKNOWN);
private final CoalescingCounter failures = new CoalescingCounter(new Duration(1, SECONDS));

public DateStatisticsSupport isSupported()
{
return supported.get();
}

public void succeeded()
{
supported.set(SUPPORTED);
}

public void failed()
{
// When `dateStatistics.size() > 1` we expect something like "TApplicationException: Required field 'colName' is unset! Struct:ColumnStatisticsObj(colName:null, colType:null, statsData:null)"
// When `dateStatistics.size() == 1` we expect something like "TTransportException: java.net.SocketTimeoutException: Read timed out"
if (failures.incrementAndGet() >= MAX_SET_DATE_STATISTICS_ATTEMPTS) {
supported.set(NOT_SUPPORTED);
}
}
}
Loading

0 comments on commit 280fee3

Please sign in to comment.