Example usage for org.apache.solr.client.solrj.request CoreAdminRequest getStatus

List of usage examples for org.apache.solr.client.solrj.request CoreAdminRequest getStatus

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request CoreAdminRequest getStatus.

Prototype

public static CoreAdminResponse getStatus(String name, SolrClient client)
            throws SolrServerException, IOException 

Source Link

Usage

From source file:com.hurence.logisland.service.solr.api.SolrClientService.java

License:Apache License

protected boolean existsCore(String name) throws IOException, SolrServerException {
    CoreAdminResponse response = CoreAdminRequest.getStatus(name, getClient());

    return response.getCoreStatus(name).size() > 1;
}

From source file:com.mmj.app.lucene.solr.client.SolrClient.java

License:Open Source License

private boolean isDataDirExsited(String namespace) {
    CoreAdminResponse status;//  w w  w.  j av a  2  s .  c  o  m
    try {
        status = CoreAdminRequest.getStatus(namespace, solrServer);
    } catch (SolrServerException e) {
        logger.error("isSolrCoreExist", e);
        throw new SolrServerUnAvailableException("isSolrCoreExist", e);
    } catch (IOException e) {
        logger.error("isSolrCoreExist", e);
        throw new SolrServerUnAvailableException("isSolrCoreExist", e);
    }
    return status != null && status.getCoreStatus(namespace).get("instanceDir") != null;
}

From source file:com.ms.scombiz.solr.solr.SolrClient.java

License:Open Source License

private boolean isDataDirExsited(String namespace) {
    CoreAdminResponse status;/*from   ww w  .j av  a 2 s  .  com*/
    try {
        status = CoreAdminRequest.getStatus(namespace, rootSolrServer);
    } catch (SolrServerException e) {
        logger.error("isSolrCoreExist", e);
        throw new SolrServerUnAvailableException("isSolrCoreExist", e);
    } catch (IOException e) {
        logger.error("isSolrCoreExist", e);
        throw new SolrServerUnAvailableException("isSolrCoreExist", e);
    }
    return status != null && status.getCoreStatus(namespace).get("instanceDir") != null;
}

From source file:io.redlink.solrlib.embedded.EmbeddedCoreContainer.java

License:Apache License

@Override
@SuppressWarnings({ "squid:S3725", "squid:S3776" })
protected synchronized void init(ExecutorService executorService) throws IOException {
    Preconditions.checkState(Objects.isNull(coreContainer), "Already initialized!");

    if (solrHome == null) {
        solrHome = Files.createTempDirectory("solr-home");
        log.debug("No solr-home set, using temp directory {}", solrHome);
        deleteOnShutdown = true;/*from  www.  jav  a2s  . co  m*/
    }

    final Path absoluteSolrHome = this.solrHome.toAbsolutePath();
    if (Files.isDirectory(absoluteSolrHome)) {
        log.trace("solr-home exists: {}", absoluteSolrHome);
    } else {
        Files.createDirectories(absoluteSolrHome);
        log.debug("Created solr-home: {}", absoluteSolrHome);
    }
    final Path lib = absoluteSolrHome.resolve("lib");
    if (Files.isDirectory(lib)) {
        log.trace("lib-directory exists: {}", lib);
    } else {
        Files.createDirectories(lib);
        log.debug("Created solr-lib directory: {}", lib);
    }

    final Path solrXml = absoluteSolrHome.resolve("solr.xml");
    if (!Files.exists(solrXml)) {
        log.info("no solr.xml found, creating new at {}", solrXml);
        try (PrintStream writer = new PrintStream(Files.newOutputStream(solrXml, StandardOpenOption.CREATE))) {
            writer.printf("<!-- Generated by %s on %tF %<tT -->%n", getClass().getSimpleName(), new Date());
            writer.println("<solr>");
            writer.printf("  <str name=\"%s\">%s</str>%n", "sharedLib", absoluteSolrHome.relativize(lib));
            writer.println("</solr>");
        }
    } else {
        log.trace("found solr.xml: {}", solrXml);
    }

    for (SolrCoreDescriptor coreDescriptor : coreDescriptors) {
        final String coreName = coreDescriptor.getCoreName();
        if (availableCores.containsKey(coreName)) {
            log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName,
                    coreDescriptor.getClass());
            continue;
        }
        final Path coreDir = absoluteSolrHome.resolve(coreName);
        Files.createDirectories(coreDir);
        coreDescriptor.initCoreDirectory(coreDir, lib);

        final Properties coreProperties = new Properties();
        final Path corePropertiesFile = coreDir.resolve("core.properties");
        if (Files.exists(corePropertiesFile)) {
            try (InputStream inStream = Files.newInputStream(corePropertiesFile, StandardOpenOption.CREATE)) {
                coreProperties.load(inStream);
            }
            log.debug("core.properties for {} found, updating", coreName);
        } else {
            log.debug("Creating new core {} in {}", coreName, coreDir);
        }
        coreProperties.setProperty("name", coreName);
        try (OutputStream outputStream = Files.newOutputStream(corePropertiesFile)) {
            coreProperties.store(outputStream, null);
        }

        if (coreDescriptor.getNumShards() > 1 || coreDescriptor.getReplicationFactor() > 1) {
            log.warn("Deploying {} to EmbeddedCoreContainer, ignoring config of shards={},replication={}",
                    coreName, coreDescriptor.getNumShards(), coreDescriptor.getReplicationFactor());
        }

        availableCores.put(coreName, coreDescriptor);
    }

    log.info("Starting {} in solr-home '{}'", getClass().getSimpleName(), absoluteSolrHome);
    coreContainer = CoreContainer.createAndLoad(absoluteSolrHome, solrXml);

    availableCores.values().forEach(coreDescriptor -> {
        final String coreName = coreDescriptor.getCoreName();
        try (SolrClient solrClient = createSolrClient(coreName)) {
            final NamedList<Object> coreStatus = CoreAdminRequest.getStatus(coreName, solrClient)
                    .getCoreStatus(coreName);
            final NamedList<Object> indexStatus = coreStatus == null ? null
                    : (NamedList<Object>) coreStatus.get("index");
            final Object lastModified = indexStatus == null ? null : indexStatus.get("lastModified");
            // lastModified is null if there was never a update
            scheduleCoreInit(executorService, coreDescriptor, lastModified == null);
        } catch (SolrServerException | IOException e) {
            if (log.isDebugEnabled()) {
                log.error("Error initializing core {}", coreName, e);
            }
            //noinspection ThrowableResultOfMethodCallIgnored
            coreInitExceptions.put(coreName, e);
        }
    });
}

