Search in sources :

Example 1 with PropertiesOutputStream

use of org.apache.solr.util.PropertiesOutputStream in project lucene-solr by apache.

the class SolrCore method writeNewIndexProps.

/**
   * Write the index.properties file with the new index sub directory name
   * @param dir a data directory (containing an index.properties file)
   * @param tmpFileName the file name to write the new index.properties to
   * @param tmpIdxDirName new index directory name
   */
private static void writeNewIndexProps(Directory dir, String tmpFileName, String tmpIdxDirName) {
    if (tmpFileName == null) {
        tmpFileName = IndexFetcher.INDEX_PROPERTIES;
    }
    final Properties p = new Properties();
    // Read existing properties
    try {
        final IndexInput input = dir.openInput(IndexFetcher.INDEX_PROPERTIES, DirectoryFactory.IOCONTEXT_NO_CACHE);
        final InputStream is = new PropertiesInputStream(input);
        try {
            p.load(new InputStreamReader(is, StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("Unable to load " + IndexFetcher.INDEX_PROPERTIES, e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    } catch (IOException e) {
    // ignore; file does not exist
    }
    p.put("index", tmpIdxDirName);
    // Write new properties
    Writer os = null;
    try {
        IndexOutput out = dir.createOutput(tmpFileName, DirectoryFactory.IOCONTEXT_NO_CACHE);
        os = new OutputStreamWriter(new PropertiesOutputStream(out), StandardCharsets.UTF_8);
        p.store(os, IndexFetcher.INDEX_PROPERTIES);
        dir.sync(Collections.singleton(tmpFileName));
    } catch (Exception e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to write " + IndexFetcher.INDEX_PROPERTIES, e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) InputStream(java.io.InputStream) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) PropertiesInputStream(org.apache.solr.util.PropertiesInputStream) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IOException(java.io.IOException) NoSuchFileException(java.nio.file.NoSuchFileException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) KeeperException(org.apache.zookeeper.KeeperException) PHPSerializedResponseWriter(org.apache.solr.response.PHPSerializedResponseWriter) XMLResponseWriter(org.apache.solr.response.XMLResponseWriter) SolrIndexWriter(org.apache.solr.update.SolrIndexWriter) SmileResponseWriter(org.apache.solr.response.SmileResponseWriter) GeoJSONResponseWriter(org.apache.solr.response.GeoJSONResponseWriter) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) PythonResponseWriter(org.apache.solr.response.PythonResponseWriter) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) SchemaXmlResponseWriter(org.apache.solr.response.SchemaXmlResponseWriter) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(java.io.Writer) PHPResponseWriter(org.apache.solr.response.PHPResponseWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GraphMLResponseWriter(org.apache.solr.response.GraphMLResponseWriter) RubyResponseWriter(org.apache.solr.response.RubyResponseWriter) CSVResponseWriter(org.apache.solr.response.CSVResponseWriter) OutputStreamWriter(java.io.OutputStreamWriter) RawResponseWriter(org.apache.solr.response.RawResponseWriter) SolrException(org.apache.solr.common.SolrException) PropertiesOutputStream(org.apache.solr.util.PropertiesOutputStream)

Example 2 with PropertiesOutputStream

use of org.apache.solr.util.PropertiesOutputStream in project lucene-solr by apache.

the class IndexFetcher method logReplicationTimeAndConfFiles.

/**
   * Helper method to record the last replication's details so that we can show them on the statistics page across
   * restarts.
   * @throws IOException on IO error
   */
@SuppressForbidden(reason = "Need currentTimeMillis for debugging/stats")
private void logReplicationTimeAndConfFiles(Collection<Map<String, Object>> modifiedConfFiles, boolean successfulInstall) throws IOException {
    List<String> confFiles = new ArrayList<>();
    if (modifiedConfFiles != null && !modifiedConfFiles.isEmpty())
        for (Map<String, Object> map1 : modifiedConfFiles) confFiles.add((String) map1.get(NAME));
    Properties props = replicationHandler.loadReplicationProperties();
    long replicationTime = System.currentTimeMillis();
    long replicationTimeTaken = getReplicationTimeElapsed();
    Directory dir = null;
    try {
        dir = solrCore.getDirectoryFactory().get(solrCore.getDataDir(), DirContext.META_DATA, solrCore.getSolrConfig().indexConfig.lockType);
        int indexCount = 1, confFilesCount = 1;
        if (props.containsKey(TIMES_INDEX_REPLICATED)) {
            indexCount = Integer.parseInt(props.getProperty(TIMES_INDEX_REPLICATED)) + 1;
        }
        StringBuilder sb = readToStringBuilder(replicationTime, props.getProperty(INDEX_REPLICATED_AT_LIST));
        props.setProperty(INDEX_REPLICATED_AT_LIST, sb.toString());
        props.setProperty(INDEX_REPLICATED_AT, String.valueOf(replicationTime));
        props.setProperty(PREVIOUS_CYCLE_TIME_TAKEN, String.valueOf(replicationTimeTaken));
        props.setProperty(TIMES_INDEX_REPLICATED, String.valueOf(indexCount));
        if (modifiedConfFiles != null && !modifiedConfFiles.isEmpty()) {
            props.setProperty(CONF_FILES_REPLICATED, confFiles.toString());
            props.setProperty(CONF_FILES_REPLICATED_AT, String.valueOf(replicationTime));
            if (props.containsKey(TIMES_CONFIG_REPLICATED)) {
                confFilesCount = Integer.parseInt(props.getProperty(TIMES_CONFIG_REPLICATED)) + 1;
            }
            props.setProperty(TIMES_CONFIG_REPLICATED, String.valueOf(confFilesCount));
        }
        props.setProperty(LAST_CYCLE_BYTES_DOWNLOADED, String.valueOf(getTotalBytesDownloaded()));
        if (!successfulInstall) {
            int numFailures = 1;
            if (props.containsKey(TIMES_FAILED)) {
                numFailures = Integer.parseInt(props.getProperty(TIMES_FAILED)) + 1;
            }
            props.setProperty(TIMES_FAILED, String.valueOf(numFailures));
            props.setProperty(REPLICATION_FAILED_AT, String.valueOf(replicationTime));
            sb = readToStringBuilder(replicationTime, props.getProperty(REPLICATION_FAILED_AT_LIST));
            props.setProperty(REPLICATION_FAILED_AT_LIST, sb.toString());
        }
        String tmpFileName = REPLICATION_PROPERTIES + "." + System.nanoTime();
        final IndexOutput out = dir.createOutput(tmpFileName, DirectoryFactory.IOCONTEXT_NO_CACHE);
        Writer outFile = new OutputStreamWriter(new PropertiesOutputStream(out), StandardCharsets.UTF_8);
        try {
            props.store(outFile, "Replication details");
            dir.sync(Collections.singleton(tmpFileName));
        } finally {
            IOUtils.closeQuietly(outFile);
        }
        solrCore.getDirectoryFactory().renameWithOverwrite(dir, tmpFileName, REPLICATION_PROPERTIES);
    } catch (Exception e) {
        LOG.warn("Exception while updating statistics", e);
    } finally {
        if (dir != null) {
            solrCore.getDirectoryFactory().release(dir);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IndexOutput(org.apache.lucene.store.IndexOutput) Properties(java.util.Properties) NoSuchFileException(java.nio.file.NoSuchFileException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OutputStreamWriter(java.io.OutputStreamWriter) Map(java.util.Map) HashMap(java.util.HashMap) IndexWriter(org.apache.lucene.index.IndexWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) Directory(org.apache.lucene.store.Directory) PropertiesOutputStream(org.apache.solr.util.PropertiesOutputStream) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 NoSuchFileException (java.nio.file.NoSuchFileException)2 Properties (java.util.Properties)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 IndexOutput (org.apache.lucene.store.IndexOutput)2 SolrException (org.apache.solr.common.SolrException)2 PropertiesOutputStream (org.apache.solr.util.PropertiesOutputStream)2 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 Directory (org.apache.lucene.store.Directory)1 IndexInput (org.apache.lucene.store.IndexInput)1 LockObtainFailedException (org.apache.lucene.store.LockObtainFailedException)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1