Skip to content

Commit

Permalink
#683 - Replace internal proprietary sun.reflect.ReflectionFactory API…
Browse files Browse the repository at this point in the history
… with Objenesis API (#7582)

* #683 - Replace internal proprietary API sun.reflect.ReflectionFactory with Objenisis API

* #683 - Replace internal proprietary API sun.reflect.ReflectionFactory with Objenisis API

* #683 - Replace deprecated KeeperException.Code constant references with corresponding enums

* #683 - Fixed pom header formatting

* #683 - Made objenesis variable a static final member variable
  • Loading branch information
vcottagiri committed Jul 17, 2020
1 parent e9d4444 commit c404b1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Expand Up @@ -173,6 +173,7 @@ flexible messaging model and an intuitive client API.</description>
<failsafe.version>2.3.1</failsafe.version>
<skyscreamer.version>1.5.0</skyscreamer.version>
<confluent.version>5.2.2</confluent.version>
<objenesis.version>3.1</objenesis.version>

<!-- Plugin dependencies -->
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>
Expand Down Expand Up @@ -1078,31 +1079,37 @@ flexible messaging model and an intuitive client API.</description>
<artifactId>opencensus-api</artifactId>
<version>${opencensus.version}</version>
</dependency>

<dependency>
<groupId>io.opencensus</groupId>
<artifactId>opencensus-contrib-grpc-metrics</artifactId>
<version>${opencensus.version}</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda.version}</version>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>${javassist.version}</version>
</dependency>

<dependency>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<version>${jcip.version}</version>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>aircompressor</artifactId>
Expand All @@ -1114,6 +1121,13 @@ flexible messaging model and an intuitive client API.</description>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>${objenesis.version}</version>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
5 changes: 5 additions & 0 deletions testmocks/pom.xml
Expand Up @@ -53,6 +53,11 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>

<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</dependency>
</dependencies>

</project>
48 changes: 24 additions & 24 deletions testmocks/src/main/java/org/apache/zookeeper/MockZooKeeper.java
Expand Up @@ -25,8 +25,6 @@
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.lang.reflect.Constructor;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
Expand All @@ -35,7 +33,6 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.BiPredicate;
Expand All @@ -50,10 +47,12 @@
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;
import org.objenesis.instantiator.ObjectInstantiator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings({ "deprecation", "restriction", "rawtypes" })
public class MockZooKeeper extends ZooKeeper {
private TreeMap<String, Pair<byte[], Integer>> tree;
private SetMultimap<String, Watcher> watchers;
Expand All @@ -67,6 +66,10 @@ public class MockZooKeeper extends ZooKeeper {
private int readOpDelayMs;

private ReentrantLock mutex;

//see details of Objenesis caching - http://objenesis.org/details.html
//see supported jvms - https://github.com/easymock/objenesis/blob/master/SupportedJVMs.md
private static final Objenesis objenesis = new ObjenesisStd();

public enum Op {
CREATE, GET, SET, GET_CHILDREN, DELETE, EXISTS, SYNC,
Expand All @@ -92,11 +95,8 @@ public static MockZooKeeper newInstance(ExecutorService executor) {

public static MockZooKeeper newInstance(ExecutorService executor, int readOpDelayMs) {
try {

sun.reflect.ReflectionFactory rf = sun.reflect.ReflectionFactory.getReflectionFactory();
Constructor objDef = Object.class.getDeclaredConstructor();
Constructor intConstr = rf.newConstructorForSerialization(MockZooKeeper.class, objDef);
MockZooKeeper zk = (MockZooKeeper) intConstr.newInstance();
ObjectInstantiator<MockZooKeeper> mockZooKeeperInstantiator = objenesis.getInstantiatorOf(MockZooKeeper.class);
MockZooKeeper zk = (MockZooKeeper) mockZooKeeperInstantiator.newInstance();
zk.init(executor);
zk.readOpDelayMs = readOpDelayMs;
zk.mutex = new ReentrantLock();
Expand Down Expand Up @@ -290,7 +290,7 @@ public void getData(final String path, boolean watch, final DataCallback cb, fin
cb.processResult(failure.get().intValue(), path, ctx, null, null);
return;
} else if (stopped) {
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null, null);
return;
}

Expand All @@ -303,7 +303,7 @@ public void getData(final String path, boolean watch, final DataCallback cb, fin
}

if (value == null) {
cb.processResult(KeeperException.Code.NoNode, path, ctx, null, null);
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null, null);
} else {
Stat stat = new Stat();
stat.setVersion(value.getRight());
Expand Down Expand Up @@ -356,13 +356,13 @@ public void getChildren(final String path, final Watcher watcher, final Children
return;
} else if (stopped) {
mutex.unlock();
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
return;
}

if (!tree.containsKey(path)) {
mutex.unlock();
cb.processResult(KeeperException.Code.NoNode, path, ctx, null);
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null);
return;
}

Expand Down Expand Up @@ -475,11 +475,11 @@ public void getChildren(final String path, boolean watcher, final Children2Callb
return;
} else if (stopped) {
mutex.unlock();
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null, null);
return;
} else if (!tree.containsKey(path)) {
mutex.unlock();
cb.processResult(KeeperException.Code.NoNode, path, ctx, null, null);
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null, null);
return;
}

Expand Down Expand Up @@ -567,7 +567,7 @@ public void exists(String path, boolean watch, StatCallback cb, Object ctx) {
return;
} else if (stopped) {
mutex.unlock();
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
return;
}

Expand All @@ -576,7 +576,7 @@ public void exists(String path, boolean watch, StatCallback cb, Object ctx) {
cb.processResult(0, path, ctx, new Stat());
} else {
mutex.unlock();
cb.processResult(KeeperException.Code.NoNode, path, ctx, null);
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null);
}
});
}
Expand All @@ -592,7 +592,7 @@ public void exists(String path, Watcher watcher, StatCallback cb, Object ctx) {
return;
} else if (stopped) {
mutex.unlock();
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
return;
}

