Search in sources :

Example 6 with ConstraintViolationSummary

use of org.apache.accumulo.core.data.ConstraintViolationSummary in project accumulo by apache.

the class ConstraintCheckerTest method testCheckException.

@Test
public void testCheckException() {
    expect(extent.contains(anyObject(BinaryComparable.class))).andReturn(true);
    replayAll();
    constraints.add(makeExceptionConstraint());
    ConstraintViolationSummary cvs = Iterables.getOnlyElement(cc.check(env, m).asList());
    assertEquals("CONSTRAINT FAILED : threw some Exception", cvs.getViolationDescription());
}
Also used : BinaryComparable(org.apache.hadoop.io.BinaryComparable) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) Test(org.junit.Test)

Example 7 with ConstraintViolationSummary

use of org.apache.accumulo.core.data.ConstraintViolationSummary in project accumulo by apache.

the class ConstraintIT method test2.

private void test2(String table, boolean doFlush) throws Exception {
    // test sending multiple mutations with multiple constrain violations... all of the non violating mutations
    // should go through
    int numericErrors = 2;
    BatchWriter bw = getConnector().createBatchWriter(table, new BatchWriterConfig());
    bw.addMutation(newMut("r1", "cf1", "cq1", "123"));
    bw.addMutation(newMut("r1", "cf1", "cq2", "I'm a bad value"));
    if (doFlush) {
        try {
            bw.flush();
            throw new Exception("Didn't find a bad mutation");
        } catch (MutationsRejectedException mre) {
            // ignored
            try {
                bw.close();
            } catch (MutationsRejectedException ex) {
            // ignored
            }
            bw = getConnector().createBatchWriter(table, new BatchWriterConfig());
            numericErrors = 1;
        }
    }
    bw.addMutation(newMut("r1", "cf1", "cq3", "I'm a naughty value"));
    bw.addMutation(newMut("@bad row@", "cf1", "cq2", "456"));
    bw.addMutation(newMut("r1", "cf1", "cq4", "789"));
    boolean sawMRE = false;
    try {
        bw.close();
        // should not get here
        throw new Exception("Test failed, constraint did not catch bad mutation");
    } catch (MutationsRejectedException mre) {
        System.out.println(mre);
        sawMRE = true;
        // verify constraint violation summary
        List<ConstraintViolationSummary> cvsl = mre.getConstraintViolationSummaries();
        if (cvsl.size() != 2) {
            throw new Exception("Unexpected constraints");
        }
        HashMap<String, Integer> expected = new HashMap<>();
        expected.put("org.apache.accumulo.test.constraints.NumericValueConstraint", numericErrors);
        expected.put("org.apache.accumulo.test.constraints.AlphaNumKeyConstraint", 1);
        for (ConstraintViolationSummary cvs : cvsl) {
            if (expected.get(cvs.constrainClass) != cvs.numberOfViolatingMutations) {
                throw new Exception("Unexpected " + cvs.constrainClass + " " + cvs.numberOfViolatingMutations);
            }
        }
    }
    if (!sawMRE) {
        throw new Exception("Did not see MutationsRejectedException");
    }
    try (Scanner scanner = getConnector().createScanner(table, Authorizations.EMPTY)) {
        Iterator<Entry<Key, Value>> iter = scanner.iterator();
        Entry<Key, Value> entry = iter.next();
        if (!entry.getKey().getRow().equals(new Text("r1")) || !entry.getKey().getColumnFamily().equals(new Text("cf1")) || !entry.getKey().getColumnQualifier().equals(new Text("cq1")) || !entry.getValue().equals(new Value("123".getBytes(UTF_8)))) {
            throw new Exception("Unexpected key or value " + entry.getKey() + " " + entry.getValue());
        }
        entry = iter.next();
        if (!entry.getKey().getRow().equals(new Text("r1")) || !entry.getKey().getColumnFamily().equals(new Text("cf1")) || !entry.getKey().getColumnQualifier().equals(new Text("cq4")) || !entry.getValue().equals(new Value("789".getBytes(UTF_8)))) {
            throw new Exception("Unexpected key or value " + entry.getKey() + " " + entry.getValue());
        }
        if (iter.hasNext()) {
            entry = iter.next();
            throw new Exception("Unexpected extra key or value " + entry.getKey() + " " + entry.getValue());
        }
    }
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) HashMap(java.util.HashMap) Text(org.apache.hadoop.io.Text) NumericValueConstraint(org.apache.accumulo.test.constraints.NumericValueConstraint) AlphaNumKeyConstraint(org.apache.accumulo.test.constraints.AlphaNumKeyConstraint) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) Entry(java.util.Map.Entry) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) List(java.util.List) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Key(org.apache.accumulo.core.data.Key) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 8 with ConstraintViolationSummary

