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 org.apache.hadoop.hdfs.protocol.Block;
021    import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.BlockUCState;
022    import org.apache.hadoop.hdfs.server.namenode.INodeFile;
023    import org.apache.hadoop.hdfs.util.LightWeightGSet;
024    
025    /**
026     * Internal class for block metadata.
027     */
028    public class BlockInfo extends Block implements LightWeightGSet.LinkedElement {
029      private INodeFile inode;
030    
031      /** For implementing {@link LightWeightGSet.LinkedElement} interface */
032      private LightWeightGSet.LinkedElement nextLinkedElement;
033    
034      /**
035       * This array contains triplets of references.
036       * For each i-th datanode the block belongs to
037       * triplets[3*i] is the reference to the DatanodeDescriptor
038       * and triplets[3*i+1] and triplets[3*i+2] are references 
039       * to the previous and the next blocks, respectively, in the 
040       * list of blocks belonging to this data-node.
041       */
042      private Object[] triplets;
043    
044      /**
045       * Construct an entry for blocksmap
046       * @param replication the block's replication factor
047       */
048      public BlockInfo(int replication) {
049        this.triplets = new Object[3*replication];
050        this.inode = null;
051      }
052      
053      public BlockInfo(Block blk, int replication) {
054        super(blk);
055        this.triplets = new Object[3*replication];
056        this.inode = null;
057      }
058    
059      /**
060       * Copy construction.
061       * This is used to convert BlockInfoUnderConstruction
062       * @param from BlockInfo to copy from.
063       */
064      protected BlockInfo(BlockInfo from) {
065        this(from, from.inode.getReplication());
066        this.inode = from.inode;
067      }
068    
069      public INodeFile getINode() {
070        return inode;
071      }
072    
073      public void setINode(INodeFile inode) {
074        this.inode = inode;
075      }
076    
077      DatanodeDescriptor getDatanode(int index) {
078        assert this.triplets != null : "BlockInfo is not initialized";
079        assert index >= 0 && index*3 < triplets.length : "Index is out of bound";
080        DatanodeDescriptor node = (DatanodeDescriptor)triplets[index*3];
081        assert node == null || 
082            DatanodeDescriptor.class.getName().equals(node.getClass().getName()) : 
083                  "DatanodeDescriptor is expected at " + index*3;
084        return node;
085      }
086    
087      BlockInfo getPrevious(int index) {
088        assert this.triplets != null : "BlockInfo is not initialized";
089        assert index >= 0 && index*3+1 < triplets.length : "Index is out of bound";
090        BlockInfo info = (BlockInfo)triplets[index*3+1];
091        assert info == null || 
092            info.getClass().getName().startsWith(BlockInfo.class.getName()) : 
093                  "BlockInfo is expected at " + index*3;
094        return info;
095      }
096    
097      BlockInfo getNext(int index) {
098        assert this.triplets != null : "BlockInfo is not initialized";
099        assert index >= 0 && index*3+2 < triplets.length : "Index is out of bound";
100        BlockInfo info = (BlockInfo)triplets[index*3+2];
101        assert info == null || 
102            info.getClass().getName().startsWith(BlockInfo.class.getName()) : 
103                  "BlockInfo is expected at " + index*3;
104        return info;
105      }
106    
107      void setDatanode(int index, DatanodeDescriptor node) {
108        assert this.triplets != null : "BlockInfo is not initialized";
109        assert index >= 0 && index*3 < triplets.length : "Index is out of bound";
110        triplets[index*3] = node;
111      }
112    
113      void setPrevious(int index, BlockInfo to) {
114        assert this.triplets != null : "BlockInfo is not initialized";
115        assert index >= 0 && index*3+1 < triplets.length : "Index is out of bound";
116        triplets[index*3+1] = to;
117      }
118    
119      void setNext(int index, BlockInfo to) {
120        assert this.triplets != null : "BlockInfo is not initialized";
121        assert index >= 0 && index*3+2 < triplets.length : "Index is out of bound";
122        triplets[index*3+2] = to;
123      }
124    
125      int getCapacity() {
126        assert this.triplets != null : "BlockInfo is not initialized";
127        assert triplets.length % 3 == 0 : "Malformed BlockInfo";
128        return triplets.length / 3;
129      }
130    
131      /**
132       * Ensure that there is enough  space to include num more triplets.
133       * @return first free triplet index.
134       */
135      private int ensureCapacity(int num) {
136        assert this.triplets != null : "BlockInfo is not initialized";
137        int last = numNodes();
138        if(triplets.length >= (last+num)*3)
139          return last;
140        /* Not enough space left. Create a new array. Should normally 
141         * happen only when replication is manually increased by the user. */
142        Object[] old = triplets;
143        triplets = new Object[(last+num)*3];
144        System.arraycopy(old, 0, triplets, 0, last*3);
145        return last;
146      }
147    
148      /**
149       * Count the number of data-nodes the block belongs to.
150       */
151      int numNodes() {
152        assert this.triplets != null : "BlockInfo is not initialized";
153        assert triplets.length % 3 == 0 : "Malformed BlockInfo";
154        for(int idx = getCapacity()-1; idx >= 0; idx--) {
155          if(getDatanode(idx) != null)
156            return idx+1;
157        }
158        return 0;
159      }
160    
161      /**
162       * Add data-node this block belongs to.
163       */
164      public boolean addNode(DatanodeDescriptor node) {
165        if(findDatanode(node) >= 0) // the node is already there
166          return false;
167        // find the last null node
168        int lastNode = ensureCapacity(1);
169        setDatanode(lastNode, node);
170        setNext(lastNode, null);
171        setPrevious(lastNode, null);
172        return true;
173      }
174    
175      /**
176       * Remove data-node from the block.
177       */
178      public boolean removeNode(DatanodeDescriptor node) {
179        int dnIndex = findDatanode(node);
180        if(dnIndex < 0) // the node is not found
181          return false;
182        assert getPrevious(dnIndex) == null && getNext(dnIndex) == null : 
183          "Block is still in the list and must be removed first.";
184        // find the last not null node
185        int lastNode = numNodes()-1; 
186        // replace current node triplet by the lastNode one 
187        setDatanode(dnIndex, getDatanode(lastNode));
188        setNext(dnIndex, getNext(lastNode)); 
189        setPrevious(dnIndex, getPrevious(lastNode)); 
190        // set the last triplet to null
191        setDatanode(lastNode, null);
192        setNext(lastNode, null); 
193        setPrevious(lastNode, null); 
194        return true;
195      }
196    
197      /**
198       * Find specified DatanodeDescriptor.
199       * @param dn
200       * @return index or -1 if not found.
201       */
202      int findDatanode(DatanodeDescriptor dn) {
203        int len = getCapacity();
204        for(int idx = 0; idx < len; idx++) {
205          DatanodeDescriptor cur = getDatanode(idx);
206          if(cur == dn)
207            return idx;
208          if(cur == null)
209            break;
210        }
211        return -1;
212      }
213    
214      /**
215       * Insert this block into the head of the list of blocks 
216       * related to the specified DatanodeDescriptor.
217       * If the head is null then form a new list.
218       * @return current block as the new head of the list.
219       */
220      public BlockInfo listInsert(BlockInfo head, DatanodeDescriptor dn) {
221        int dnIndex = this.findDatanode(dn);
222        assert dnIndex >= 0 : "Data node is not found: current";
223        assert getPrevious(dnIndex) == null && getNext(dnIndex) == null : 
224                "Block is already in the list and cannot be inserted.";
225        this.setPrevious(dnIndex, null);
226        this.setNext(dnIndex, head);
227        if(head != null)
228          head.setPrevious(head.findDatanode(dn), this);
229        return this;
230      }
231    
232      /**
233       * Remove this block from the list of blocks 
234       * related to the specified DatanodeDescriptor.
235       * If this block is the head of the list then return the next block as 
236       * the new head.
237       * @return the new head of the list or null if the list becomes
238       * empty after deletion.
239       */
240      public BlockInfo listRemove(BlockInfo head, DatanodeDescriptor dn) {
241        if(head == null)
242          return null;
243        int dnIndex = this.findDatanode(dn);
244        if(dnIndex < 0) // this block is not on the data-node list
245          return head;
246    
247        BlockInfo next = this.getNext(dnIndex);
248        BlockInfo prev = this.getPrevious(dnIndex);
249        this.setNext(dnIndex, null);
250        this.setPrevious(dnIndex, null);
251        if(prev != null)
252          prev.setNext(prev.findDatanode(dn), next);
253        if(next != null)
254          next.setPrevious(next.findDatanode(dn), prev);
255        if(this == head)  // removing the head
256          head = next;
257        return head;
258      }
259    
260      /**
261       * BlockInfo represents a block that is not being constructed.
262       * In order to start modifying the block, the BlockInfo should be converted
263       * to {@link BlockInfoUnderConstruction}.
264       * @return {@link BlockUCState#COMPLETE}
265       */
266      public BlockUCState getBlockUCState() {
267        return BlockUCState.COMPLETE;
268      }
269    
270      /**
271       * Is this block complete?
272       * 
273       * @return true if the state of the block is {@link BlockUCState#COMPLETE}
274       */
275      public boolean isComplete() {
276        return getBlockUCState().equals(BlockUCState.COMPLETE);
277      }
278    
279      /**
280       * Convert a complete block to an under construction block.
281       * 
282       * @return BlockInfoUnderConstruction -  an under construction block.
283       */
284      public BlockInfoUnderConstruction convertToBlockUnderConstruction(
285          BlockUCState s, DatanodeDescriptor[] targets) {
286        if(isComplete()) {
287          return new BlockInfoUnderConstruction(
288              this, getINode().getReplication(), s, targets);
289        }
290        // the block is already under construction
291        BlockInfoUnderConstruction ucBlock = (BlockInfoUnderConstruction)this;
292        ucBlock.setBlockUCState(s);
293        ucBlock.setExpectedLocations(targets);
294        return ucBlock;
295      }
296    
297      @Override
298      public int hashCode() {
299        // Super implementation is sufficient
300        return super.hashCode();
301      }
302      
303      @Override
304      public boolean equals(Object obj) {
305        // Sufficient to rely on super's implementation
306        return (this == obj) || super.equals(obj);
307      }
308    
309      @Override
310      public LightWeightGSet.LinkedElement getNext() {
311        return nextLinkedElement;
312      }
313    
314      @Override
315      public void setNext(LightWeightGSet.LinkedElement next) {
316        this.nextLinkedElement = next;
317      }
318    }