Search in sources :

Example 1 with IndexReference

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

the class FstLinkingEngineTest method setup.

@BeforeClass
public static void setup() throws Exception {
    // get the working directory
    // use property substitution to test this feature!
    String prefix = System.getProperty("basedir") == null ? "." : "${basedir}";
    String solrServerDir = prefix + TEST_INDEX_REL_PATH;
    log.info("Test Solr Server Directory: {}", solrServerDir);
    System.setProperty(ManagedSolrServer.MANAGED_SOLR_DIR_PROPERTY, solrServerDir);
    SolrYardConfig config = new SolrYardConfig(TEST_YARD_ID, TEST_SOLR_CORE_NAME);
    config.setAllowInitialisation(false);
    // the dbpedia default data
    config.setIndexConfigurationName(TEST_SOLR_CORE_CONFIGURATION);
    // init from datafile provider
    config.setAllowInitialisation(true);
    config.setName("DBpedia.org default data");
    config.setDescription("Data used for FstLinkingEngie tests");
    // create the Yard used for the tests
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    SolrServer server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrIndexRef, config.getIndexConfigurationName());
    Assert.assertNotNull("Unable to initialise SolrServer for testing", server);
    core = ((EmbeddedSolrServer) server).getCoreContainer().getCore(solrIndexRef.getIndex());
    Assert.assertNotNull("Unable to get SolrCore '" + config.getIndexConfigurationName() + "' from SolrServer " + server, core);
    yard = new SolrYard(server, config, null);
    // setup the index configuration
    LanguageConfiguration langConf = new LanguageConfiguration("not.used", new String[] { "en;field=dbpedia-ont:surfaceForm;generate=true" });
    fstConfig = new IndexConfiguration(langConf, core, FieldEncodingEnum.SolrYard, "");
    fstConfig.setExecutorService(Executors.newFixedThreadPool(1));
    fstConfig.setTypeField("rdf:type");
    fstConfig.setRankingField("entityhub:entityRank");
    // fstConfig.setEntityCacheManager(new FastLRUCacheManager(2048));
    fstConfig.setOrigin(new PlainLiteralImpl(TEST_ORIGIN));
    // activate the FST config
    // activate this configuration
    fstConfig.activate();
    // validate that the index contains the expected entities
    validateTestIndex();
    // now create the FST models
    List<Future<?>> creationTasks = new ArrayList<Future<?>>();
    for (CorpusInfo corpus : fstConfig.getCorpora()) {
        Assert.assertTrue("Failure in UnitTest - all FST models need to be generate=true", corpus.allowCreation);
        if (!corpus.isFstFile()) {
            // create a task on the FST corpus creation service
            creationTasks.add(fstConfig.getExecutorService().submit(new CorpusCreationTask(fstConfig, corpus)));
        }
    }
    // typical hardware
    for (Future<?> future : creationTasks) {
        try {
            future.get(FST_CREATION_WAIT_TIME, TimeUnit.SECONDS);
        } catch (TimeoutException e) {
        // we assert on future.isDone instead
        }
        Assert.assertTrue("FST Model creation not finished after " + FST_CREATION_WAIT_TIME + "seconds", future.isDone());
    }
}
Also used : PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) ArrayList(java.util.ArrayList) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) SolrYardConfig(org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig) IndexConfiguration(org.apache.stanbol.enhancer.engines.lucenefstlinking.IndexConfiguration) CorpusInfo(org.apache.stanbol.enhancer.engines.lucenefstlinking.CorpusInfo) Future(java.util.concurrent.Future) CorpusCreationTask(org.apache.stanbol.enhancer.engines.lucenefstlinking.CorpusCreationTask) LanguageConfiguration(org.apache.stanbol.enhancer.nlp.utils.LanguageConfiguration) IndexReference(org.apache.stanbol.commons.solr.IndexReference) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) TimeoutException(java.util.concurrent.TimeoutException) BeforeClass(org.junit.BeforeClass)

Example 2 with IndexReference

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

the class ConfiguredSolrCoreTracker method configureSolrCore.