use of org.apache.accumulo.core.data.ConstraintViolationSummary in project accumulo by apache.

the class ConstraintIT method test1.

private void test1(String tableName) throws Exception {
    BatchWriter bw = getConnector().createBatchWriter(tableName, new BatchWriterConfig());
    Mutation mut1 = new Mutation(new Text("r1"));
    mut1.put(new Text("cf1"), new Text("cq1"), new Value("123".getBytes(UTF_8)));
    bw.addMutation(mut1);
    // should not throw any exceptions
    bw.close();
    bw = getConnector().createBatchWriter(tableName, new BatchWriterConfig());
    // create a mutation with a non numeric value
    Mutation mut2 = new Mutation(new Text("r1"));
    mut2.put(new Text("cf1"), new Text("cq1"), new Value("123a".getBytes(UTF_8)));
    bw.addMutation(mut2);
    boolean sawMRE = false;
    try {
        bw.close();
        // should not get here
        throw new Exception("Test failed, constraint did not catch bad mutation");
    } catch (MutationsRejectedException mre) {
        sawMRE = true;
        // verify constraint violation summary
        List<ConstraintViolationSummary> cvsl = mre.getConstraintViolationSummaries();
        if (cvsl.size() != 1) {
            throw new Exception("Unexpected constraints");
        }
        for (ConstraintViolationSummary cvs : cvsl) {
            if (!cvs.constrainClass.equals(NumericValueConstraint.class.getName())) {
                throw new Exception("Unexpected constraint class " + cvs.constrainClass);
            }
            if (cvs.numberOfViolatingMutations != 1) {
                throw new Exception("Unexpected # violating mutations " + cvs.numberOfViolatingMutations);
            }
        }
    }
    if (!sawMRE) {
        throw new Exception("Did not see MutationsRejectedException");
    }
    // verify mutation did not go through
    try (Scanner scanner = getConnector().createScanner(tableName, Authorizations.EMPTY)) {
        scanner.setRange(new Range(new Text("r1")));
        Iterator<Entry<Key, Value>> iter = scanner.iterator();
        Entry<Key, Value> entry = iter.next();
        if (!entry.getKey().getRow().equals(new Text("r1")) || !entry.getKey().getColumnFamily().equals(new Text("cf1")) || !entry.getKey().getColumnQualifier().equals(new Text("cq1")) || !entry.getValue().equals(new Value("123".getBytes(UTF_8)))) {
            throw new Exception("Unexpected key or value " + entry.getKey() + " " + entry.getValue());
        }
        if (iter.hasNext()) {
            entry = iter.next();
            throw new Exception("Unexpected extra key or value " + entry.getKey() + " " + entry.getValue());
        }
        // remove the numeric value constraint
        getConnector().tableOperations().removeConstraint(tableName, 2);
        sleepUninterruptibly(1, TimeUnit.SECONDS);
        // now should be able to add a non numeric value
        bw = getConnector().createBatchWriter(tableName, new BatchWriterConfig());
        bw.addMutation(mut2);
        bw.close();
        // verify mutation went through
        iter = scanner.iterator();
        entry = iter.next();
        if (!entry.getKey().getRow().equals(new Text("r1")) || !entry.getKey().getColumnFamily().equals(new Text("cf1")) || !entry.getKey().getColumnQualifier().equals(new Text("cq1")) || !entry.getValue().equals(new Value("123a".getBytes(UTF_8)))) {
            throw new Exception("Unexpected key or value " + entry.getKey() + " " + entry.getValue());
        }
        if (iter.hasNext()) {
            entry = iter.next();
            throw new Exception("Unexpected extra key or value " + entry.getKey() + " " + entry.getValue());
        }
        // add a constraint that references a non-existant class
        getConnector().tableOperations().setProperty(tableName, Property.TABLE_CONSTRAINT_PREFIX + "1", "com.foobar.nonExistantClass");
        sleepUninterruptibly(1, TimeUnit.SECONDS);
        // add a mutation
        bw = getConnector().createBatchWriter(tableName, new BatchWriterConfig());
        Mutation mut3 = new Mutation(new Text("r1"));
        mut3.put(new Text("cf1"), new Text("cq1"), new Value("foo".getBytes(UTF_8)));
        bw.addMutation(mut3);
        sawMRE = false;
        try {
            bw.close();
            // should not get here
            throw new Exception("Test failed, mutation went through when table had bad constraints");
        } catch (MutationsRejectedException mre) {
            sawMRE = true;
        }
        if (!sawMRE) {
            throw new Exception("Did not see MutationsRejectedException");
        }
        // verify the mutation did not go through
        iter = scanner.iterator();
        entry = iter.next();
        if (!entry.getKey().getRow().equals(new Text("r1")) || !entry.getKey().getColumnFamily().equals(new Text("cf1")) || !entry.getKey().getColumnQualifier().equals(new Text("cq1")) || !entry.getValue().equals(new Value("123a".getBytes(UTF_8)))) {
            throw new Exception("Unexpected key or value " + entry.getKey() + " " + entry.getValue());
        }
        if (iter.hasNext()) {
            entry = iter.next();
            throw new Exception("Unexpected extra key or value " + entry.getKey() + " " + entry.getValue());
        }
        // remove the bad constraint
        getConnector().tableOperations().removeConstraint(tableName, 1);
        sleepUninterruptibly(1, TimeUnit.SECONDS);
        // try the mutation again
        bw = getConnector().createBatchWriter(tableName, new BatchWriterConfig());
        bw.addMutation(mut3);
        bw.close();
        // verify it went through
        iter = scanner.iterator();
        entry = iter.next();
        if (!entry.getKey().getRow().equals(new Text("r1")) || !entry.getKey().getColumnFamily().equals(new Text("cf1")) || !entry.getKey().getColumnQualifier().equals(new Text("cq1")) || !entry.getValue().equals(new Value("foo".getBytes(UTF_8)))) {
            throw new Exception("Unexpected key or value " + entry.getKey() + " " + entry.getValue());
        }
        if (iter.hasNext()) {
            entry = iter.next();
            throw new Exception("Unexpected extra key or value " + entry.getKey() + " " + entry.getValue());
        }
    }
}
Also used : Scanner(org.apache.accumulo.core.client.Scanner) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) Entry(java.util.Map.Entry) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) List(java.util.List) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) NumericValueConstraint(org.apache.accumulo.test.constraints.NumericValueConstraint) Key(org.apache.accumulo.core.data.Key) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 9 with ConstraintViolationSummary