Expand All @@ -605,7 +605,7 @@ public void exists(String path, Watcher watcher, StatCallback cb, Object ctx) {
cb.processResult(0, path, ctx, new Stat());
} else {
mutex.unlock();
cb.processResult(KeeperException.Code.NoNode, path, ctx, null);
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null);
}
});
}
Expand All @@ -618,7 +618,7 @@ public void sync(String path, VoidCallback cb, Object ctx) {
cb.processResult(failure.get().intValue(), path, ctx);
return;
} else if (stopped) {
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx);
return;
}

Expand Down Expand Up @@ -675,7 +675,7 @@ public Stat setData(final String path, byte[] data, int version) throws KeeperEx
@Override
public void setData(final String path, final byte[] data, int version, final StatCallback cb, final Object ctx) {
if (stopped) {
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
return;
}

Expand All @@ -691,13 +691,13 @@ public void setData(final String path, final byte[] data, int version, final Sta
return;
} else if (stopped) {
mutex.unlock();
cb.processResult(KeeperException.Code.ConnectionLoss, path, ctx, null);
cb.processResult(KeeperException.Code.CONNECTIONLOSS.intValue(), path, ctx, null);
return;
}

if (!tree.containsKey(path)) {
mutex.unlock();
cb.processResult(KeeperException.Code.NoNode, path, ctx, null);
cb.processResult(KeeperException.Code.NONODE.intValue(), path, ctx, null);
return;
}

Expand All @@ -707,7 +707,7 @@ public void setData(final String path, final byte[] data, int version, final Sta
if (version != -1 && version != currentVersion) {
log.debug("[{}] Current version: {} -- Expected: {}", path, currentVersion, version);
mutex.unlock();
cb.processResult(KeeperException.Code.BadVersion, path, ctx, null);
cb.processResult(KeeperException.Code.BADVERSION.intValue(), path, ctx, null);
return;
}

Expand Down

0 comments on commit c404b1e

Please sign in to comment.