From source file:io.redlink.solrlib.standalone.SolrServerConnector.java

License:Apache License

@Override
@SuppressWarnings("squid:S3776")
protected void init(ExecutorService executorService) throws IOException, SolrServerException {
    Preconditions.checkState(initialized.compareAndSet(false, true));
    Preconditions.checkArgument(Objects.nonNull(solrBaseUrl));

    if (configuration.isDeployCores() && Objects.nonNull(configuration.getSolrHome())) {
        final Path solrHome = configuration.getSolrHome();
        Files.createDirectories(solrHome);
        final Path libDir = solrHome.resolve("lib");
        Files.createDirectories(libDir);

        try (HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) {
            for (SolrCoreDescriptor coreDescriptor : coreDescriptors) {
                final String coreName = coreDescriptor.getCoreName();
                if (availableCores.containsKey(coreName)) {
                    log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName,
                            coreDescriptor.getClass());
                    continue;
                }//from   ww  w. j av a2  s .co  m
                final String remoteName = createRemoteName(coreName);

                final Path coreHome = solrHome.resolve(remoteName);
                coreDescriptor.initCoreDirectory(coreHome, libDir);

                final Path corePropertiesFile = coreHome.resolve("core.properties");
                // core.properties is created by the CreateCore-Command.
                Files.deleteIfExists(corePropertiesFile);

                if (coreDescriptor.getNumShards() > 1 || coreDescriptor.getReplicationFactor() > 1) {
                    log.warn("Deploying {} to SolrServerConnector, ignoring config of shards={},replication={}",
                            coreName, coreDescriptor.getNumShards(), coreDescriptor.getReplicationFactor());
                }

                // Create or reload the core
                if (CoreAdminRequest.getStatus(remoteName, solrClient).getStartTime(remoteName) == null) {
                    final CoreAdminResponse adminResponse = CoreAdminRequest.createCore(remoteName,
                            coreHome.toAbsolutePath().toString(), solrClient);
                } else {
                    final CoreAdminResponse adminResponse = CoreAdminRequest.reloadCore(remoteName, solrClient);
                }
                // schedule client-side core init
                final boolean isNewCore = findInNamedList(
                        CoreAdminRequest.getStatus(remoteName, solrClient).getCoreStatus(remoteName), "index",
                        "lastModified") == null;
                scheduleCoreInit(executorService, coreDescriptor, isNewCore);

                availableCores.put(coreName, coreDescriptor);
            }
        }
    } else {
        try (HttpSolrClient solrClient = new HttpSolrClient.Builder(solrBaseUrl).build()) {
            for (SolrCoreDescriptor coreDescriptor : coreDescriptors) {
                final String coreName = coreDescriptor.getCoreName();
                if (availableCores.containsKey(coreName)) {
                    log.warn("CoreName-Clash: {} already initialized. Skipping {}", coreName,
                            coreDescriptor.getClass());
                    continue;
                }
                final String remoteName = createRemoteName(coreName);
                if (CoreAdminRequest.getStatus(remoteName, solrClient).getStartTime(remoteName) == null) {
                    // Core does not exists
                    log.warn("Collection {} (remote: {}) not available in Solr '{}' "
                            + "but deployCores is set to false", coreName, remoteName, solrBaseUrl);
                } else {
                    log.debug("Collection {} exists in Solr '{}' as {}", coreName, solrBaseUrl, remoteName);
                    scheduleCoreInit(executorService, coreDescriptor, false);
                    availableCores.put(coreName, coreDescriptor);
                }
            }
        }
    }
}

