/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string path = StringUtils.PopOptionWithArgument("-path", args); if (path == null) { System.Console.Error.WriteLine("Please specify the path for setting the storage " + "policy.\nUsage: " + GetLongUsage()); return(1); } string policyName = StringUtils.PopOptionWithArgument("-policy", args); if (policyName == null) { System.Console.Error.WriteLine("Please specify the policy name.\nUsage: " + GetLongUsage ()); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { dfs.SetStoragePolicy(new Path(path), policyName); System.Console.Out.WriteLine("Set storage policy " + policyName + " on " + path); } catch (Exception e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } return(0); }
/// <exception cref="System.Exception"/> public virtual int Run(string[] args) { if (args.Length == 0) { AdminHelper.PrintUsage(false, "storagepolicies", Commands); return(1); } AdminHelper.Command command = AdminHelper.DetermineCommand(args[0], Commands); if (command == null) { System.Console.Error.WriteLine("Can't understand command '" + args[0] + "'"); if (!args[0].StartsWith("-")) { System.Console.Error.WriteLine("Command names must start with dashes."); } AdminHelper.PrintUsage(false, "storagepolicies", Commands); return(1); } IList <string> argsList = new List <string>(); Sharpen.Collections.AddAll(argsList, Arrays.AsList(args).SubList(1, args.Length)); try { return(command.Run(GetConf(), argsList)); } catch (ArgumentException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(-1); } }
/// <exception cref="System.IO.IOException"/> public virtual int Run(string[] args) { if (args.Length == 0) { AdminHelper.PrintUsage(false, "cacheadmin", Commands); return(1); } AdminHelper.Command command = AdminHelper.DetermineCommand(args[0], Commands); if (command == null) { System.Console.Error.WriteLine("Can't understand command '" + args[0] + "'"); if (!args[0].StartsWith("-")) { System.Console.Error.WriteLine("Command names must start with dashes."); } AdminHelper.PrintUsage(false, "cacheadmin", Commands); return(1); } IList <string> argsList = new List <string>(); for (int j = 1; j < args.Length; j++) { argsList.AddItem(args[j]); } try { return(command.Run(GetConf(), argsList)); } catch (ArgumentException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(-1); } }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string name = StringUtils.PopFirstNonOption(args); if (name == null) { System.Console.Error.WriteLine("You must specify a name when deleting a " + "cache pool." ); return(1); } if (!args.IsEmpty()) { System.Console.Error.Write("Can't understand arguments: " + Joiner.On(" ").Join(args ) + "\n"); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { dfs.RemoveCachePool(name); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } System.Console.Out.WriteLine("Successfully removed cache pool " + name + "."); return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string path = StringUtils.PopOptionWithArgument("-path", args); if (path == null) { System.Console.Error.WriteLine("You must specify a path with -path."); return(1); } if (!args.IsEmpty()) { System.Console.Error.WriteLine("Can't understand argument: " + args[0]); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } int exitCode = 0; try { DistributedFileSystem dfs = AdminHelper.GetDFS(conf); RemoteIterator <CacheDirectiveEntry> iter = dfs.ListCacheDirectives(new CacheDirectiveInfo.Builder ().SetPath(new Path(path)).Build()); while (iter.HasNext()) { CacheDirectiveEntry entry = iter.Next(); try { dfs.RemoveCacheDirective(entry.GetInfo().GetId()); System.Console.Out.WriteLine("Removed cache directive " + entry.GetInfo().GetId() ); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); exitCode = 2; } } } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); exitCode = 2; } if (exitCode == 0) { System.Console.Out.WriteLine("Removed every cache directive with path " + path); } return(exitCode); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string idString = StringUtils.PopFirstNonOption(args); if (idString == null) { System.Console.Error.WriteLine("You must specify a directive ID to remove."); return(1); } long id; try { id = long.Parse(idString); } catch (FormatException) { System.Console.Error.WriteLine("Invalid directive ID " + idString + ": expected " + "a numeric value."); return(1); } if (id <= 0) { System.Console.Error.WriteLine("Invalid directive ID " + id + ": ids must " + "be greater than 0." ); return(1); } if (!args.IsEmpty()) { System.Console.Error.WriteLine("Can't understand argument: " + args[0]); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { dfs.GetClient().RemoveCacheDirective(id); System.Console.Out.WriteLine("Removed cached directive " + id); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string path = StringUtils.PopOptionWithArgument("-path", args); if (path == null) { System.Console.Error.WriteLine("Please specify the path with -path.\nUsage:" + GetLongUsage ()); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { HdfsFileStatus status = dfs.GetClient().GetFileInfo(path); if (status == null) { System.Console.Error.WriteLine("File/Directory does not exist: " + path); return(2); } byte storagePolicyId = status.GetStoragePolicy(); if (storagePolicyId == BlockStoragePolicySuite.IdUnspecified) { System.Console.Out.WriteLine("The storage policy of " + path + " is unspecified"); return(0); } BlockStoragePolicy[] policies = dfs.GetStoragePolicies(); foreach (BlockStoragePolicy p in policies) { if (p.GetId() == storagePolicyId) { System.Console.Out.WriteLine("The storage policy of " + path + ":\n" + p); return(0); } } } catch (Exception e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } System.Console.Error.WriteLine("Cannot identify the storage policy for " + path); return(2); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { BlockStoragePolicy[] policies = dfs.GetStoragePolicies(); System.Console.Out.WriteLine("Block Storage Policies:"); foreach (BlockStoragePolicy policy in policies) { if (policy != null) { System.Console.Out.WriteLine("\t" + policy); } } } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string name = StringUtils.PopFirstNonOption(args); bool printStats = StringUtils.PopOption("-stats", args); if (!args.IsEmpty()) { System.Console.Error.Write("Can't understand arguments: " + Joiner.On(" ").Join(args ) + "\n"); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); TableListing.Builder builder = new TableListing.Builder().AddField("NAME", TableListing.Justification .Left).AddField("OWNER", TableListing.Justification.Left).AddField("GROUP", TableListing.Justification .Left).AddField("MODE", TableListing.Justification.Left).AddField("LIMIT", TableListing.Justification .Right).AddField("MAXTTL", TableListing.Justification.Right); if (printStats) { builder.AddField("BYTES_NEEDED", TableListing.Justification.Right).AddField("BYTES_CACHED" , TableListing.Justification.Right).AddField("BYTES_OVERLIMIT", TableListing.Justification .Right).AddField("FILES_NEEDED", TableListing.Justification.Right).AddField("FILES_CACHED" , TableListing.Justification.Right); } TableListing listing = builder.Build(); int numResults = 0; try { RemoteIterator <CachePoolEntry> iter = dfs.ListCachePools(); while (iter.HasNext()) { CachePoolEntry entry = iter.Next(); CachePoolInfo info = entry.GetInfo(); List <string> row = new List <string>(); if (name == null || info.GetPoolName().Equals(name)) { row.AddItem(info.GetPoolName()); row.AddItem(info.GetOwnerName()); row.AddItem(info.GetGroupName()); row.AddItem(info.GetMode() != null ? info.GetMode().ToString() : null); long limit = info.GetLimit(); string limitString; if (limit != null && limit.Equals(CachePoolInfo.LimitUnlimited)) { limitString = "unlimited"; } else { limitString = string.Empty + limit; } row.AddItem(limitString); long maxTtl = info.GetMaxRelativeExpiryMs(); string maxTtlString = null; if (maxTtl != null) { if (maxTtl == CachePoolInfo.RelativeExpiryNever) { maxTtlString = "never"; } else { maxTtlString = DFSUtil.DurationToString(maxTtl); } } row.AddItem(maxTtlString); if (printStats) { CachePoolStats stats = entry.GetStats(); row.AddItem(System.Convert.ToString(stats.GetBytesNeeded())); row.AddItem(System.Convert.ToString(stats.GetBytesCached())); row.AddItem(System.Convert.ToString(stats.GetBytesOverlimit())); row.AddItem(System.Convert.ToString(stats.GetFilesNeeded())); row.AddItem(System.Convert.ToString(stats.GetFilesCached())); } listing.AddRow(Sharpen.Collections.ToArray(row, new string[row.Count])); ++numResults; if (name != null) { break; } } } } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } System.Console.Out.Write(string.Format("Found %d result%s.%n", numResults, (numResults == 1 ? string.Empty : "s"))); if (numResults > 0) { System.Console.Out.Write(listing); } // If list pools succeed, we return 0 (success exit code) return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string owner = StringUtils.PopOptionWithArgument("-owner", args); string group = StringUtils.PopOptionWithArgument("-group", args); string modeString = StringUtils.PopOptionWithArgument("-mode", args); int mode = (modeString == null) ? null : System.Convert.ToInt32(modeString, 8); string limitString = StringUtils.PopOptionWithArgument("-limit", args); long limit = AdminHelper.ParseLimitString(limitString); string maxTtlString = StringUtils.PopOptionWithArgument("-maxTtl", args); long maxTtl; try { maxTtl = AdminHelper.ParseTtlString(maxTtlString); } catch (IOException e) { System.Console.Error.WriteLine("Error while parsing maxTtl value: " + e.Message); return(1); } string name = StringUtils.PopFirstNonOption(args); if (name == null) { System.Console.Error.WriteLine("You must specify a name when creating a " + "cache pool." ); return(1); } if (!args.IsEmpty()) { System.Console.Error.Write("Can't understand arguments: " + Joiner.On(" ").Join(args ) + "\n"); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } bool changed = false; CachePoolInfo info = new CachePoolInfo(name); if (owner != null) { info.SetOwnerName(owner); changed = true; } if (group != null) { info.SetGroupName(group); changed = true; } if (mode != null) { info.SetMode(new FsPermission(mode)); changed = true; } if (limit != null) { info.SetLimit(limit); changed = true; } if (maxTtl != null) { info.SetMaxRelativeExpiryMs(maxTtl); changed = true; } if (!changed) { System.Console.Error.WriteLine("You must specify at least one attribute to " + "change in the cache pool." ); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { dfs.ModifyCachePool(info); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } System.Console.Out.Write("Successfully modified cache pool " + name); string prefix = " to have "; if (owner != null) { System.Console.Out.Write(prefix + "owner name " + owner); prefix = " and "; } if (group != null) { System.Console.Out.Write(prefix + "group name " + group); prefix = " and "; } if (mode != null) { System.Console.Out.Write(prefix + "mode " + new FsPermission(mode)); prefix = " and "; } if (limit != null) { System.Console.Out.Write(prefix + "limit " + limit); prefix = " and "; } if (maxTtl != null) { System.Console.Out.Write(prefix + "max time-to-live " + maxTtlString); } System.Console.Out.Write("\n"); return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { string name = StringUtils.PopFirstNonOption(args); if (name == null) { System.Console.Error.WriteLine("You must specify a name when creating a " + "cache pool." ); return(1); } CachePoolInfo info = new CachePoolInfo(name); string owner = StringUtils.PopOptionWithArgument("-owner", args); if (owner != null) { info.SetOwnerName(owner); } string group = StringUtils.PopOptionWithArgument("-group", args); if (group != null) { info.SetGroupName(group); } string modeString = StringUtils.PopOptionWithArgument("-mode", args); if (modeString != null) { short mode = short.ParseShort(modeString, 8); info.SetMode(new FsPermission(mode)); } string limitString = StringUtils.PopOptionWithArgument("-limit", args); long limit = AdminHelper.ParseLimitString(limitString); if (limit != null) { info.SetLimit(limit); } string maxTtlString = StringUtils.PopOptionWithArgument("-maxTtl", args); try { long maxTtl = AdminHelper.ParseTtlString(maxTtlString); if (maxTtl != null) { info.SetMaxRelativeExpiryMs(maxTtl); } } catch (IOException e) { System.Console.Error.WriteLine("Error while parsing maxTtl value: " + e.Message); return(1); } if (!args.IsEmpty()) { System.Console.Error.Write("Can't understand arguments: " + Joiner.On(" ").Join(args ) + "\n"); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); try { dfs.AddCachePool(info); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } System.Console.Out.WriteLine("Successfully added cache pool " + name + "."); return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { CacheDirectiveInfo.Builder builder = new CacheDirectiveInfo.Builder(); string pathFilter = StringUtils.PopOptionWithArgument("-path", args); if (pathFilter != null) { builder.SetPath(new Path(pathFilter)); } string poolFilter = StringUtils.PopOptionWithArgument("-pool", args); if (poolFilter != null) { builder.SetPool(poolFilter); } bool printStats = StringUtils.PopOption("-stats", args); string idFilter = StringUtils.PopOptionWithArgument("-id", args); if (idFilter != null) { builder.SetId(long.Parse(idFilter)); } if (!args.IsEmpty()) { System.Console.Error.WriteLine("Can't understand argument: " + args[0]); return(1); } TableListing.Builder tableBuilder = new TableListing.Builder().AddField("ID", TableListing.Justification .Right).AddField("POOL", TableListing.Justification.Left).AddField("REPL", TableListing.Justification .Right).AddField("EXPIRY", TableListing.Justification.Left).AddField("PATH", TableListing.Justification .Left); if (printStats) { tableBuilder.AddField("BYTES_NEEDED", TableListing.Justification.Right).AddField( "BYTES_CACHED", TableListing.Justification.Right).AddField("FILES_NEEDED", TableListing.Justification .Right).AddField("FILES_CACHED", TableListing.Justification.Right); } TableListing tableListing = tableBuilder.Build(); try { DistributedFileSystem dfs = AdminHelper.GetDFS(conf); RemoteIterator <CacheDirectiveEntry> iter = dfs.ListCacheDirectives(builder.Build( )); int numEntries = 0; while (iter.HasNext()) { CacheDirectiveEntry entry = iter.Next(); CacheDirectiveInfo directive = entry.GetInfo(); CacheDirectiveStats stats = entry.GetStats(); IList <string> row = new List <string>(); row.AddItem(string.Empty + directive.GetId()); row.AddItem(directive.GetPool()); row.AddItem(string.Empty + directive.GetReplication()); string expiry; // This is effectively never, round for nice printing if (directive.GetExpiration().GetMillis() > CacheDirectiveInfo.Expiration.MaxRelativeExpiryMs / 2) { expiry = "never"; } else { expiry = directive.GetExpiration().ToString(); } row.AddItem(expiry); row.AddItem(directive.GetPath().ToUri().GetPath()); if (printStats) { row.AddItem(string.Empty + stats.GetBytesNeeded()); row.AddItem(string.Empty + stats.GetBytesCached()); row.AddItem(string.Empty + stats.GetFilesNeeded()); row.AddItem(string.Empty + stats.GetFilesCached()); } tableListing.AddRow(Sharpen.Collections.ToArray(row, new string[row.Count])); numEntries++; } System.Console.Out.Write(string.Format("Found %d entr%s%n", numEntries, numEntries == 1 ? "y" : "ies")); if (numEntries > 0) { System.Console.Out.Write(tableListing); } } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { CacheDirectiveInfo.Builder builder = new CacheDirectiveInfo.Builder(); bool modified = false; string idString = StringUtils.PopOptionWithArgument("-id", args); if (idString == null) { System.Console.Error.WriteLine("You must specify a directive ID with -id."); return(1); } builder.SetId(long.Parse(idString)); string path = StringUtils.PopOptionWithArgument("-path", args); if (path != null) { builder.SetPath(new Path(path)); modified = true; } bool force = StringUtils.PopOption("-force", args); string replicationString = StringUtils.PopOptionWithArgument("-replication", args ); if (replicationString != null) { builder.SetReplication(short.ParseShort(replicationString)); modified = true; } string poolName = StringUtils.PopOptionWithArgument("-pool", args); if (poolName != null) { builder.SetPool(poolName); modified = true; } string ttlString = StringUtils.PopOptionWithArgument("-ttl", args); try { CacheDirectiveInfo.Expiration ex = ParseExpirationString(ttlString); if (ex != null) { builder.SetExpiration(ex); modified = true; } } catch (IOException e) { System.Console.Error.WriteLine("Error while parsing ttl value: " + e.Message); return(1); } if (!args.IsEmpty()) { System.Console.Error.WriteLine("Can't understand argument: " + args[0]); System.Console.Error.WriteLine("Usage is " + GetShortUsage()); return(1); } if (!modified) { System.Console.Error.WriteLine("No modifications were specified."); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); EnumSet <CacheFlag> flags = EnumSet.NoneOf <CacheFlag>(); if (force) { flags.AddItem(CacheFlag.Force); } try { dfs.ModifyCacheDirective(builder.Build(), flags); System.Console.Out.WriteLine("Modified cache directive " + idString); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } return(0); }
/// <exception cref="System.IO.IOException"/> public virtual int Run(Configuration conf, IList <string> args) { CacheDirectiveInfo.Builder builder = new CacheDirectiveInfo.Builder(); string path = StringUtils.PopOptionWithArgument("-path", args); if (path == null) { System.Console.Error.WriteLine("You must specify a path with -path."); return(1); } builder.SetPath(new Path(path)); string poolName = StringUtils.PopOptionWithArgument("-pool", args); if (poolName == null) { System.Console.Error.WriteLine("You must specify a pool name with -pool."); return(1); } builder.SetPool(poolName); bool force = StringUtils.PopOption("-force", args); string replicationString = StringUtils.PopOptionWithArgument("-replication", args ); if (replicationString != null) { short replication = short.ParseShort(replicationString); builder.SetReplication(replication); } string ttlString = StringUtils.PopOptionWithArgument("-ttl", args); try { CacheDirectiveInfo.Expiration ex = ParseExpirationString(ttlString); if (ex != null) { builder.SetExpiration(ex); } } catch (IOException e) { System.Console.Error.WriteLine("Error while parsing ttl value: " + e.Message); return(1); } if (!args.IsEmpty()) { System.Console.Error.WriteLine("Can't understand argument: " + args[0]); return(1); } DistributedFileSystem dfs = AdminHelper.GetDFS(conf); CacheDirectiveInfo directive = builder.Build(); EnumSet <CacheFlag> flags = EnumSet.NoneOf <CacheFlag>(); if (force) { flags.AddItem(CacheFlag.Force); } try { long id = dfs.AddCacheDirective(directive, flags); System.Console.Out.WriteLine("Added cache directive " + id); } catch (IOException e) { System.Console.Error.WriteLine(AdminHelper.PrettifyException(e)); return(2); } return(0); }