Begins state transferring mode, all txns and snapshots persisted in state
transferring mode will not show up after recovery if you do not call
{@link endStateTransfer}.
772278
void beginStateTransfer() throws IOException {
    this.isTransferring = true;
    this.dataDir =
      Files.createTempDirectory(this.rootDir.toPath(), "tmp_data").toFile();
    this.log = createLog(this.dataDir);
  }
747684
public boolean containsTransition(final TransitionableState state, final String transition) {
        if (state == null) {
            LOGGER.error("State is not configured correctly and cannot be null.");
            return false;
        }
        return state.getTransition(transition) != null;
    }
46362
@Override
    public final synchronized void commit(ContainerTx tx)
                    throws RemoteException
    {
        final boolean isTraceOn = TraceComponent.isAnyTracingEnabled();
        if (isTraceOn && tc.isEntryEnabled())
            Tr.entry(tc, "commit: " + getStateName(state), tx);

        if (removed) {
            if (isTraceOn && tc.isEntryEnabled())
                Tr.exit(tc, "commit: removed");
            return;
        }

        if (tx.ivRemoveBeanO == this)
        {
            completeRemoveMethod(tx); // d647928
        }
        else
        {
            // Perform normal afterCompletion processing.... calling
            // afterCompletion if present.
            setState(COMMITTING_OUTSIDE_METHOD, AFTER_COMPLETION); // d159152

            // Session Synchronization AfterCompletion needs to be called if either
            // the annotation (or XML) was specified or the bean implemented
            // the interface and a global transaction is active.        F743-25855
            if ((ivAfterCompletion != null || sessionSync != null) &&
                tx.isTransactionGlobal())
            {
                EJBThreadData threadData = EJSContainer.getThreadData();

                threadData.pushContexts(this);
                try
                {
                    if (ivAfterCompletion != null) {
                        invokeSessionSynchMethod(ivAfterCompletion, new Object[] { true });
                    } else {
                        sessionSync.afterCompletion(true);
                    }
                } finally
                {
                    threadData.popContexts();
                }
            }

            setState(AFTER_COMPLETION, METHOD_READY); // d159152
        }

        // currentTx is now reset in atCommit/atRollback AFTER the final pin
        // is undone.                                                      d258770
        // currentTx = null;                                            // PQ99986

        if (isTraceOn && tc.isEntryEnabled())
            Tr.exit(tc, "commit: " + getStateName(state));
    }
237280
@Override
    public void chainStarted(ChainData chainData) {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.entry(tc, "chainStarted", chainData);

        chainState.set(ChainState.STARTED.val);

        if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
            SibTr.exit(tc, "chainStarted");

    }
361666
void deliverUndeliveredTxns() throws IOException {
    LOG.debug("Delivering all the undelivered txns after {}",
              lastDeliveredZxid);
    Zxid startZxid =
      new Zxid(lastDeliveredZxid.getEpoch(), lastDeliveredZxid.getXid() + 1);
    Log log = persistence.getLog();
    try (Log.LogIterator iter = log.getIterator(startZxid)) {
      while (iter.hasNext()) {
        Transaction txn = iter.next();
        if (txn.getType() == ProposalType.USER_REQUEST_VALUE) {
          // Only delivers REQUEST proposal, ignores COP.
          stateMachine.deliver(txn.getZxid(), txn.getBody(), null, null);
        }
        lastDeliveredZxid = txn.getZxid();
      }
    }
  }
1298858
protected final synchronized void transitionState(OperationState newState) {
    getLogger().debug("Transitioned state from %s to %s", state, newState);
    state = newState;
    // Discard our buffer when we no longer need it.
    if(state != OperationState.WRITE_QUEUED
        && state != OperationState.WRITING) {
      cmd = null;
    }
    if (state == OperationState.COMPLETE) {
      callback.complete();
    }
  }