use of org.apache.accumulo.core.data.ConstraintViolationSummary in project accumulo by apache.

the class InsertCommand method execute.

@Override
public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws AccumuloException, AccumuloSecurityException, TableNotFoundException, IOException, ConstraintViolationException {
    shellState.checkTableState();
    final Mutation m = new Mutation(new Text(cl.getArgs()[0].getBytes(Shell.CHARSET)));
    final Text colf = new Text(cl.getArgs()[1].getBytes(Shell.CHARSET));
    final Text colq = new Text(cl.getArgs()[2].getBytes(Shell.CHARSET));
    final Value val = new Value(cl.getArgs()[3].getBytes(Shell.CHARSET));
    if (cl.hasOption(insertOptAuths.getOpt())) {
        final ColumnVisibility le = new ColumnVisibility(cl.getOptionValue(insertOptAuths.getOpt()));
        Shell.log.debug("Authorization label will be set to: " + le.toString());
        if (cl.hasOption(timestampOpt.getOpt()))
            m.put(colf, colq, le, Long.parseLong(cl.getOptionValue(timestampOpt.getOpt())), val);
        else
            m.put(colf, colq, le, val);
    } else if (cl.hasOption(timestampOpt.getOpt()))
        m.put(colf, colq, Long.parseLong(cl.getOptionValue(timestampOpt.getOpt())), val);
    else
        m.put(colf, colq, val);
    final BatchWriterConfig cfg = new BatchWriterConfig().setMaxMemory(Math.max(m.estimatedMemoryUsed(), 1024)).setMaxWriteThreads(1).setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
    if (cl.hasOption(durabilityOption.getOpt())) {
        String userDurability = cl.getOptionValue(durabilityOption.getOpt());
        switch(userDurability) {
            case "sync":
                cfg.setDurability(Durability.SYNC);
                break;
            case "flush":
                cfg.setDurability(Durability.FLUSH);
                break;
            case "none":
                cfg.setDurability(Durability.NONE);
                break;
            case "log":
                cfg.setDurability(Durability.NONE);
                break;
            default:
                throw new IllegalArgumentException("Unknown durability: " + userDurability);
        }
    }
    final BatchWriter bw = shellState.getConnector().createBatchWriter(shellState.getTableName(), cfg);
    bw.addMutation(m);
    try {
        bw.close();
    } catch (MutationsRejectedException e) {
        final ArrayList<String> lines = new ArrayList<>();
        if (!e.getSecurityErrorCodes().isEmpty()) {
            lines.add("\tAuthorization Failures:");
        }
        for (Entry<TabletId, Set<SecurityErrorCode>> entry : e.getSecurityErrorCodes().entrySet()) {
            lines.add("\t\t" + entry);
        }
        if (!e.getConstraintViolationSummaries().isEmpty()) {
            lines.add("\tConstraint Failures:");
        }
        for (ConstraintViolationSummary cvs : e.getConstraintViolationSummaries()) {
            lines.add("\t\t" + cvs.toString());
        }
        if (lines.size() == 0 || e.getUnknownExceptions() > 0) {
            // must always print something
            lines.add(" " + e.getClass().getName() + " : " + e.getMessage());
            if (e.getCause() != null)
                lines.add("   Caused by : " + e.getCause().getClass().getName() + " : " + e.getCause().getMessage());
        }
        shellState.printLines(lines.iterator(), false);
        return 1;
    }
    return 0;
}
Also used : SecurityErrorCode(org.apache.accumulo.core.client.security.SecurityErrorCode) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) Entry(java.util.Map.Entry) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) BatchWriter(org.apache.accumulo.core.client.BatchWriter) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 10 with ConstraintViolationSummary

