use of org.apache.accumulo.core.data.ConditionalMutation in project accumulo by apache.
the class ConditionalWriterIT method testTrace.
@Test
public void testTrace() throws Exception {
// Need to add a getClientConfig() to AccumuloCluster
Assume.assumeTrue(getClusterType() == ClusterType.MINI);
Process tracer = null;
Connector conn = getConnector();
AccumuloCluster cluster = getCluster();
MiniAccumuloClusterImpl mac = (MiniAccumuloClusterImpl) cluster;
if (!conn.tableOperations().exists("trace")) {
tracer = mac.exec(TraceServer.class);
while (!conn.tableOperations().exists("trace")) {
sleepUninterruptibly(1, TimeUnit.SECONDS);
}
}
String tableName = getUniqueNames(1)[0];
conn.tableOperations().create(tableName);
DistributedTrace.enable("localhost", "testTrace", mac.getClientConfig());
sleepUninterruptibly(1, TimeUnit.SECONDS);
Span root = Trace.on("traceTest");
try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig())) {
// mutation conditional on column tx:seq not exiting
ConditionalMutation cm0 = new ConditionalMutation("99006", new Condition("tx", "seq"));
cm0.put("name", "last", "doe");
cm0.put("name", "first", "john");
cm0.put("tx", "seq", "1");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm0).getStatus());
root.stop();
}
try (Scanner scanner = conn.createScanner("trace", Authorizations.EMPTY)) {
scanner.setRange(new Range(new Text(Long.toHexString(root.traceId()))));
loop: while (true) {
final StringBuilder finalBuffer = new StringBuilder();
int traceCount = TraceDump.printTrace(scanner, new Printer() {
@Override
public void print(final String line) {
try {
finalBuffer.append(line).append("\n");
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
});
String traceOutput = finalBuffer.toString();
log.info("Trace output:" + traceOutput);
if (traceCount > 0) {
int lastPos = 0;
for (String part : "traceTest, startScan,startConditionalUpdate,conditionalUpdate,Check conditions,apply conditional mutations".split(",")) {
log.info("Looking in trace output for '" + part + "'");
int pos = traceOutput.indexOf(part);
if (-1 == pos) {
log.info("Trace output doesn't contain '" + part + "'");
Thread.sleep(1000);
break loop;
}
assertTrue("Did not find '" + part + "' in output", pos > 0);
assertTrue("'" + part + "' occurred earlier than the previous element unexpectedly", pos > lastPos);
lastPos = pos;
}
break;
} else {
log.info("Ignoring trace output as traceCount not greater than zero: " + traceCount);
Thread.sleep(1000);
}
}
if (tracer != null) {
tracer.destroy();
}
}
}
use of org.apache.accumulo.core.data.ConditionalMutation in project accumulo by apache.
the class ConditionalWriterIT method testFields.
@Test
public void testFields() throws Exception {
Connector conn = getConnector();
String tableName = getUniqueNames(1)[0];
String user = null;
ClientConfiguration clientConf = cluster.getClientConfig();
final boolean saslEnabled = clientConf.hasSasl();
ClusterUser user1 = getUser(0);
user = user1.getPrincipal();
if (saslEnabled) {
// The token is pointless for kerberos
conn.securityOperations().createLocalUser(user, null);
} else {
conn.securityOperations().createLocalUser(user, new PasswordToken(user1.getPassword()));
}
Authorizations auths = new Authorizations("A", "B");
conn.securityOperations().changeUserAuthorizations(user, auths);
conn.securityOperations().grantSystemPermission(user, SystemPermission.CREATE_TABLE);
conn = conn.getInstance().getConnector(user, user1.getToken());
conn.tableOperations().create(tableName);
try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig().setAuthorizations(auths));
Scanner scanner = conn.createScanner(tableName, auths)) {
ColumnVisibility cva = new ColumnVisibility("A");
ColumnVisibility cvb = new ColumnVisibility("B");
ConditionalMutation cm0 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cva));
cm0.put("name", "last", cva, "doe");
cm0.put("name", "first", cva, "john");
cm0.put("tx", "seq", cva, "1");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm0).getStatus());
scanner.setRange(new Range("99006"));
// TODO verify all columns
scanner.fetchColumn(new Text("tx"), new Text("seq"));
Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("1", entry.getValue().toString());
long ts = entry.getKey().getTimestamp();
// test wrong colf
ConditionalMutation cm1 = new ConditionalMutation("99006", new Condition("txA", "seq").setVisibility(cva).setValue("1"));
cm1.put("name", "last", cva, "Doe");
cm1.put("name", "first", cva, "John");
cm1.put("tx", "seq", cva, "2");
Assert.assertEquals(Status.REJECTED, cw.write(cm1).getStatus());
// test wrong colq
ConditionalMutation cm2 = new ConditionalMutation("99006", new Condition("tx", "seqA").setVisibility(cva).setValue("1"));
cm2.put("name", "last", cva, "Doe");
cm2.put("name", "first", cva, "John");
cm2.put("tx", "seq", cva, "2");
Assert.assertEquals(Status.REJECTED, cw.write(cm2).getStatus());
// test wrong colv
ConditionalMutation cm3 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cvb).setValue("1"));
cm3.put("name", "last", cva, "Doe");
cm3.put("name", "first", cva, "John");
cm3.put("tx", "seq", cva, "2");
Assert.assertEquals(Status.REJECTED, cw.write(cm3).getStatus());
// test wrong timestamp
ConditionalMutation cm4 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cva).setTimestamp(ts + 1).setValue("1"));
cm4.put("name", "last", cva, "Doe");
cm4.put("name", "first", cva, "John");
cm4.put("tx", "seq", cva, "2");
Assert.assertEquals(Status.REJECTED, cw.write(cm4).getStatus());
// test wrong timestamp
ConditionalMutation cm5 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cva).setTimestamp(ts - 1).setValue("1"));
cm5.put("name", "last", cva, "Doe");
cm5.put("name", "first", cva, "John");
cm5.put("tx", "seq", cva, "2");
Assert.assertEquals(Status.REJECTED, cw.write(cm5).getStatus());
// ensure no updates were made
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("1", entry.getValue().toString());
// set all columns correctly
ConditionalMutation cm6 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cva).setTimestamp(ts).setValue("1"));
cm6.put("name", "last", cva, "Doe");
cm6.put("name", "first", cva, "John");
cm6.put("tx", "seq", cva, "2");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm6).getStatus());
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("2", entry.getValue().toString());
}
}
use of org.apache.accumulo.core.data.ConditionalMutation in project accumulo by apache.
the class ConditionalWriterIT method testIterators.
@Test
public void testIterators() throws Exception {
Connector conn = getConnector();
String tableName = getUniqueNames(1)[0];
conn.tableOperations().create(tableName, new NewTableConfiguration().withoutDefaultIterators());
BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
Mutation m = new Mutation("ACCUMULO-1000");
m.put("count", "comments", "1");
bw.addMutation(m);
bw.addMutation(m);
bw.addMutation(m);
m = new Mutation("ACCUMULO-1001");
m.put("count2", "comments", "1");
bw.addMutation(m);
bw.addMutation(m);
m = new Mutation("ACCUMULO-1002");
m.put("count2", "comments", "1");
bw.addMutation(m);
bw.addMutation(m);
bw.close();
IteratorSetting iterConfig = new IteratorSetting(10, SummingCombiner.class);
SummingCombiner.setEncodingType(iterConfig, Type.STRING);
SummingCombiner.setColumns(iterConfig, Collections.singletonList(new IteratorSetting.Column("count")));
IteratorSetting iterConfig2 = new IteratorSetting(10, SummingCombiner.class);
SummingCombiner.setEncodingType(iterConfig2, Type.STRING);
SummingCombiner.setColumns(iterConfig2, Collections.singletonList(new IteratorSetting.Column("count2", "comments")));
IteratorSetting iterConfig3 = new IteratorSetting(5, VersioningIterator.class);
VersioningIterator.setMaxVersions(iterConfig3, 1);
try (Scanner scanner = conn.createScanner(tableName, new Authorizations())) {
scanner.addScanIterator(iterConfig);
scanner.setRange(new Range("ACCUMULO-1000"));
scanner.fetchColumn(new Text("count"), new Text("comments"));
Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("3", entry.getValue().toString());
try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig())) {
ConditionalMutation cm0 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setValue("3"));
cm0.put("count", "comments", "1");
Assert.assertEquals(Status.REJECTED, cw.write(cm0).getStatus());
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("3", entry.getValue().toString());
ConditionalMutation cm1 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(iterConfig).setValue("3"));
cm1.put("count", "comments", "1");
Assert.assertEquals(Status.ACCEPTED, cw.write(cm1).getStatus());
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("4", entry.getValue().toString());
ConditionalMutation cm2 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setValue("4"));
cm2.put("count", "comments", "1");
Assert.assertEquals(Status.REJECTED, cw.write(cm1).getStatus());
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("4", entry.getValue().toString());
// run test with multiple iterators passed in same batch and condition with two iterators
ConditionalMutation cm3 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(iterConfig).setValue("4"));
cm3.put("count", "comments", "1");
ConditionalMutation cm4 = new ConditionalMutation("ACCUMULO-1001", new Condition("count2", "comments").setIterators(iterConfig2).setValue("2"));
cm4.put("count2", "comments", "1");
ConditionalMutation cm5 = new ConditionalMutation("ACCUMULO-1002", new Condition("count2", "comments").setIterators(iterConfig2, iterConfig3).setValue("2"));
cm5.put("count2", "comments", "1");
Iterator<Result> results = cw.write(Arrays.asList(cm3, cm4, cm5).iterator());
Map<String, Status> actual = new HashMap<>();
while (results.hasNext()) {
Result result = results.next();
String k = new String(result.getMutation().getRow());
Assert.assertFalse("Did not expect to see multiple resultus for the row: " + k, actual.containsKey(k));
actual.put(k, result.getStatus());
}
Map<String, Status> expected = new HashMap<>();
expected.put("ACCUMULO-1000", Status.ACCEPTED);
expected.put("ACCUMULO-1001", Status.ACCEPTED);
expected.put("ACCUMULO-1002", Status.REJECTED);
Assert.assertEquals(expected, actual);
}
}
}
use of org.apache.accumulo.core.data.ConditionalMutation in project accumulo by apache.
the class ConditionalWriterIT method testBatch.
@Test
public void testBatch() throws Exception {
Connector conn = getConnector();
String tableName = getUniqueNames(1)[0];
conn.tableOperations().create(tableName);
conn.securityOperations().changeUserAuthorizations(getAdminPrincipal(), new Authorizations("A", "B"));
ColumnVisibility cvab = new ColumnVisibility("A|B");
ArrayList<ConditionalMutation> mutations = new ArrayList<>();
ConditionalMutation cm0 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cvab));
cm0.put("name", "last", cvab, "doe");
cm0.put("name", "first", cvab, "john");
cm0.put("tx", "seq", cvab, "1");
mutations.add(cm0);
ConditionalMutation cm1 = new ConditionalMutation("59056", new Condition("tx", "seq").setVisibility(cvab));
cm1.put("name", "last", cvab, "doe");
cm1.put("name", "first", cvab, "jane");
cm1.put("tx", "seq", cvab, "1");
mutations.add(cm1);
ConditionalMutation cm2 = new ConditionalMutation("19059", new Condition("tx", "seq").setVisibility(cvab));
cm2.put("name", "last", cvab, "doe");
cm2.put("name", "first", cvab, "jack");
cm2.put("tx", "seq", cvab, "1");
mutations.add(cm2);
try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig().setAuthorizations(new Authorizations("A")));
Scanner scanner = conn.createScanner(tableName, new Authorizations("A"))) {
Iterator<Result> results = cw.write(mutations.iterator());
int count = 0;
while (results.hasNext()) {
Result result = results.next();
Assert.assertEquals(Status.ACCEPTED, result.getStatus());
count++;
}
Assert.assertEquals(3, count);
scanner.fetchColumn(new Text("tx"), new Text("seq"));
for (String row : new String[] { "99006", "59056", "19059" }) {
scanner.setRange(new Range(row));
Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("1", entry.getValue().toString());
}
TreeSet<Text> splits = new TreeSet<>();
splits.add(new Text("7"));
splits.add(new Text("3"));
conn.tableOperations().addSplits(tableName, splits);
mutations.clear();
ConditionalMutation cm3 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cvab).setValue("1"));
cm3.put("name", "last", cvab, "Doe");
cm3.put("tx", "seq", cvab, "2");
mutations.add(cm3);
ConditionalMutation cm4 = new ConditionalMutation("59056", new Condition("tx", "seq").setVisibility(cvab));
cm4.put("name", "last", cvab, "Doe");
cm4.put("tx", "seq", cvab, "1");
mutations.add(cm4);
ConditionalMutation cm5 = new ConditionalMutation("19059", new Condition("tx", "seq").setVisibility(cvab).setValue("2"));
cm5.put("name", "last", cvab, "Doe");
cm5.put("tx", "seq", cvab, "3");
mutations.add(cm5);
results = cw.write(mutations.iterator());
int accepted = 0;
int rejected = 0;
while (results.hasNext()) {
Result result = results.next();
if (new String(result.getMutation().getRow()).equals("99006")) {
Assert.assertEquals(Status.ACCEPTED, result.getStatus());
accepted++;
} else {
Assert.assertEquals(Status.REJECTED, result.getStatus());
rejected++;
}
}
Assert.assertEquals("Expected only one accepted conditional mutation", 1, accepted);
Assert.assertEquals("Expected two rejected conditional mutations", 2, rejected);
for (String row : new String[] { "59056", "19059" }) {
scanner.setRange(new Range(row));
Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("1", entry.getValue().toString());
}
scanner.setRange(new Range("99006"));
Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("2", entry.getValue().toString());
scanner.clearColumns();
scanner.fetchColumn(new Text("name"), new Text("last"));
entry = Iterables.getOnlyElement(scanner);
Assert.assertEquals("Doe", entry.getValue().toString());
}
}
use of org.apache.accumulo.core.data.ConditionalMutation in project accumulo by apache.
the class ConditionalWriterIT method testOffline.
@Test
public void testOffline() throws Exception {
String table = getUniqueNames(1)[0];
Connector conn = getConnector();
conn.tableOperations().create(table);
try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig())) {
conn.tableOperations().offline(table, true);
ConditionalMutation cm1 = new ConditionalMutation("r1", new Condition("tx", "seq"));
cm1.put("tx", "seq", "1");
cm1.put("data", "x", "a");
Result result = cw.write(cm1);
try {
Status status = result.getStatus();
Assert.fail("Expected exception writing conditional mutation to offline table. Got status: " + status);
} catch (AccumuloException ae) {
Assert.assertEquals(TableOfflineException.class, ae.getCause().getClass());
}
try {
conn.createConditionalWriter(table, new ConditionalWriterConfig());
Assert.fail("Expected exception creating conditional writer to offline table");
} catch (TableOfflineException e) {
}
}
}
Aggregations