internal virtual void AddReplicaIfNotPresent(DatanodeStorageInfo storage, Block block , HdfsServerConstants.ReplicaState rState) { IEnumerator <BlockInfoContiguousUnderConstruction.ReplicaUnderConstruction> it = replicas .GetEnumerator(); while (it.HasNext()) { BlockInfoContiguousUnderConstruction.ReplicaUnderConstruction r = it.Next(); DatanodeStorageInfo expectedLocation = r.GetExpectedStorageLocation(); if (expectedLocation == storage) { // Record the gen stamp from the report r.SetGenerationStamp(block.GetGenerationStamp()); return; } else { if (expectedLocation != null && expectedLocation.GetDatanodeDescriptor() == storage .GetDatanodeDescriptor()) { // The Datanode reported that the block is on a different storage // than the one chosen by BlockPlacementPolicy. This can occur as // we allow Datanodes to choose the target storage. Update our // state by removing the stale entry and adding a new one. it.Remove(); break; } } } replicas.AddItem(new BlockInfoContiguousUnderConstruction.ReplicaUnderConstruction (block, storage, rState)); }
/// <summary>Initialize lease recovery for this block.</summary> /// <remarks> /// Initialize lease recovery for this block. /// Find the first alive data-node starting from the previous primary and /// make it primary. /// </remarks> public virtual void InitializeBlockRecovery(long recoveryId) { SetBlockUCState(HdfsServerConstants.BlockUCState.UnderRecovery); blockRecoveryId = recoveryId; if (replicas.Count == 0) { NameNode.blockStateChangeLog.Warn("BLOCK*" + " BlockInfoUnderConstruction.initLeaseRecovery:" + " No blocks found, lease removed."); } bool allLiveReplicasTriedAsPrimary = true; for (int i = 0; i < replicas.Count; i++) { // Check if all replicas have been tried or not. if (replicas[i].IsAlive()) { allLiveReplicasTriedAsPrimary = (allLiveReplicasTriedAsPrimary && replicas[i].GetChosenAsPrimary ()); } } if (allLiveReplicasTriedAsPrimary) { // Just set all the replicas to be chosen whether they are alive or not. for (int i_1 = 0; i_1 < replicas.Count; i_1++) { replicas[i_1].SetChosenAsPrimary(false); } } long mostRecentLastUpdate = 0; BlockInfoContiguousUnderConstruction.ReplicaUnderConstruction primary = null; primaryNodeIndex = -1; for (int i_2 = 0; i_2 < replicas.Count; i_2++) { // Skip alive replicas which have been chosen for recovery. if (!(replicas[i_2].IsAlive() && !replicas[i_2].GetChosenAsPrimary())) { continue; } BlockInfoContiguousUnderConstruction.ReplicaUnderConstruction ruc = replicas[i_2]; long lastUpdate = ruc.GetExpectedStorageLocation().GetDatanodeDescriptor().GetLastUpdateMonotonic (); if (lastUpdate > mostRecentLastUpdate) { primaryNodeIndex = i_2; primary = ruc; mostRecentLastUpdate = lastUpdate; } } if (primary != null) { primary.GetExpectedStorageLocation().GetDatanodeDescriptor().AddBlockToBeRecovered (this); primary.SetChosenAsPrimary(true); NameNode.blockStateChangeLog.Info("BLOCK* {} recovery started, primary={}", this, primary); } }