Search in sources :

Example 1 with DatasetProperties

use of co.cask.cdap.api.dataset.DatasetProperties in project cdap by caskdata.

the class DatasetAdminOpHTTPHandler method create.

@POST
@Path("/data/datasets/{name}/admin/create")
public void create(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) {
    propagateUserId(request);
    InternalDatasetCreationParams params = GSON.fromJson(request.getContent().toString(Charsets.UTF_8), InternalDatasetCreationParams.class);
    Preconditions.checkArgument(params.getProperties() != null, "Missing required 'instanceProps' parameter.");
    Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter.");
    DatasetProperties props = params.getProperties();
    DatasetTypeMeta typeMeta = params.getTypeMeta();
    try {
        DatasetId instanceId = new DatasetId(namespaceId, name);
        DatasetSpecification spec = datasetAdminService.createOrUpdate(instanceId, typeMeta, props, null);
        responder.sendJson(HttpResponseStatus.OK, spec);
    } catch (BadRequestException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Exception e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) BadRequestException(co.cask.cdap.common.BadRequestException) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetId(co.cask.cdap.proto.id.DatasetId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 2 with DatasetProperties

use of co.cask.cdap.api.dataset.DatasetProperties in project cdap by caskdata.

the class DatasetAdminOpHTTPHandler method update.

@POST
@Path("/data/datasets/{name}/admin/update")
public void update(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("name") String name) {
    propagateUserId(request);
    InternalDatasetUpdateParams params = GSON.fromJson(request.getContent().toString(Charsets.UTF_8), InternalDatasetUpdateParams.class);
    Preconditions.checkArgument(params.getProperties() != null, "Missing required 'instanceProps' parameter.");
    Preconditions.checkArgument(params.getTypeMeta() != null, "Missing required 'typeMeta' parameter.");
    Preconditions.checkArgument(params.getExistingSpec() != null, "Missing required 'existingSpec' parameter.");
    DatasetProperties props = params.getProperties();
    DatasetSpecification existing = params.getExistingSpec();
    DatasetTypeMeta typeMeta = params.getTypeMeta();
    try {
        DatasetId instanceId = new DatasetId(namespaceId, name);
        DatasetSpecification spec = datasetAdminService.createOrUpdate(instanceId, typeMeta, props, existing);
        responder.sendJson(HttpResponseStatus.OK, spec);
    } catch (NotFoundException e) {
        LOG.debug("Got handler exception", e);
        responder.sendString(HttpResponseStatus.NOT_FOUND, StringUtils.defaultIfEmpty(e.getMessage(), ""));
    } catch (BadRequestException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (IncompatibleUpdateException e) {
        responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
    } catch (Exception e) {
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, e.getMessage());
    }
}
Also used : DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetTypeMeta(co.cask.cdap.proto.DatasetTypeMeta) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) DatasetId(co.cask.cdap.proto.id.DatasetId) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 3 with DatasetProperties

use of co.cask.cdap.api.dataset.DatasetProperties in project cdap by caskdata.

the class HBaseQueueAdmin method createStateStoreDataset.

private DatasetId createStateStoreDataset(String namespace) throws IOException {
    try {
        DatasetId stateStoreId = getStateStoreId(namespace);
        DatasetProperties configProperties = TableProperties.builder().setColumnFamily(QueueEntryRow.COLUMN_FAMILY).build();
        DatasetsUtil.createIfNotExists(datasetFramework, stateStoreId, HBaseQueueDatasetModule.STATE_STORE_TYPE_NAME, configProperties);
        return stateStoreId;
    } catch (DatasetManagementException e) {
        throw new IOException(e);
    }
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) IOException(java.io.IOException) DatasetId(co.cask.cdap.proto.id.DatasetId)

Example 4 with DatasetProperties

use of co.cask.cdap.api.dataset.DatasetProperties in project cdap by caskdata.

the class TableTest method testMultiIncrementWithFlush.

private void testMultiIncrementWithFlush(boolean readless) throws Exception {
    final String tableName = "incrFlush";
    DatasetProperties props = TableProperties.builder().setReadlessIncrementSupport(readless).build();
    DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
    admin.create();
    Map<String, String> args = new HashMap<>();
    if (readless) {
        args.put(HBaseTable.SAFE_INCREMENTS, "true");
    }
    Table table = getTable(CONTEXT1, tableName, props, args);
    Transaction tx = txClient.startShort();
    try {
        ((TransactionAware) table).startTx(tx);
        // Write an increment, then flush it by calling commitTx.
        table.increment(new Increment(R1, C1, 10L));
        ((TransactionAware) table).commitTx();
    } finally {
        // invalidate the tx, leaving an excluded write in the table
        txClient.invalidate(tx.getTransactionId());
    }
    // validate the first write is not visible
    tx = txClient.startShort();
    try {
        ((TransactionAware) table).startTx(tx);
        Assert.assertEquals(null, table.get(new Get(R1, C1)).getLong(C1));
    } finally {
        txClient.commitOrThrow(tx);
    }
    tx = txClient.startShort();
    try {
        ((TransactionAware) table).startTx(tx);
        // Write an increment, then flush it by calling commitTx.
        table.increment(new Increment(R1, C1, 1L));
        ((TransactionAware) table).commitTx();
        // Write another increment, from both table instances
        table.increment(new Increment(R1, C1, 1L));
        if (readless) {
            Table table2 = getTable(CONTEXT1, tableName, props, args);
            ((TransactionAware) table2).startTx(tx);
            table2.increment(new Increment(R1, C1, 1L));
            ((TransactionAware) table2).commitTx();
        }
        ((TransactionAware) table).commitTx();
    } finally {
        txClient.commitOrThrow(tx);
    }
    // validate all increments are visible to a new tx
    tx = txClient.startShort();
    try {
        ((TransactionAware) table).startTx(tx);
        Assert.assertEquals(new Long(readless ? 3L : 2L), table.get(new Get(R1, C1)).getLong(C1));
    } finally {
        txClient.commitOrThrow(tx);
    }
    // drop table
    admin.drop();
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) HashMap(java.util.HashMap) TransactionAware(org.apache.tephra.TransactionAware) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) Increment(co.cask.cdap.api.dataset.table.Increment) Get(co.cask.cdap.api.dataset.table.Get) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin)

