Search in sources :

Example 21 with ConditionalWriter

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

the class ConditionalWriterIT method testError.

@Test
public void testError() throws Exception {
    String table = getUniqueNames(1)[0];
    Connector conn = getConnector();
    conn.tableOperations().create(table);
    try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig())) {
        IteratorSetting iterSetting = new IteratorSetting(5, BadIterator.class);
        ConditionalMutation cm1 = new ConditionalMutation("r1", new Condition("tx", "seq").setIterators(iterSetting));
        cm1.put("tx", "seq", "1");
        cm1.put("data", "x", "a");
        Result result = cw.write(cm1);
        try {
            Status status = result.getStatus();
            Assert.fail("Expected exception using iterator which throws an error, Got status: " + status);
        } catch (AccumuloException ae) {
        }
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Status(org.apache.accumulo.core.client.ConditionalWriter.Status) Connector(org.apache.accumulo.core.client.Connector) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) AccumuloException(org.apache.accumulo.core.client.AccumuloException) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) Test(org.junit.Test)

Example 22 with ConditionalWriter

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

the class ConditionalWriterIT method testBigBatch.

@Test
public void testBigBatch() throws Exception {
    Connector conn = getConnector();
    String tableName = getUniqueNames(1)[0];
    conn.tableOperations().create(tableName);
    conn.tableOperations().addSplits(tableName, nss("2", "4", "6"));
    sleepUninterruptibly(2, TimeUnit.SECONDS);
    int num = 100;
    ArrayList<byte[]> rows = new ArrayList<>(num);
    ArrayList<ConditionalMutation> cml = new ArrayList<>(num);
    Random r = new Random();
    byte[] e = new byte[0];
    for (int i = 0; i < num; i++) {
        rows.add(FastFormat.toZeroPaddedString(abs(r.nextLong()), 16, 16, e));
    }
    for (int i = 0; i < num; i++) {
        ConditionalMutation cm = new ConditionalMutation(rows.get(i), new Condition("meta", "seq"));
        cm.put("meta", "seq", "1");
        cm.put("meta", "tx", UUID.randomUUID().toString());
        cml.add(cm);
    }
    try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig())) {
        Iterator<Result> results = cw.write(cml.iterator());
        int count = 0;
        // TODO check got each row back
        while (results.hasNext()) {
            Result result = results.next();
            Assert.assertEquals(Status.ACCEPTED, result.getStatus());
            count++;
        }
        Assert.assertEquals("Did not receive the expected number of results", num, count);
        ArrayList<ConditionalMutation> cml2 = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            ConditionalMutation cm = new ConditionalMutation(rows.get(i), new Condition("meta", "seq").setValue("1"));
            cm.put("meta", "seq", "2");
            cm.put("meta", "tx", UUID.randomUUID().toString());
            cml2.add(cm);
        }
        count = 0;
        results = cw.write(cml2.iterator());
        while (results.hasNext()) {
            Result result = results.next();
            Assert.assertEquals(Status.ACCEPTED, result.getStatus());
            count++;
        }
        Assert.assertEquals("Did not receive the expected number of results", num, count);
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) Connector(org.apache.accumulo.core.client.Connector) ArrayList(java.util.ArrayList) AlphaNumKeyConstraint(org.apache.accumulo.test.constraints.AlphaNumKeyConstraint) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) Random(java.util.Random) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) Test(org.junit.Test)

Example 23 with ConditionalWriter

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

the class ConditionalWriterIT method testNoConditions.

