Java Code Examples for org.apache.lucene.store.Directory#sync()

The following examples show how to use org.apache.lucene.store.Directory#sync() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaDataStateFormat.java    From crate with Apache License 2.0 6 votes vote down vote up
private static void copyStateToExtraLocations(List<Tuple<Path, Directory>> stateDirs, String tmpFileName)
        throws WriteStateException {
    Directory srcStateDir = stateDirs.get(0).v2();
    for (int i = 1; i < stateDirs.size(); i++) {
        Tuple<Path, Directory> extraStatePathAndDir = stateDirs.get(i);
        Path extraStateLocation = extraStatePathAndDir.v1();
        Directory extraStateDir = extraStatePathAndDir.v2();
        try {
            deleteFileIfExists(extraStateLocation, extraStateDir, tmpFileName);
            extraStateDir.copyFrom(srcStateDir, tmpFileName, tmpFileName, IOContext.DEFAULT);
            extraStateDir.sync(Collections.singleton(tmpFileName));
        } catch (Exception e) {
            throw new WriteStateException(false, "failed to copy tmp state file to extra location " + extraStateLocation, e);
        }
    }
}
 
Example 2
Source File: SegmentInfos.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void write(Directory directory) throws IOException {

    long nextGeneration = getNextPendingGeneration();
    String segmentFileName = IndexFileNames.fileNameFromGeneration(IndexFileNames.PENDING_SEGMENTS,
                                                                   "",
                                                                   nextGeneration);

    // Always advance the generation on write:
    generation = nextGeneration;
    
    IndexOutput segnOutput = null;
    boolean success = false;

    try {
      segnOutput = directory.createOutput(segmentFileName, IOContext.DEFAULT);
      write(segnOutput);
      segnOutput.close();
      directory.sync(Collections.singleton(segmentFileName));
      success = true;
    } finally {
      if (success) {
        pendingCommit = true;
      } else {
        // We hit an exception above; try to close the file
        // but suppress any exception:
        IOUtils.closeWhileHandlingException(segnOutput);
        // Try not to leave a truncated segments_N file in
        // the index:
        IOUtils.deleteFilesIgnoringExceptions(directory, segmentFileName);
      }
    }
  }
 
Example 3
Source File: TestBoolean2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Directory copyOf(Directory dir) throws IOException {
  Directory copy = newFSDirectory(createTempDir());
  for(String name : dir.listAll()) {
    if (name.startsWith("extra")) {
      continue;
    }
    copy.copyFrom(dir, name, name, IOContext.DEFAULT);
    copy.sync(Collections.singleton(name));
  }
  return copy;
}
 
Example 4
Source File: MetaDataStateFormat.java    From crate with Apache License 2.0 5 votes vote down vote up
private void writeStateToFirstLocation(final T state, Path stateLocation, Directory stateDir, String tmpFileName)
        throws WriteStateException {
    try {
        deleteFileIfExists(stateLocation, stateDir, tmpFileName);
        try (IndexOutput out = stateDir.createOutput(tmpFileName, IOContext.DEFAULT)) {
            CodecUtil.writeHeader(out, STATE_FILE_CODEC, STATE_FILE_VERSION);
            out.writeInt(FORMAT.index());
            try (XContentBuilder builder = newXContentBuilder(FORMAT, new IndexOutputOutputStream(out) {
                @Override
                public void close() {
                    // this is important since some of the XContentBuilders write bytes on close.
                    // in order to write the footer we need to prevent closing the actual index input.
                }
            })) {
                builder.startObject();
                toXContent(builder, state);
                builder.endObject();
            }
            CodecUtil.writeFooter(out);
        }

        stateDir.sync(Collections.singleton(tmpFileName));
    } catch (Exception e) {
        throw new WriteStateException(false, "failed to write state to the first location tmp file " +
                stateLocation.resolve(tmpFileName), e);
    }
}
 
Example 5
Source File: IndexFetcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * 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 (clearLocalIndexFirst) {
      props.setProperty(CLEARED_LOCAL_IDX, "true");
    }
    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);
    }
  }
}
 
Example 6
Source File: TestDirectoryFactory.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private void testExistsBehavior(Class<? extends DirectoryFactory> clazz) throws Exception {
  final String path = createTempDir().toString() + "/" + clazz + "_somedir";
  DirectoryFactory dirFac = null;
  try {
    dirFac = clazz.getConstructor().newInstance();
    dirFac.initCoreContainer(null); // greybox testing directly against path
    dirFac.init(new NamedList());

    assertFalse(path + " should not exist yet", dirFac.exists(path));
    Directory dir = dirFac.get(path, DirectoryFactory.DirContext.DEFAULT,
                               DirectoryFactory.LOCK_TYPE_SINGLE);
    try {
      assertFalse(path + " should still not exist", dirFac.exists(path));
      try (IndexOutput file = dir.createOutput("test_file", IOContext.DEFAULT)) {
        file.writeInt(42);

        // TODO: even StandardDirectoryFactory & NRTCachingDirectoryFactory can't agree on this...
        // ... should we consider this explicitly undefinied?
        // ... or should *all* Caching DirFactories consult the cache as well as the disk itself?
        // assertFalse(path + " should still not exist until file is closed", dirFac.exists(path));
        
      } // implicitly close file...
      
      // TODO: even StandardDirectoryFactory & NRTCachingDirectoryFactory can't agree on this...
      // ... should we consider this explicitly undefinied?
      // ... or should *all* Caching DirFactories consult the cache as well as the disk itself?
      // assertTrue(path + " should exist once file is closed", dirFac.exists(path));
      
      dir.sync(Collections.singleton("test_file"));
      assertTrue(path + " should exist once file is synced", dirFac.exists(path));

      
    } finally {
      dirFac.release(dir);
    }
    assertTrue(path + " should still exist even after being released", dirFac.exists(path));
    
  } catch (AssertionError ae) {
    throw new AssertionError(clazz + ": " + ae.getMessage());
  } finally {
    if (null != dirFac) {
      dirFac.close();
    }
  }
}