Search in sources :

Example 1 with ManagedIndexSchema

use of org.apache.solr.schema.ManagedIndexSchema in project lucene-solr by apache.

the class SolrCore method getConfListener.

public static Runnable getConfListener(SolrCore core, ZkSolrResourceLoader zkSolrResourceLoader) {
    final String coreName = core.getName();
    final CoreContainer cc = core.getCoreContainer();
    final String overlayPath = zkSolrResourceLoader.getConfigSetZkPath() + "/" + ConfigOverlay.RESOURCE_NAME;
    final String solrConfigPath = zkSolrResourceLoader.getConfigSetZkPath() + "/" + core.getSolrConfig().getName();
    String schemaRes = null;
    if (core.getLatestSchema().isMutable() && core.getLatestSchema() instanceof ManagedIndexSchema) {
        ManagedIndexSchema mis = (ManagedIndexSchema) core.getLatestSchema();
        schemaRes = mis.getResourceName();
    }
    final String managedSchmaResourcePath = schemaRes == null ? null : zkSolrResourceLoader.getConfigSetZkPath() + "/" + schemaRes;
    return () -> {
        log.info("config update listener called for core {}", coreName);
        SolrZkClient zkClient = cc.getZkController().getZkClient();
        int solrConfigversion, overlayVersion, managedSchemaVersion = 0;
        SolrConfig cfg = null;
        try (SolrCore solrCore = cc.solrCores.getCoreFromAnyList(coreName, true)) {
            if (solrCore == null || solrCore.isClosed())
                return;
            cfg = solrCore.getSolrConfig();
            solrConfigversion = solrCore.getSolrConfig().getOverlay().getZnodeVersion();
            overlayVersion = solrCore.getSolrConfig().getZnodeVersion();
            if (managedSchmaResourcePath != null) {
                managedSchemaVersion = ((ManagedIndexSchema) solrCore.getLatestSchema()).getSchemaZkVersion();
            }
        }
        if (cfg != null) {
            cfg.refreshRequestParams();
        }
        if (checkStale(zkClient, overlayPath, solrConfigversion) || checkStale(zkClient, solrConfigPath, overlayVersion) || checkStale(zkClient, managedSchmaResourcePath, managedSchemaVersion)) {
            log.info("core reload {}", coreName);
            SolrConfigHandler configHandler = ((SolrConfigHandler) core.getRequestHandler("/config"));
            if (configHandler.getReloadLock().tryLock()) {
                try {
                    cc.reload(coreName);
                } catch (SolrCoreState.CoreIsClosedException e) {
                /*no problem this core is already closed*/
                } finally {
                    configHandler.getReloadLock().unlock();
                }
            } else {
                log.info("Another reload is in progress. Not doing anything.");
            }
            return;
        }
        //some files in conf directory may have  other than managedschema, overlay, params
        try (SolrCore solrCore = cc.solrCores.getCoreFromAnyList(coreName, true)) {
            if (solrCore == null || solrCore.isClosed())
                return;
            for (Runnable listener : solrCore.confListeners) {
                try {
                    listener.run();
                } catch (Exception e) {
                    log.error("Error in listener ", e);
                }
            }
        }
    };
}
Also used : SolrConfigHandler(org.apache.solr.handler.SolrConfigHandler) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) ManagedIndexSchema(org.apache.solr.schema.ManagedIndexSchema) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with ManagedIndexSchema

use of org.apache.solr.schema.ManagedIndexSchema in project lucene-solr by apache.

the class TestSearcherReuse method test.

public void test() throws Exception {
    // seed some docs & segments
    int numDocs = atLeast(1);
    for (int i = 1; i <= numDocs; i++) {
        // NOTE: starting at "1", we'll use id=0 later
        assertU(adoc("id", "" + i));
        if (random().nextBoolean()) {
            assertU(commit());
        }
    }
    // with random merge policies, a regular commit can cause a segment to be flushed that can kick off a background merge
    // that can cause a later commit to actually see changes and open a new searcher.  This should not be possible with optimize
    assertU(optimize());
    // seed a single query into the cache
    assertQ(req("*:*"), "//*[@numFound='" + numDocs + "']");
    final SolrQueryRequest baseReq = req("q", "foo");
    try {
        // we make no index changes in this block, so the searcher should always be the same
        // NOTE: we *have* to call getSearcher() in advance, it's a delayed binding
        final SolrIndexSearcher expectedSearcher = getMainSearcher(baseReq);
        assertU(commit());
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(commit("openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(commit("softCommit", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        assertU(delQ("id:match_no_documents"));
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
        // no doc has this id, yet
        assertU(delI("0"));
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasNotChanged(expectedSearcher);
    } finally {
        baseReq.close();
    }
    // now do a variety of things that *should* always guarantee a new searcher
    SolrQueryRequest beforeReq;
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        assertU(delI("1"));
        assertU(commit());
        assertSearcherHasChanged(before);
    } finally {
        beforeReq.close();
    }
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        assertU(adoc("id", "0"));
        assertU(commit());
        assertSearcherHasChanged(before);
    } finally {
        beforeReq.close();
    }
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        assertU(delQ("id:[0 TO 5]"));
        assertU(commit());
        assertSearcherHasChanged(before);
    } finally {
        beforeReq.close();
    }
    beforeReq = req("q", "foo");
    try {
        // NOTE: we *have* to call getSearcher() in advance: delayed binding
        SolrIndexSearcher before = getMainSearcher(beforeReq);
        // create a new field & add it.
        assertTrue("schema not mutable", beforeReq.getSchema().isMutable());
        ManagedIndexSchema oldSchema = (ManagedIndexSchema) beforeReq.getSchema();
        SchemaField newField = oldSchema.newField("hoss", "string", Collections.<String, Object>emptyMap());
        IndexSchema newSchema = oldSchema.addField(newField);
        h.getCore().setLatestSchema(newSchema);
        // sanity check, later asserts assume this
        assertNotSame(oldSchema, newSchema);
        // the schema has changed - but nothing has requested a new Searcher yet
        assertSearcherHasNotChanged(before);
        // only now should we get a new searcher...
        assertU(commit("softCommit", "true", "openSearcher", "true"));
        assertSearcherHasChanged(before);
        // sanity that opening the new searcher was useful to get new schema...
        SolrQueryRequest afterReq = req("q", "foo");
        try {
            assertSame(newSchema, afterReq.getSchema());
            assertSame(newSchema, getMainSearcher(afterReq).getSchema());
        } finally {
            afterReq.close();
        }
    } finally {
        beforeReq.close();
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) ManagedIndexSchema(org.apache.solr.schema.ManagedIndexSchema) IndexSchema(org.apache.solr.schema.IndexSchema) ManagedIndexSchema(org.apache.solr.schema.ManagedIndexSchema)

Aggregations

ManagedIndexSchema (org.apache.solr.schema.ManagedIndexSchema)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)1 SolrException (org.apache.solr.common.SolrException)1 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)1 SolrConfigHandler (org.apache.solr.handler.SolrConfigHandler)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 IndexSchema (org.apache.solr.schema.IndexSchema)1 SchemaField (org.apache.solr.schema.SchemaField)1 KeeperException (org.apache.zookeeper.KeeperException)1