Search in sources :

Example 1 with Constraint

use of org.apache.accumulo.core.constraints.Constraint in project accumulo by apache.

the class ConstraintChecker method check.

public Violations check(Environment env, Mutation m) {
    if (!env.getExtent().contains(new ComparableBytes(m.getRow()))) {
        Violations violations = new Violations();
        ConstraintViolationSummary cvs = new ConstraintViolationSummary(SystemConstraint.class.getName(), (short) -1, "Mutation outside of tablet extent", 1);
        violations.add(cvs);
        // do not bother with further checks since this mutation does not go with this tablet
        return violations;
    }
    // violations is intentionally initialized as null for performance
    Violations violations = null;
    for (Constraint constraint : getConstraints()) {
        try {
            List<Short> violationCodes = constraint.check(env, m);
            if (violationCodes != null) {
                String className = constraint.getClass().getName();
                for (Short vcode : violationCodes) {
                    violations = addViolation(violations, new ConstraintViolationSummary(className, vcode, constraint.getViolationDescription(vcode), 1));
                }
            }
        } catch (Throwable throwable) {
            log.warn("CONSTRAINT FAILED : {}", throwable.getMessage(), throwable);
            // constraint failed in some way, do not allow mutation to pass
            short vcode;
            String msg;
            if (throwable instanceof NullPointerException) {
                vcode = -1;
                msg = "threw NullPointerException";
            } else if (throwable instanceof ArrayIndexOutOfBoundsException) {
                vcode = -2;
                msg = "threw ArrayIndexOutOfBoundsException";
            } else if (throwable instanceof NumberFormatException) {
                vcode = -3;
                msg = "threw NumberFormatException";
            } else if (throwable instanceof IOException) {
                vcode = -4;
                msg = "threw IOException (or subclass of)";
            } else {
                vcode = -100;
                msg = "threw some Exception";
            }
            violations = addViolation(violations, new ConstraintViolationSummary(constraint.getClass().getName(), vcode, "CONSTRAINT FAILED : " + msg, 1));
        }
    }
    return violations;
}
Also used : Constraint(org.apache.accumulo.core.constraints.Constraint) IOException(java.io.IOException) Violations(org.apache.accumulo.core.constraints.Violations) ComparableBytes(org.apache.accumulo.core.data.impl.ComparableBytes) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary)

Example 2 with Constraint

use of org.apache.accumulo.core.constraints.Constraint in project accumulo by apache.

the class ConstraintCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws Exception {
    final String tableName;
    final String namespace;
    if (cl.hasOption(namespaceOpt.getOpt())) {
        namespace = cl.getOptionValue(namespaceOpt.getOpt());
    } else {
        namespace = null;
    }
    if (cl.hasOption(OptUtil.tableOpt().getOpt()) || !shellState.getTableName().isEmpty()) {
        tableName = OptUtil.getTableOpt(cl, shellState);
    } else {
        tableName = null;
    }
    int i;
    switch(OptUtil.getAldOpt(cl)) {
        case ADD:
            for (String constraint : cl.getArgs()) {
                if (namespace != null) {
                    if (!shellState.getConnector().namespaceOperations().testClassLoad(namespace, constraint, Constraint.class.getName())) {
                        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + constraint + " as type " + Constraint.class.getName());
                    }
                    i = shellState.getConnector().namespaceOperations().addConstraint(namespace, constraint);
                    shellState.getReader().println("Added constraint " + constraint + " to namespace " + namespace + " with number " + i);
                } else if (tableName != null && !tableName.isEmpty()) {
                    if (!shellState.getConnector().tableOperations().testClassLoad(tableName, constraint, Constraint.class.getName())) {
                        throw new ShellCommandException(ErrorCode.INITIALIZATION_FAILURE, "Servers are unable to load " + constraint + " as type " + Constraint.class.getName());
                    }
                    i = shellState.getConnector().tableOperations().addConstraint(tableName, constraint);
                    shellState.getReader().println("Added constraint " + constraint + " to table " + tableName + " with number " + i);
                } else {
                    throw new IllegalArgumentException("Please specify either a table or a namespace");
                }
            }
            break;
        case DELETE:
            for (String constraint : cl.getArgs()) {
                i = Integer.parseInt(constraint);
                if (namespace != null) {
                    shellState.getConnector().namespaceOperations().removeConstraint(namespace, i);
                    shellState.getReader().println("Removed constraint " + i + " from namespace " + namespace);
                } else if (tableName != null) {
                    shellState.getConnector().tableOperations().removeConstraint(tableName, i);
                    shellState.getReader().println("Removed constraint " + i + " from table " + tableName);
                } else {
                    throw new IllegalArgumentException("Please specify either a table or a namespace");
                }
            }
            break;
        case LIST:
            if (namespace != null) {
                for (Entry<String, Integer> property : shellState.getConnector().namespaceOperations().listConstraints(namespace).entrySet()) {
                    shellState.getReader().println(property.toString());
                }
            } else if (tableName != null) {
                for (Entry<String, Integer> property : shellState.getConnector().tableOperations().listConstraints(tableName).entrySet()) {
                    shellState.getReader().println(property.toString());
                }
            } else {
                throw new IllegalArgumentException("Please specify either a table or a namespace");
            }
    }
    return 0;
}
Also used : Entry(java.util.Map.Entry) Constraint(org.apache.accumulo.core.constraints.Constraint) ShellCommandException(org.apache.accumulo.shell.ShellCommandException) Constraint(org.apache.accumulo.core.constraints.Constraint)

Example 3 with Constraint

use of org.apache.accumulo.core.constraints.Constraint in project accumulo by apache.

the class ConstraintCheckerTest method makeFailureConstraint.

private Constraint makeFailureConstraint() {
    Constraint c = createMock(Constraint.class);
    short code1 = 2;
    short code2 = 4;
    List<Short> vCodes = ImmutableList.of(code1, code2);
    expect(c.getViolationDescription(code1)).andReturn("ViolationCode1");
    expect(c.getViolationDescription(code2)).andReturn("ViolationCode2");
    expect(c.check(env, m)).andReturn(vCodes);
    replay(c);
    return c;
}
Also used : Constraint(org.apache.accumulo.core.constraints.Constraint)

Example 4 with Constraint

use of org.apache.accumulo.core.constraints.Constraint in project accumulo by apache.

the class ConstraintCheckerTest method makeSuccessConstraint.

private Constraint makeSuccessConstraint() {
    Constraint c = createMock(Constraint.class);
    // no violations
    expect(c.check(env, m)).andReturn(null);
    replay(c);
    return c;
}
Also used : Constraint(org.apache.accumulo.core.constraints.Constraint)

Example 5 with Constraint

use of org.apache.accumulo.core.constraints.Constraint in project accumulo by apache.

the class ConstraintCheckerTest method makeExceptionConstraint.

private Constraint makeExceptionConstraint() {
    Constraint c = createMock(Constraint.class);
    expect(c.check(env, m)).andThrow(new RuntimeException("some exception"));
    replay(c);
    return c;
}
Also used : Constraint(org.apache.accumulo.core.constraints.Constraint)

Aggregations

Constraint (org.apache.accumulo.core.constraints.Constraint)5 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 Violations (org.apache.accumulo.core.constraints.Violations)1 ConstraintViolationSummary (org.apache.accumulo.core.data.ConstraintViolationSummary)1 ComparableBytes (org.apache.accumulo.core.data.impl.ComparableBytes)1 ShellCommandException (org.apache.accumulo.shell.ShellCommandException)1