Search in sources :

Example 1 with Violations

use of org.apache.accumulo.core.constraints.Violations 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 Violations

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

the class Tablet method prepareMutationsForCommit.

public CommitSession prepareMutationsForCommit(TservConstraintEnv cenv, List<Mutation> mutations) throws TConstraintViolationException {
    ConstraintChecker cc = constraintChecker.get();
    List<Mutation> violators = null;
    Violations violations = new Violations();
    cenv.setExtent(extent);
    for (Mutation mutation : mutations) {
        Violations more = cc.check(cenv, mutation);
        if (more != null) {
            violations.add(more);
            if (violators == null)
                violators = new ArrayList<>();
            violators.add(mutation);
        }
    }
    long time = tabletTime.setUpdateTimes(mutations);
    if (!violations.isEmpty()) {
        HashSet<Mutation> violatorsSet = new HashSet<>(violators);
        ArrayList<Mutation> nonViolators = new ArrayList<>();
        for (Mutation mutation : mutations) {
            if (!violatorsSet.contains(mutation)) {
                nonViolators.add(mutation);
            }
        }
        CommitSession commitSession = null;
        if (nonViolators.size() > 0) {
            // if everything is a violation, then it is expected that
            // code calling this will not log or commit
            commitSession = finishPreparingMutations(time);
            if (commitSession == null)
                return null;
        }
        throw new TConstraintViolationException(violations, violators, nonViolators, commitSession);
    }
    return finishPreparingMutations(time);
}
Also used : Violations(org.apache.accumulo.core.constraints.Violations) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Mutation(org.apache.accumulo.core.data.Mutation) ConstraintChecker(org.apache.accumulo.tserver.constraints.ConstraintChecker) TConstraintViolationException(org.apache.accumulo.tserver.TConstraintViolationException) HashSet(java.util.HashSet)

Aggregations

Violations (org.apache.accumulo.core.constraints.Violations)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Constraint (org.apache.accumulo.core.constraints.Constraint)1 ConstraintViolationSummary (org.apache.accumulo.core.data.ConstraintViolationSummary)1 Mutation (org.apache.accumulo.core.data.Mutation)1 ComparableBytes (org.apache.accumulo.core.data.impl.ComparableBytes)1 TConstraintViolationException (org.apache.accumulo.tserver.TConstraintViolationException)1 ConstraintChecker (org.apache.accumulo.tserver.constraints.ConstraintChecker)1