Search in sources :

Example 1 with SolrCoreAware

use of org.apache.solr.util.plugin.SolrCoreAware in project lucene-solr by apache.

the class SolrResourceLoader method inform.

/**
   * Tell all {@link SolrCoreAware} instances about the SolrCore
   */
public void inform(SolrCore core) {
    this.dataDir = core.getDataDir();
    // make a copy to avoid potential deadlock of a callback calling newInstance and trying to
    // add something to waitingForCore.
    SolrCoreAware[] arr;
    while (waitingForCore.size() > 0) {
        synchronized (waitingForCore) {
            arr = waitingForCore.toArray(new SolrCoreAware[waitingForCore.size()]);
            waitingForCore.clear();
        }
        for (SolrCoreAware aware : arr) {
            aware.inform(core);
        }
    }
    // this is the last method to be called in SolrCore before the latch is released.
    live = true;
}
Also used : SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware)

Example 2 with SolrCoreAware

use of org.apache.solr.util.plugin.SolrCoreAware in project lucene-solr by apache.

the class IndexSchema method readSchema.

protected void readSchema(InputSource is) {
    try {
        // pass the config resource loader to avoid building an empty one for no reason:
        // in the current case though, the stream is valid so we wont load the resource by name
        Config schemaConf = new Config(loader, SCHEMA, is, SLASH + SCHEMA + SLASH);
        Document document = schemaConf.getDocument();
        final XPath xpath = schemaConf.getXPath();
        String expression = stepsToPath(SCHEMA, AT + NAME);
        Node nd = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        String coreName = getCoreName("null");
        StringBuilder sb = new StringBuilder();
        // Another case where the initialization from the test harness is different than the "real world"
        sb.append("[");
        sb.append(coreName);
        sb.append("] ");
        if (nd == null) {
            sb.append("schema has no name!");
            log.warn(sb.toString());
        } else {
            name = nd.getNodeValue();
            sb.append("Schema ");
            sb.append(NAME);
            sb.append("=");
            sb.append(name);
            log.info(sb.toString());
        }
        //                      /schema/@version
        expression = stepsToPath(SCHEMA, AT + VERSION);
        version = schemaConf.getFloat(expression, 1.0f);
        // load the Field Types
        final FieldTypePluginLoader typeLoader = new FieldTypePluginLoader(this, fieldTypes, schemaAware);
        expression = getFieldTypeXPathExpressions();
        NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
        typeLoader.load(loader, nodes);
        // load the fields
        Map<String, Boolean> explicitRequiredProp = loadFields(document, xpath);
        //   /schema/similarity
        expression = stepsToPath(SCHEMA, SIMILARITY);
        Node node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        similarityFactory = readSimilarity(loader, node);
        if (similarityFactory == null) {
            final boolean modernSim = getDefaultLuceneMatchVersion().onOrAfter(Version.LUCENE_6_0_0);
            final Class simClass = modernSim ? SchemaSimilarityFactory.class : ClassicSimilarityFactory.class;
            // use the loader to ensure proper SolrCoreAware handling
            similarityFactory = loader.newInstance(simClass.getName(), SimilarityFactory.class);
            similarityFactory.init(new ModifiableSolrParams());
        } else {
            isExplicitSimilarity = true;
        }
        if (!(similarityFactory instanceof SolrCoreAware)) {
            // then we are responsible for erroring if a field type is trying to specify a sim.
            for (FieldType ft : fieldTypes.values()) {
                if (null != ft.getSimilarity()) {
                    String msg = "FieldType '" + ft.getTypeName() + "' is configured with a similarity, but the global similarity does not support it: " + similarityFactory.getClass();
                    log.error(msg);
                    throw new SolrException(ErrorCode.SERVER_ERROR, msg);
                }
            }
        }
        //                      /schema/defaultSearchField/text()
        expression = stepsToPath(SCHEMA, "defaultSearchField", TEXT_FUNCTION);
        node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        if (node != null) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Setting defaultSearchField in schema not supported since Solr 7");
        }
        //                      /schema/solrQueryParser/@defaultOperator
        expression = stepsToPath(SCHEMA, "solrQueryParser", AT + "defaultOperator");
        node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        if (node != null) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Setting default operator in schema (solrQueryParser/@defaultOperator) not supported");
        }
        //                      /schema/uniqueKey/text()
        expression = stepsToPath(SCHEMA, UNIQUE_KEY, TEXT_FUNCTION);
        node = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
        if (node == null) {
            log.warn("no " + UNIQUE_KEY + " specified in schema.");
        } else {
            uniqueKeyField = getIndexedField(node.getNodeValue().trim());
            if (null != uniqueKeyField.getDefaultValue()) {
                String msg = UNIQUE_KEY + " field (" + uniqueKeyFieldName + ") can not be configured with a default value (" + uniqueKeyField.getDefaultValue() + ")";
                log.error(msg);
                throw new SolrException(ErrorCode.SERVER_ERROR, msg);
            }
            if (!uniqueKeyField.stored()) {
                log.warn(UNIQUE_KEY + " is not stored - distributed search and MoreLikeThis will not work");
            }
            if (uniqueKeyField.multiValued()) {
                String msg = UNIQUE_KEY + " field (" + uniqueKeyFieldName + ") can not be configured to be multivalued";
                log.error(msg);
                throw new SolrException(ErrorCode.SERVER_ERROR, msg);
            }
            uniqueKeyFieldName = uniqueKeyField.getName();
            uniqueKeyFieldType = uniqueKeyField.getType();
            // Unless the uniqueKeyField is marked 'required=false' then make sure it exists
            if (Boolean.FALSE != explicitRequiredProp.get(uniqueKeyFieldName)) {
                uniqueKeyField.required = true;
                requiredFields.add(uniqueKeyField);
            }
        }
        /////////////// parse out copyField commands ///////////////
        // Map<String,ArrayList<SchemaField>> cfields = new HashMap<String,ArrayList<SchemaField>>();
        // expression = "/schema/copyField";
        dynamicCopyFields = new DynamicCopy[] {};
        loadCopyFields(document, xpath);
        postReadInform();
    } catch (SolrException e) {
        throw new SolrException(ErrorCode.getErrorCode(e.code()), "Can't load schema " + loader.resourceLocation(resourceName) + ": " + e.getMessage(), e);
    } catch (Exception e) {
        // unexpected exception...
        throw new SolrException(ErrorCode.SERVER_ERROR, "Can't load schema " + loader.resourceLocation(resourceName) + ": " + e.getMessage(), e);
    }
    // create the field analyzers
    refreshAnalyzers();
    log.info("Loaded schema {}/{} with uniqueid field {}", name, version, uniqueKeyFieldName);
}
Also used : XPath(javax.xml.xpath.XPath) SolrConfig(org.apache.solr.core.SolrConfig) Config(org.apache.solr.core.Config) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) SchemaSimilarityFactory(org.apache.solr.search.similarities.SchemaSimilarityFactory) ClassicSimilarityFactory(org.apache.solr.search.similarities.ClassicSimilarityFactory) Document(org.w3c.dom.Document) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware) SolrException(org.apache.solr.common.SolrException)

