Skip to content

SOLR-15415 Use Embedded ZK in Tests #522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ Bug Fixes
to take advantage of it's "Workload Identity" and other role-based access feature. (Jacek Kikiewicz, Martin Stocker
via Jason Gerlowski)

* SOLR-15919: Replace File with Path for many ZK operations (Mike Drob)

================== 8.11.2 ==================

Bug Fixes
Expand Down
1 change: 1 addition & 0 deletions solr/core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dependencies {

// ZooKeeper & Curator
implementation 'org.apache.zookeeper:zookeeper'
testImplementation 'org.apache.zookeeper:zookeeper:3.7.0:tests'
// required for instantiating a Zookeeper server (for embedding ZK or running tests)
runtimeOnly ('org.xerial.snappy:snappy-java')
implementation ('org.apache.curator:curator-client')
Expand Down
8 changes: 2 additions & 6 deletions solr/core/src/java/org/apache/solr/cloud/Overseer.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.apache.solr.common.SolrCloseable;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ClusterState;
import org.apache.solr.common.cloud.ConnectionManager;
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.SolrZkClient;
Expand Down Expand Up @@ -905,11 +904,8 @@ ZkDistributedQueue getOverseerQuitNotificationQueue() {
* @return a {@link ZkDistributedQueue} object
*/
ZkDistributedQueue getStateUpdateQueue(Stats zkStats) {
return new ZkDistributedQueue(reader.getZkClient(), "/overseer/queue", zkStats, STATE_UPDATE_MAX_QUEUE, new ConnectionManager.IsClosed(){
public boolean isClosed() {
return Overseer.this.isClosed() || zkController.getCoreContainer().isShutDown();
}
});
return new ZkDistributedQueue(reader.getZkClient(), "/overseer/queue", zkStats, STATE_UPDATE_MAX_QUEUE, () ->
Overseer.this.isClosed() || zkController.getCoreContainer().isShutDown());
}

/**
Expand Down
7 changes: 1 addition & 6 deletions solr/core/src/java/org/apache/solr/cloud/ZkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,7 @@ public void command() {
closeOutstandingElections(descriptorsSupplier);
markAllAsNotLeader(descriptorsSupplier);
}
}, zkACLProvider, new ConnectionManager.IsClosed() {

@Override
public boolean isClosed() {
return cc.isShutDown();
}});
}, zkACLProvider, cc::isShutDown);

// Refuse to start if ZK has a non empty /clusterstate.json
checkNoOldClusterstate(zkClient);
Expand Down
2 changes: 1 addition & 1 deletion solr/core/src/java/org/apache/solr/core/CoreContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@ public void reload(String name, UUID coreId) {

if (docCollection != null) {
Replica replica = docCollection.getReplica(cd.getCloudDescriptor().getCoreNodeName());
assert replica != null;
assert replica != null : cd.getCloudDescriptor().getCoreNodeName() + " had no replica";
if (replica.getType() == Replica.Type.TLOG) { // TODO: needed here?
getZkController().stopReplicationFromLeader(core.getName());
if (!cd.getCloudDescriptor().isLeader()) {
Expand Down
1 change: 1 addition & 0 deletions solr/core/src/test-files/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<Loggers>
<!-- Use <AsyncLogger/<AsyncRoot and <Logger/<Root for asynchronous logging or synchonous logging respectively -->
<Logger name="org.apache.zookeeper" level="WARN"/>
<Logger name="org.apache.zookeeper.PortAssignment" level="INFO"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will go away later.

<Logger name="org.apache.hadoop" level="WARN"/>
<Logger name="org.apache.directory" level="WARN"/>
<Logger name="org.apache.solr.hadoop" level="INFO"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.client.solrj.SolrQuery;
Expand All @@ -32,8 +31,7 @@ public class TestEmbeddedSolrServerConstructors extends SolrTestCaseJ4 {
@Test
@SuppressWarnings({"try"})
public void testPathConstructor() throws IOException {
Path path = Paths.get(TEST_HOME());
try (EmbeddedSolrServer server = new EmbeddedSolrServer(path, "collection1")) {
try (EmbeddedSolrServer server = new EmbeddedSolrServer(TEST_PATH(), "collection1")) {

}
}
Expand All @@ -43,7 +41,7 @@ public void testNodeConfigConstructor() throws Exception {
Path path = createTempDir();

NodeConfig config = new NodeConfig.NodeConfigBuilder("testnode", path)
.setConfigSetBaseDirectory(Paths.get(TEST_HOME()).resolve("configsets").toString())
.setConfigSetBaseDirectory(TEST_PATH().resolve("configsets").toString())
.build();

try (EmbeddedSolrServer server = new EmbeddedSolrServer(config, "newcore")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class TestJettySolrRunner extends SolrTestCaseJ4 {
Expand All @@ -42,7 +41,7 @@ public void testPassSolrHomeToRunner() throws Exception {
Path solrHome = createTempDir();
Path coresDir = createTempDir("crazy_path_to_cores");

Path configsets = Paths.get(TEST_HOME()).resolve("configsets");
Path configsets = TEST_PATH().resolve("configsets");

String solrxml
= "<solr><str name=\"configSetBaseDir\">CONFIGSETS</str><str name=\"coreRootDirectory\">COREROOT</str></solr>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.cloud.ConnectionManager;
import org.apache.solr.common.cloud.DefaultConnectionStrategy;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.util.SolrNamedThreadFactory;
import org.apache.zookeeper.TestableZooKeeper;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Ignore;
import org.junit.Test;

Expand All @@ -54,12 +55,11 @@ public void testConnectionManager() throws Exception {
try {
assertFalse(cm.isLikelyExpired());

zkClient.getSolrZooKeeper().closeCnxn();

long sessionId = zkClient.getSolrZooKeeper().getSessionId();
server.expire(sessionId);
Thread.sleep(TIMEOUT);

ZooKeeper zk = zkClient.getZooKeeper();
assertTrue(zk instanceof TestableZooKeeper);
((TestableZooKeeper) zk).testableConnloss();
zk.getTestable().injectSessionExpiration();

assertTrue(cm.isLikelyExpired());
} finally {
cm.close();
Expand Down Expand Up @@ -122,7 +122,7 @@ public void testReconnectWhenZkDisappeared() throws Exception {
server.run();

MockZkClientConnectionStrategy strat = new MockZkClientConnectionStrategy();
SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT, strat , null);
SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT, strat, null);
ConnectionManager cm = zkClient.getConnectionManager();

try {
Expand All @@ -144,7 +144,7 @@ public void testReconnectWhenZkDisappeared() throws Exception {
}
}

private static class MockZkClientConnectionStrategy extends DefaultConnectionStrategy {
private static class MockZkClientConnectionStrategy extends TestConnectionStrategy {
int called = 0;
boolean exceptionThrown = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -65,7 +64,7 @@ public class DistribJoinFromCollectionTest extends SolrCloudTestCase{

@BeforeClass
public static void setupCluster() throws Exception {
final Path configDir = Paths.get(TEST_HOME(), "collection1", "conf");
final Path configDir = TEST_COLL1_CONF();

String configName = "solrCloudCollectionConfig";
int nodeCount = 5;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void testPeekElements() throws Exception {

private void forceSessionExpire() throws InterruptedException, TimeoutException {
long sessionId = zkClient.getSolrZooKeeper().getSessionId();
zkServer.expire(sessionId);
zkClient.getZooKeeper().getTestable().injectSessionExpiration();
zkClient.getConnectionManager().waitForDisconnected(10000);
zkClient.getConnectionManager().waitForConnected(10000);
for (int i = 0; i < 100; ++i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public void testSimpleSliceLeaderElection() throws Exception {
JettySolrRunner jetty = getRunner(leader);
ZkController zkController = jetty.getCoreContainer().getZkController();

zkController.getZkClient().getSolrZooKeeper().closeCnxn();
cluster.getZkServer().expire(zkController.getZkClient().getSolrZooKeeper().getSessionId());
ChaosMonkey.causeConnectionLoss(zkController.getZkClient().getZooKeeper());
zkController.getZkClient().getZooKeeper().getTestable().injectSessionExpiration();

for (int i = 0; i < 60; i++) { // wait till leader is changed
if (jetty != getRunner(getLeader(collection))) {
Expand Down
16 changes: 7 additions & 9 deletions solr/core/src/test/org/apache/solr/cloud/LeaderElectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.NoNodeException;
import org.apache.zookeeper.KeeperException.SessionExpiredException;
import org.apache.zookeeper.TestableZooKeeper;
import org.apache.zookeeper.ZooKeeper;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -493,20 +495,17 @@ public void run() {
}
};

Thread connLossThread = new Thread() {
@Override
public void run() {

Thread connLossThread = new Thread(() -> {
while (!stopStress) {
try {
Thread.sleep(50);
int j;
j = random().nextInt(threads.size());
try {
threads.get(j).es.zkClient.getSolrZooKeeper().closeCnxn();
ZooKeeper zk = threads.get(j).es.zkClient.getZooKeeper();
((TestableZooKeeper) zk).testableConnloss();
if (random().nextBoolean()) {
long sessionId = zkClient.getSolrZooKeeper().getSessionId();
server.expire(sessionId);
zk.getTestable().injectSessionExpiration();
}
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -517,8 +516,7 @@ public void run() {

}
}
}
};
});

scheduleThread.start();
connLossThread.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -52,8 +51,8 @@ public static void beforeClass() throws Exception {
.addConfig("_default", configset("cloud-minimal"))
.configure();
// replace schema.xml with schema-test.xml
Path schemaPath = Paths.get(TEST_HOME()).resolve("collection1").resolve("conf").resolve("schema-nest.xml");
cluster.getZkClient().setData("/configs/_default/schema.xml", schemaPath.toFile(),true);
Path schemaPath = TEST_COLL1_CONF().resolve("schema-nest.xml");
cluster.getZkClient().setData("/configs/_default/schema.xml", schemaPath, true);

cloudClient = cluster.getSolrClient();
cloudClient.setDefaultCollection(DEFAULT_COLLECTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void setUp() throws Exception {
System.setProperty("zkHost", zkServer.getZkAddress());

try (SolrZkClient zkClient = new SolrZkClientWithACLs(zkServer.getZkHost(), AbstractZkTestCase.TIMEOUT)) {
ZooKeeperSaslClient saslClient = zkClient.getSolrZooKeeper().getConnection().zooKeeperSaslClient;
ZooKeeperSaslClient saslClient = zkClient.getZooKeeper().getSaslClient();
assumeFalse("Could not set up ZK with SASL", saslClient.isFailed());
zkClient.makePath("/solr", false, true);
} catch (KeeperException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -113,7 +112,7 @@ private static void afterClass() throws Exception {
private static void createMiniSolrCloudCluster() throws Exception {

final String configName = "solrCloudCollectionConfig";
final Path configDir = Paths.get(TEST_HOME(), "collection1", "conf");
final Path configDir = TEST_COLL1_CONF();

configureCluster(NUM_SERVERS)
.addConfig(configName, configDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -68,8 +67,8 @@ private static void createMiniSolrCloudCluster() throws Exception {
final int numNodes = (numShards * repFactor);

final String configName = DEBUG_LABEL + "_config-set";
final Path configDir = Paths.get(TEST_HOME(), "collection1", "conf");
final Path configDir = TEST_COLL1_CONF();

configureCluster(numNodes).addConfig(configName, configDir).configure();

Map<String, String> collectionProperties = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -70,8 +69,8 @@ private static void createMiniSolrCloudCluster() throws Exception {
final int numNodes = 1 + (numShards * repFactor);

final String configName = DEBUG_LABEL + "_config-set";
final Path configDir = Paths.get(TEST_HOME(), "collection1", "conf");
final Path configDir = TEST_COLL1_CONF();

configureCluster(numNodes).addConfig(configName, configDir).configure();

Map<String, String> collectionProperties = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void testLeaderElectionWithZkExpiry() throws Exception {
public void run() {
long timeout = System.nanoTime() + TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
while (System.nanoTime() < timeout) {
long sessionId = zkController.getZkClient().getSolrZooKeeper().getSessionId();
server.expire(sessionId);
zkController.getZkClient().getZooKeeper().getTestable().injectSessionExpiration();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -133,7 +132,7 @@ public static void createMiniSolrCloudCluster() throws Exception {
final int numNodes = 1 + (singleCoreMode ? 0 : (numShards * repFactor));

final String configName = DEBUG_LABEL + "_config-set";
final Path configDir = Paths.get(TEST_HOME(), "collection1", "conf");
final Path configDir = TEST_COLL1_CONF();

configureCluster(numNodes).addConfig(configName, configDir).configure();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.invoke.MethodHandles;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -123,8 +122,8 @@ private static void createMiniSolrCloudCluster() throws Exception {
final int numNodes = numShards * repFactor;

final String configName = DEBUG_LABEL + "_config-set";
final Path configDir = Paths.get(TEST_HOME(), "collection1", "conf");
final Path configDir = TEST_COLL1_CONF();

configureCluster(numNodes).addConfig(configName, configDir).configure();

CLOUD_CLIENT = cluster.getSolrClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ public void run() {
// simulate session expiration

// one option
long sessionId = zkClient.getSolrZooKeeper().getSessionId();
server.expire(sessionId);

zkClient.getZooKeeper().getTestable().injectSessionExpiration();

// another option
//zkClient.getSolrZooKeeper().getConnection().disconnect();

Expand Down
Loading