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);
}
}
}
};
}
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();
}
}
Aggregations