Search in sources :

Example 1 with ConstraintViolationSummary

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

the class TestIngest method ingest.

public static void ingest(Connector connector, FileSystem fs, Opts opts, BatchWriterOpts bwOpts) throws IOException, AccumuloException, AccumuloSecurityException, TableNotFoundException, MutationsRejectedException, TableExistsException {
    long stopTime;
    byte[][] bytevals = generateValues(opts.dataSize);
    byte[] randomValue = new byte[opts.dataSize];
    Random random = new Random();
    long bytesWritten = 0;
    createTable(connector, opts);
    BatchWriter bw = null;
    FileSKVWriter writer = null;
    if (opts.outputFile != null) {
        Configuration conf = CachedConfiguration.getInstance();
        writer = FileOperations.getInstance().newWriterBuilder().forFile(opts.outputFile + "." + RFile.EXTENSION, fs, conf).withTableConfiguration(DefaultConfiguration.getInstance()).build();
        writer.startDefaultLocalityGroup();
    } else {
        bw = connector.createBatchWriter(opts.getTableName(), bwOpts.getBatchWriterConfig());
        connector.securityOperations().changeUserAuthorizations(opts.getPrincipal(), AUTHS);
    }
    Text labBA = new Text(opts.columnVisibility.getExpression());
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < opts.rows; i++) {
        int rowid;
        if (opts.stride > 0) {
            rowid = ((i % opts.stride) * (opts.rows / opts.stride)) + (i / opts.stride);
        } else {
            rowid = i;
        }
        Text row = generateRow(rowid, opts.startRow);
        Mutation m = new Mutation(row);
        for (int j = 0; j < opts.cols; j++) {
            Text colf = new Text(opts.columnFamily);
            Text colq = new Text(FastFormat.toZeroPaddedString(j, 7, 10, COL_PREFIX));
            if (writer != null) {
                Key key = new Key(row, colf, colq, labBA);
                if (opts.timestamp >= 0) {
                    key.setTimestamp(opts.timestamp);
                } else {
                    key.setTimestamp(startTime);
                }
                if (opts.delete) {
                    key.setDeleted(true);
                } else {
                    key.setDeleted(false);
                }
                bytesWritten += key.getSize();
                if (opts.delete) {
                    writer.append(key, new Value(new byte[0]));
                } else {
                    byte[] value;
                    if (opts.random != null) {
                        value = genRandomValue(random, randomValue, opts.random, rowid + opts.startRow, j);
                    } else {
                        value = bytevals[j % bytevals.length];
                    }
                    Value v = new Value(value);
                    writer.append(key, v);
                    bytesWritten += v.getSize();
                }
            } else {
                Key key = new Key(row, colf, colq, labBA);
                bytesWritten += key.getSize();
                if (opts.delete) {
                    if (opts.timestamp >= 0)
                        m.putDelete(colf, colq, opts.columnVisibility, opts.timestamp);
                    else
                        m.putDelete(colf, colq, opts.columnVisibility);
                } else {
                    byte[] value;
                    if (opts.random != null) {
                        value = genRandomValue(random, randomValue, opts.random, rowid + opts.startRow, j);
                    } else {
                        value = bytevals[j % bytevals.length];
                    }
                    bytesWritten += value.length;
                    if (opts.timestamp >= 0) {
                        m.put(colf, colq, opts.columnVisibility, opts.timestamp, new Value(value, true));
                    } else {
                        m.put(colf, colq, opts.columnVisibility, new Value(value, true));
                    }
                }
            }
        }
        if (bw != null)
            bw.addMutation(m);
    }
    if (writer != null) {
        writer.close();
    } else if (bw != null) {
        try {
            bw.close();
        } catch (MutationsRejectedException e) {
            if (e.getSecurityErrorCodes().size() > 0) {
                for (Entry<TabletId, Set<SecurityErrorCode>> entry : e.getSecurityErrorCodes().entrySet()) {
                    System.err.println("ERROR : Not authorized to write to : " + entry.getKey() + " due to " + entry.getValue());
                }
            }
            if (e.getConstraintViolationSummaries().size() > 0) {
                for (ConstraintViolationSummary cvs : e.getConstraintViolationSummaries()) {
                    System.err.println("ERROR : Constraint violates : " + cvs);
                }
            }
            throw e;
        }
    }
    stopTime = System.currentTimeMillis();
    int totalValues = opts.rows * opts.cols;
    double elapsed = (stopTime - startTime) / 1000.0;
    System.out.printf("%,12d records written | %,8d records/sec | %,12d bytes written | %,8d bytes/sec | %6.3f secs   %n", totalValues, (int) (totalValues / elapsed), bytesWritten, (int) (bytesWritten / elapsed), elapsed);
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) Configuration(org.apache.hadoop.conf.Configuration) DefaultConfiguration(org.apache.accumulo.core.conf.DefaultConfiguration) CachedConfiguration(org.apache.accumulo.core.util.CachedConfiguration) FileSKVWriter(org.apache.accumulo.core.file.FileSKVWriter) Text(org.apache.hadoop.io.Text) Random(java.util.Random) Value(org.apache.accumulo.core.data.Value) TabletId(org.apache.accumulo.core.data.TabletId) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) TabletServerBatchWriter(org.apache.accumulo.core.client.impl.TabletServerBatchWriter) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) Key(org.apache.accumulo.core.data.Key) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 2 with ConstraintViolationSummary

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

