use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.
the class ManagedSolrServerImpl method swapIndexes.
@Override
public void swapIndexes(String indexName1, String indexName2) {
if (!(isManagedIndex(indexName1) && isManagedIndex(indexName2))) {
throw new IllegalArgumentException(String.format("Both core names (%s,%s) must correspond to a managed index", indexName1, indexName2));
}
if (!(managedCores.isInState(ManagedIndexState.ACTIVE, indexName1) && managedCores.isInState(ManagedIndexState.ACTIVE, indexName2))) {
throw new IllegalStateException(String.format("Both cores (%s,%s) should be in ManagedIndexState.ACTIVE state", indexName1, indexName2));
}
Object token = new Object();
synchronized (serverInUser) {
serverInUser.add(token);
}
try {
server.swap(indexName1, indexName2);
IndexMetadata core1Metadata = getIndexMetadata(indexName1);
IndexMetadata core2Metadata = getIndexMetadata(indexName2);
String core2Directory = core2Metadata.getDirectory();
core2Metadata.setDirectory(core1Metadata.getDirectory());
core1Metadata.setDirectory(core2Directory);
managedCores.store(core1Metadata);
managedCores.store(core2Metadata);
} finally {
synchronized (serverInUser) {
serverInUser.remove(token);
token = null;
serverInUser.notifyAll();
}
}
}
use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.
the class ManagedSolrServerImpl method removeIndex.
@Override
public void removeIndex(String indexName, boolean deleteFiles) {
if (indexName == null || indexName.isEmpty()) {
throw new IllegalArgumentException("The parsed index name MUST NOT be NULL nor empty!");
}
// remove the index from the metadata
IndexMetadata metadata = managedCores.remove(indexName);
if (metadata != null) {
// and also tracked index archives from the DataFileTracker
indexArchiveTracker.removeTracking(metadata);
uninitialiseCore(metadata, deleteFiles);
}
}
use of org.apache.stanbol.commons.solr.managed.IndexMetadata 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;
}
use of org.apache.stanbol.commons.solr.managed.IndexMetadata in project stanbol by apache.
the class StandaloneManagedSolrServer method updateIndex.
@Override
public IndexMetadata updateIndex(String name, String parsedResourceName, Properties properties) throws IOException {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("The parsed index name MUST NOT be NULL nor empty!");
}
String resourceName;
if (!ConfigUtils.isValidSolrIndexFileName(parsedResourceName)) {
log.debug("add SolrIndexFileExtension to parsed indexArchive {}", parsedResourceName);
resourceName = ConfigUtils.appandSolrIndexFileExtension(parsedResourceName, null);
} else {
resourceName = parsedResourceName;
}
Map<String, String> comments = new HashMap<String, String>();
if (properties != null) {
for (Entry<Object, Object> prop : properties.entrySet()) {
comments.put(prop.getKey().toString(), prop.getValue().toString());
}
}
InputStream is = null;
for (Iterator<DataFileProvider> it = dataFileProviders.iterator(); is == null && it.hasNext(); ) {
DataFileProvider dfp = it.next();
try {
is = dfp.getInputStream(null, resourceName, comments);
} catch (IOException e) {
// not found
}
}
if (is != null || new File(managedSolrDir, parsedResourceName).isDirectory()) {
ArchiveInputStream ais;
try {
ais = ManagementUtils.getArchiveInputStream(resourceName, is);
} catch (ArchiveException e) {
throw new IOException("Unable to open ArchiveInputStream for resource '" + resourceName + "'!", e);
}
IndexMetadata metadata = new IndexMetadata();
if (properties != null) {
metadata.putAll(properties);
}
metadata.setIndexName(name);
metadata.setServerName(DEFAULT_SERVER_NAME);
metadata.setSynchronized(false);
metadata.setState(ManagedIndexState.ACTIVE);
metadata.setArchive(resourceName);
return updateCore(metadata, ais);
} else {
return null;
}
}
use of org.apache.stanbol.commons.solr.managed.IndexMetadata 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);
}
}
Aggregations