Search in sources :

Example 1 with ConditionalWriterConfig

use of org.apache.accumulo.core.client.ConditionalWriterConfig in project accumulo by apache.

the class ProxyServer method createConditionalWriter.

@Override
public String createConditionalWriter(ByteBuffer login, String tableName, ConditionalWriterOptions options) throws org.apache.accumulo.proxy.thrift.AccumuloException, org.apache.accumulo.proxy.thrift.AccumuloSecurityException, org.apache.accumulo.proxy.thrift.TableNotFoundException, TException {
    try {
        ConditionalWriterConfig cwc = new ConditionalWriterConfig();
        if (options.getMaxMemory() != 0) {
        // TODO
        }
        if (options.isSetThreads() && options.getThreads() != 0)
            cwc.setMaxWriteThreads(options.getThreads());
        if (options.isSetTimeoutMs() && options.getTimeoutMs() != 0)
            cwc.setTimeout(options.getTimeoutMs(), TimeUnit.MILLISECONDS);
        if (options.isSetAuthorizations() && options.getAuthorizations() != null)
            cwc.setAuthorizations(getAuthorizations(options.getAuthorizations()));
        if (options.isSetDurability() && options.getDurability() != null)
            cwc.setDurability(getDurability(options.getDurability()));
        ConditionalWriter cw = getConnector(login).createConditionalWriter(tableName, cwc);
        UUID id = UUID.randomUUID();
        conditionalWriterCache.put(id, cw);
        return id.toString();
    } catch (Exception e) {
        handleExceptionTNF(e);
        return null;
    }
}
Also used : ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) UUID(java.util.UUID) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) NamespaceNotFoundException(org.apache.accumulo.core.client.NamespaceNotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) TableExistsException(org.apache.accumulo.core.client.TableExistsException) TException(org.apache.thrift.TException) NoMoreEntriesException(org.apache.accumulo.proxy.thrift.NoMoreEntriesException) ThriftTableOperationException(org.apache.accumulo.core.client.impl.thrift.ThriftTableOperationException) NamespaceExistsException(org.apache.accumulo.core.client.NamespaceExistsException) NamespaceNotEmptyException(org.apache.accumulo.core.client.NamespaceNotEmptyException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) AccumuloException(org.apache.accumulo.core.client.AccumuloException)

Example 2 with ConditionalWriterConfig

use of org.apache.accumulo.core.client.ConditionalWriterConfig in project accumulo by apache.

the class ConditionalWriterIT method testTimeout.

