Search in sources :

Example 11 with IndexMetadata

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

the class ManagedIndexMetadata method getIndexMetadata.

public IndexMetadata getIndexMetadata(String indexName) {
    IndexMetadata metadata = null;
    synchronized (inMemoryModelLock) {
        Iterator<Map<String, IndexMetadata>> inStateIt = managed.values().iterator();
        while (metadata == null && inStateIt.hasNext()) {
            metadata = inStateIt.next().get(indexName);
        }
    }
    // the internal state!
    if (metadata != null) {
        IndexMetadata clone = new IndexMetadata();
        clone.putAll(metadata);
        return clone;
    } else {
        return null;
    }
}
Also used : IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Example 12 with IndexMetadata

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

the class ManagedIndexMetadata method addUninitialisedIndex.

public void addUninitialisedIndex(String indexName, String indexArchiveName, Boolean sync) throws IOException {
    IndexMetadata config = new IndexMetadata();
    config = new IndexMetadata();
    config.setIndexName(indexName);
    config.setServerName(serverName);
    config.setState(ManagedIndexState.UNINITIALISED);
    if (sync != null) {
        config.setSynchronized(sync);
    }
    // no need to clone, because we have created the instance
    updateIndexProperties(null, config, false);
}
Also used : IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata)

Example 13 with IndexMetadata

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

the class ManagedSolrServerImpl method activateIndex.

@Override
public IndexMetadata activateIndex(String indexName) throws IOException, SAXException {
    IndexMetadata metadata = managedCores.getIndexMetadata(indexName);
    if (metadata != null && metadata.getState() == ManagedIndexState.INACTIVE) {
        try {
            activateCore(metadata, server);
            metadata.setState(ManagedIndexState.ACTIVE);
        } catch (IOException e) {
            metadata.setError(e);
            throw e;
        } catch (SAXException e) {
            metadata.setError(e);
            throw e;
        } catch (RuntimeException e) {
            metadata.setError(e);
            throw e;
        } finally {
            managedCores.store(metadata);
        }
    }
    return metadata;
}
Also used : IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) SAXException(org.xml.sax.SAXException)

Example 14 with IndexMetadata

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

the class ManagedSolrServerImpl method updateIndex.

@Override
public IndexMetadata updateIndex(String indexName, ArchiveInputStream ais, String archiveCoreName) throws IOException, SAXException {
    if (indexName == null || indexName.isEmpty()) {
        throw new IllegalArgumentException("The parsed name for the index MUST NOT" + "be NULL nor empty!");
    }
    if (ais == null) {
        throw new IOException("The parsed ArchiveInputStream MUST NOT be NULL!");
    }
    IndexMetadata metadata = new IndexMetadata();
    metadata.setServerName(serverName);
    metadata.setIndexName(indexName);
    metadata.setSynchronized(false);
    if (archiveCoreName != null) {
        metadata.setArchive(archiveCoreName);
    }
    try {
        updateCore(metadata, ais);
    } finally {
        managedCores.store(metadata);
    }
    return metadata;
}
Also used : IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata)

Example 15 with IndexMetadata

use of org.apache.stanbol.commons.solr.managed.IndexMetadata 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)21 IOException (java.io.IOException)7 File (java.io.File)3 HashMap (java.util.HashMap)3 ManagedIndexState (org.apache.stanbol.commons.solr.managed.ManagedIndexState)3 SAXException (org.xml.sax.SAXException)3 InputStream (java.io.InputStream)2 HashSet (java.util.HashSet)2 SolrCore (org.apache.solr.core.SolrCore)2 IndexReference (org.apache.stanbol.commons.solr.IndexReference)2 FileInputStream (java.io.FileInputStream)1 EnumMap (java.util.EnumMap)1 Map (java.util.Map)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1 SuffixFileFilter (org.apache.commons.io.filefilter.SuffixFileFilter)1 Activate (org.apache.felix.scr.annotations.Activate)1 SolrServer (org.apache.solr.client.solrj.SolrServer)1 HttpSolrServer (org.apache.solr.client.solrj.impl.HttpSolrServer)1