Search in sources :

Example 1 with TxCallable

use of co.cask.cdap.data2.transaction.TxCallable in project cdap by caskdata.

the class DefaultCheckpointManager method saveCheckpoints.

@Override
public void saveCheckpoints(final Map<Integer, ? extends Checkpoint> checkpoints) throws Exception {
    // if the checkpoints have not changed, we skip writing to table and return.
    if (lastCheckpoint.equals(checkpoints)) {
        return;
    }
    try {
        lastCheckpoint = Transactions.execute(transactional, new TxCallable<Map<Integer, Checkpoint>>() {

            @Override
            public Map<Integer, Checkpoint> call(DatasetContext context) throws Exception {
                Map<Integer, Checkpoint> result = new HashMap<>();
                Table table = getCheckpointTable(context);
                for (Map.Entry<Integer, ? extends Checkpoint> entry : checkpoints.entrySet()) {
                    byte[] key = Bytes.add(rowKeyPrefix, Bytes.toBytes(entry.getKey()));
                    Checkpoint checkpoint = entry.getValue();
                    table.put(key, OFFSET_COL_NAME, Bytes.toBytes(checkpoint.getNextOffset()));
                    table.put(key, NEXT_TIME_COL_NAME, Bytes.toBytes(checkpoint.getNextEventTime()));
                    table.put(key, MAX_TIME_COL_NAME, Bytes.toBytes(checkpoint.getMaxEventTime()));
                    result.put(entry.getKey(), new Checkpoint(checkpoint.getNextOffset(), checkpoint.getNextEventTime(), checkpoint.getMaxEventTime()));
                }
                return result;
            }
        });
    } catch (TransactionFailureException e) {
        throw Transactions.propagate(e, ServiceUnavailableException.class);
    }
    LOG.trace("Saved checkpoints for partitions {}", checkpoints);
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) Table(co.cask.cdap.api.dataset.table.Table) HashMap(java.util.HashMap) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DatasetContext(co.cask.cdap.api.data.DatasetContext) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) TxCallable(co.cask.cdap.data2.transaction.TxCallable)

Aggregations

DatasetContext (co.cask.cdap.api.data.DatasetContext)1 Table (co.cask.cdap.api.dataset.table.Table)1 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)1 TxCallable (co.cask.cdap.data2.transaction.TxCallable)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TransactionFailureException (org.apache.tephra.TransactionFailureException)1