@Test
public void testTimeout() throws Exception {
    Connector conn = getConnector();
    String table = getUniqueNames(1)[0];
    conn.tableOperations().create(table);
    try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig().setTimeout(3, TimeUnit.SECONDS));
        Scanner scanner = conn.createScanner(table, Authorizations.EMPTY)) {
        ConditionalMutation cm1 = new ConditionalMutation("r1", new Condition("tx", "seq"));
        cm1.put("tx", "seq", "1");
        cm1.put("data", "x", "a");
        Assert.assertEquals(cw.write(cm1).getStatus(), Status.ACCEPTED);
        IteratorSetting is = new IteratorSetting(5, SlowIterator.class);
        SlowIterator.setSeekSleepTime(is, 5000);
        ConditionalMutation cm2 = new ConditionalMutation("r1", new Condition("tx", "seq").setValue("1").setIterators(is));
        cm2.put("tx", "seq", "2");
        cm2.put("data", "x", "b");
        Assert.assertEquals(cw.write(cm2).getStatus(), Status.UNKNOWN);
        for (Entry<Key, Value> entry : scanner) {
            String cf = entry.getKey().getColumnFamilyData().toString();
            String cq = entry.getKey().getColumnQualifierData().toString();
            String val = entry.getValue().toString();
            if (cf.equals("tx") && cq.equals("seq"))
                Assert.assertEquals("Unexpected value in tx:seq", "1", val);
            else if (cf.equals("data") && cq.equals("x"))
                Assert.assertEquals("Unexpected value in data:x", "a", val);
            else
                Assert.fail("Saw unexpected column family and qualifier: " + entry);
        }
        ConditionalMutation cm3 = new ConditionalMutation("r1", new Condition("tx", "seq").setValue("1"));
        cm3.put("tx", "seq", "2");
        cm3.put("data", "x", "b");
        Assert.assertEquals(cw.write(cm3).getStatus(), Status.ACCEPTED);
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Connector(org.apache.accumulo.core.client.Connector) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 3 with ConditionalWriterConfig

use of org.apache.accumulo.core.client.ConditionalWriterConfig in project accumulo by apache.

the class ConditionalWriterIT method testTableAndConditionIterators.

@Test
public void testTableAndConditionIterators() throws Exception {
    // test w/ table that has iterators configured
    Connector conn = getConnector();
    String tableName = getUniqueNames(1)[0];
    IteratorSetting aiConfig1 = new IteratorSetting(30, "AI1", AddingIterator.class);
    aiConfig1.addOption("amount", "2");
    IteratorSetting aiConfig2 = new IteratorSetting(35, "MI1", MultiplyingIterator.class);
    aiConfig2.addOption("amount", "3");
    IteratorSetting aiConfig3 = new IteratorSetting(40, "AI2", AddingIterator.class);
    aiConfig3.addOption("amount", "5");
    conn.tableOperations().create(tableName);
    BatchWriter bw = conn.createBatchWriter(tableName, new BatchWriterConfig());
    Mutation m = new Mutation("ACCUMULO-1000");
    m.put("count", "comments", "6");
    bw.addMutation(m);
    m = new Mutation("ACCUMULO-1001");
    m.put("count", "comments", "7");
    bw.addMutation(m);
    m = new Mutation("ACCUMULO-1002");
    m.put("count", "comments", "8");
    bw.addMutation(m);
    bw.close();
    conn.tableOperations().attachIterator(tableName, aiConfig1, EnumSet.of(IteratorScope.scan));
    conn.tableOperations().offline(tableName, true);
    conn.tableOperations().online(tableName, true);
    try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig());
        Scanner scanner = conn.createScanner(tableName, new Authorizations())) {
        ConditionalMutation cm6 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setValue("8"));
        cm6.put("count", "comments", "7");
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm6).getStatus());
        scanner.setRange(new Range("ACCUMULO-1000"));
        scanner.fetchColumn(new Text("count"), new Text("comments"));
        Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("9", entry.getValue().toString());
        ConditionalMutation cm7 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(aiConfig2).setValue("27"));
        cm7.put("count", "comments", "8");
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm7).getStatus());
        entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("10", entry.getValue().toString());
        ConditionalMutation cm8 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(aiConfig2, aiConfig3).setValue("35"));
        cm8.put("count", "comments", "9");
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm8).getStatus());
        entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("11", entry.getValue().toString());
        ConditionalMutation cm3 = new ConditionalMutation("ACCUMULO-1000", new Condition("count", "comments").setIterators(aiConfig2).setValue("33"));
        cm3.put("count", "comments", "3");
        ConditionalMutation cm4 = new ConditionalMutation("ACCUMULO-1001", new Condition("count", "comments").setIterators(aiConfig3).setValue("14"));
        cm4.put("count", "comments", "3");
        ConditionalMutation cm5 = new ConditionalMutation("ACCUMULO-1002", new Condition("count", "comments").setIterators(aiConfig3).setValue("10"));
        cm5.put("count", "comments", "3");
        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);
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Status(org.apache.accumulo.core.client.ConditionalWriter.Status) Connector(org.apache.accumulo.core.client.Connector) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) Authorizations(org.apache.accumulo.core.security.Authorizations) HashMap(java.util.HashMap) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) Value(org.apache.accumulo.core.data.Value) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Example 4 with ConditionalWriterConfig

use of org.apache.accumulo.core.client.ConditionalWriterConfig in project accumulo by apache.

the class ConditionalWriterIT method testDeleteTable.

@Test
public void testDeleteTable() throws Exception {
    String table = getUniqueNames(1)[0];
    Connector conn = getConnector();
    try {
        conn.createConditionalWriter(table, new ConditionalWriterConfig());
        Assert.fail("Creating conditional writer for table that doesn't exist should fail");
    } catch (TableNotFoundException e) {
    }
    conn.tableOperations().create(table);
    try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig())) {
        conn.tableOperations().delete(table);
        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 deleted table. Got status: " + status);
        } catch (AccumuloException ae) {
            Assert.assertEquals(TableDeletedException.class, ae.getCause().getClass());
        }
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Status(org.apache.accumulo.core.client.ConditionalWriter.Status) Connector(org.apache.accumulo.core.client.Connector) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) AccumuloException(org.apache.accumulo.core.client.AccumuloException) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) TableDeletedException(org.apache.accumulo.core.client.TableDeletedException) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) Test(org.junit.Test)

Example 5 with ConditionalWriterConfig

use of org.apache.accumulo.core.client.ConditionalWriterConfig in project accumulo by apache.

the class ConditionalWriterIT method testBasic.

