Skip to content

Commit

Permalink
use bytebuddy instead
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Nov 24, 2021
1 parent bde0c11 commit 05d460d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import com.google.protobuf.ByteString;
import io.grpc.Status;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -1337,4 +1339,12 @@ public int getMasterInfoPort() throws IOException {
public void rollWALWriter(ServerName serverName) throws IOException, FailedLogCloseException {
throw new UnsupportedOperationException("rollWALWriter"); // TODO
}

/** Handler for unsupported operations for generating Admin class at runtime. */
public static class UnsupportedOperationsHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
throw new UnsupportedOperationException(method.getName());
}
}
}
5 changes: 3 additions & 2 deletions bigtable-hbase-1.x-parent/bigtable-hbase-1.x-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ limitations under the License.
<exclude>META-INF/NOTICE</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/NOTICE.txt</exclude>
<exclude>META-INF/versions/9/module-info.class</exclude>
</excludes>
</filter>
</filters>
Expand Down Expand Up @@ -332,8 +333,8 @@ limitations under the License.
<shadedPattern>com.google.bigtable.repackaged.javax.annotation</shadedPattern>
</relocation>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>com.google.bigtable.repackaged.javassist</shadedPattern>
<pattern>net.bytebuddy</pattern>
<shadedPattern>com.google.bigtable.repackaged.net.bytebuddy</shadedPattern>
</relocation>
</relocations>
</configuration>
Expand Down
7 changes: 4 additions & 3 deletions bigtable-hbase-1.x-parent/bigtable-hbase-1.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ limitations under the License.
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.2</version>
</dependency>