50302
@Override
    public void setState(TransferState state) {
        super.setState(state);

        switch (state) {
        case Waiting:
            fireProgressEvent(ProgressEventType.TRANSFER_PREPARING_EVENT);
            break;
        case InProgress:
            if ( subTransferStarted.compareAndSet(false, true) ) {
                /* The first InProgress signal */
                fireProgressEvent(ProgressEventType.TRANSFER_STARTED_EVENT);
            }
            /* Don't need any event code update for subsequent InProgress signals */
            break;
        case Completed:
            fireProgressEvent(ProgressEventType.TRANSFER_COMPLETED_EVENT);
            break;
        case Canceled:
            fireProgressEvent(ProgressEventType.TRANSFER_CANCELED_EVENT);
            break;
        case Failed:
            fireProgressEvent(ProgressEventType.TRANSFER_FAILED_EVENT);
            break;
        default:
            break;
        }
    }
835429
@Override
    public void end() {
        final EntityManager em = entityManager.get();

        // Let's not penalize users for calling end() multiple times.
        if (null == em) {
          return;
        }

        final EntityTransaction tx = em.getTransaction();

        if(tx.isActive()) {
            LOG.warn("There was an active transaction at the end of a request");
            tx.commit();
        }

        em.close();
        entityManager.remove();
    }
1356228
public void sessionEnd(SessionStatus finishState) {
    if (sessionStatusToMetrics.containsKey(finishState)) {
      sessionStatusToMetrics.get(finishState).inc();
    } else {
      throw new IllegalArgumentException("Invalid end state " + finishState);
    }
  }
492412
@Override
	public void commitChanges( SynchronizationBarrier barrier ) throws JMSException
    {
    	checkNotClosed();
    	checkTransactionLock();
    	
    	if (persistentStore != null)
    	{
    		long start = System.currentTimeMillis();
    		synchronized (storeLock)
			{
    			persistentStore.commitChanges(barrier);
			}
    		long end = System.currentTimeMillis();
    		notifyCommitTime(end-start);

    		// Clear pending commit flag
    		pendingChanges = false;
    	}
    }
121389
private void takeSnapshotIfCommitIndexAdvanced() {
        long commitIndex = state.commitIndex();
        if ((commitIndex - state.log().snapshotIndex()) < commitIndexAdvanceCountToSnapshot) {
            return;
        }

        if (isTerminatedOrSteppedDown()) {
            // If the status is UPDATING_MEMBER_LIST or TERMINATING, it means the status is normally ACTIVE
            // and there is an appended but not committed RaftGroupCmd.
            // If the status is TERMINATED or STEPPED_DOWN, then there will not be any new appends.
            return;
        }

        RaftLog log = state.log();
        Object snapshot = raftIntegration.takeSnapshot(commitIndex);
        if (snapshot instanceof Throwable) {
            Throwable t = (Throwable) snapshot;
            logger.severe("Could not take snapshot at commit index: " + commitIndex, t);
            return;
        }

        int snapshotTerm = log.getLogEntry(commitIndex).term();
        RaftGroupMembers members = state.committedGroupMembers();
        SnapshotEntry snapshotEntry = new SnapshotEntry(snapshotTerm, commitIndex, snapshot, members.index(), members.members());

        long minMatchIndex = 0L;
        LeaderState leaderState = state.leaderState();
        if (leaderState != null) {
            long[] indices = leaderState.matchIndices();
            // Last slot is reserved for leader index,
            // and always zero. That's why we are skipping it.
            Arrays.sort(indices, 0, indices.length - 1);
            minMatchIndex = indices[0];
        }

        long truncateLogsUpToIndex = max(commitIndex - maxNumberOfLogsToKeepAfterSnapshot, minMatchIndex);
        int truncated = log.setSnapshot(snapshotEntry, truncateLogsUpToIndex);

        if (logger.isFineEnabled()) {
            logger.fine(snapshotEntry + " is taken, " + truncated + " entries are truncated.");
        }
    }
459179
public void cancel() {
      if (!isCancelled) {
         isCancelled = true;

         IntSet segmentsCopy = getUnfinishedSegments();
         synchronized (segments) {
            unfinishedSegments.clear();
         }
         if (trace) {
            log.tracef("Cancelling inbound state transfer from %s with unfinished segments %s", source, segmentsCopy);
         }

         sendCancelCommand(segmentsCopy);

         notifyCompletion(false);
      }
   }
996947
public static void endTransaction() {
        ifNotNullThen(localConnection.get(),
                () -> {
                    Connection connection = localConnection.get();
                    ifThen(connection.isRollbackOnClose(), connection::close);
                    localConnection.remove();
                });
    }
