Search in sources :

Example 1 with ManagedIndexState

use of org.apache.stanbol.commons.solr.managed.ManagedIndexState in project stanbol by apache.

the class ManagedIndexMetadata method updateIndexProperties.

/**
     * Adds, update and deletes index metadata
     * @param name the name of the index (can be <code>null</code> of properties are parsed)
     * @param properties the properties or <code>null</code> to remove
     * @param clone If <code>true</code> the parsed properties are cloned. Clones
     * are required for parsed properties to prevent external changes of properties
     * stored in the internal lists. Only parse <code>false</code> in case the
     * parsed properties are already a clone
     * @return The old {@link IndexMetadata} instance (especially usefull in case of
     * remove operations)
     */
private IndexMetadata updateIndexProperties(String name, IndexMetadata properties, boolean clone) {
    if (name == null && properties == null) {
        return null;
    }
    if (name != null && properties != null && !name.equals(properties.getIndexName())) {
        throw new IllegalArgumentException("The value of the Index-Name property '" + properties.getIndexName() + "' is not the same as the parsed name '" + name + "'!");
    }
    if (name == null) {
        name = properties.getIndexName();
    }
    //first persist
    if (properties != null) {
        try {
            saveIndexConfig(name, properties);
        } catch (IOException e) {
            log.error("Unable to store Properties (see Exception below): {}", properties.toString());
            throw new IllegalStateException("Unable to save Index metadata for index '" + name + "'!", e);
        }
    } else {
        removeIndexConfig(name);
    }
    //clone is meaningless if properties are NULL
    if (clone && properties != null) {
        IndexMetadata tmp = properties;
        properties = new IndexMetadata();
        properties.putAll(tmp);
    }
    Map<String, IndexMetadata> toAdd, toRemove;
    IndexMetadata oldMetadata = null;
    synchronized (inMemoryModelLock) {
        ManagedIndexState currentState = getState(name);
        if (currentState != null) {
            toRemove = managed.get(currentState);
        } else {
            toRemove = null;
        }
        if (properties == null) {
            //remove
            toAdd = null;
        } else {
            ManagedIndexState newState = properties.getState();
            toAdd = managed.get(newState);
        }
        //now update in-memory state
        if (toRemove != null) {
            oldMetadata = toRemove.remove(name);
        }
        if (toAdd != null) {
            toAdd.put(name, properties);
        }
        //now update the archive name to core name mappings
        if (oldMetadata != null) {
            for (String indexArchive : oldMetadata.getIndexArchives()) {
                Collection<String> indexes = archiveName2CoreName.get(indexArchive);
                if (indexes.remove(name) && indexes.isEmpty()) {
                    archiveName2CoreName.remove(indexArchive);
                }
            }
        }
        if (properties != null) {
            for (String indexArchive : properties.getIndexArchives()) {
                Collection<String> indexes = archiveName2CoreName.get(indexArchive);
                if (indexes == null) {
                    indexes = new HashSet<String>();
                    archiveName2CoreName.put(indexArchive, indexes);
                }
                indexes.add(name);
            }
        }
    }
    return oldMetadata;
}
Also used : IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) ManagedIndexState(org.apache.stanbol.commons.solr.managed.ManagedIndexState)

Example 2 with ManagedIndexState

use of org.apache.stanbol.commons.solr.managed.ManagedIndexState in project stanbol by apache.

the class ConfiguredSolrCoreTracker method checkInitSolrIndex.

/**
     * Checks if the SolrIndex is available and if not it tries to initialise it
     * @param indexReference the SolrCore reference
     * @param solrCoreConfig the name of the SolrIndex configuration ({name}.solrindex.zip)
     * @return
     * @throws IOException
     * @throws ConfigurationException
     * @throws SAXException
     */