From source file:org.apache.gora.solr.store.SolrStore.java

License:Apache License

@Override
public boolean schemaExists() {
    boolean exists = false;
    try {//w w w.  j  av  a2  s  .  com
        CoreAdminResponse rsp = CoreAdminRequest.getStatus(mapping.getCoreName(), adminServer);
        exists = rsp.getUptime(mapping.getCoreName()) != null;
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
    }
    return exists;
}

From source file:org.codice.solr.factory.impl.HttpSolrClientFactory.java

License:Open Source License

private static boolean solrCoreExists(SolrClient client, String coreName)
        throws IOException, SolrServerException {
    CoreAdminResponse response = CoreAdminRequest.getStatus(coreName, client);
    return response.getCoreStatus(coreName).get("instanceDir") != null;
}

From source file:org.codice.solr.factory.SolrServerFactory.java

License:Open Source License

private static boolean solrCoreExists(SolrServer solrServer, String coreName) {
    try {// w  w  w. java 2 s  .  c  o m
        CoreAdminResponse response = CoreAdminRequest.getStatus(coreName, solrServer);
        return response.getCoreStatus(coreName).get("instanceDir") != null;
    } catch (SolrServerException e) {
        LOGGER.info("SolrServerException getting " + coreName + " core status", e);
        return false;
    } catch (IOException e) {
        LOGGER.info("IOException getting " + coreName + " core status", e);
        return false;
    }
}

From source file:org.dspace.util.SolrImportExport.java

License:BSD License

/**
 * Reindexes the specified core/*from  w w w  .j  ava2  s  .  co  m*/
 *
 * @param indexName the name of the core to reindex
 * @param exportDirName the name of the directory to use for export. If this directory doesn't exist, it will be created.
 * @param keepExport whether to keep the contents of the exportDir after the reindex. If keepExport is false and the
 *                      export directory was created by this method, the export directory will be deleted at the end of the reimport.
 */