use of org.apache.accumulo.core.data.ConstraintViolationSummary in project accumulo by apache.

the class TabletServerBatchWriter method checkForFailures.

private void checkForFailures() throws MutationsRejectedException {
    if (somethingFailed) {
        List<ConstraintViolationSummary> cvsList = violations.asList();
        HashMap<TabletId, Set<org.apache.accumulo.core.client.security.SecurityErrorCode>> af = new HashMap<>();
        for (Entry<KeyExtent, Set<SecurityErrorCode>> entry : authorizationFailures.entrySet()) {
            HashSet<org.apache.accumulo.core.client.security.SecurityErrorCode> codes = new HashSet<>();
            for (SecurityErrorCode sce : entry.getValue()) {
                codes.add(org.apache.accumulo.core.client.security.SecurityErrorCode.valueOf(sce.name()));
            }
            af.put(new TabletIdImpl(entry.getKey()), codes);
        }
        throw new MutationsRejectedException(context.getInstance(), cvsList, af, serverSideErrors, unknownErrors, lastUnknownError);
    }
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SecurityErrorCode(org.apache.accumulo.core.client.impl.thrift.SecurityErrorCode) TabletIdImpl(org.apache.accumulo.core.data.impl.TabletIdImpl) KeyExtent(org.apache.accumulo.core.data.impl.KeyExtent) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) TabletId(org.apache.accumulo.core.data.TabletId) HashSet(java.util.HashSet) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Aggregations

ConstraintViolationSummary (org.apache.accumulo.core.data.ConstraintViolationSummary)10 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)6 Value (org.apache.accumulo.core.data.Value)5 BatchWriter (org.apache.accumulo.core.client.BatchWriter)4 Key (org.apache.accumulo.core.data.Key)4 Mutation (org.apache.accumulo.core.data.Mutation)4 Text (org.apache.hadoop.io.Text)4 Entry (java.util.Map.Entry)3 BatchWriterConfig (org.apache.accumulo.core.client.BatchWriterConfig)3 BinaryComparable (org.apache.hadoop.io.BinaryComparable)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 Scanner (org.apache.accumulo.core.client.Scanner)2 TabletId (org.apache.accumulo.core.data.TabletId)2 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)2