Search in sources :

Example 1 with OnReconnect

use of org.apache.solr.common.cloud.OnReconnect in project lucene-solr by apache.

the class TestOnReconnectListenerSupport method test.

@Test
public void test() throws Exception {
    waitForThingsToLevelOut(30000);
    String testCollectionName = "c8n_onreconnect_1x1";
    String shardId = "shard1";
    createCollectionRetry(testCollectionName, 1, 1, 1);
    cloudClient.setDefaultCollection(testCollectionName);
    Replica leader = getShardLeader(testCollectionName, shardId, 30);
    JettySolrRunner leaderJetty = getJettyOnPort(getReplicaPort(leader));
    // get the ZkController for the node hosting the leader
    CoreContainer cores = leaderJetty.getCoreContainer();
    ZkController zkController = cores.getZkController();
    assertNotNull("ZkController is null", zkController);
    String leaderCoreName = leader.getStr(CORE_NAME_PROP);
    String leaderCoreId;
    try (SolrCore leaderCore = cores.getCore(leaderCoreName)) {
        assertNotNull("SolrCore for " + leaderCoreName + " not found!", leaderCore);
        leaderCoreId = leaderCore.getName() + ":" + leaderCore.getStartNanoTime();
    }
    // verify the ZkIndexSchemaReader is a registered OnReconnect listener
    Set<OnReconnect> listeners = zkController.getCurrentOnReconnectListeners();
    assertNotNull("ZkController returned null OnReconnect listeners", listeners);
    ZkIndexSchemaReader expectedListener = null;
    for (OnReconnect listener : listeners) {
        if (listener instanceof ZkIndexSchemaReader) {
            ZkIndexSchemaReader reader = (ZkIndexSchemaReader) listener;
            if (leaderCoreId.equals(reader.getUniqueCoreId())) {
                expectedListener = reader;
                break;
            }
        }
    }
    assertNotNull("ZkIndexSchemaReader for core " + leaderCoreName + " not registered as an OnReconnect listener and should be", expectedListener);
    // reload the collection
    boolean wasReloaded = reloadCollection(leader, testCollectionName);
    assertTrue("Collection '" + testCollectionName + "' failed to reload within a reasonable amount of time!", wasReloaded);
    // after reload, the new core should be registered as an OnReconnect listener and the old should not be
    String reloadedLeaderCoreId;
    try (SolrCore leaderCore = cores.getCore(leaderCoreName)) {
        reloadedLeaderCoreId = leaderCore.getName() + ":" + leaderCore.getStartNanoTime();
    }
    // they shouldn't be equal after reload
    assertTrue(!leaderCoreId.equals(reloadedLeaderCoreId));
    listeners = zkController.getCurrentOnReconnectListeners();
    assertNotNull("ZkController returned null OnReconnect listeners", listeners);
    // reset
    expectedListener = null;
    for (OnReconnect listener : listeners) {
        if (listener instanceof ZkIndexSchemaReader) {
            ZkIndexSchemaReader reader = (ZkIndexSchemaReader) listener;
            if (leaderCoreId.equals(reader.getUniqueCoreId())) {
                fail("Previous core " + leaderCoreId + " should no longer be a registered OnReconnect listener! Current listeners: " + listeners);
            } else if (reloadedLeaderCoreId.equals(reader.getUniqueCoreId())) {
                expectedListener = reader;
                break;
            }
        }
    }
    assertNotNull("ZkIndexSchemaReader for core " + reloadedLeaderCoreId + " not registered as an OnReconnect listener and should be", expectedListener);
    // try to clean up
    try {
        CollectionAdminRequest.deleteCollection(testCollectionName).process(cloudClient);
    } catch (Exception e) {
        // don't fail the test
        log.warn("Could not delete collection {} after test completed", testCollectionName);
    }
    listeners = zkController.getCurrentOnReconnectListeners();
    for (OnReconnect listener : listeners) {
        if (listener instanceof ZkIndexSchemaReader) {
            ZkIndexSchemaReader reader = (ZkIndexSchemaReader) listener;
            if (reloadedLeaderCoreId.equals(reader.getUniqueCoreId())) {
                fail("Previous core " + reloadedLeaderCoreId + " should no longer be a registered OnReconnect listener after collection delete!");
            }
        }
    }
    log.info("TestOnReconnectListenerSupport succeeded ... shutting down now!");
}
Also used : OnReconnect(org.apache.solr.common.cloud.OnReconnect) ZkIndexSchemaReader(org.apache.solr.schema.ZkIndexSchemaReader) CoreContainer(org.apache.solr.core.CoreContainer) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) SolrCore(org.apache.solr.core.SolrCore) Replica(org.apache.solr.common.cloud.Replica) Test(org.junit.Test)

Aggregations

JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)1 OnReconnect (org.apache.solr.common.cloud.OnReconnect)1 Replica (org.apache.solr.common.cloud.Replica)1 CoreContainer (org.apache.solr.core.CoreContainer)1 SolrCore (org.apache.solr.core.SolrCore)1 ZkIndexSchemaReader (org.apache.solr.schema.ZkIndexSchemaReader)1 Test (org.junit.Test)1