Search in sources :

Example 1 with SolrServerAdapter

use of org.apache.stanbol.commons.solr.SolrServerAdapter in project stanbol by apache.

the class ManagedSolrServerImpl method uninitialiseCore.

/**
 * Uninitialise the index referenced by the parsed metadata and also deletes
 * the index data from the local file system if deleteFiles is enabled.
 * Updates to the state of the index are stored within the parsed
 * {@link IndexMetadata}.<p>
 * If the index is active, than the {@link SolrCore}
 * is first deactivated.
 * @param metadata the metadata for the core. This instance is modified
 * but not saved to {@link #managedCores} within this method.
 * So depending if callers want to remove or only uninitialise this core
 * the might want to store the updated version of this instance after this
 * method completes!
 * @param deleteFiles if the files on the local fileSystem should be deleted
 */
protected final void uninitialiseCore(IndexMetadata metadata, boolean deleteFiles) {
    SolrServerAdapter server = this.server;
    File coreDir = null;
    if (metadata.isActive()) {
        coreDir = deactivateCore(metadata.getIndexName(), server);
    }
    if (coreDir == null) {
        String coreDirName = metadata.getDirectory();
        if (coreDirName != null) {
            coreDir = new File(coreDirName);
        }
    }
    if (deleteFiles) {
        // no directory assigned
        metadata.setDirectory(null);
        // no archive used for the index
        metadata.setArchive(null);
        if (coreDir != null) {
            try {
                FileUtils.deleteDirectory(coreDir);
            } catch (IOException e) {
                log.error(String.format("Unable to delete Directory %s of the " + "removed index '%s' of the managed SolrServer '{}'. " + "Please try to delete this directory manually!", coreDir.getAbsolutePath(), metadata.getIndexName(), serverName), e);
            }
        }
    }
    metadata.setState(ManagedIndexState.UNINITIALISED);
}
Also used : IOException(java.io.IOException) File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter)

Example 2 with SolrServerAdapter

use of org.apache.stanbol.commons.solr.SolrServerAdapter in project stanbol by apache.

the class ManagedSolrServerImpl method getSolrIndexDirectory.

@Override
public File getSolrIndexDirectory(String indexName) {
    if (indexName == null || indexName.isEmpty()) {
        throw new IllegalArgumentException("The parsed index name MUST NOT be NULL nor empty!");
    }
    SolrServerAdapter server = this.server;
    ServiceReference ref = server.getCore(indexName);
    String dirName = ref != null ? (String) ref.getProperty(SolrConstants.PROPERTY_CORE_DIR) : null;
    return dirName == null ? null : new File(dirName);
}
Also used : File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with SolrServerAdapter

use of org.apache.stanbol.commons.solr.SolrServerAdapter 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)

Example 4 with SolrServerAdapter

use of org.apache.stanbol.commons.solr.SolrServerAdapter in project stanbol by apache.

the class ManagedSolrServerImpl method updateCore.

/**
 * Updates the core with the parsed name with the data parsed within the
 * ArchiveInputStream and stores updated to the state of the index within the
 * parsed {@link IndexMetadata} instance.
 * @param metadata the metadata of the index to update. The parsed instance
 * is updated within this method
 * @param ais the data
 * @throws IOException On any Error while copying the data for the Index
 * @throws SAXException On any Error while parsing the Solr Configuration for
 */
