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 */
018package org.apache.hadoop.hdfs.server.datanode.fsdataset;
019
020import java.io.Closeable;
021import java.io.File;
022import java.io.IOException;
023import java.nio.channels.ClosedChannelException;
024
025import org.apache.hadoop.fs.StorageType;
026import org.apache.hadoop.hdfs.protocol.ExtendedBlock;
027
028/**
029 * This is an interface for the underlying volume.
030 */
031public interface FsVolumeSpi {
032  /**
033   * Obtain a reference object that had increased 1 reference count of the
034   * volume.
035   *
036   * It is caller's responsibility to close {@link FsVolumeReference} to decrease
037   * the reference count on the volume.
038   */
039  FsVolumeReference obtainReference() throws ClosedChannelException;
040
041  /** @return the StorageUuid of the volume */
042  public String getStorageID();
043
044  /** @return a list of block pools. */
045  public String[] getBlockPoolList();
046
047  /** @return the available storage space in bytes. */
048  public long getAvailable() throws IOException;
049
050  /** @return the base path to the volume */
051  public String getBasePath();
052
053  /** @return the path to the volume */
054  public String getPath(String bpid) throws IOException;
055
056  /** @return the directory for the finalized blocks in the block pool. */
057  public File getFinalizedDir(String bpid) throws IOException;
058  
059  public StorageType getStorageType();
060
061  /**
062   * Reserve disk space for an RBW block so a writer does not run out of
063   * space before the block is full.
064   */
065  public void reserveSpaceForRbw(long bytesToReserve);
066
067  /**
068   * Release disk space previously reserved for RBW block.
069   */
070  public void releaseReservedSpace(long bytesToRelease);
071
072  /** Returns true if the volume is NOT backed by persistent storage. */
073  public boolean isTransientStorage();
074
075  /**
076   * BlockIterator will return ExtendedBlock entries from a block pool in
077   * this volume.  The entries will be returned in sorted order.<p/>
078   *
079   * BlockIterator objects themselves do not always have internal
080   * synchronization, so they can only safely be used by a single thread at a
081   * time.<p/>
082   *
083   * Closing the iterator does not save it.  You must call save to save it.
084   */
085  public interface BlockIterator extends Closeable {
086    /**
087     * Get the next block.<p/>
088     *
089     * Note that this block may be removed in between the time we list it,
090     * and the time the caller tries to use it, or it may represent a stale
091     * entry.  Callers should handle the case where the returned block no
092     * longer exists.
093     *
094     * @return               The next block, or null if there are no
095     *                         more blocks.  Null if there was an error
096     *                         determining the next block.
097     *
098     * @throws IOException   If there was an error getting the next block in
099     *                         this volume.  In this case, EOF will be set on
100     *                         the iterator.
101     */
102    public ExtendedBlock nextBlock() throws IOException;
103
104    /**
105     * Returns true if we got to the end of the block pool.
106     */
107    public boolean atEnd();
108
109    /**
110     * Repositions the iterator at the beginning of the block pool.
111     */
112    public void rewind();
113
114    /**
115     * Save this block iterator to the underlying volume.
116     * Any existing saved block iterator with this name will be overwritten.
117     * maxStalenessMs will not be saved.
118     *
119     * @throws IOException   If there was an error when saving the block
120     *                         iterator.
121     */
122    public void save() throws IOException;
123
124    /**
125     * Set the maximum staleness of entries that we will return.<p/>
126     *
127     * A maximum staleness of 0 means we will never return stale entries; a
128     * larger value will allow us to reduce resource consumption in exchange
129     * for returning more potentially stale entries.  Even with staleness set
130     * to 0, consumers of this API must handle race conditions where block
131     * disappear before they can be processed.
132     */
133    public void setMaxStalenessMs(long maxStalenessMs);
134
135    /**
136     * Get the wall-clock time, measured in milliseconds since the Epoch,
137     * when this iterator was created.
138     */
139    public long getIterStartMs();
140
141    /**
142     * Get the wall-clock time, measured in milliseconds since the Epoch,
143     * when this iterator was last saved.  Returns iterStartMs if the
144     * iterator was never saved.
145     */
146    public long getLastSavedMs();
147
148    /**
149     * Get the id of the block pool which this iterator traverses.
150     */
151    public String getBlockPoolId();
152  }
153
154  /**
155   * Create a new block iterator.  It will start at the beginning of the
156   * block set.
157   *
158   * @param bpid             The block pool id to iterate over.
159   * @param name             The name of the block iterator to create.
160   *
161   * @return                 The new block iterator.
162   */
163  public BlockIterator newBlockIterator(String bpid, String name);
164
165  /**
166   * Load a saved block iterator.
167   *
168   * @param bpid             The block pool id to iterate over.
169   * @param name             The name of the block iterator to load.
170   *
171   * @return                 The saved block iterator.
172   * @throws IOException     If there was an IO error loading the saved
173   *                           block iterator.
174   */
175  public BlockIterator loadBlockIterator(String bpid, String name)
176      throws IOException;
177
178  /**
179   * Get the FSDatasetSpi which this volume is a part of.
180   */
181  public FsDatasetSpi getDataset();
182
183  /**
184   * Load last partial chunk checksum from checksum file.
185   * Need to be called with FsDataset lock acquired.
186   * @param blockFile
187   * @param metaFile
188   * @return the last partial checksum
189   * @throws IOException
190   */
191  byte[] loadLastPartialChunkChecksum(File blockFile, File metaFile)
192      throws IOException;
193}