protected IndexReference checkInitSolrIndex(IndexReference indexReference, String solrCoreConfig) throws IOException, ConfigurationException, SAXException {
    // if the solr core is managed, check that the index is properly activated
    if (managedSolrServer != null && indexReference.checkServer(managedSolrServer.getServerName()) && context != null && solrCoreConfig != null) {
        log.info(" > check/init index {} on ManagedSolrServer {}", indexReference, managedSolrServer.getServerName());
        String indexName = indexReference.getIndex();
        final IndexMetadata indexMetadata;
        ManagedIndexState indexState = managedSolrServer.getIndexState(indexName);
        if (indexState == null) {
            if (solrCoreConfig.indexOf(".solrindex.") < 0) {
                //if the suffix is missing
                //append it
                solrCoreConfig = solrCoreConfig + ".solrindex.zip";
            }
            log.info("Create SolrCore {} (config: {}) on ManagedSolrServer {} ...", new Object[] { indexName, solrCoreConfig, managedSolrServer.getServerName() });
            indexMetadata = managedSolrServer.createSolrIndex(indexName, solrCoreConfig, null);
            if (indexMetadata != null)
                log.info("  ... created {}", indexMetadata.getIndexReference());
        } else {
            indexMetadata = managedSolrServer.getIndexMetadata(indexName);
            if (indexState != ManagedIndexState.ACTIVE) {
                log.info("  ... activate {}", indexMetadata.getIndexReference());
                managedSolrServer.activateIndex(indexName);
            } else {
                log.info("  ... index {} already active", indexMetadata.getIndexReference());
            }
        }
        //            IndexMetadata indexMetadata = managedSolrServer.getIndexMetadata(indexName);
        //            if (indexMetadata == null) {
        //                // TODO: debug the DataFileProvider init race conditions instead
        //                // indexMetadata = managedSolrServer.createSolrIndex(indexName, indexArchiveName, null);
        //                dfp.getInputStream(context.getBundleContext().getBundle().getSymbolicName(), 
        //                    indexArchiveName + ".solrindex.zip", null);
        //                URL archiveUrl = context.getBundleContext().getBundle()
        //                        .getEntry("/data-files/" + indexArchiveName + ".solrindex.zip");
        //                if (archiveUrl == null) {
        //                    throw new ConfigurationException(solrCoreId, "Could not find index archive for "
        //                                                                 + indexArchiveName);
        //                }
        //                ZipArchiveInputStream zis = new ZipArchiveInputStream(archiveUrl.openStream());
        //                indexMetadata = managedSolrServer.updateIndex(indexName, zis, indexArchiveName);
        //            }
        //            if (!indexMetadata.isActive()) {
        //                managedSolrServer.activateIndex(indexName);
        //            }
        indexReference = indexMetadata.getIndexReference();
    }
    return indexReference;
}
Also used : IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) ManagedIndexState(org.apache.stanbol.commons.solr.managed.ManagedIndexState)

Example 3 with ManagedIndexState

use of org.apache.stanbol.commons.solr.managed.ManagedIndexState in project stanbol by apache.

the class ManagedSolrServerImpl method activate.