<!-- Test dependencies-->
<dependency>
<groupId>com.google.cloud.bigtable</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
import com.google.cloud.bigtable.hbase.adapters.SampledRowKeysAdapter;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.ExecutorService;
import javassist.Modifier;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.InvocationHandlerAdapter;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.AbstractBigtableAdmin;
import org.apache.hadoop.hbase.client.AbstractBigtableConnection;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.CommonConnection;
Expand Down Expand Up @@ -64,29 +65,24 @@ public BigtableConnection(Configuration conf) throws IOException {
/** {@inheritDoc} */
@Override
public Admin getAdmin() throws IOException {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(BigtableAdmin.class);
factory.setFilter(
new MethodFilter() {
@Override
public boolean isHandled(Method method) {
return Modifier.isAbstract(method.getModifiers());
}
});

MethodHandler handler =
new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
throws Throwable {
throw new UnsupportedOperationException(thisMethod.getName());
}
};
try {
Admin admin =
(Admin)
factory.create(
new Class<?>[] {CommonConnection.class}, new CommonConnection[] {this}, handler);
BigtableAdmin admin =
new ByteBuddy()
.subclass(BigtableAdmin.class)
.defineConstructor(Visibility.PUBLIC)
.intercept(
MethodCall.invoke(
BigtableAdmin.class.getDeclaredConstructor(CommonConnection.class))
.with(this))
.method(ElementMatchers.isAbstract())
.intercept(
InvocationHandlerAdapter.of(
new AbstractBigtableAdmin.UnsupportedOperationsHandler()))
.make()
.load(BigtableAdmin.class.getClassLoader())
.getLoaded()
.getDeclaredConstructor()
.newInstance();
return admin;
} catch (Exception e) {
throw new IOException(e);
Expand Down
5 changes: 3 additions & 2 deletions bigtable-hbase-2.x-parent/bigtable-hbase-2.x-shaded/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ limitations under the License.
<exclude>META-INF/NOTICE</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/NOTICE.txt</exclude>
<exclude>META-INF/versions/9/module-info.class</exclude>
</excludes>
</filter>
</filters>
Expand Down Expand Up @@ -322,8 +323,8 @@ limitations under the License.
<shadedPattern>com.google.bigtable.repackaged.javax.annotation</shadedPattern>
</relocation>
<relocation>
<pattern>javassist</pattern>
<shadedPattern>com.google.bigtable.repackaged.javassist</shadedPattern>
<pattern>net.bytebuddy</pattern>
<shadedPattern>com.google.bigtable.repackaged.net.bytebuddy</shadedPattern>
</relocation>
</relocations>
</configuration>
Expand Down
6 changes: 3 additions & 3 deletions bigtable-hbase-2.x-parent/bigtable-hbase-2.x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ limitations under the License.
<artifactId>threetenbp</artifactId>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.12.2</version>
</dependency>

<!-- Test dependencies-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.Futures;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
Expand All @@ -32,10 +31,11 @@
import java.util.concurrent.CompletionException;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import javassist.Modifier;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.InvocationHandlerAdapter;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.hadoop.hbase.ClusterMetrics;
import org.apache.hadoop.hbase.ClusterMetrics.Option;
import org.apache.hadoop.hbase.ClusterStatus;
Expand Down Expand Up @@ -65,31 +65,24 @@ public abstract class BigtableAdmin extends AbstractBigtableAdmin {

public BigtableAdmin(AbstractBigtableConnection connection) throws IOException {
super(connection);
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(BigtableAsyncAdmin.class);
factory.setFilter(
new MethodFilter() {
@Override
public boolean isHandled(Method method) {
return Modifier.isAbstract(method.getModifiers());
}
});

MethodHandler handler =
new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
throws Throwable {
throw new UnsupportedOperationException(thisMethod.getName());
}
};
try {
BigtableAsyncAdmin admin =
(BigtableAsyncAdmin)
factory.create(
new Class<?>[] {CommonConnection.class},
new CommonConnection[] {connection},
handler);
new ByteBuddy()
.subclass(BigtableAsyncAdmin.class)
.defineConstructor(Visibility.PUBLIC)
.intercept(
MethodCall.invoke(
BigtableAsyncAdmin.class.getDeclaredConstructor(CommonConnection.class))
.with(connection))
.method(ElementMatchers.isAbstract())
.intercept(
InvocationHandlerAdapter.of(
new AbstractBigtableAdmin.UnsupportedOperationsHandler()))
.make()
.load(BigtableAsyncAdmin.class.getClassLoader())
.getLoaded()
.getDeclaredConstructor()
.newInstance();
asyncAdmin = admin;
} catch (Exception e) {
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import javassist.Modifier;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.InvocationHandlerAdapter;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HRegionLocation;
import org.apache.hadoop.hbase.ServerName;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.AbstractBigtableAdmin;
import org.apache.hadoop.hbase.client.AbstractBigtableConnection;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Hbck;
Expand Down Expand Up @@ -87,31 +89,25 @@ protected HRegionLocation createRegionLocation(byte[] startKey, byte[] endKey) {
/** {@inheritDoc} */
@Override
public Admin getAdmin() throws IOException {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(BigtableAdmin.class);
factory.setFilter(
new MethodFilter() {
@Override
public boolean isHandled(Method method) {
return Modifier.isAbstract(method.getModifiers());
}
});

MethodHandler handler =
new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
throws Throwable {
throw new UnsupportedOperationException(thisMethod.getName());
}
};
try {
Admin admin =
(Admin)
factory.create(
new Class<?>[] {AbstractBigtableConnection.class},
new AbstractBigtableConnection[] {this},
handler);
BigtableAdmin admin =
new ByteBuddy()
.subclass(BigtableAdmin.class)
.defineConstructor(Visibility.PUBLIC)
.intercept(
MethodCall.invoke(
BigtableAdmin.class.getDeclaredConstructor(
AbstractBigtableConnection.class))
.with(this))
.method(ElementMatchers.isAbstract())
.intercept(
InvocationHandlerAdapter.of(
new AbstractBigtableAdmin.UnsupportedOperationsHandler()))
.make()
.load(BigtableAdmin.class.getClassLoader())
.getLoaded()
.getDeclaredConstructor()
.newInstance();
return admin;
} catch (Exception e) {
throw new IOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import com.google.cloud.bigtable.hbase2_x.BigtableAsyncTableRegionLocator;
import java.io.Closeable;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -45,10 +43,11 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javassist.Modifier;
import javassist.util.proxy.MethodFilter;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.implementation.InvocationHandlerAdapter;
import net.bytebuddy.implementation.MethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.HRegionLocation;
Expand Down Expand Up @@ -177,38 +176,28 @@ public AsyncAdminBuilder setMaxAttempts(int arg0) {
@Override
public AsyncAdmin build() {
try {
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(BigtableAsyncAdmin.class);
factory.setFilter(
new MethodFilter() {
@Override
public boolean isHandled(Method method) {
return Modifier.isAbstract(method.getModifiers());
}
});

MethodHandler handler =
new MethodHandler() {
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
throws Throwable {
throw new UnsupportedOperationException(thisMethod.getName());
}
};
try {
AsyncAdmin admin =
(AsyncAdmin)
factory.create(
new Class<?>[] {CommonConnection.class},
new CommonConnection[] {BigtableAsyncConnection.this},
handler);
return admin;
} catch (Exception e) {
throw new IOException(e);
}
} catch (IOException e) {
BigtableAsyncAdmin admin =
new ByteBuddy()
.subclass(BigtableAsyncAdmin.class)
.defineConstructor(Visibility.PUBLIC)
.intercept(
MethodCall.invoke(
BigtableAsyncAdmin.class.getDeclaredConstructor(
CommonConnection.class))
.with(BigtableAsyncConnection.this))
.method(ElementMatchers.isAbstract())
.intercept(
InvocationHandlerAdapter.of(
new AbstractBigtableAdmin.UnsupportedOperationsHandler()))
.make()
.load(BigtableAsyncAdmin.class.getClassLoader())
.getLoaded()
.getDeclaredConstructor()
.newInstance();
return admin;
} catch (Exception e) {
LOG.error("failed to build BigtableAsyncAdmin", e);
throw new UncheckedIOException("failed to build BigtableAsyncAdmin", e);
throw new RuntimeException("failed to build BigtableAsyncAdmin", e);
}
}
};
Expand Down

0 comments on commit 05d460d

Please sign in to comment.