public void closeWriter()

in encryption/src/main/java/org/apache/solr/update/DirectUpdateHandler2Copy.java [905:1000]


  public void closeWriter(IndexWriter writer) throws IOException {
    log.trace("closeWriter({}): ulog={}", writer, ulog);

    assert TestInjection.injectNonGracefullClose(core.getCoreContainer());

    boolean clearRequestInfo = false;

    SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());
    SolrQueryResponse rsp = new SolrQueryResponse();
    if (SolrRequestInfo.getRequestInfo() == null) {
      clearRequestInfo = true;
      SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp)); // important for debugging
    }
    try {

      if (TestInjection.injectSkipIndexWriterCommitOnClose(writer)) {
        // if this TestInjection triggers, we do some simple rollback()
        // (which closes the underlying IndexWriter) and then return immediately
        log.warn("Skipping commit for IndexWriter.close() due to TestInjection");
        if (writer != null) {
          writer.rollback();
        }
        // we shouldn't close the transaction logs either, but leaving them open
        // means we can't delete them on windows (needed for tests)
        if (ulog != null) ulog.close(false);

        return;
      }

      // do a commit before we quit?
      boolean tryToCommit =
        writer != null
          && ulog != null
          && ulog.hasUncommittedChanges()
          && ulog.getState() == UpdateLog.State.ACTIVE;

      // be tactical with this lock! closing the updatelog can deadlock when it tries to commit
      solrCoreState.getCommitLock().lock();
      try {
        try {
          if (log.isInfoEnabled()) {
            log.info(
              "Committing on IndexWriter.close() {}.",
              (tryToCommit ? "" : " ... SKIPPED (unnecessary)"));
          }
          if (tryToCommit) {
            CommitUpdateCommand cmd = new CommitUpdateCommand(req, false);
            cmd.openSearcher = false;
            cmd.waitSearcher = false;
            cmd.softCommit = false;

            // TODO: keep other commit callbacks from being called?
            // this.commit(cmd); // too many test failures using this method... is it because of
            // callbacks?

            synchronized (solrCoreState.getUpdateLock()) {
              ulog.preCommit(cmd);
            }

            // todo: refactor this shared code (or figure out why a real CommitUpdateCommand can't
            // be used)
            SolrIndexWriter.setCommitData(writer, cmd.getVersion(), null);
            writer.commit();

            synchronized (solrCoreState.getUpdateLock()) {
              ulog.postCommit(cmd);
            }
          }
        } catch (Throwable th) {
          log.error("Error in final commit", th);
          if (th instanceof OutOfMemoryError) {
            throw (OutOfMemoryError) th;
          }
        }

      } finally {
        solrCoreState.getCommitLock().unlock();
      }
    } finally {
      if (clearRequestInfo) SolrRequestInfo.clearRequestInfo();
    }
    // we went through the normal process to commit, so we don't have to artificially
    // cap any ulog files.
    try {
      if (ulog != null) ulog.close(false);
    } catch (Throwable th) {
      log.error("Error closing log files", th);
      if (th instanceof OutOfMemoryError) {
        throw (OutOfMemoryError) th;
      }
    }

    if (writer != null) {
      writer.close();
    }
  }