@Test(expected = IllegalArgumentException.class)
public void testNoConditions() throws AccumuloException, AccumuloSecurityException, TableExistsException, TableNotFoundException {
    String table = getUniqueNames(1)[0];
    Connector conn = getConnector();
    conn.tableOperations().create(table);
    try (ConditionalWriter cw = conn.createConditionalWriter(table, new ConditionalWriterConfig())) {
        ConditionalMutation cm1 = new ConditionalMutation("r1");
        cm1.put("tx", "seq", "1");
        cm1.put("data", "x", "a");
        cw.write(cm1);
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) Test(org.junit.Test)

Example 24 with ConditionalWriter

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

the class ConditionalWriterIT method testBatchErrors.

@Test
public void testBatchErrors() throws Exception {
    Connector conn = getConnector();
    String tableName = getUniqueNames(1)[0];
    conn.tableOperations().create(tableName);
    conn.tableOperations().addConstraint(tableName, AlphaNumKeyConstraint.class.getName());
    conn.tableOperations().clone(tableName, tableName + "_clone", true, new HashMap<>(), new HashSet<>());
    conn.securityOperations().changeUserAuthorizations(getAdminPrincipal(), new Authorizations("A", "B"));
    ColumnVisibility cvaob = new ColumnVisibility("A|B");
    ColumnVisibility cvaab = new ColumnVisibility("A&B");
    switch((new Random()).nextInt(3)) {
        case 1:
            conn.tableOperations().addSplits(tableName, nss("6"));
            break;
        case 2:
            conn.tableOperations().addSplits(tableName, nss("2", "95"));
            break;
    }
    ArrayList<ConditionalMutation> mutations = new ArrayList<>();
    ConditionalMutation cm0 = new ConditionalMutation("99006", new Condition("tx", "seq").setVisibility(cvaob));
    cm0.put("name+", "last", cvaob, "doe");
    cm0.put("name", "first", cvaob, "john");
    cm0.put("tx", "seq", cvaob, "1");
    mutations.add(cm0);
    ConditionalMutation cm1 = new ConditionalMutation("59056", new Condition("tx", "seq").setVisibility(cvaab));
    cm1.put("name", "last", cvaab, "doe");
    cm1.put("name", "first", cvaab, "jane");
    cm1.put("tx", "seq", cvaab, "1");
    mutations.add(cm1);
    ConditionalMutation cm2 = new ConditionalMutation("19059", new Condition("tx", "seq").setVisibility(cvaob));
    cm2.put("name", "last", cvaob, "doe");
    cm2.put("name", "first", cvaob, "jack");
    cm2.put("tx", "seq", cvaob, "1");
    mutations.add(cm2);
    ConditionalMutation cm3 = new ConditionalMutation("90909", new Condition("tx", "seq").setVisibility(cvaob).setValue("1"));
    cm3.put("name", "last", cvaob, "doe");
    cm3.put("name", "first", cvaob, "john");
    cm3.put("tx", "seq", cvaob, "2");
    mutations.add(cm3);
    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());
        HashSet<String> rows = new HashSet<>();
        while (results.hasNext()) {
            Result result = results.next();
            String row = new String(result.getMutation().getRow());
            if (row.equals("19059")) {
                Assert.assertEquals(Status.ACCEPTED, result.getStatus());
            } else if (row.equals("59056")) {
                Assert.assertEquals(Status.INVISIBLE_VISIBILITY, result.getStatus());
            } else if (row.equals("99006")) {
                Assert.assertEquals(Status.VIOLATED, result.getStatus());
            } else if (row.equals("90909")) {
                Assert.assertEquals(Status.REJECTED, result.getStatus());
            }
            rows.add(row);
        }
        Assert.assertEquals(4, rows.size());
        scanner.fetchColumn(new Text("tx"), new Text("seq"));
        Entry<Key, Value> entry = Iterables.getOnlyElement(scanner);
        Assert.assertEquals("1", entry.getValue().toString());
    }
}
Also used : Condition(org.apache.accumulo.core.data.Condition) 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) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) AlphaNumKeyConstraint(org.apache.accumulo.test.constraints.AlphaNumKeyConstraint) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) Random(java.util.Random) Value(org.apache.accumulo.core.data.Value) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Key(org.apache.accumulo.core.data.Key) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with ConditionalWriter

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

the class ConditionalWriterIT method testThreads.