Example 5 with DatasetProperties

use of co.cask.cdap.api.dataset.DatasetProperties in project cdap by caskdata.

the class TableTest method testConflictDetection.

private void testConflictDetection(ConflictDetection level) throws Exception {
    // we use tableX_Y format for variable names which means "tableX that is used in tx Y"
    String table1 = "table1";
    String table2 = "table2";
    DatasetProperties props = TableProperties.builder().setConflictDetection(level).build();
    DatasetAdmin admin1 = getTableAdmin(CONTEXT1, table1, props);
    DatasetAdmin admin2 = getTableAdmin(CONTEXT1, table2, props);
    admin1.create();
    admin2.create();
    try {
        // 1) Test conflicts when using different tables
        Transaction tx1 = txClient.startShort();
        Table table11 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table11).startTx(tx1);
        // write r1->c1,v1 but not commit
        table11.put(R1, a(C1), a(V1));
        // start new tx
        Transaction tx2 = txClient.startShort();
        Table table22 = getTable(CONTEXT1, table2, props);
        ((TransactionAware) table22).startTx(tx2);
        // change in tx2 same data but in different table
        table22.put(R1, a(C1), a(V2));
        // start new tx
        Transaction tx3 = txClient.startShort();
        Table table13 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table13).startTx(tx3);
        // change in tx3 same data in same table as tx1
        table13.put(R1, a(C1), a(V2));
        // committing tx1
        txClient.canCommitOrThrow(tx1, ((TransactionAware) table11).getTxChanges());
        Assert.assertTrue(((TransactionAware) table11).commitTx());
        txClient.commitOrThrow(tx1);
        // no conflict should be when committing tx2
        txClient.canCommitOrThrow(tx2, ((TransactionAware) table22).getTxChanges());
        // but conflict should be when committing tx3
        if (level != ConflictDetection.NONE) {
            try {
                txClient.canCommitOrThrow(tx3, ((TransactionAware) table13).getTxChanges());
                Assert.fail("Conflict not detected!");
            } catch (TransactionConflictException e) {
            // expected
            }
            ((TransactionAware) table13).rollbackTx();
            txClient.abort(tx3);
        } else {
            txClient.canCommitOrThrow(tx3, ((TransactionAware) table13).getTxChanges());
        }
        // 2) Test conflicts when using different rows
        Transaction tx4 = txClient.startShort();
        Table table14 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table14).startTx(tx4);
        // write r1->c1,v1 but not commit
        table14.put(R1, a(C1), a(V1));
        // start new tx
        Transaction tx5 = txClient.startShort();
        Table table15 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table15).startTx(tx5);
        // change in tx5 same data but in different row
        table15.put(R2, a(C1), a(V2));
        // start new tx
        Transaction tx6 = txClient.startShort();
        Table table16 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table16).startTx(tx6);
        // change in tx6 in same row as tx1
        table16.put(R1, a(C2), a(V2));
        // committing tx4
        txClient.canCommitOrThrow(tx4, ((TransactionAware) table14).getTxChanges());
        Assert.assertTrue(((TransactionAware) table14).commitTx());
        txClient.commitOrThrow(tx4);
        // no conflict should be when committing tx5
        txClient.canCommitOrThrow(tx5, ((TransactionAware) table15).getTxChanges());
        // but conflict should be when committing tx6 iff we resolve on row level
        if (level == ConflictDetection.ROW) {
            try {
                txClient.canCommitOrThrow(tx6, ((TransactionAware) table16).getTxChanges());
                Assert.fail("Conflict not detected!");
            } catch (TransactionConflictException e) {
            // expected
            }
            ((TransactionAware) table16).rollbackTx();
            txClient.abort(tx6);
        } else {
            txClient.canCommitOrThrow(tx6, ((TransactionAware) table16).getTxChanges());
        }
        // 3) Test conflicts when using different columns
        Transaction tx7 = txClient.startShort();
        Table table17 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table17).startTx(tx7);
        // write r1->c1,v1 but not commit
        table17.put(R1, a(C1), a(V1));
        // start new tx
        Transaction tx8 = txClient.startShort();
        Table table18 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table18).startTx(tx8);
        // change in tx8 same data but in different column
        table18.put(R1, a(C2), a(V2));
        // start new tx
        Transaction tx9 = txClient.startShort();
        Table table19 = getTable(CONTEXT1, table1, props);
        ((TransactionAware) table19).startTx(tx9);
        // change in tx9 same column in same column as tx1
        table19.put(R1, a(C1), a(V2));
        // committing tx7
        txClient.canCommitOrThrow(tx7, ((TransactionAware) table17).getTxChanges());
        Assert.assertTrue(((TransactionAware) table17).commitTx());
        txClient.commitOrThrow(tx7);
        // no conflict should be when committing tx8 iff we resolve on column level
        if (level == ConflictDetection.COLUMN || level == ConflictDetection.NONE) {
            txClient.canCommitOrThrow(tx8, ((TransactionAware) table18).getTxChanges());
        } else {
            try {
                txClient.canCommitOrThrow(tx8, ((TransactionAware) table18).getTxChanges());
                Assert.fail("Conflict not detected!");
            } catch (TransactionConflictException e) {
            // expected
            }
            ((TransactionAware) table18).rollbackTx();
            txClient.abort(tx8);
        }
        // but conflict should be when committing tx9
        if (level != ConflictDetection.NONE) {
            try {
                txClient.canCommitOrThrow(tx9, ((TransactionAware) table19).getTxChanges());
                Assert.fail("Conflict not detected!");
            } catch (TransactionConflictException e) {
            // expected
            }
            ((TransactionAware) table19).rollbackTx();
            txClient.abort(tx9);
        } else {
            txClient.canCommitOrThrow(tx9, ((TransactionAware) table19).getTxChanges());
        }
    } finally {
        // NOTE: we are doing our best to cleanup junk between tests to isolate errors, but we are not going to be
        // crazy about it
        admin1.drop();
        admin2.drop();
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) DatasetProperties(co.cask.cdap.api.dataset.DatasetProperties) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) TransactionConflictException(org.apache.tephra.TransactionConflictException)

Aggregations

DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)38 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)17 Test (org.junit.Test)12 Transaction (org.apache.tephra.Transaction)11 DatasetId (co.cask.cdap.proto.id.DatasetId)10 IncompatibleUpdateException (co.cask.cdap.api.dataset.IncompatibleUpdateException)9 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)8 Table (co.cask.cdap.api.dataset.table.Table)7 BufferingTableTest (co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest)6 TransactionAware (org.apache.tephra.TransactionAware)6 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)5 DatasetTypeMeta (co.cask.cdap.proto.DatasetTypeMeta)5 IOException (java.io.IOException)5 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)4 Reconfigurable (co.cask.cdap.api.dataset.Reconfigurable)4 Get (co.cask.cdap.api.dataset.table.Get)4 Scan (co.cask.cdap.api.dataset.table.Scan)4 Scanner (co.cask.cdap.api.dataset.table.Scanner)4 DetachedTxSystemClient (org.apache.tephra.inmemory.DetachedTxSystemClient)4 BadRequestException (co.cask.cdap.common.BadRequestException)3