|
@@ -21,6 +21,7 @@ import java.io.IOException;
|
|
|
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 @@ import org.apache.hadoop.util.Time;
|
|
|
/** 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 @@ public class INodeDirectorySnapshottable extends INodeDirectoryWithQuota {
|
|
|
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 @@ public class INodeDirectorySnapshottable extends INodeDirectoryWithQuota {
|
|
|
/** 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 class INodeDirectorySnapshottable extends INodeDirectoryWithQuota {
|
|
|
}
|
|
|
|
|
|
/** 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);
|
|
|
|