the class DeleterFormatter method next.

/**
 * @return null, because the iteration will provide prompts and handle deletes internally.
 */
@Override
public String next() {
    Entry<Key, Value> next = getScannerIterator().next();
    Key key = next.getKey();
    Mutation m = new Mutation(key.getRow());
    String entryStr = formatEntry(next, isDoTimestamps());
    boolean delete = force;
    try {
        if (!force) {
            shellState.getReader().flush();
            String line = shellState.getReader().readLine("Delete { " + entryStr + " } ? ");
            more = line != null;
            delete = line != null && (line.equalsIgnoreCase("y") || line.equalsIgnoreCase("yes"));
        }
        if (delete) {
            m.putDelete(key.getColumnFamily(), key.getColumnQualifier(), new ColumnVisibility(key.getColumnVisibility()), key.getTimestamp());
            try {
                writer.addMutation(m);
            } catch (MutationsRejectedException e) {
                log.error(e.toString());
                if (Shell.isDebuggingEnabled())
                    for (ConstraintViolationSummary cvs : e.getConstraintViolationSummaries()) log.trace(cvs.toString());
            }
        }
        shellState.getReader().print(String.format("[%s] %s%n", delete ? "DELETED" : "SKIPPED", entryStr));
    } catch (IOException e) {
        log.error("Cannot write to console", e);
        throw new RuntimeException(e);
    }
    return null;
}
Also used : Value(org.apache.accumulo.core.data.Value) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) IOException(java.io.IOException) Key(org.apache.accumulo.core.data.Key) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 3 with ConstraintViolationSummary

use of org.apache.accumulo.core.data.ConstraintViolationSummary 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 4 with ConstraintViolationSummary

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

the class ConstraintCheckerTest method testCheckMutationOutsideKeyExtent.

@Test
public void testCheckMutationOutsideKeyExtent() {
    expect(extent.contains(anyObject(BinaryComparable.class))).andReturn(false);
    replayAll();
    ConstraintViolationSummary cvs = Iterables.getOnlyElement(cc.check(env, m).asList());
    assertEquals(SystemConstraint.class.getName(), cvs.getConstrainClass());
}
Also used : BinaryComparable(org.apache.hadoop.io.BinaryComparable) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) Test(org.junit.Test)

Example 5 with ConstraintViolationSummary

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

the class ConstraintCheckerTest method testCheckFailure.

@Test
public void testCheckFailure() {
    expect(extent.contains(anyObject(BinaryComparable.class))).andReturn(true);
    replayAll();
    constraints.add(makeFailureConstraint());
    List<ConstraintViolationSummary> cvsList = cc.check(env, m).asList();
    assertEquals(2, cvsList.size());
    Set<String> violationDescs = new HashSet<>();
    for (ConstraintViolationSummary cvs : cvsList) {
        violationDescs.add(cvs.getViolationDescription());
    }
    assertEquals(ImmutableSet.of("ViolationCode1", "ViolationCode2"), violationDescs);
}
Also used : BinaryComparable(org.apache.hadoop.io.BinaryComparable) ConstraintViolationSummary(org.apache.accumulo.core.data.ConstraintViolationSummary) HashSet(java.util.HashSet) Test(org.junit.Test)

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