Skip to content

Commit

Permalink
Added extra option to add readOnly thrift HMS uri (#308)
Browse files Browse the repository at this point in the history
* Added extra option to add readOnly thrift HMS uri which will be called on read only calls for better spread of traffic
  • Loading branch information
Patrick Duin committed Feb 8, 2024
1 parent f6520ea commit 19f57a5
Show file tree
Hide file tree
Showing 22 changed files with 318 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## [3.12.0] - 2023-02-08
### Added
- Added optional `primary-meta-store.read-only-remote-meta-store-uris` config to allow traffic to be diverted based on calls made. See README.md.

## [3.11.7] - 2023-11-30
### Changed
- Fixed log statement that was not logging the exception correctly.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ The table below describes all the available configuration values for Waggle Danc
| `primary-meta-store.hive-metastore-filter-hook` | No | Name of the class which implements the `MetaStoreFilterHook` interface from Hive. This allows a metastore filter hook to be applied to the corresponding Hive metastore calls. Can be configured with the `configuration-properties` specified in the `waggle-dance-server.yml` configuration. They will be added in the HiveConf object that is given to the constructor of the `MetaStoreFilterHook` implementation you provide. |
| `primary-meta-store.database-name-mapping` | No | BiDirectional Map of database names and mapped name, where key=`<database name as known in the primary metastore>` and value=`<name that should be shown to a client>`. See the [Database Name Mapping](#database-name-mapping) section.|
| `primary-meta-store.glue-config` | No | Can be used instead of `remote-meta-store-uris` to federate to an AWS Glue Catalog ([AWS Glue](https://docs.aws.amazon.com/glue/index.html). See the [Federate to AWS Glue Catalog](#federate-to-aws-glue-catalog) section.|
| `primary-meta-store.read-only-remote-meta-store-uris` | No | Can be used to configure an extra read-only endpoint for the primary Metastore. This is an optimization if your environment runs separate Metastore endpoints and traffic needs to be diverted efficiently. Waggle Dance will direct traffic to the read-write or read-only endpoints based on the call being done. For instance `get_table` will be a read-only call but `alter_table` will be forwarded to the read-write Metastore.|
| `federated-meta-stores` | No | Possible empty list of read only federated metastores. |
| `federated-meta-stores[n].remote-meta-store-uris` | Yes | Thrift URIs of the federated read-only metastore. |
| `federated-meta-stores[n].name` | Yes | Name that uniquely identifies this metastore. Used internally. Cannot be empty. |
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.hotels</groupId>
<artifactId>waggle-dance-parent</artifactId>
<version>3.11.8-SNAPSHOT</version>
<version>3.12.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Waggle Dance Parent</name>
<description>Hive Metastore federation service.</description>
Expand Down
2 changes: 1 addition & 1 deletion waggle-dance-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.hotels</groupId>
<artifactId>waggle-dance-parent</artifactId>
<version>3.11.8-SNAPSHOT</version>
<version>3.12.0-SNAPSHOT</version>
</parent>

<artifactId>waggle-dance-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2023 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -57,6 +57,7 @@ public abstract class AbstractMetaStore {
private long latency = 0;
private transient @JsonIgnore HashBiMap<String, String> databaseNameBiMapping = HashBiMap.create();
private GlueConfig glueConfig;
private String readOnlyRemoteMetaStoreUris;

public AbstractMetaStore() {}

Expand Down Expand Up @@ -124,6 +125,14 @@ public void setRemoteMetaStoreUris(String remoteMetaStoreUris) {
this.remoteMetaStoreUris = remoteMetaStoreUris;
}

public String getReadOnlyRemoteMetaStoreUris() {
return readOnlyRemoteMetaStoreUris;
}

public void setReadOnlyRemoteMetaStoreUris(String readOnlyRemoteMetaStoreUris) {
this.readOnlyRemoteMetaStoreUris = readOnlyRemoteMetaStoreUris;
}

public MetastoreTunnel getMetastoreTunnel() {
return metastoreTunnel;
}
Expand Down Expand Up @@ -244,6 +253,7 @@ public String toString() {
.add("databasePrefix", databasePrefix)
.add("federationType", getFederationType())
.add("remoteMetaStoreUris", remoteMetaStoreUris)
.add("readOnlyRemoteMetaStoreUris", readOnlyRemoteMetaStoreUris)
.add("metastoreTunnel", metastoreTunnel)
.add("accessControlType", accessControlType)
.add("writableDatabaseWhiteList", writableDatabaseWhitelist)
Expand All @@ -252,4 +262,5 @@ public String toString() {
.toString();
}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2022 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -72,7 +72,7 @@ public void nullDatabasePrefix() {

@Test
public void toJson() throws Exception {
String expected = "{\"accessControlType\":\"READ_ONLY\",\"connectionType\":\"DIRECT\",\"databaseNameMapping\":{},\"databasePrefix\":\"name_\",\"federationType\":\"FEDERATED\",\"glueConfig\":null,\"hiveMetastoreFilterHook\":null,\"latency\":0,\"mappedDatabases\":null,\"mappedTables\":null,\"metastoreTunnel\":null,\"name\":\"name\",\"remoteMetaStoreUris\":\"uri\",\"status\":\"UNKNOWN\",\"writableDatabaseWhiteList\":[]}";
String expected = "{\"accessControlType\":\"READ_ONLY\",\"connectionType\":\"DIRECT\",\"databaseNameMapping\":{},\"databasePrefix\":\"name_\",\"federationType\":\"FEDERATED\",\"glueConfig\":null,\"hiveMetastoreFilterHook\":null,\"latency\":0,\"mappedDatabases\":null,\"mappedTables\":null,\"metastoreTunnel\":null,\"name\":\"name\",\"readOnlyRemoteMetaStoreUris\":null,\"remoteMetaStoreUris\":\"uri\",\"status\":\"UNKNOWN\",\"writableDatabaseWhiteList\":[]}";
ObjectMapper mapper = new ObjectMapper();
// Sorting to get deterministic test behaviour
mapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2022 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,7 +89,7 @@ public void nonEmptyDatabasePrefix() {

@Test
public void toJson() throws Exception {
String expected = "{\"accessControlType\":\"READ_ONLY\",\"connectionType\":\"DIRECT\",\"databaseNameMapping\":{},\"databasePrefix\":\"\",\"federationType\":\"PRIMARY\",\"glueConfig\":null,\"hiveMetastoreFilterHook\":null,\"latency\":0,\"mappedDatabases\":null,\"mappedTables\":null,\"metastoreTunnel\":null,\"name\":\"name\",\"remoteMetaStoreUris\":\"uri\",\"status\":\"UNKNOWN\",\"writableDatabaseWhiteList\":[]}";
String expected = "{\"accessControlType\":\"READ_ONLY\",\"connectionType\":\"DIRECT\",\"databaseNameMapping\":{},\"databasePrefix\":\"\",\"federationType\":\"PRIMARY\",\"glueConfig\":null,\"hiveMetastoreFilterHook\":null,\"latency\":0,\"mappedDatabases\":null,\"mappedTables\":null,\"metastoreTunnel\":null,\"name\":\"name\",\"readOnlyRemoteMetaStoreUris\":null,\"remoteMetaStoreUris\":\"uri\",\"status\":\"UNKNOWN\",\"writableDatabaseWhiteList\":[]}";
ObjectMapper mapper = new ObjectMapper();
// Sorting to get deterministic test behaviour
mapper.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
Expand Down
2 changes: 1 addition & 1 deletion waggle-dance-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.hotels</groupId>
<artifactId>waggle-dance-parent</artifactId>
<version>3.11.8-SNAPSHOT</version>
<version>3.12.0-SNAPSHOT</version>
</parent>

<artifactId>waggle-dance-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion waggle-dance-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>com.hotels</groupId>
<artifactId>waggle-dance-parent</artifactId>
<version>3.11.8-SNAPSHOT</version>
<version>3.12.0-SNAPSHOT</version>
</parent>

<artifactId>waggle-dance-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2022 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,16 +44,19 @@ public class CloseableThriftHiveMetastoreIfaceClientFactory {
private final int defaultConnectionTimeout = (int) TimeUnit.SECONDS.toMillis(2L);
private final WaggleDanceConfiguration waggleDanceConfiguration;
private final GlueClientFactory glueClientFactory;
private final SplitTrafficMetastoreClientFactory splitTrafficMetaStoreClientFactory;

public CloseableThriftHiveMetastoreIfaceClientFactory(
TunnelingMetaStoreClientFactory tunnelingMetaStoreClientFactory,
DefaultMetaStoreClientFactory defaultMetaStoreClientFactory,
GlueClientFactory glueClientFactory,
WaggleDanceConfiguration waggleDanceConfiguration) {
WaggleDanceConfiguration waggleDanceConfiguration,
SplitTrafficMetastoreClientFactory splitTrafficMetaStoreClientFactory) {
this.tunnelingMetaStoreClientFactory = tunnelingMetaStoreClientFactory;
this.defaultMetaStoreClientFactory = defaultMetaStoreClientFactory;
this.glueClientFactory = glueClientFactory;
this.waggleDanceConfiguration = waggleDanceConfiguration;
this.splitTrafficMetaStoreClientFactory = splitTrafficMetaStoreClientFactory;
}

public CloseableThriftHiveMetastoreIface newInstance(AbstractMetaStore metaStore) {
Expand All @@ -64,14 +67,24 @@ public CloseableThriftHiveMetastoreIface newInstance(AbstractMetaStore metaStore
if (metaStore.getGlueConfig() != null) {
return newGlueInstance(metaStore, properties);
}
return newHiveInstance(metaStore, properties);
String name = metaStore.getName().toLowerCase(Locale.ROOT);
if (metaStore.getReadOnlyRemoteMetaStoreUris() != null) {
CloseableThriftHiveMetastoreIface readWrite = newHiveInstance(metaStore, name, metaStore.getRemoteMetaStoreUris(),
properties);
CloseableThriftHiveMetastoreIface readOnly = newHiveInstance(metaStore, name + "_ro",
metaStore.getReadOnlyRemoteMetaStoreUris(), properties);
return splitTrafficMetaStoreClientFactory.newInstance(readWrite, readOnly);

}
return newHiveInstance(metaStore, name, metaStore.getRemoteMetaStoreUris(), properties);
}

private CloseableThriftHiveMetastoreIface newHiveInstance(
AbstractMetaStore metaStore,
String name,
String metaStoreUris,
Map<String, String> properties) {
String uris = MetaStoreUriNormaliser.normaliseMetaStoreUris(metaStore.getRemoteMetaStoreUris());
String name = metaStore.getName().toLowerCase(Locale.ROOT);
String uris = MetaStoreUriNormaliser.normaliseMetaStoreUris(metaStoreUris);
// Connection timeout should not be less than 1
// A timeout of zero is interpreted as an infinite timeout, so this is avoided
int connectionTimeout = Math.max(1, defaultConnectionTimeout + (int) metaStore.getLatency());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright (C) 2016-2024 Expedia, Inc.
*
* 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 com.hotels.bdp.waggledance.client;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class splits the traffic for read only calls (get* for instance getTable, getPartition) to the readOnly client
* and everything else will go to readWrite client.
*/
public class SplitTrafficMetastoreClientFactory {

static final Class<?>[] INTERFACES = new Class<?>[] { CloseableThriftHiveMetastoreIface.class };

private static class SplitTrafficClientInvocationHandler implements InvocationHandler {

private static Logger log = LoggerFactory
.getLogger(SplitTrafficMetastoreClientFactory.SplitTrafficClientInvocationHandler.class);

private final CloseableThriftHiveMetastoreIface readWrite;
private final CloseableThriftHiveMetastoreIface readOnly;

public SplitTrafficClientInvocationHandler(
CloseableThriftHiveMetastoreIface readWrite,
CloseableThriftHiveMetastoreIface readOnly) {
this.readWrite = readWrite;
this.readOnly = readOnly;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
switch (method.getName()) {
case "isOpen":
return readWrite.isOpen() && readOnly.isOpen();
case "close":
try {
readWrite.close();
} finally {
readOnly.close();
}
return null;
case "set_ugi":
Object result = doRealCall(readWrite, method, args);
// we skip the result for readOnly (it should always be the same).
doRealCall(readOnly, method, args);
return result;
default:
if (method.getName().startsWith("get")) {
log.info("Calling {}.{}", "readOnly", method.getName());
return doRealCall(readOnly, method, args);
}
log.info("Calling {}.{}", "readWrite", method.getName());
return doRealCall(readWrite, method, args);
}
}

private Object doRealCall(CloseableThriftHiveMetastoreIface client, Method method, Object[] args)
throws IllegalAccessException, Throwable {
try {
return method.invoke(client, args);
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
throw realException;
}
}
}

public CloseableThriftHiveMetastoreIface newInstance(
CloseableThriftHiveMetastoreIface readWrite,
CloseableThriftHiveMetastoreIface readOnly) {
return (CloseableThriftHiveMetastoreIface) Proxy
.newProxyInstance(getClass().getClassLoader(), INTERFACES,
new SplitTrafficClientInvocationHandler(readWrite, readOnly));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2022 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2019 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2023 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import com.hotels.bdp.waggledance.client.CloseableThriftHiveMetastoreIfaceClientFactory;
import com.hotels.bdp.waggledance.client.DefaultMetaStoreClientFactory;
import com.hotels.bdp.waggledance.client.GlueClientFactory;
import com.hotels.bdp.waggledance.client.SplitTrafficMetastoreClientFactory;
import com.hotels.bdp.waggledance.client.tunnelling.TunnelingMetaStoreClientFactory;
import com.hotels.bdp.waggledance.conf.WaggleDanceConfiguration;
import com.hotels.bdp.waggledance.mapping.model.ASTQueryMapping;
Expand Down Expand Up @@ -53,11 +54,16 @@ public PrefixNamingStrategy prefixNamingStrategy(WaggleDanceConfiguration waggle
return new LowerCasePrefixNamingStrategy();
}

@Bean
public SplitTrafficMetastoreClientFactory splitTrafficMetaStoreClientFactory() {
return new SplitTrafficMetastoreClientFactory();
}

@Bean
public CloseableThriftHiveMetastoreIfaceClientFactory metaStoreClientFactory(
WaggleDanceConfiguration waggleDanceConfiguration) {
WaggleDanceConfiguration waggleDanceConfiguration, SplitTrafficMetastoreClientFactory splitTrafficMetaStoreClientFactory) {
return new CloseableThriftHiveMetastoreIfaceClientFactory(new TunnelingMetaStoreClientFactory(),
new DefaultMetaStoreClientFactory(), new GlueClientFactory(), waggleDanceConfiguration);
new DefaultMetaStoreClientFactory(), new GlueClientFactory(), waggleDanceConfiguration, splitTrafficMetaStoreClientFactory);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (C) 2016-2022 Expedia, Inc.
* Copyright (C) 2016-2024 Expedia, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit 19f57a5

Please sign in to comment.