use of alluxio.exception.status.InvalidArgumentException in project alluxio by Alluxio.
the class CpCommand method validateArgs.
@Override
public void validateArgs(CommandLine cl) throws InvalidArgumentException {
CommandUtils.checkNumOfArgsEquals(this, cl, 2);
if (cl.hasOption(BUFFER_SIZE_OPTION.getLongOpt())) {
try {
int bufSize = ((Number) cl.getParsedOptionValue(BUFFER_SIZE_OPTION.getLongOpt())).intValue();
if (bufSize < 0) {
throw new InvalidArgumentException(BUFFER_SIZE_OPTION.getLongOpt() + " must be > 0");
}
mCopyFromLocalBufferSize = bufSize;
mCopyToLocalBufferSize = bufSize;
} catch (ParseException e) {
throw new InvalidArgumentException("Failed to parse option " + BUFFER_SIZE_OPTION.getLongOpt() + " into an integer", e);
}
}
if (cl.hasOption(THREAD_OPTION.getLongOpt())) {
try {
mThread = ((Number) cl.getParsedOptionValue(THREAD_OPTION.getLongOpt())).intValue();
if (mThread <= 0) {
throw new InvalidArgumentException(THREAD_OPTION.getLongOpt() + " must be > 0");
}
} catch (ParseException e) {
throw new InvalidArgumentException("Failed to parse option " + THREAD_OPTION.getLongOpt() + " into an integer", e);
}
}
if (cl.hasOption(PRESERVE_OPTION.getLongOpt())) {
mPreservePermissions = true;
} else {
mPreservePermissions = false;
}
}
use of alluxio.exception.status.InvalidArgumentException in project alluxio by Alluxio.
the class AbstractShell method run.
/**
* Handles the specified shell command request, displaying usage if the command format is invalid.
*
* @param argv [] Array of arguments given by the user's input from the terminal
* @return 0 if command is successful, -1 if an error occurred
*/
public int run(String... argv) {
if (argv.length == 0) {
printUsage();
return -1;
}
// Sanity check on the number of arguments
String cmd = argv[0];
Command command = mCommands.get(cmd);
if (command == null) {
String[] replacementCmd = getReplacementCmd(cmd);
if (replacementCmd == null) {
// Unknown command (we didn't find the cmd in our dict)
System.err.println(String.format("%s is an unknown command.", cmd));
printUsage();
return -1;
} else {
// Handle command alias
if (mUnstableAlias != null && mUnstableAlias.contains(cmd)) {
String deprecatedMsg = String.format("WARNING: %s is not a stable CLI command. It may be removed in the " + "future. Use with caution in scripts. You may use '%s' instead.", cmd, StringUtils.join(replacementCmd, " "));
System.out.println(deprecatedMsg);
}
String[] replacementArgv = (String[]) ArrayUtils.addAll(replacementCmd, ArrayUtils.subarray(argv, 1, argv.length));
return run(replacementArgv);
}
}
// Find the inner-most command and its argument line.
CommandLine cmdline;
try {
String[] currArgs = Arrays.copyOf(argv, argv.length);
while (command.hasSubCommand()) {
if (currArgs.length < 2) {
throw new InvalidArgumentException("No sub-command is specified");
}
if (!command.getSubCommands().containsKey(currArgs[1])) {
throw new InvalidArgumentException("Unknown sub-command: " + currArgs[1]);
}
command = command.getSubCommands().get(currArgs[1]);
currArgs = Arrays.copyOfRange(currArgs, 1, currArgs.length);
}
currArgs = Arrays.copyOfRange(currArgs, 1, currArgs.length);
cmdline = command.parseAndValidateArgs(currArgs);
} catch (InvalidArgumentException e) {
// It outputs a prompt message when passing wrong args to CLI
System.out.println(e.getMessage());
System.out.println("Usage: " + command.getUsage());
System.out.println(command.getDescription());
LOG.error("Invalid arguments for command {}:", command.getCommandName(), e);
return -1;
}
// Handle the command
try {
return command.run(cmdline);
} catch (Exception e) {
System.out.println(e.getMessage());
LOG.error("Error running " + StringUtils.join(argv, " "), e);
return -1;
}
}
use of alluxio.exception.status.InvalidArgumentException in project alluxio by Alluxio.
the class Command method parseAndValidateArgs.
/**
* Parses and validates the arguments.
*
* @param args the arguments for the command, excluding the command name
* @return the parsed command line object
* @throws InvalidArgumentException when arguments are not valid
*/
default CommandLine parseAndValidateArgs(String... args) throws InvalidArgumentException {
CommandLine cmdline;
Options opts = getOptions();
CommandLineParser parser = new DefaultParser();
try {
cmdline = parser.parse(opts, args);
} catch (ParseException e) {
throw new InvalidArgumentException(String.format("Failed to parse args for %s: %s", getCommandName(), e.getMessage()), e);
}
validateArgs(cmdline);
return cmdline;
}
use of alluxio.exception.status.InvalidArgumentException in project alluxio by Alluxio.
the class SnapshotUploader method onNextInternal.
private void onNextInternal(R value) throws IOException {
LOG.debug("Received request {}", value);
if (mStream == null) {
throw new IllegalStateException("No request stream assigned");
}
if (!mSnapshotFile.exists()) {
throw new FileNotFoundException(String.format("Snapshot file %s does not exist", mSnapshotFile.getPath()));
}
long offsetReceived = mOffsetGetter.apply(value);
// TODO(feng): implement better flow control
if (mOffset != offsetReceived) {
throw new InvalidArgumentException(String.format("Received mismatched offset: %d. Expect %d", offsetReceived, mOffset));
}
LOG.debug("Streaming data at {}", mOffset);
try (InputStream is = new FileInputStream(mSnapshotFile)) {
is.skip(mOffset);
boolean eof = false;
int chunkSize = SNAPSHOT_CHUNK_SIZE;
long available = mLength - mOffset;
if (available <= SNAPSHOT_CHUNK_SIZE) {
eof = true;
chunkSize = (int) available;
}
byte[] buffer = new byte[chunkSize];
IOUtils.readFully(is, buffer);
LOG.debug("Read {} bytes from file {}", chunkSize, mSnapshotFile);
mStream.onNext(mDataMessageBuilder.apply(SnapshotData.newBuilder().setOffset(mOffset).setEof(eof).setChunk(UnsafeByteOperations.unsafeWrap(buffer)).setSnapshotTerm(mSnapshotInfo.getTerm()).setSnapshotIndex(mSnapshotInfo.getIndex()).build()));
mOffset += chunkSize;
LOG.debug("Uploaded total {} bytes of file {}", mOffset, mSnapshotFile);
}
}
use of alluxio.exception.status.InvalidArgumentException in project alluxio by Alluxio.
the class DefaultBlockMaster method getWorkerReport.
@Override
public List<WorkerInfo> getWorkerReport(GetWorkerReportOptions options) throws UnavailableException, InvalidArgumentException {
if (mSafeModeManager.isInSafeMode()) {
throw new UnavailableException(ExceptionMessage.MASTER_IN_SAFEMODE.getMessage());
}
Set<MasterWorkerInfo> selectedLiveWorkers = new HashSet<>();
Set<MasterWorkerInfo> selectedLostWorkers = new HashSet<>();
WorkerRange workerRange = options.getWorkerRange();
switch(workerRange) {
case ALL:
selectedLiveWorkers.addAll(mWorkers);
selectedLostWorkers.addAll(mLostWorkers);
break;
case LIVE:
selectedLiveWorkers.addAll(mWorkers);
break;
case LOST:
selectedLostWorkers.addAll(mLostWorkers);
break;
case SPECIFIED:
Set<String> addresses = options.getAddresses();
Set<String> workerNames = new HashSet<>();
selectedLiveWorkers = selectInfoByAddress(addresses, mWorkers, workerNames);
selectedLostWorkers = selectInfoByAddress(addresses, mLostWorkers, workerNames);
if (!addresses.isEmpty()) {
String info = String.format("Unrecognized worker names: %s%n" + "Supported worker names: %s%n", addresses.toString(), workerNames.toString());
throw new InvalidArgumentException(info);
}
break;
default:
throw new InvalidArgumentException("Unrecognized worker range: " + workerRange);
}
List<WorkerInfo> workerInfoList = new ArrayList<>(selectedLiveWorkers.size() + selectedLostWorkers.size());
for (MasterWorkerInfo worker : selectedLiveWorkers) {
// extractWorkerInfo handles the locking internally
workerInfoList.add(extractWorkerInfo(worker, options.getFieldRange(), true));
}
for (MasterWorkerInfo worker : selectedLostWorkers) {
// extractWorkerInfo handles the locking internally
workerInfoList.add(extractWorkerInfo(worker, options.getFieldRange(), false));
}
return workerInfoList;
}
Aggregations