Search in sources :

Example 21 with DataSetException

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

the class BufferingTable method delete.

/**
   * NOTE: Depending on the use-case, calling this method may be much less efficient than calling same method
   *       with columns as parameters because it will require a round trip to persistent store.
   */
@WriteOnly
@Override
public void delete(byte[] row) {
    ensureTransactionIsStarted();
    // this is going to be expensive, but the only we can do as delete implementation act on per-column level
    try {
        Map<byte[], byte[]> rowMap = getRowMap(row);
        delete(row, rowMap.keySet().toArray(new byte[rowMap.keySet().size()][]));
        // "0" because we don't know what gets deleted
        reportWrite(1, 0);
    } catch (Exception e) {
        LOG.debug("delete failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("delete failed", e);
    }
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException) WriteOnly(co.cask.cdap.api.annotation.WriteOnly)

Example 22 with DataSetException

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

the class BufferingTable method compareAndSwap.

@ReadWrite
@Override
public boolean compareAndSwap(byte[] row, byte[] column, byte[] expectedValue, byte[] newValue) {
    ensureTransactionIsStarted();
    // TODO: add support for empty values; see https://issues.cask.co/browse/TEPHRA-45 for details.
    if (newValue != null && newValue.length == 0) {
        warnAboutEmptyValue(column);
    }
    // NOTE: there is more efficient way to do it, but for now we want more simple implementation, not over-optimizing
    byte[][] columns = new byte[][] { column };
    try {
        byte[] currentValue = getRowMap(row, columns).get(column);
        reportRead(1);
        if (Arrays.equals(expectedValue, currentValue)) {
            putInternal(row, columns, new byte[][] { newValue });
            reportWrite(1, getSize(row) + getSize(column) + getSize(newValue));
            return true;
        }
    } catch (Exception e) {
        LOG.debug("compareAndSwap failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("compareAndSwap failed", e);
    }
    return false;
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException) ReadWrite(co.cask.cdap.api.annotation.ReadWrite)

Example 23 with DataSetException

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

the class BufferingTable method get.

@ReadOnly
@Override
public Row get(byte[] row, byte[][] columns) {
    ensureTransactionIsStarted();
    reportRead(1);
    try {
        return new Result(row, getRowMap(row, columns));
    } catch (Exception e) {
        LOG.debug("get failed for table: " + getTransactionAwareName() + ", row: " + Bytes.toStringBinary(row), e);
        throw new DataSetException("get failed", e);
    }
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException) Result(co.cask.cdap.api.dataset.table.Result) ReadOnly(co.cask.cdap.api.annotation.ReadOnly)

Example 24 with DataSetException

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

the class AppWithCustomTx method recordTransaction.

/**
   * If in a transaction, records the timeout that the current transaction was given, or "default" if no explicit
   * timeout was given. Otherwise does nothing.
   *
   * Note: we know whether and what explicit timeout was given, because we inject a {@link RevealingTxSystemClient},
   *       which returns a {@link RevealingTransaction} for {@link TransactionSystemClient#startShort(int)} only.
   */
static void recordTransaction(DatasetContext context, String row, String column) {
    TransactionCapturingTable capture = context.getDataset(CAPTURE);
    Transaction tx = capture.getTx();
    // we cannot cast because the RevealingTransaction is not visible in the program class loader
    String value = DEFAULT;
    if (tx == null) {
        try {
            capture.getTable().put(new Put(row, column, value));
            throw new RuntimeException("put to table without transaction should have failed.");
        } catch (DataSetException e) {
        // expected
        }
        return;
    }
    if ("RevealingTransaction".equals(tx.getClass().getSimpleName())) {
        int txTimeout;
        try {
            txTimeout = (int) tx.getClass().getField("timeout").get(tx);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
        value = String.valueOf(txTimeout);
    }
    capture.getTable().put(new Put(row, column, value));
}
Also used : Transaction(org.apache.tephra.Transaction) RevealingTransaction(co.cask.cdap.test.RevealingTxSystemClient.RevealingTransaction) DataSetException(co.cask.cdap.api.dataset.DataSetException) Put(co.cask.cdap.api.dataset.table.Put) TransactionFailureException(org.apache.tephra.TransactionFailureException) IOException(java.io.IOException) DataSetException(co.cask.cdap.api.dataset.DataSetException)

Example 25 with DataSetException

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

the class ObjectStoreDataset method decode.

private T decode(byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    // decode T using schema
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    BinaryDecoder decoder = new BinaryDecoder(bis);
    try {
        return getReflectionDatumReader().read(decoder, this.schema);
    } catch (IOException e) {
        // SHOULD NEVER happen
        throw new DataSetException("Failed to decode read object: " + e.getMessage(), e);
    }
}
Also used : DataSetException(co.cask.cdap.api.dataset.DataSetException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Aggregations

DataSetException (co.cask.cdap.api.dataset.DataSetException)35 IOException (java.io.IOException)26 Map (java.util.Map)8 ReadOnly (co.cask.cdap.api.annotation.ReadOnly)6 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)5 Result (co.cask.cdap.api.dataset.table.Result)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 TransactionFailureException (org.apache.tephra.TransactionFailureException)5 WriteOnly (co.cask.cdap.api.annotation.WriteOnly)4 TimePartitionedFileSet (co.cask.cdap.api.dataset.lib.TimePartitionedFileSet)4 Put (co.cask.cdap.api.dataset.table.Put)4 HashMap (java.util.HashMap)4 NavigableMap (java.util.NavigableMap)4 Location (org.apache.twill.filesystem.Location)4 Test (org.junit.Test)4 PartitionNotFoundException (co.cask.cdap.api.dataset.PartitionNotFoundException)3 Row (co.cask.cdap.api.dataset.table.Row)3 DatasetId (co.cask.cdap.proto.id.DatasetId)3 Put (org.apache.hadoop.hbase.client.Put)3 TransactionAware (org.apache.tephra.TransactionAware)3