@Activate
protected void activate(ComponentContext context) throws ConfigurationException {
    log.info("Activate ManagedSolrServer:");
    //        this.context = context;
    BundleContext bc = context.getBundleContext();
    //first parse the configured Servername
    Object value = context.getProperties().get(PROPERTY_SERVER_NAME);
    if (value == null || value.toString().isEmpty()) {
        throw new ConfigurationException(PROPERTY_SERVER_NAME, "The Server Name is a required" + "Configuration and MUST NOT be NULL nor empty!");
    } else {
        serverName = value.toString();
        log.info(" > Name = {}", value.toString());
    }
    value = context.getProperties().get(MANAGED_SOLR_DIR_PROPERTY);
    if (value == null || value.toString().isEmpty()) {
        managedSolrDir = new File(FilenameUtils.separatorsToSystem(substituteProperty(DEFAULT_ROOT_PATH, bc)), serverName);
    } else {
        //note that property substitution is used on the parsed 
        //PROPERTY_SERVER_DIR value
        managedSolrDir = new File(FilenameUtils.separatorsToSystem(substituteProperty(value.toString(), bc)));
        if (!managedSolrDir.isAbsolute()) {
            managedSolrDir = new File(DEFAULT_ROOT_PATH, //make sure to convert '/' and '\' to the platform separator
            FilenameUtils.separatorsToSystem(value.toString()));
        }
    }
    log.info(" > managedDir = {}", managedSolrDir.getAbsolutePath());
    if (managedSolrDir.isFile()) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, String.format("The configured managed directory '%s'(dir: %s|name:%s) " + "exists but is no Directory!", managedSolrDir.getAbsolutePath(), value, serverName));
    }
    // check if the "solr.xml" file exists in the directory
    File solrConf = new File(managedSolrDir, "solr.xml");
    if (!solrConf.exists()) {
        log.info("   ... initialise managed directory '{}'", managedSolrDir);
        try {
            managedSolrDir = ConfigUtils.copyDefaultConfig(bc.getBundle(), managedSolrDir, false);
        } catch (IOException e) {
            throw new IllegalStateException(String.format("Unable to copy default configuration for the manages Solr Directory " + "to the configured path '%s'!", managedSolrDir.getAbsoluteFile()), e);
        }
    } else {
        log.info("   .... managed directory '{}' already present and initialised", managedSolrDir);
    }
    //init the SolrServerProperties and read the other parameters form the config
    SolrServerProperties serverProperties = new SolrServerProperties(managedSolrDir);
    serverProperties.setServerName(serverName);
    value = context.getProperties().get(PROPERTY_SERVER_RANKING);
    if (value instanceof Number) {
        serverProperties.setServerRanking(((Number) value).intValue());
    } else if (value != null && !value.toString().isEmpty()) {
        try {
            serverProperties.setServerRanking(Integer.parseInt(value.toString()));
            log.info(" > Ranking = {}", serverProperties.getServerRanking());
        } catch (NumberFormatException e) {
            throw new ConfigurationException(PROPERTY_SERVER_RANKING, "The configured Server Ranking '" + value + " can not be converted to an Integer!", e);
        }
    }
    //else not present or empty string -> do not set a ranking!
    value = context.getProperties().get(PROPERTY_SERVER_PUBLISH_REST);
    if (value == null || value instanceof Boolean) {
        serverProperties.setPublishREST((Boolean) value);
    } else {
        serverProperties.setPublishREST(Boolean.parseBoolean(value.toString()));
    }
    try {
        server = new SolrServerAdapter(context.getBundleContext(), serverProperties);
    } catch (SolrException e) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, "Unable to initialise " + "a SolrServer based on the Directory '" + serverProperties.getServerDir() + "'!", e);
    }
    //        dfpServiceRegistration = context.getBundleContext().registerService(
    //            DataFileProvider.class.getName(), 
    //            new ClassPathSolrIndexConfigProvider(
    //                context.getBundleContext().getBundle().getSymbolicName()), null);
    managedCores = new ManagedIndexMetadata(context);
    //After a restart of the CoreContainer we need to synchronise the state of
    //The cores with the state in the configs.
    //This may result in the activation of missing SolrCores as well as the
    //deactivation of unknown or inactive cores. It may also need to
    //change the state in the configuration in case a user has manually fixed
    //encountered problems while this service was deactivated.
    Collection<String> activeByMetadata = managedCores.getInState(ManagedIndexState.ACTIVE);
    Collection<String> activeOnSolrServer = new HashSet<String>(server.getCores());
    activeOnSolrServer.removeAll(activeByMetadata);
    activeByMetadata.removeAll(server.getCores());
    //(1) Try to activate missing cores on the CoreContainer
    if (!activeByMetadata.isEmpty()) {
        log.info("The following active managed Cores are not available on " + "the SolrServer: {}", activeByMetadata);
        for (String indexName : activeByMetadata) {
            IndexMetadata metadata = managedCores.getIndexMetadata(indexName);
            try {
                activateCore(metadata, server);
                log.info("  ... index {} successfully started!", indexName);
            } catch (IOException e) {
                metadata.setError(e);
                log.error("Unable to activate previously active SolrIndex '" + metadata.getIndexReference() + "'!", e);
            } catch (SAXException e) {
                metadata.setError(e);
                log.error("Unable to activate previously active SolrIndex '" + metadata.getIndexReference() + "'!", e);
            } catch (RuntimeException e) {
                metadata.setError(e);
                log.error("Unable to activate previously active SolrIndex '" + metadata.getIndexReference() + "'!", e);
            //} finally { The metadata are not modified anyway!
            //    managedCores.store(metadata);
            }
        }
    }
    //    based on the configuration
    if (!activeOnSolrServer.isEmpty()) {
        log.info("The following Cores active on the SolrServer are not " + "marked as active in the Metadata: {}", activeOnSolrServer);
        log.info("Based on the Metadata (UNKNOWN ... no Index for that name):");
        for (String indexName : activeOnSolrServer) {
            IndexMetadata metadata = managedCores.getIndexMetadata(indexName);
            ManagedIndexState state = metadata != null ? metadata.getState() : null;
            log.info("   - {} has state {}", indexName, state != null ? state : "UNKNOWN");
            if (metadata == null) {
                //unknown core ... deactivate
                deactivateCore(indexName, server);
                log.info("  ... deactiaved UNKOWN SolrCore {} on managed Solr Server {}", indexName, serverName);
            } else if (state == ManagedIndexState.INACTIVE) {
                ////the metadata way this core should be deactivated!
                deactivateCore(indexName, server);
                log.info("  ... deactiaved INACTIVE SolrCore {} on managed Solr Server {}", indexName, serverName);
            } else if (state == ManagedIndexState.ERROR) {
                //looks like that the error was resolved ...
                // ... maybe someone has manually edited some files and 
                //     restarted this server
                metadata.setState(ManagedIndexState.ACTIVE);
                managedCores.store(metadata);
                log.info("  ... successfully ACTIVATED SolrCore {} on managed Solr Server {}", indexName, serverName);
            } else if (state == ManagedIndexState.UNINITIALISED) {
                //looks like someone has copied the required files manually
                //to the solrServer ... update the metadata an activate
                ManagementUtils.updateMetadata(metadata, server.getCore(indexName));
                metadata.setState(ManagedIndexState.ACTIVE);
                managedCores.store(metadata);
                log.info("  ... successfully ACTIVATED SolrCore {} on managed Solr Server {}", indexName, serverName);
            }
        }
    }
    //now init uninitialised cores and dataFile tracking for those
    //(1) start the daemon that asyc updates cores on DataFileListener events
    updateDaemon = new IndexUpdateDaemon();
    //start the thread
    updateDaemon.start();
    //(2) init IndexArhive tracking
    indexArchiveTracker = new IndexArchiveTracker(dataFileTracker, dataFileProvider, managedCores, updateDaemon);
    log.info("   ... Managed SolrServer '{}' successfully initialised!", serverName);
}
Also used : SolrServerProperties(org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) ManagedIndexState(org.apache.stanbol.commons.solr.managed.ManagedIndexState) File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter) SolrException(org.apache.solr.common.SolrException) BundleContext(org.osgi.framework.BundleContext) HashSet(java.util.HashSet) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

IndexMetadata (org.apache.stanbol.commons.solr.managed.IndexMetadata)3 ManagedIndexState (org.apache.stanbol.commons.solr.managed.ManagedIndexState)3 IOException (java.io.IOException)2 File (java.io.File)1 HashSet (java.util.HashSet)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Activate (org.apache.felix.scr.annotations.Activate)1 SolrException (org.apache.solr.common.SolrException)1 SolrServerAdapter (org.apache.stanbol.commons.solr.SolrServerAdapter)1 SolrServerProperties (org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties)1 BundleContext (org.osgi.framework.BundleContext)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1 SAXException (org.xml.sax.SAXException)1