Example 3 with SolrCoreAware

use of org.apache.solr.util.plugin.SolrCoreAware in project lucene-solr by apache.

the class SolrResourceLoader method newInstance.

public <T> T newInstance(String cName, Class<T> expectedType, String[] subPackages, Class[] params, Object[] args) {
    Class<? extends T> clazz = findClass(cName, expectedType, subPackages);
    if (clazz == null) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Can not find class: " + cName + " in " + classLoader);
    }
    T obj = null;
    try {
        Constructor<? extends T> constructor = null;
        try {
            constructor = clazz.getConstructor(params);
            obj = constructor.newInstance(args);
        } catch (NoSuchMethodException e) {
            //look for a zero arg constructor if the constructor args do not match
            try {
                constructor = clazz.getConstructor();
                obj = constructor.newInstance();
            } catch (NoSuchMethodException e1) {
                throw e;
            }
        }
    } catch (Error err) {
        log.error("Loading Class " + cName + " (" + clazz.getName() + ") triggered serious java error: " + err.getClass().getName(), err);
        throw err;
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error instantiating class: '" + clazz.getName() + "'", e);
    }
    if (!live) {
        if (obj instanceof SolrCoreAware) {
            assertAwareCompatibility(SolrCoreAware.class, obj);
            waitingForCore.add((SolrCoreAware) obj);
        }
        if (obj instanceof ResourceLoaderAware) {
            assertAwareCompatibility(ResourceLoaderAware.class, obj);
            waitingForResources.add((ResourceLoaderAware) obj);
        }
        if (obj instanceof SolrInfoBean) {
            //TODO: Assert here?
            infoMBeans.add((SolrInfoBean) obj);
        }
    }
    return obj;
}
Also used : SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware) ResourceLoaderAware(org.apache.lucene.analysis.util.ResourceLoaderAware) SolrException(org.apache.solr.common.SolrException) NoInitialContextException(javax.naming.NoInitialContextException) NamingException(javax.naming.NamingException) SolrException(org.apache.solr.common.SolrException) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException)

