001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.hadoop.hdfs.server.blockmanagement;
019
020 import java.io.IOException;
021 import java.util.ArrayList;
022 import java.util.Iterator;
023 import java.util.List;
024
025 import org.apache.hadoop.hdfs.protocol.Block;
026 import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.BlockUCState;
027 import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.ReplicaState;
028 import org.apache.hadoop.hdfs.server.namenode.NameNode;
029
030 /**
031 * Represents a block that is currently being constructed.<br>
032 * This is usually the last block of a file opened for write or append.
033 */
034 public class BlockInfoUnderConstruction extends BlockInfo {
035 /** Block state. See {@link BlockUCState} */
036 private BlockUCState blockUCState;
037
038 /**
039 * Block replicas as assigned when the block was allocated.
040 * This defines the pipeline order.
041 */
042 private List<ReplicaUnderConstruction> replicas;
043
044 /** A data-node responsible for block recovery. */
045 private int primaryNodeIndex = -1;
046
047 /**
048 * The new generation stamp, which this block will have
049 * after the recovery succeeds. Also used as a recovery id to identify
050 * the right recovery if any of the abandoned recoveries re-appear.
051 */
052 private long blockRecoveryId = 0;
053
054 /**
055 * ReplicaUnderConstruction contains information about replicas while
056 * they are under construction.
057 * The GS, the length and the state of the replica is as reported by
058 * the data-node.
059 * It is not guaranteed, but expected, that data-nodes actually have
060 * corresponding replicas.
061 */
062 static class ReplicaUnderConstruction extends Block {
063 private DatanodeDescriptor expectedLocation;
064 private ReplicaState state;
065
066 ReplicaUnderConstruction(Block block,
067 DatanodeDescriptor target,
068 ReplicaState state) {
069 super(block);
070 this.expectedLocation = target;
071 this.state = state;
072 }
073
074 /**
075 * Expected block replica location as assigned when the block was allocated.
076 * This defines the pipeline order.
077 * It is not guaranteed, but expected, that the data-node actually has
078 * the replica.
079 */
080 DatanodeDescriptor getExpectedLocation() {
081 return expectedLocation;
082 }
083
084 /**
085 * Get replica state as reported by the data-node.
086 */
087 ReplicaState getState() {
088 return state;
089 }
090
091 /**
092 * Set replica state.
093 */
094 void setState(ReplicaState s) {
095 state = s;
096 }
097
098 /**
099 * Is data-node the replica belongs to alive.
100 */
101 boolean isAlive() {
102 return expectedLocation.isAlive;
103 }
104
105 @Override // Block
106 public int hashCode() {
107 return super.hashCode();
108 }
109
110 @Override // Block
111 public boolean equals(Object obj) {
112 // Sufficient to rely on super's implementation
113 return (this == obj) || super.equals(obj);
114 }
115
116 @Override
117 public String toString() {
118 final StringBuilder b = new StringBuilder(50);
119 appendStringTo(b);
120 return b.toString();
121 }
122
123 @Override
124 public void appendStringTo(StringBuilder sb) {
125 sb.append("ReplicaUnderConstruction[")
126 .append(expectedLocation)
127 .append("|")
128 .append(state)
129 .append("]");
130 }
131 }
132
133 /**
134 * Create block and set its state to
135 * {@link BlockUCState#UNDER_CONSTRUCTION}.
136 */
137 public BlockInfoUnderConstruction(Block blk, int replication) {
138 this(blk, replication, BlockUCState.UNDER_CONSTRUCTION, null);
139 }
140
141 /**
142 * Create a block that is currently being constructed.
143 */
144 public BlockInfoUnderConstruction(Block blk, int replication,
145 BlockUCState state,
146 DatanodeDescriptor[] targets) {
147 super(blk, replication);
148 assert getBlockUCState() != BlockUCState.COMPLETE :
149 "BlockInfoUnderConstruction cannot be in COMPLETE state";
150 this.blockUCState = state;
151 setExpectedLocations(targets);
152 }
153
154 /**
155 * Convert an under construction block to a complete block.
156 *
157 * @return BlockInfo - a complete block.
158 * @throws IOException if the state of the block
159 * (the generation stamp and the length) has not been committed by
160 * the client or it does not have at least a minimal number of replicas
161 * reported from data-nodes.
162 */
163 BlockInfo convertToCompleteBlock() throws IOException {
164 assert getBlockUCState() != BlockUCState.COMPLETE :
165 "Trying to convert a COMPLETE block";
166 return new BlockInfo(this);
167 }
168
169 /** Set expected locations */
170 public void setExpectedLocations(DatanodeDescriptor[] targets) {
171 int numLocations = targets == null ? 0 : targets.length;
172 this.replicas = new ArrayList<ReplicaUnderConstruction>(numLocations);
173 for(int i = 0; i < numLocations; i++)
174 replicas.add(
175 new ReplicaUnderConstruction(this, targets[i], ReplicaState.RBW));
176 }
177
178 /**
179 * Create array of expected replica locations
180 * (as has been assigned by chooseTargets()).
181 */
182 public DatanodeDescriptor[] getExpectedLocations() {
183 int numLocations = replicas == null ? 0 : replicas.size();
184 DatanodeDescriptor[] locations = new DatanodeDescriptor[numLocations];
185 for(int i = 0; i < numLocations; i++)
186 locations[i] = replicas.get(i).getExpectedLocation();
187 return locations;
188 }
189
190 /** Get the number of expected locations */
191 public int getNumExpectedLocations() {
192 return replicas == null ? 0 : replicas.size();
193 }
194
195 /**
196 * Return the state of the block under construction.
197 * @see BlockUCState
198 */
199 @Override // BlockInfo
200 public BlockUCState getBlockUCState() {
201 return blockUCState;
202 }
203
204 void setBlockUCState(BlockUCState s) {
205 blockUCState = s;
206 }
207
208 /** Get block recovery ID */
209 public long getBlockRecoveryId() {
210 return blockRecoveryId;
211 }
212
213 /**
214 * Commit block's length and generation stamp as reported by the client.
215 * Set block state to {@link BlockUCState#COMMITTED}.
216 * @param block - contains client reported block length and generation
217 * @throws IOException if block ids are inconsistent.
218 */
219 void commitBlock(Block block) throws IOException {
220 if(getBlockId() != block.getBlockId())
221 throw new IOException("Trying to commit inconsistent block: id = "
222 + block.getBlockId() + ", expected id = " + getBlockId());
223 blockUCState = BlockUCState.COMMITTED;
224 this.set(getBlockId(), block.getNumBytes(), block.getGenerationStamp());
225 }
226
227 /**
228 * Initialize lease recovery for this block.
229 * Find the first alive data-node starting from the previous primary and
230 * make it primary.
231 */
232 public void initializeBlockRecovery(long recoveryId) {
233 setBlockUCState(BlockUCState.UNDER_RECOVERY);
234 blockRecoveryId = recoveryId;
235 if (replicas.size() == 0) {
236 NameNode.blockStateChangeLog.warn("BLOCK*"
237 + " BlockInfoUnderConstruction.initLeaseRecovery:"
238 + " No blocks found, lease removed.");
239 }
240
241 int previous = primaryNodeIndex;
242 for(int i = 1; i <= replicas.size(); i++) {
243 int j = (previous + i)%replicas.size();
244 if (replicas.get(j).isAlive()) {
245 primaryNodeIndex = j;
246 DatanodeDescriptor primary = replicas.get(j).getExpectedLocation();
247 primary.addBlockToBeRecovered(this);
248 NameNode.blockStateChangeLog.info("BLOCK* " + this
249 + " recovery started, primary=" + primary);
250 return;
251 }
252 }
253 }
254
255 void addReplicaIfNotPresent(DatanodeDescriptor dn,
256 Block block,
257 ReplicaState rState) {
258 for(ReplicaUnderConstruction r : replicas)
259 if(r.getExpectedLocation() == dn)
260 return;
261 replicas.add(new ReplicaUnderConstruction(block, dn, rState));
262 }
263
264 @Override // BlockInfo
265 // BlockInfoUnderConstruction participates in maps the same way as BlockInfo
266 public int hashCode() {
267 return super.hashCode();
268 }
269
270 @Override // BlockInfo
271 public boolean equals(Object obj) {
272 // Sufficient to rely on super's implementation
273 return (this == obj) || super.equals(obj);
274 }
275
276 @Override
277 public String toString() {
278 final StringBuilder b = new StringBuilder(100);
279 appendStringTo(b);
280 return b.toString();
281 }
282
283 @Override
284 public void appendStringTo(StringBuilder sb) {
285 super.appendStringTo(sb);
286 appendUCParts(sb);
287 }
288
289 private void appendUCParts(StringBuilder sb) {
290 sb.append("{blockUCState=").append(blockUCState)
291 .append(", primaryNodeIndex=").append(primaryNodeIndex)
292 .append(", replicas=[");
293 Iterator<ReplicaUnderConstruction> iter = replicas.iterator();
294 if (iter.hasNext()) {
295 iter.next().appendStringTo(sb);
296 while (iter.hasNext()) {
297 sb.append(", ");
298 iter.next().appendStringTo(sb);
299 }
300 }
301 sb.append("]}");
302 }
303 }