@Test
public void testBasic() throws Exception {
    Connector conn = getConnector();
    String tableName = getUniqueNames(1)[0];
    conn.tableOperations().create(tableName);
    try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig());
        Scanner scanner = conn.createScanner(tableName, Authorizations.EMPTY)) {
        // mutation conditional on column tx:seq not existing
        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());
        Assert.assertEquals(Status.REJECTED, cw.write(cm0).getStatus());
        // mutation conditional on column tx:seq being 1
        ConditionalMutation cm1 = new ConditionalMutation("99006", new Condition("tx", "seq").setValue("1"));
        cm1.put("name", "last", "Doe");
        cm1.put("tx", "seq", "2");
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm1).getStatus());
        // test condition where value differs
        ConditionalMutation cm2 = new ConditionalMutation("99006", new Condition("tx", "seq").setValue("1"));
        cm2.put("name", "last", "DOE");
        cm2.put("tx", "seq", "2");
        Assert.assertEquals(Status.REJECTED, cw.write(cm2).getStatus());
        // test condition where column does not exists
        ConditionalMutation cm3 = new ConditionalMutation("99006", new Condition("txtypo", "seq").setValue("1"));
        cm3.put("name", "last", "deo");
        cm3.put("tx", "seq", "2");
        Assert.assertEquals(Status.REJECTED, cw.write(cm3).getStatus());
        // test two conditions, where one should fail
        ConditionalMutation cm4 = new ConditionalMutation("99006", new Condition("tx", "seq").setValue("2"), new Condition("name", "last").setValue("doe"));
        cm4.put("name", "last", "deo");
        cm4.put("tx", "seq", "3");
        Assert.assertEquals(Status.REJECTED, cw.write(cm4).getStatus());
        // test two conditions, where one should fail
        ConditionalMutation cm5 = new ConditionalMutation("99006", new Condition("tx", "seq").setValue("1"), new Condition("name", "last").setValue("Doe"));
        cm5.put("name", "last", "deo");
        cm5.put("tx", "seq", "3");
        Assert.assertEquals(Status.REJECTED, cw.write(cm5).getStatus());
        // ensure rejected mutations did not write
        scanner.fetchColumn(new Text("name"), new Text("last"));
        scanner.setRange(new Range("99006"));
        Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("Doe", entry.getValue().toString());
        // test w/ two conditions that are met
        ConditionalMutation cm6 = new ConditionalMutation("99006", new Condition("tx", "seq").setValue("2"), new Condition("name", "last").setValue("Doe"));
        cm6.put("name", "last", "DOE");
        cm6.put("tx", "seq", "3");
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm6).getStatus());
        entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("DOE", entry.getValue().toString());
        // test a conditional mutation that deletes
        ConditionalMutation cm7 = new ConditionalMutation("99006", new Condition("tx", "seq").setValue("3"));
        cm7.putDelete("name", "last");
        cm7.putDelete("name", "first");
        cm7.putDelete("tx", "seq");
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm7).getStatus());
        Assert.assertFalse("Did not expect to find any results", scanner.iterator().hasNext());
        // add the row back
        Assert.assertEquals(Status.ACCEPTED, cw.write(cm0).getStatus());
        Assert.assertEquals(Status.REJECTED, cw.write(cm0).getStatus());
        entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("doe", entry.getValue().toString());
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Connector(org.apache.accumulo.core.client.Connector) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) Value(org.apache.accumulo.core.data.Value) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) Text(org.apache.hadoop.io.Text) Range(org.apache.accumulo.core.data.Range) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test)

Aggregations

ConditionalWriterConfig (org.apache.accumulo.core.client.ConditionalWriterConfig)25 ConditionalWriter (org.apache.accumulo.core.client.ConditionalWriter)23 ConditionalMutation (org.apache.accumulo.core.data.ConditionalMutation)22 Connector (org.apache.accumulo.core.client.Connector)20 Test (org.junit.Test)20 Condition (org.apache.accumulo.core.data.Condition)19 IsolatedScanner (org.apache.accumulo.core.client.IsolatedScanner)12 Scanner (org.apache.accumulo.core.client.Scanner)12 Result (org.apache.accumulo.core.client.ConditionalWriter.Result)11 Status (org.apache.accumulo.core.client.ConditionalWriter.Status)10 Value (org.apache.accumulo.core.data.Value)10 Key (org.apache.accumulo.core.data.Key)9 Range (org.apache.accumulo.core.data.Range)8 AccumuloException (org.apache.accumulo.core.client.AccumuloException)7 Authorizations (org.apache.accumulo.core.security.Authorizations)7 Text (org.apache.hadoop.io.Text)7 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)6 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)5 AlphaNumKeyConstraint (org.apache.accumulo.test.constraints.AlphaNumKeyConstraint)5 ArrayList (java.util.ArrayList)4