Example 4 with SolrCoreAware

use of org.apache.solr.util.plugin.SolrCoreAware in project lucene-solr by apache.

the class SolrCore method initCodec.

private Codec initCodec(SolrConfig solrConfig, final IndexSchema schema) {
    final PluginInfo info = solrConfig.getPluginInfo(CodecFactory.class.getName());
    final CodecFactory factory;
    if (info != null) {
        factory = schema.getResourceLoader().newInstance(info.className, CodecFactory.class);
        factory.init(info.initArgs);
    } else {
        factory = new CodecFactory() {

            @Override
            public Codec getCodec() {
                return Codec.getDefault();
            }
        };
    }
    if (factory instanceof SolrCoreAware) {
        // CodecFactory needs SolrCore before inform() is called on all registered
        // SolrCoreAware listeners, at the end of the SolrCore constructor
        ((SolrCoreAware) factory).inform(this);
    } else {
        for (FieldType ft : schema.getFieldTypes().values()) {
            if (null != ft.getPostingsFormat()) {
                String msg = "FieldType '" + ft.getTypeName() + "' is configured with a postings format, but the codec does not support it: " + factory.getClass();
                log.error(msg);
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, msg);
            }
            if (null != ft.getDocValuesFormat()) {
                String msg = "FieldType '" + ft.getTypeName() + "' is configured with a docValues format, but the codec does not support it: " + factory.getClass();
                log.error(msg);
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, msg);
            }
        }
    }
    return factory.getCodec();
}
Also used : Codec(org.apache.lucene.codecs.Codec) SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware) SolrException(org.apache.solr.common.SolrException) FieldType(org.apache.solr.schema.FieldType)

Example 5 with SolrCoreAware

use of org.apache.solr.util.plugin.SolrCoreAware in project lucene-solr by apache.

the class SolrCore method setLatestSchema.

/** 
   * Sets the latest schema snapshot to be used by this core instance. 
   * If the specified <code>replacementSchema</code> uses a {@link SimilarityFactory} which is 
   * {@link SolrCoreAware} then this method will {@link SolrCoreAware#inform} that factory about 
   * this SolrCore prior to using the <code>replacementSchema</code>
   * @see #getLatestSchema
   */
public void setLatestSchema(IndexSchema replacementSchema) {
    // 1) For a newly instantiated core, the Similarity needs SolrCore before inform() is called on
    // any registered SolrCoreAware listeners (which will likeley need to use the SolrIndexSearcher.
    //
    // 2) If a new IndexSchema is assigned to an existing live SolrCore (ie: managed schema
    // replacement via SolrCloud) then we need to explicitly inform() the similarity because
    // we can't rely on the normal SolrResourceLoader lifecycle because the sim was instantiated
    // after the SolrCore was already live (see: SOLR-8311 + SOLR-8280)
    final SimilarityFactory similarityFactory = replacementSchema.getSimilarityFactory();
    if (similarityFactory instanceof SolrCoreAware) {
        ((SolrCoreAware) similarityFactory).inform(this);
    }
    this.schema = replacementSchema;
}
Also used : SolrCoreAware(org.apache.solr.util.plugin.SolrCoreAware) SimilarityFactory(org.apache.solr.schema.SimilarityFactory)

Aggregations

SolrCoreAware (org.apache.solr.util.plugin.SolrCoreAware)5 SolrException (org.apache.solr.common.SolrException)3 IOException (java.io.IOException)2 CharacterCodingException (java.nio.charset.CharacterCodingException)1 NamingException (javax.naming.NamingException)1 NoInitialContextException (javax.naming.NoInitialContextException)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 ResourceLoaderAware (org.apache.lucene.analysis.util.ResourceLoaderAware)1 Codec (org.apache.lucene.codecs.Codec)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 Config (org.apache.solr.core.Config)1 SolrConfig (org.apache.solr.core.SolrConfig)1 FieldType (org.apache.solr.schema.FieldType)1 SimilarityFactory (org.apache.solr.schema.SimilarityFactory)1 ClassicSimilarityFactory (org.apache.solr.search.similarities.ClassicSimilarityFactory)1 SchemaSimilarityFactory (org.apache.solr.search.similarities.SchemaSimilarityFactory)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1