protected void configureSolrCore(Dictionary<String, Object> config, String solrCoreProperty, String defaultCoreId, String solrCoreConfigProperty) throws ConfigurationException {
    Object solrCoreInfo = config.get(solrCoreProperty);
    if (solrCoreInfo instanceof SolrServer) {
        // Bind a fixed Solr server client instead of doing dynamic OSGi lookup using the service tracker.
        // This can be useful both for unit-testing .
        solrServer = (SolrServer) config.get(solrCoreProperty);
        solrCoreConfig = TopicClassificationEngine.DEFAULT_SOLR_CORE_CONFIG;
    } else {
        if (context == null) {
            throw new ConfigurationException(solrCoreProperty, solrCoreProperty + " should be a SolrServer instance for using" + " the engine without any OSGi context. Got: " + solrCoreId);
        }
        if (solrCoreInfo != null && !solrCoreInfo.toString().trim().isEmpty()) {
            this.solrCoreId = solrCoreInfo.toString().trim();
        } else {
            this.solrCoreId = defaultCoreId;
        }
        solrCoreConfig = getRequiredStringParam(config, solrCoreConfigProperty, this.solrCoreId + ".solrindex.zip");
        try {
            IndexReference indexReference = IndexReference.parse(solrCoreId);
            // String configName = getRequiredStringParam(config, SOLR_CONFIG, defaultValue)
            indexReference = checkInitSolrIndex(indexReference, solrCoreConfig);
            // track the solr core OSGi updates
            indexTracker = new RegisteredSolrServerTracker(context.getBundleContext(), indexReference);
            indexTracker.open();
        } catch (Exception e) {
            throw new ConfigurationException(solrCoreProperty, e.getMessage(), e);
        }
    }
}
Also used : RegisteredSolrServerTracker(org.apache.stanbol.commons.solr.RegisteredSolrServerTracker) ConfigurationException(org.osgi.service.cm.ConfigurationException) SolrServer(org.apache.solr.client.solrj.SolrServer) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) IndexReference(org.apache.stanbol.commons.solr.IndexReference) IOException(java.io.IOException) ConfigurationException(org.osgi.service.cm.ConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with IndexReference

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

the class LDPathTestBase method setup.

@BeforeClass
public static void setup() throws Exception {
    // get the working directory
    // use property substitution to test this feature!
    String prefix = System.getProperty("basedir") == null ? "." : "${basedir}";
    String solrServerDir = prefix + TEST_INDEX_REL_PATH;
    log.info("Test Solr Server Directory: {}", solrServerDir);
    System.setProperty(ManagedSolrServer.MANAGED_SOLR_DIR_PROPERTY, solrServerDir);
    SolrYardConfig config = new SolrYardConfig(TEST_YARD_ID, TEST_SOLR_CORE_NAME);
    config.setAllowInitialisation(false);
    // the dbpedia default data
    config.setIndexConfigurationName(TEST_SOLR_CORE_CONFIGURATION);
    // init from datafile provider
    config.setAllowInitialisation(true);
    config.setName("DBpedia.org default data");
    config.setDescription("Data used for the LDPath setup");
    // create the Yard used for the tests
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    SolrServer server = StandaloneEmbeddedSolrServerProvider.getInstance().getSolrServer(solrIndexRef, config.getIndexConfigurationName());
    Assert.assertNotNull("Unable to initialise SolrServer for testing", server);
    yard = new SolrYard(server, config, null);
    backend = new YardBackend(yard);
}
Also used : YardBackend(org.apache.stanbol.entityhub.ldpath.backend.YardBackend) IndexReference(org.apache.stanbol.commons.solr.IndexReference) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) SolrYard(org.apache.stanbol.entityhub.yard.solr.impl.SolrYard) SolrYardConfig(org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig) BeforeClass(org.junit.BeforeClass)

Example 4 with IndexReference

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

the class SolrYardComponent method initManagedSolrIndex.

/**
 * initialise ManagedSolrIndex and that starts tracking for the {@link SolrCore}
 * @param managedServer the managedServer to init the SolrCore (if necessary)
 * @param config the {@link SolrYardConfig}
 * @throws IllegalStateException if the initialization fails
 */