1323273
private boolean completeSnapshot(long index) {
    // Compute the lowest completed index for all sessions that belong to this state machine.
    long lastCompleted = index;
    for (RaftSession session : raft.getSessions().getSessions()) {
      lastCompleted = Math.min(lastCompleted, session.getLastCompleted());
    }
    return lastCompleted >= index;
  }
1226236
@Override
  public boolean persistState() {
    if (!safeMode) {
      LOG.info(
        "Cannot persist state because ClusterManager is not in Safe Mode");
      return false;
    }

    try {
      JsonGenerator jsonGenerator = CoronaSerializer.createJsonGenerator(conf);
      jsonGenerator.writeStartObject();

      jsonGenerator.writeFieldName("startTime");
      jsonGenerator.writeNumber(startTime);

      jsonGenerator.writeFieldName("nodeManager");
      nodeManager.write(jsonGenerator);

      jsonGenerator.writeFieldName("sessionManager");
      sessionManager.write(jsonGenerator);

      jsonGenerator.writeFieldName("sessionNotifier");
      sessionNotifier.write(jsonGenerator);

      jsonGenerator.writeEndObject();
      jsonGenerator.close();
    } catch (IOException e) {
      LOG.info("Could not persist the state: ", e);
      return false;
    }
    return true;
  }
953690
@Override
    public void transferProgressed(TransferEvent event) {
        TransferResource resource = event.getResource();

        long lastTransferred = downloads.get(resource);
        long transferred = event.getTransferredBytes();

        if (transferred - lastTransferred >= TRANSFER_THRESHOLD) {
            downloads.put(resource, Long.valueOf(transferred));
            long total = resource.getContentLength();
            log.finer(getStatus(transferred, total) + ", ");
        }
    }
