HDFS-4091. Add snapshot quota to limit the number of snapshots allowed.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/HDFS-2802@1401868 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tsz-wo Sze 2012-10-24 20:39:26 +00:00
parent 4fde5c0190
commit b897bb8992
3 changed files with 34 additions and 6 deletions

View File

@ -23,3 +23,6 @@ Branch-2802 Snapshot (Unreleased)
HDFS-4084. Provide CLI support to allow and disallow snapshot
on a directory. (Brondon Li via suresh)
HDFS-4091. Add snapshot quota to limit the number of snapshots allowed.
(szetszwo)

View File

@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.hdfs.server.namenode.INode;
import org.apache.hadoop.hdfs.server.namenode.INodeDirectory;
@ -30,7 +31,8 @@
/** Directories where taking snapshots is allowed. */
@InterfaceAudience.Private
public class INodeDirectorySnapshottable extends INodeDirectoryWithQuota {
static public INodeDirectorySnapshottable newInstance(final INodeDirectory dir) {
static public INodeDirectorySnapshottable newInstance(
final INodeDirectory dir, final int snapshotQuota) {
long nsq = -1L;
long dsq = -1L;
@ -39,7 +41,7 @@ static public INodeDirectorySnapshottable newInstance(final INodeDirectory dir)
nsq = q.getNsQuota();
dsq = q.getDsQuota();
}
return new INodeDirectorySnapshottable(nsq, dsq, dir);
return new INodeDirectorySnapshottable(nsq, dsq, dir, snapshotQuota);
}
/** Cast INode to INodeDirectorySnapshottable. */
@ -55,10 +57,25 @@ static public INodeDirectorySnapshottable valueOf(
/** A list of snapshots of this directory. */
private final List<INodeDirectorySnapshotRoot> snapshots
= new ArrayList<INodeDirectorySnapshotRoot>();
/** Number of snapshots is allowed. */
private int snapshotQuota;
private INodeDirectorySnapshottable(long nsQuota, long dsQuota,
INodeDirectory dir) {
INodeDirectory dir, final int snapshotQuota) {
super(nsQuota, dsQuota, dir);
setSnapshotQuota(snapshotQuota);
}
public int getSnapshotQuota() {
return snapshotQuota;
}
public void setSnapshotQuota(int snapshotQuota) {
if (snapshotQuota <= 0) {
throw new HadoopIllegalArgumentException(
"Cannot set snapshot quota to " + snapshotQuota + " <= 0");
}
this.snapshotQuota = snapshotQuota;
}
@Override
@ -67,7 +84,15 @@ public boolean isSnapshottable() {
}
/** Add a snapshot root under this directory. */
INodeDirectorySnapshotRoot addSnapshotRoot(final String name) {
INodeDirectorySnapshotRoot addSnapshotRoot(final String name
) throws SnapshotException {
//check snapshot quota
if (snapshots.size() + 1 > snapshotQuota) {
throw new SnapshotException("Failed to add snapshot: there are already "
+ snapshots.size() + " snapshot(s) and the snapshot quota is "
+ snapshotQuota);
}
final INodeDirectorySnapshotRoot r = new INodeDirectorySnapshotRoot(name, this);
snapshots.add(r);

View File

@ -43,7 +43,7 @@ public SnapshotManager(final Namesystem namesystem) {
* Otherwise, the {@link INodeDirectory} of the path is replaced by an
* {@link INodeDirectorySnapshottable}.
*/
public void setSnapshottable(final String path,
public void setSnapshottable(final String path, final int snapshotQuota,
final FSDirectory fsdir) throws IOException {
namesystem.writeLock();
try {
@ -54,7 +54,7 @@ public void setSnapshottable(final String path,
}
final INodeDirectorySnapshottable s
= INodeDirectorySnapshottable.newInstance(d);
= INodeDirectorySnapshottable.newInstance(d, snapshotQuota);
fsdir.replaceINodeDirectory(path, d, s);
snapshottables.add(s);
} finally {