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);
}
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;
}
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;
}
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());
}
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);
}
Aggregations