551887
public boolean recoverTransitionRead(StartupOption startOpt)
      throws IOException {
    
    FLOG.info("Startup: recovering namenode storage");
    
    assert startOpt != StartupOption.FORMAT : 
      "NameNode formatting should be performed before reading the image";
    
    Collection<File> imageDirs = storage.getImageDirectories();

    // none of the data dirs exist
    if(imageDirs.size() == 0 && startOpt != StartupOption.IMPORT)  
      throw new IOException(
          "All specified directories are not accessible or do not exist.");
    editLog.checkJournals();
    
    storage.setUpgradeManager(namesystem.upgradeManager);
    
    Map<ImageManager, RemoteStorageState> remoteImageStates = Maps.newHashMap();
    Map<JournalManager, RemoteStorageState> remoteJournalStates = Maps.newHashMap();

    List<ImageManager> nonFileImageManagers = getNonFileImageManagers();
    List<JournalManager> nonFileJournalManagers = getNonFileJournalManagers();

    updateRemoteStates(remoteImageStates, remoteJournalStates,
        nonFileImageManagers, nonFileJournalManagers);
    
    // number of non-file storage locations
    int nonFileStorageLocations = nonFileImageManagers.size()
        + nonFileJournalManagers.size();
    
    FLOG.info("Startup: checking storage directory state.");
    // 1. For each data directory calculate its state and 
    // check whether all is consistent before transitioning.
    Map<StorageDirectory, StorageState> dataDirStates = 
             new HashMap<StorageDirectory, StorageState>();
    boolean isFormatted = recoverStorageDirs(startOpt, dataDirStates);
    
    // Recover the non-file storage locations.
    editLog.transitionNonFileJournals(null, false,
        Transition.RECOVER, startOpt);
    imageSet.transitionNonFileImages(null, false, Transition.RECOVER,
        startOpt);

    if (!isFormatted && startOpt != StartupOption.ROLLBACK 
                     && startOpt != StartupOption.IMPORT) {
      for(Entry<StorageDirectory, StorageState> e : dataDirStates.entrySet()) {
        LOG.info("State : " + e.getKey().getCurrentDir() + " state: " +e.getValue());
      }
      throw new IOException("NameNode is not formatted." + dataDirStates);      
    }


    int layoutVersion = storage.getLayoutVersion();
    if (layoutVersion < Storage.LAST_PRE_UPGRADE_LAYOUT_VERSION) {
      NNStorage.checkVersionUpgradable(storage.getLayoutVersion());
    }
    if (startOpt != StartupOption.UPGRADE
        && layoutVersion < Storage.LAST_PRE_UPGRADE_LAYOUT_VERSION
        && layoutVersion != FSConstants.LAYOUT_VERSION) {
      throw new IOException(
          "\nFile system image contains an old layout version " 
          + storage.getLayoutVersion() + ".\nAn upgrade to version "
          + FSConstants.LAYOUT_VERSION + " is required.\n"
          + "Please restart NameNode with -upgrade option.");
    }
       
    editLog.updateNamespaceInfo(storage);

    // check whether distributed upgrade is required and/or should be continued
    storage.verifyDistributedUpgradeProgress(startOpt);

    FLOG.info("Startup: formatting unformatted directories.");
    
    // 2. Format unformatted dirs.
    for (Iterator<StorageDirectory> it = storage.dirIterator(); it.hasNext();) {
      StorageDirectory sd = it.next();
      StorageState curState = dataDirStates.get(sd);
      switch(curState) {
      case NON_EXISTENT:
        throw new IOException(StorageState.NON_EXISTENT + 
                              " state cannot be here");
      case NOT_FORMATTED:        
        LOG.info("Storage directory " + sd.getRoot() + " is not formatted.");
        if (!sd.isEmpty()) {
          LOG.error("Storage directory " + sd.getRoot()
            + " is not empty, and will not be formatted! Exiting.");
          throw new IOException(
            "Storage directory " + sd.getRoot() + " is not empty!");
        }   
        LOG.info("Formatting ...");
        sd.clearDirectory(); // create empty currrent dir
        break;
      default:
        break;
      }
    }
    
    // check non-file images
    for (Entry<ImageManager, RemoteStorageState> e : remoteImageStates
        .entrySet()) {
      checkAllowedNonFileState(e.getValue().getStorageState(), e.getKey());
    }
    // check non-file journals
    for (Entry<JournalManager, RemoteStorageState> e : remoteJournalStates
        .entrySet()) {
      checkAllowedNonFileState(e.getValue().getStorageState(), e.getKey());
    }  

    FLOG.info("Startup: Transitions.");
    // 3. Do transitions
    switch(startOpt) {
    case UPGRADE:
      doUpgrade();
      return false; // upgrade saved image already
    case IMPORT:
      doImportCheckpoint();
      if (nonFileStorageLocations > 0) {
        throwIOException("Import not supported for non-file storage");
      }
      return false; // import checkpoint saved image already
    case ROLLBACK:
      doRollback(remoteImageStates, remoteJournalStates);
      // Update the states since the remote states have changed after rollback.
      updateRemoteStates(remoteImageStates, remoteJournalStates,
          nonFileImageManagers, nonFileJournalManagers);
      InjectionHandler.processEvent(InjectionEvent.FSIMAGE_ROLLBACK_DONE);
      break;
    case REGULAR:
      // just load the image
    }
    
    if (inUpgradeStatus()) {
      namesystem.setUpgradeStartTime(FSNamesystem.now());
    }
    
    // final consistency check for non-file images and journals
    // read version file first
    FSImageStorageInspector inspector = storage.readAndInspectDirs();
    
    FLOG.info("Startup: starting with storage info: " + storage.toColonSeparatedString());
    
    // format unformatted journals and images
    // check if the formatted ones are consistent with local storage
    for (Entry<ImageManager, RemoteStorageState> e : remoteImageStates
        .entrySet()) {
      if (e.getValue().getStorageState() != StorageState.NORMAL) {
        LOG.info("Formatting remote image: " + e.getKey());
        e.getKey().transitionImage(storage, Transition.FORMAT, null);
      } else {
        checkConsistency(e.getValue().getStorageInfo(), storage, true, e.getKey());
      }
    } 
    for (Entry<JournalManager, RemoteStorageState> e : remoteJournalStates
        .entrySet()) {
      if (e.getValue().getStorageState() != StorageState.NORMAL) {
        LOG.info("Formatting remote journal: " + e.getKey());
        e.getKey().transitionJournal(storage, Transition.FORMAT, null);
      } else {
        checkConsistency(e.getValue().getStorageInfo(), storage, true, e.getKey());
      }
    }

    // load the image
    return loadFSImage(inspector);
  }