private static void reindex(String indexName, String exportDirName, boolean keepExport)
        throws IOException, SolrServerException, SolrImportExportException {
    String tempIndexName = indexName + "-temp";

    String origSolrUrl = makeSolrUrl(indexName);
    String baseSolrUrl = StringUtils.substringBeforeLast(origSolrUrl, "/"); // need to get non-core solr URL
    String tempSolrUrl = baseSolrUrl + "/" + tempIndexName;

    String solrInstanceDir = ConfigurationManager.getProperty("dspace.dir") + File.separator + "solr"
            + File.separator + indexName;
    // the [dspace]/solr/[indexName]/conf directory needs to be available on the local machine for this to work
    // -- we need access to the schema.xml and solrconfig.xml file, plus files referenced from there
    // if this directory can't be found, output an error message and skip this index
    File solrInstance = new File(solrInstanceDir);
    if (!solrInstance.exists() || !solrInstance.canRead() || !solrInstance.isDirectory()) {
        throw new SolrImportExportException("Directory " + solrInstanceDir
                + "/conf/ doesn't exist or isn't readable."
                + " The reindexing process requires the Solr configuration directory for this index to be present on the local machine"
                + " even if Solr is running on a different host. Not reindexing index " + indexName);
    }

    String timeField = makeTimeField(indexName);

    // Ensure the export directory exists and is writable
    File exportDir = new File(exportDirName);
    boolean createdExportDir = exportDir.mkdirs();
    if (!createdExportDir && !exportDir.exists()) {
        throw new SolrImportExportException("Could not create export directory " + exportDirName);
    }
    if (!exportDir.canWrite()) {
        throw new SolrImportExportException("Can't write to export directory " + exportDirName);
    }

    try {
        HttpSolrServer adminSolr = new HttpSolrServer(baseSolrUrl);

        // try to find out size of core and compare with free space in export directory
        CoreAdminResponse status = CoreAdminRequest.getStatus(indexName, adminSolr);
        Object coreSizeObj = status.getCoreStatus(indexName).get("sizeInBytes");
        long coreSize = coreSizeObj != null ? Long.valueOf(coreSizeObj.toString()) : -1;
        long usableExportSpace = exportDir.getUsableSpace();
        if (coreSize >= 0 && usableExportSpace < coreSize) {
            System.err.println("Not enough space in export directory " + exportDirName
                    + "; need at least as much space as the index ("
                    + FileUtils.byteCountToDisplaySize(coreSize)
                    + ") but usable space in export directory is only "
                    + FileUtils.byteCountToDisplaySize(usableExportSpace)
                    + ". Not continuing with reindex, please use the " + DIRECTORY_OPTION
                    + " option to specify an alternative export directy with sufficient space.");
            return;
        }

        // Create a temp directory to store temporary core data
        File tempDataDir = new File(ConfigurationManager.getProperty("dspace.dir") + File.separator + "temp"
                + File.separator + "solr-data");
        boolean createdTempDataDir = tempDataDir.mkdirs();
        if (!createdTempDataDir && !tempDataDir.exists()) {
            throw new SolrImportExportException(
                    "Could not create temporary data directory " + tempDataDir.getCanonicalPath());
        }
        if (!tempDataDir.canWrite()) {
            throw new SolrImportExportException(
                    "Can't write to temporary data directory " + tempDataDir.getCanonicalPath());
        }

        try {
            // create a temporary core to hold documents coming in during the reindex
            CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
            createRequest.setInstanceDir(solrInstanceDir);
            createRequest.setDataDir(tempDataDir.getCanonicalPath());
            createRequest.setCoreName(tempIndexName);

            createRequest.process(adminSolr).getStatus();
        } catch (SolrServerException e) {
            // try to continue -- it may just be that the core already existed from a previous, failed attempt
            System.err.println("Caught exception when trying to create temporary core: " + e.getMessage()
                    + "; trying to recover.");
            e.printStackTrace(System.err);
        }

        // swap actual core with temporary one
        CoreAdminRequest swapRequest = new CoreAdminRequest();
        swapRequest.setCoreName(indexName);
        swapRequest.setOtherCoreName(tempIndexName);
        swapRequest.setAction(CoreAdminParams.CoreAdminAction.SWAP);
        swapRequest.process(adminSolr);

        try {
            // export from the actual core (from temp core name, actual data dir)
            exportIndex(indexName, exportDir, tempSolrUrl, timeField);

            // clear actual core (temp core name, clearing actual data dir) & import
            importIndex(indexName, exportDir, tempSolrUrl, true, true);
        } catch (Exception e) {
            // we ran into some problems with the export/import -- keep going to try and restore the solr cores
            System.err.println("Encountered problem during reindex: " + e.getMessage()
                    + ", will attempt to restore Solr cores");
            e.printStackTrace(System.err);
        }

        // commit changes
        HttpSolrServer origSolr = new HttpSolrServer(origSolrUrl);
        origSolr.commit();

        // swap back (statistics now going to actual core name in actual data dir)
        swapRequest = new CoreAdminRequest();
        swapRequest.setCoreName(tempIndexName);
        swapRequest.setOtherCoreName(indexName);
        swapRequest.setAction(CoreAdminParams.CoreAdminAction.SWAP);
        swapRequest.process(adminSolr);

        // export all docs from now-temp core into export directory -- this won't cause name collisions with the actual export
        // because the core name for the temporary export has -temp in it while the actual core doesn't
        exportIndex(tempIndexName, exportDir, tempSolrUrl, timeField);
        // ...and import them into the now-again-actual core *without* clearing
        importIndex(tempIndexName, exportDir, origSolrUrl, false, true);

        // commit changes
        origSolr.commit();

        // unload now-temp core (temp core name)
        CoreAdminRequest.unloadCore(tempIndexName, false, false, adminSolr);

        // clean up temporary data dir if this method created it
        if (createdTempDataDir && tempDataDir.exists()) {
            FileUtils.deleteDirectory(tempDataDir);
        }
    } finally {
        // clean up export dir if appropriate
        if (!keepExport && createdExportDir && exportDir.exists()) {
            FileUtils.deleteDirectory(exportDir);
        }
    }
}

From source file:org.emonocot.harvest.common.SolrOptimizingTasklet.java

License:Open Source License

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

    CoreAdminResponse coreAdminResponse = CoreAdminRequest.getStatus(core, solrServer);
    NamedList<Object> index = (NamedList<Object>) coreAdminResponse.getCoreStatus(core).get("index");
    Integer segmentCount = (Integer) index.get("segmentCount");

    if (segmentCount < maxSegments) {
        logger.debug("Core " + core + " only has " + segmentCount + " segments, skipping optimization");
    } else {//  w ww.  j  a v a 2 s  .c  om
        logger.debug("Core " + core + " has " + segmentCount + " segments, starting optimization");
        solrServer.optimize(true, true);
        logger.debug("Core " + core + " optimized");
    }

    return RepeatStatus.FINISHED;
}