private void initManagedSolrIndex(final ManagedSolrServer managedServer, final SolrYardConfig config) {
    if (managedServer == null || config == null) {
        // component was not yet activated ... will be called again
        return;
    }
    IndexReference solrIndexRef = IndexReference.parse(config.getSolrServerLocation());
    if (config.isAllowInitialisation()) {
        // are we allowed to create the SolrServer
        // get the name of the config to be used (default: default.solrindex.zip")
        String configName = config.getIndexConfigurationName();
        IndexMetadata metadata = managedServer.getIndexMetadata(solrIndexRef.getIndex());
        if (metadata == null) {
            // create a new index
            log.info(" ... creating Managed SolrIndex {} (configName: {}) on Server {}", new Object[] { solrIndexRef.getIndex(), configName, managedServer.getServerName() });
            try {
                metadata = managedServer.createSolrIndex(solrIndexRef.getIndex(), configName, null);
            } catch (IOException e) {
                throw new IllegalStateException("Unable to create Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            }
        } else if (metadata.getState() != ManagedIndexState.ACTIVE) {
            log.info(" ... activating Managed SolrIndex {} on Server {} (current state: {})", new Object[] { solrIndexRef.getIndex(), managedServer.getServerName(), metadata.getState() });
            try {
                managedServer.activateIndex(metadata.getIndexName());
            } catch (IOException e) {
                throw new IllegalStateException("Unable to activate Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            } catch (SAXException e) {
                throw new IllegalStateException("Unable to activate Managed SolrIndex " + solrIndexRef.getIndex() + " (configName: " + configName + ") on Server " + managedServer.getServerName() + "!", e);
            }
        }
        // else already active nothing todo
        solrIndexRef = metadata.getIndexReference();
    }
    // else the SolrServer will be supplied (e.g. created by installing a full index)
    try {
        registeredServerTracker = new RegisteredSolrServerTracker(context, solrIndexRef, null) {

            @Override
            public void removedService(ServiceReference reference, Object service) {
                updateSolrYardRegistration(registeredServerTracker.getService(), config);
                super.removedService(reference, service);
            }

            @Override
            public void modifiedService(ServiceReference reference, Object service) {
                updateSolrYardRegistration(registeredServerTracker.getService(), config);
                super.modifiedService(reference, service);
            }

            @Override
            public SolrServer addingService(ServiceReference reference) {
                SolrServer server = super.addingService(reference);
                if (solrServer != null) {
                    log.warn("Multiple SolrServer for IndexLocation {} available!", config.getSolrServerLocation());
                } else {
                    updateSolrYardRegistration(server, config);
                }
                return server;
            }
        };
        log.info(" ... start tracking for SolrCore based on {}", solrIndexRef);
        // start tracking
        registeredServerTracker.open();
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException("Unable to track Managed SolrIndex " + solrIndexRef.getIndex() + "on Server " + managedServer.getServerName() + "!", e);
    }
}
Also used : RegisteredSolrServerTracker(org.apache.stanbol.commons.solr.RegisteredSolrServerTracker) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) IndexMetadata(org.apache.stanbol.commons.solr.managed.IndexMetadata) IndexReference(org.apache.stanbol.commons.solr.IndexReference) ManagedSolrServer(org.apache.stanbol.commons.solr.managed.ManagedSolrServer) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) SolrServer(org.apache.solr.client.solrj.SolrServer) SAXException(org.xml.sax.SAXException) ServiceReference(org.osgi.framework.ServiceReference)

Example 5 with IndexReference

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

the class SolrYardComponent method activate.

@Activate
protected void activate(ComponentContext ctx) throws ConfigurationException {
    this.context = ctx.getBundleContext();
    @SuppressWarnings("unchecked") SolrYardConfig config = new SolrYardConfig((Dictionary<String, Object>) ctx.getProperties());
    log.info("activate {} (name:{})", getClass().getSimpleName(), config.getId());
    String indexLocation = config.getSolrServerLocation();
    if (indexLocation.startsWith("http") && indexLocation.indexOf("://") > 0) {
        solrServer = new HttpSolrServer(indexLocation);
        // directly register configs that use a remote server
        updateSolrYardRegistration(solrServer, config);
    } else {
        // locally managed Server
        IndexReference solrServerRef = IndexReference.parse(config.getSolrServerLocation());
        // default
        if (config.isAllowInitialisation() && solrServerRef.getServer() != null) {
            throw new ConfigurationException(SolrYardConfig.SOLR_SERVER_LOCATION, "The SolrServerLocation ({server-name}:{index-name}) MUST NOT define a " + "{server-name} if '" + SolrYardConfig.ALLOW_INITIALISATION_STATE + "' is enabled. Change the cofiguration to use just a {index-name}");
        }
        // check if we need to init the SolrIndex
        initManagedSolrIndex(managedSolrServer, config);
    }
    // globally set the config
    this.config = config;
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) HttpSolrServer(org.apache.solr.client.solrj.impl.HttpSolrServer) IndexReference(org.apache.stanbol.commons.solr.IndexReference) Activate(org.apache.felix.scr.annotations.Activate)

Aggregations

IndexReference (org.apache.stanbol.commons.solr.IndexReference)8 SolrServer (org.apache.solr.client.solrj.SolrServer)5 ManagedSolrServer (org.apache.stanbol.commons.solr.managed.ManagedSolrServer)5 SolrYard (org.apache.stanbol.entityhub.yard.solr.impl.SolrYard)4 IOException (java.io.IOException)3 SolrYardConfig (org.apache.stanbol.entityhub.yard.solr.impl.SolrYardConfig)3 BeforeClass (org.junit.BeforeClass)3 EmbeddedSolrServer (org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)2 HttpSolrServer (org.apache.solr.client.solrj.impl.HttpSolrServer)2 RegisteredSolrServerTracker (org.apache.stanbol.commons.solr.RegisteredSolrServerTracker)2 IndexMetadata (org.apache.stanbol.commons.solr.managed.IndexMetadata)2 StandaloneManagedSolrServer (org.apache.stanbol.commons.solr.managed.standalone.StandaloneManagedSolrServer)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 SAXException (org.xml.sax.SAXException)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)1 Activate (org.apache.felix.scr.annotations.Activate)1