Search in sources :

Example 11 with InvalidArgumentException

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;
    }
}
Also used : InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) ParseException(org.apache.commons.cli.ParseException)

Example 12 with InvalidArgumentException

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;
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) IOException(java.io.IOException) InvalidArgumentException(alluxio.exception.status.InvalidArgumentException)

Example 13 with InvalidArgumentException

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;
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 14 with InvalidArgumentException

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);
    }
}
Also used : InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Example 15 with InvalidArgumentException

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;
}
Also used : InvalidArgumentException(alluxio.exception.status.InvalidArgumentException) UnavailableException(alluxio.exception.status.UnavailableException) MasterWorkerInfo(alluxio.master.block.meta.MasterWorkerInfo) WorkerRange(alluxio.client.block.options.GetWorkerReportOptions.WorkerRange) ArrayList(java.util.ArrayList) WorkerInfo(alluxio.wire.WorkerInfo) MasterWorkerInfo(alluxio.master.block.meta.MasterWorkerInfo) ConcurrentHashSet(alluxio.collections.ConcurrentHashSet) HashSet(java.util.HashSet)

Aggregations

InvalidArgumentException (alluxio.exception.status.InvalidArgumentException)22 IOException (java.io.IOException)6 CommandLine (org.apache.commons.cli.CommandLine)5 AlluxioURI (alluxio.AlluxioURI)3 ParseException (org.apache.commons.cli.ParseException)3 JournalMasterClient (alluxio.client.journal.JournalMasterClient)2 AlluxioConfiguration (alluxio.conf.AlluxioConfiguration)2 AlluxioException (alluxio.exception.AlluxioException)2 InvalidPathException (alluxio.exception.InvalidPathException)2 JournalDomain (alluxio.grpc.JournalDomain)2 HashSet (java.util.HashSet)2 CommandLineParser (org.apache.commons.cli.CommandLineParser)2 DefaultParser (org.apache.commons.cli.DefaultParser)2 Options (org.apache.commons.cli.Options)2 AbstractFsAdminCommand (alluxio.cli.fsadmin.command.AbstractFsAdminCommand)1 Context (alluxio.cli.fsadmin.command.Context)1 ConfigurationCommand (alluxio.cli.fsadmin.doctor.ConfigurationCommand)1 StorageCommand (alluxio.cli.fsadmin.doctor.StorageCommand)1 CapacityCommand (alluxio.cli.fsadmin.report.CapacityCommand)1 JobServiceMetricsCommand (alluxio.cli.fsadmin.report.JobServiceMetricsCommand)1