From 44e3b475ac5be15b81ad34e13064b5a34d510ebc Mon Sep 17 00:00:00 2001 From: Mateusz Gajewski Date: Thu, 30 Jun 2022 13:04:30 +0200 Subject: [PATCH] Patch phoenix5 client to work with JDK17 --- lib/trino-phoenix5-patched/pom.xml | 90 ++++++++++ .../zookeeper/client/StaticHostProvider.java | 167 ++++++++++++++++++ .../java/org.apache.phoenix/TestDummy.java | 22 +++ plugin/trino-phoenix5/pom.xml | 42 ++++- pom.xml | 11 +- 5 files changed, 325 insertions(+), 7 deletions(-) create mode 100644 lib/trino-phoenix5-patched/pom.xml create mode 100644 lib/trino-phoenix5-patched/src/main/java/org/apache/phoenix/shaded/org/apache/zookeeper/client/StaticHostProvider.java create mode 100644 lib/trino-phoenix5-patched/src/test/java/org.apache.phoenix/TestDummy.java diff --git a/lib/trino-phoenix5-patched/pom.xml b/lib/trino-phoenix5-patched/pom.xml new file mode 100644 index 000000000000..cf7c4b98822a --- /dev/null +++ b/lib/trino-phoenix5-patched/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + + + io.trino + trino-root + 389-SNAPSHOT + ../../pom.xml + + + trino-phoenix5-patched + trino-phoenix5-patched + Trino - patched Phoenix5 client to work with JDK17 + + + ${project.parent.basedir} + + + + + org.apache.phoenix + phoenix-client-embedded-hbase-2.2 + 5.1.2 + + + + org.testng + testng + test + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.3.0 + + + package + + shade + + + true + false + false + ${project.build.directory}/pom.xml + false + + + + + + + org.apache.zookeeper + org.apache.phoenix.shaded.org.apache.zookeeper + + + + + org.apache.phoenix:phoenix-client-embedded-hbase-2.2 + + org/apache/phoenix/shaded/org/apache/zookeeper/client/StaticHostProvider.class + org/apache/phoenix/shaded/org/apache/zookeeper/client/StaticHostProvider$*.class + + + + + + + + + org.basepom.maven + duplicate-finder-maven-plugin + + + + org.apache.phoenix + phoenix-client-embedded-hbase-2.2 + + + + + + + diff --git a/lib/trino-phoenix5-patched/src/main/java/org/apache/phoenix/shaded/org/apache/zookeeper/client/StaticHostProvider.java b/lib/trino-phoenix5-patched/src/main/java/org/apache/phoenix/shaded/org/apache/zookeeper/client/StaticHostProvider.java new file mode 100644 index 000000000000..cb089c757f81 --- /dev/null +++ b/lib/trino-phoenix5-patched/src/main/java/org/apache/phoenix/shaded/org/apache/zookeeper/client/StaticHostProvider.java @@ -0,0 +1,167 @@ +/* + * 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 org.apache.phoenix.shaded.org.apache.zookeeper.client; + +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +// TODO(https://github.com/trinodb/trino/issues/13051): Remove when Phoenix 5.2 is release +public final class StaticHostProvider + implements HostProvider +{ + public interface Resolver + { + InetAddress[] getAllByName(String name) throws UnknownHostException; + } + + private final List serverAddresses = new ArrayList(5); + + private int lastIndex = -1; + + private int currentIndex = -1; + + private Resolver resolver; + + /** + * Constructs a SimpleHostSet. + * + * @param serverAddresses + * possibly unresolved ZooKeeper server addresses + * @throws IllegalArgumentException + * if serverAddresses is empty or resolves to an empty list + */ + public StaticHostProvider(Collection serverAddresses) + { + this.resolver = name -> InetAddress.getAllByName(name); + init(serverAddresses); + } + + /** + * Introduced for testing purposes. getAllByName() is a static method of InetAddress, therefore cannot be easily mocked. + * By abstraction of Resolver interface we can easily inject a mocked implementation in tests. + * + * @param serverAddresses + * possibly unresolved ZooKeeper server addresses + * @param resolver + * custom resolver implementation + * @throws IllegalArgumentException + * if serverAddresses is empty or resolves to an empty list + */ + public StaticHostProvider(Collection serverAddresses, Resolver resolver) + { + this.resolver = resolver; + init(serverAddresses); + } + + /** + * Common init method for all constructors. + * Resolve all unresolved server addresses, put them in a list and shuffle. + */ + private void init(Collection serverAddresses) + { + if (serverAddresses.isEmpty()) { + throw new IllegalArgumentException( + "A HostProvider may not be empty!"); + } + + this.serverAddresses.addAll(serverAddresses); + Collections.shuffle(this.serverAddresses); + } + + /** + * Evaluate to a hostname if one is available and otherwise it returns the + * string representation of the IP address. + * + * In Java 7, we have a method getHostString, but earlier versions do not support it. + * This method is to provide a replacement for InetSocketAddress.getHostString(). + * + * @param addr + * @return Hostname string of address parameter + */ + private String getHostString(InetSocketAddress addr) + { + String hostString = ""; + + if (addr == null) { + return hostString; + } + if (!addr.isUnresolved()) { + InetAddress ia = addr.getAddress(); + + // If the string starts with '/', then it has no hostname + // and we want to avoid the reverse lookup, so we return + // the string representation of the address. + if (ia.toString().startsWith("/")) { + hostString = ia.getHostAddress(); + } + else { + hostString = addr.getHostName(); + } + } + else { + hostString = addr.getHostString(); + } + + return hostString; + } + + @Override + public int size() + { + return serverAddresses.size(); + } + + @Override + public InetSocketAddress next(long spinDelay) + { + currentIndex = ++currentIndex % serverAddresses.size(); + if (currentIndex == lastIndex && spinDelay > 0) { + try { + Thread.sleep(spinDelay); + } + catch (InterruptedException e) { + } + } + else if (lastIndex == -1) { + // We don't want to sleep on the first ever connect attempt. + lastIndex = 0; + } + + InetSocketAddress curAddr = serverAddresses.get(currentIndex); + try { + String curHostString = getHostString(curAddr); + List resolvedAddresses = new ArrayList(Arrays.asList(this.resolver.getAllByName(curHostString))); + if (resolvedAddresses.isEmpty()) { + return curAddr; + } + Collections.shuffle(resolvedAddresses); + return new InetSocketAddress(resolvedAddresses.get(0), curAddr.getPort()); + } + catch (UnknownHostException e) { + return curAddr; + } + } + + @Override + public void onConnected() + { + lastIndex = currentIndex; + } +} diff --git a/lib/trino-phoenix5-patched/src/test/java/org.apache.phoenix/TestDummy.java b/lib/trino-phoenix5-patched/src/test/java/org.apache.phoenix/TestDummy.java new file mode 100644 index 000000000000..324d7da818bd --- /dev/null +++ b/lib/trino-phoenix5-patched/src/test/java/org.apache.phoenix/TestDummy.java @@ -0,0 +1,22 @@ +/* + * 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 org.apache.phoenix; + +import org.testng.annotations.Test; + +public class TestDummy +{ + @Test + public void buildRequiresTestToExist() {} +} diff --git a/plugin/trino-phoenix5/pom.xml b/plugin/trino-phoenix5/pom.xml index 008f29adae8d..b5d271c0800b 100644 --- a/plugin/trino-phoenix5/pom.xml +++ b/plugin/trino-phoenix5/pom.xml @@ -16,6 +16,12 @@ ${project.parent.basedir} 2.2.6 + + + + --add-opens=java.base/sun.nio.ch=ALL-UNNAMED + --add-opens=java.base/java.nio=ALL-UNNAMED + @@ -24,6 +30,18 @@ trino-base-jdbc + + + io.trino + trino-phoenix5-patched + + + org.apache.phoenix + * + + + + io.trino trino-plugin-toolkit @@ -84,12 +102,6 @@ joda-time - - org.apache.phoenix - phoenix-client-embedded-hbase-2.2 - 5.1.2 - - org.gaul modernizer-maven-annotations @@ -367,9 +379,27 @@ javax.xml.bind jaxb-api + + org.apache.phoenix + phoenix-client-embedded-hbase-2.2 + + + + + + errorprone-compiler + + + org.apache.phoenix + phoenix-client-embedded-hbase-2.2 + 5.1.2 + + + + diff --git a/pom.xml b/pom.xml index f6721cf5b221..453e1fc9565c 100644 --- a/pom.xml +++ b/pom.xml @@ -105,6 +105,8 @@ lib/trino-memory-context lib/trino-orc lib/trino-parquet + + lib/trino-phoenix5-patched lib/trino-plugin-toolkit lib/trino-rcfile lib/trino-record-decoder @@ -440,6 +442,13 @@ ${project.version} + + + io.trino + trino-phoenix5-patched + ${project.version} + + io.trino trino-pinot @@ -1979,7 +1988,7 @@ io.github.gitflow-incremental-builder gitflow-incremental-builder - 4.1.0 + 4.1.1 true master