protected final void updateCore(final IndexMetadata metadata, ArchiveInputStream ais) throws IOException, SAXException {
    if (metadata == null) {
        throw new IllegalArgumentException("The parsed metadata for the Solr index MUST NOT be NULL");
    }
    if (metadata.isEmpty()) {
        throw new IllegalArgumentException("The parsed metadata for the Solr index MUST NOT be empty");
    }
    String coreName = metadata.getIndexName();
    if (coreName == null || coreName.isEmpty()) {
        throw new IllegalArgumentException("The parse metadata do not contain a valid value for the '" + INDEX_NAME + "'!");
    }
    SolrServerAdapter server = this.server;
    if (server == null) {
        log.info("Unable to update core '{}' because this ManagedSolrServer is already deactivated.");
        return;
    }
    ServiceReference coreRef = server.getCore(coreName);
    // dir of the previous version
    File currentCoreDir;
    if (coreRef != null) {
        currentCoreDir = getCoreDir(coreRef, true);
    } else {
        // no old version
        currentCoreDir = null;
    }
    // the name of the "new" core directory
    String coreDirName;
    synchronized (coreSuffixDateFormat) {
        // SimpleDateFormat is not thread save. It may fail badly on two
        // concurrent calls.
        coreDirName = coreName + '-' + coreSuffixDateFormat.format(new Date());
    }
    File coreDir = new File(managedSolrDir, coreDirName);
    int count = 1;
    while (coreDir.exists()) {
        // if this is the second call on a day ... add a count
        // if directories get deleted directories with a higher count might
        // be older versions than directories without or with a lower count!
        coreDir = new File(managedSolrDir, coreDirName + "-" + count);
        count++;
    }
    // TODO maybe we need to call getAbsolute path
    metadata.setDirectory(coreDir.getName());
    // no the initialisation/update of this core starts!
    synchronized (initCores) {
        log.debug(" > start initializing SolrIndex {}" + coreName);
        initCores.put(coreName, coreDir);
    }
    try {
        // outer try for finally removal from initCores
        try {
            // not the third parameter (coreName) is not the name of this
            // core, but the original name within the indexArchive
            String archiveCoreName = getArchiveCoreName(metadata);
            ConfigUtils.copyCore(ais, coreDir, archiveCoreName, false);
        } catch (IOException e) {
            e = new IOException(String.format("Unable to copy Data for index '%s' (server '%s')", coreName, serverName), e);
            // store this Error in the metadata
            metadata.setError(e);
            throw e;
        }
        try {
            activateCore(metadata, server);
            metadata.setState(ManagedIndexState.ACTIVE);
            if (currentCoreDir != null) {
                // remove the data of the old core
                try {
                    FileUtils.deleteDirectory(currentCoreDir);
                } catch (IOException e) {
                    // only log an Error and do not throw an Exception in that case
                    log.error(String.format("Unable to delete Directory %s of the " + "old (and no longer needed) version of the index '%s' " + "of the managed SolrServer '{}'. Please try to " + "delete this directory manually!", currentCoreDir.getAbsolutePath(), coreName, serverName), e);
                }
            }
        } catch (IOException e) {
            // store Errors in the metadata
            metadata.setError(e);
            throw e;
        } catch (SAXException e) {
            metadata.setError(e);
            throw e;
        } catch (RuntimeException e) {
            metadata.setError(e);
            throw e;
        }
    } finally {
        // indexes and notify all other waiting for the initialisation
        synchronized (initCores) {
            // initialisation done
            initCores.remove(coreName);
            log.debug("   ... notify after trying to init SolrIndex {}" + coreName);
            // notify that the initialisation completed or failed
            initCores.notifyAll();
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter) Date(java.util.Date) ServiceReference(org.osgi.framework.ServiceReference) SAXException(org.xml.sax.SAXException)

Example 5 with SolrServerAdapter

use of org.apache.stanbol.commons.solr.SolrServerAdapter in project stanbol by apache.

the class ReferencedSolrServer method activate.

/*
     * NOTE: one could here also get the properties of the parsed 
     * ComponentContext and directly parse the values to the constructor of the
     * SolrServerAdapter. However here the configured values are all checkted
     * to generate meaningful error messages
     */
@Activate
protected void activate(ComponentContext context) throws ConfigurationException {
    log.info("Activate {}: ", getClass().getSimpleName());
    SolrServerProperties properties = null;
    Object value = context.getProperties().get(PROPERTY_SERVER_DIR);
    if (value == null || value.toString().isEmpty()) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, "The Server directory is a " + "required configuration and MUST NOT be NULL nor empty!");
    } else {
        File solrServerDir = new File(value.toString());
        if (solrServerDir.isDirectory()) {
            log.info(" > solrDir = {}", solrServerDir);
            properties = new SolrServerProperties(solrServerDir);
        } else {
            throw new ConfigurationException(PROPERTY_SERVER_DIR, "The parsed Solr Server directpry '" + value + "' does not exist or is not a directory!");
        }
    }
    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 {
        properties.setServerName(value.toString());
        log.info(" > Name = {}", value.toString());
    }
    value = context.getProperties().get(PROPERTY_SERVER_RANKING);
    if (value instanceof Number) {
        properties.setServerRanking(((Number) value).intValue());
    } else if (value != null && !value.toString().isEmpty()) {
        try {
            properties.setServerRanking(Integer.parseInt(value.toString()));
            log.info(" > Ranking = {}", properties.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 = properties.get(PROPERTY_SERVER_PUBLISH_REST);
    if (value == null || value instanceof Boolean) {
        properties.setPublishREST((Boolean) value);
    } else {
        properties.setPublishREST(Boolean.parseBoolean(value.toString()));
    }
    log.info(" > publisRest = {}", properties.isPublishREST());
    try {
        server = new SolrServerAdapter(context.getBundleContext(), properties);
    } catch (SolrException e) {
        throw new ConfigurationException(PROPERTY_SERVER_DIR, "Unable to initialise " + "a SolrServer based on the Directory '" + properties.getServerDir() + "'!", e);
    }
    log.info(" ... SolrServer successfully initialised!");
}
Also used : SolrServerProperties(org.apache.stanbol.commons.solr.SolrServerAdapter.SolrServerProperties) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) SolrServerAdapter(org.apache.stanbol.commons.solr.SolrServerAdapter) SolrException(org.apache.solr.common.SolrException) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

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