@Test
public void testThreads() throws Exception {
    // test multiple threads using a single conditional writer
    String tableName = getUniqueNames(1)[0];
    Connector conn = getConnector();
    conn.tableOperations().create(tableName);
    Random rand = new Random();
    switch(rand.nextInt(3)) {
        case 1:
            conn.tableOperations().addSplits(tableName, nss("4"));
            break;
        case 2:
            conn.tableOperations().addSplits(tableName, nss("3", "5"));
            break;
    }
    try (ConditionalWriter cw = conn.createConditionalWriter(tableName, new ConditionalWriterConfig())) {
        ArrayList<ByteSequence> rows = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            rows.add(new ArrayByteSequence(FastFormat.toZeroPaddedString(abs(rand.nextLong()), 16, 16, new byte[0])));
        }
        ArrayList<ConditionalMutation> mutations = new ArrayList<>();
        for (ByteSequence row : rows) mutations.add(new Stats(row).toMutation());
        ArrayList<ByteSequence> rows2 = new ArrayList<>();
        Iterator<Result> results = cw.write(mutations.iterator());
        while (results.hasNext()) {
            Result result = results.next();
            Assert.assertEquals(Status.ACCEPTED, result.getStatus());
            rows2.add(new ArrayByteSequence(result.getMutation().getRow()));
        }
        Collections.sort(rows);
        Collections.sort(rows2);
        Assert.assertEquals(rows, rows2);
        AtomicBoolean failed = new AtomicBoolean(false);
        ExecutorService tp = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            tp.submit(new MutatorTask(tableName, conn, rows, cw, failed));
        }
        tp.shutdown();
        while (!tp.isTerminated()) {
            tp.awaitTermination(1, TimeUnit.MINUTES);
        }
        Assert.assertFalse("A MutatorTask failed with an exception", failed.get());
    }
    try (Scanner scanner = conn.createScanner(tableName, Authorizations.EMPTY)) {
        RowIterator rowIter = new RowIterator(scanner);
        while (rowIter.hasNext()) {
            Iterator<Entry<Key, Value>> row = rowIter.next();
            new Stats(row);
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) IsolatedScanner(org.apache.accumulo.core.client.IsolatedScanner) Scanner(org.apache.accumulo.core.client.Scanner) ArrayList(java.util.ArrayList) AlphaNumKeyConstraint(org.apache.accumulo.test.constraints.AlphaNumKeyConstraint) Result(org.apache.accumulo.core.client.ConditionalWriter.Result) ConditionalWriter(org.apache.accumulo.core.client.ConditionalWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConditionalMutation(org.apache.accumulo.core.data.ConditionalMutation) Entry(java.util.Map.Entry) Random(java.util.Random) RowIterator(org.apache.accumulo.core.client.RowIterator) ExecutorService(java.util.concurrent.ExecutorService) ConditionalWriterConfig(org.apache.accumulo.core.client.ConditionalWriterConfig) ArrayByteSequence(org.apache.accumulo.core.data.ArrayByteSequence) ByteSequence(org.apache.accumulo.core.data.ByteSequence) ArrayByteSequence(org.apache.accumulo.core.data.ArrayByteSequence) Test(org.junit.Test)

Aggregations

ConditionalWriter (org.apache.accumulo.core.client.ConditionalWriter)26 ConditionalMutation (org.apache.accumulo.core.data.ConditionalMutation)24 ConditionalWriterConfig (org.apache.accumulo.core.client.ConditionalWriterConfig)23 Condition (org.apache.accumulo.core.data.Condition)20 Connector (org.apache.accumulo.core.client.Connector)19 Test (org.junit.Test)18 Result (org.apache.accumulo.core.client.ConditionalWriter.Result)12 IsolatedScanner (org.apache.accumulo.core.client.IsolatedScanner)12 Scanner (org.apache.accumulo.core.client.Scanner)12 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 AccumuloException (org.apache.accumulo.core.client.AccumuloException)8 Range (org.apache.accumulo.core.data.Range)8 Text (org.apache.hadoop.io.Text)8 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)7 Authorizations (org.apache.accumulo.core.security.Authorizations)7 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)6 ArrayList (java.util.ArrayList)5 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)5