Search in sources :

Example 51 with Transaction

use of org.apache.tephra.Transaction in project phoenix by apache.

the class IndexMetaDataCacheFactory method newCache.

@Override
public Closeable newCache(ImmutableBytesWritable cachePtr, byte[] txState, final MemoryChunk chunk, boolean useProtoForIndexMaintainer) throws SQLException {
    // just use the standard keyvalue builder - this doesn't really need to be fast
    final List<IndexMaintainer> maintainers = IndexMaintainer.deserialize(cachePtr, GenericKeyValueBuilder.INSTANCE, useProtoForIndexMaintainer);
    final Transaction txn;
    try {
        txn = txState.length != 0 ? MutationState.decodeTransaction(txState) : null;
    } catch (IOException e) {
        throw new SQLException(e);
    }
    return new IndexMetaDataCache() {

        @Override
        public void close() throws IOException {
            chunk.close();
        }

        @Override
        public List<IndexMaintainer> getIndexMaintainers() {
            return maintainers;
        }

        @Override
        public Transaction getTransaction() {
            return txn;
        }
    };
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) Transaction(org.apache.tephra.Transaction) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 52 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

the class ConsumerConfigCache method updateCache.

/**
 * This forces an immediate update of the config cache. It should only be called from the refresh thread or from
 * tests, to avoid having to add a sleep for the duration of the refresh interval.
 *
 * This method is synchronized to protect from race conditions if called directly from a test. Otherwise this is
 * only called from the refresh thread, and there will not be concurrent invocations.
 *
 * @throws IOException if failed to update config cache
 */
@VisibleForTesting
public synchronized void updateCache() throws IOException {
    Map<byte[], QueueConsumerConfig> newCache = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    long now = System.currentTimeMillis();
    TransactionVisibilityState txSnapshot = transactionSnapshotSupplier.get();
    if (txSnapshot == null) {
        LOG.debug("No transaction snapshot is available. Not updating the consumer config cache.");
        return;
    }
    HTableInterface table = hTableSupplier.getInput();
    try {
        // Scan the table with the transaction snapshot
        Scan scan = new Scan();
        scan.addFamily(QueueEntryRow.COLUMN_FAMILY);
        Transaction tx = TxUtils.createDummyTransaction(txSnapshot);
        setScanAttribute(scan, TxConstants.TX_OPERATION_ATTRIBUTE_KEY, txCodec.encode(tx));
        ResultScanner scanner = table.getScanner(scan);
        int configCnt = 0;
        for (Result result : scanner) {
            if (!result.isEmpty()) {
                NavigableMap<byte[], byte[]> familyMap = result.getFamilyMap(QueueEntryRow.COLUMN_FAMILY);
                if (familyMap != null) {
                    configCnt++;
                    Map<ConsumerInstance, byte[]> consumerInstances = new HashMap<>();
                    // Gather the startRow of all instances across all consumer groups.
                    int numGroups = 0;
                    Long groupId = null;
                    for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
                        if (entry.getKey().length != STATE_COLUMN_SIZE) {
                            continue;
                        }
                        long gid = Bytes.toLong(entry.getKey());
                        int instanceId = Bytes.toInt(entry.getKey(), Bytes.SIZEOF_LONG);
                        consumerInstances.put(new ConsumerInstance(gid, instanceId), entry.getValue());
                        // Columns are sorted by groupId, hence if it change, then numGroups would get +1
                        if (groupId == null || groupId != gid) {
                            numGroups++;
                            groupId = gid;
                        }
                    }
                    byte[] queueName = result.getRow();
                    newCache.put(queueName, new QueueConsumerConfig(consumerInstances, numGroups));
                }
            }
        }
        long elapsed = System.currentTimeMillis() - now;
        this.configCache = newCache;
        this.lastUpdated = now;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated consumer config cache with {} entries, took {} msec", configCnt, elapsed);
        }
    } finally {
        try {
            table.close();
        } catch (IOException ioe) {
            LOG.error("Error closing table {}", queueConfigTableName, ioe);
        }
    }
}
Also used : ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) HashMap(java.util.HashMap) IOException(java.io.IOException) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) Result(org.apache.hadoop.hbase.client.Result) Transaction(org.apache.tephra.Transaction) TransactionVisibilityState(org.apache.tephra.persist.TransactionVisibilityState) Scan(org.apache.hadoop.hbase.client.Scan) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(java.util.NavigableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 53 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

the class AbstractQueueProducer method commitTx.

@Override
public boolean commitTx() throws Exception {
    Preconditions.checkState(transaction != null, "Commit without starting transaction.");
    Transaction tx = transaction;
    transaction = null;
    List<QueueEntry> entries = Lists.newArrayListWithCapacity(queue.size());
    queue.drainTo(entries);
    lastEnqueueCount = entries.size();
    lastEnqueueBytes = persist(entries, tx);
    return true;
}
Also used : Transaction(org.apache.tephra.Transaction) QueueEntry(co.cask.cdap.data2.queue.QueueEntry)

Example 54 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

the class AbstractQueueProducer method rollbackTx.

@Override
public boolean rollbackTx() throws Exception {
    Transaction tx = transaction;
    transaction = null;
    doRollback();
    return true;
}
Also used : Transaction(org.apache.tephra.Transaction)

Example 55 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

the class DequeueScanObserver method preScannerOpen.

@Override
public RegionScanner preScannerOpen(ObserverContext<RegionCoprocessorEnvironment> e, Scan scan, RegionScanner s) throws IOException {
    ConsumerConfig consumerConfig = DequeueScanAttributes.getConsumerConfig(scan);
    Transaction tx = DequeueScanAttributes.getTx(scan);
    if (consumerConfig == null || tx == null) {
        return super.preScannerOpen(e, scan, s);
    }
    Filter dequeueFilter = new DequeueFilter(consumerConfig, tx);
    Filter existing = scan.getFilter();
    if (existing != null) {
        Filter combined = new FilterList(FilterList.Operator.MUST_PASS_ALL, existing, dequeueFilter);
        scan.setFilter(combined);
    } else {
        scan.setFilter(dequeueFilter);
    }
    return super.preScannerOpen(e, scan, s);
}
Also used : Transaction(org.apache.tephra.Transaction) Filter(org.apache.hadoop.hbase.filter.Filter) ConsumerConfig(co.cask.cdap.data2.queue.ConsumerConfig) FilterList(org.apache.hadoop.hbase.filter.FilterList)

Aggregations

Transaction (org.apache.tephra.Transaction)99 Test (org.junit.Test)54 TransactionAware (org.apache.tephra.TransactionAware)34 Table (co.cask.cdap.api.dataset.table.Table)29 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)27 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)22 Put (co.cask.cdap.api.dataset.table.Put)12 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)11 Get (co.cask.cdap.api.dataset.table.Get)10 TransactionSystemClient (org.apache.tephra.TransactionSystemClient)10 Row (co.cask.cdap.api.dataset.table.Row)8 ConsumerConfig (co.cask.cdap.data2.queue.ConsumerConfig)8 KeyStructValueTableDefinition (co.cask.cdap.explore.service.datasets.KeyStructValueTableDefinition)8 Scan (co.cask.cdap.api.dataset.table.Scan)7 ArrayList (java.util.ArrayList)7 CConfiguration (co.cask.cdap.common.conf.CConfiguration)6 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)6 DatasetId (co.cask.cdap.proto.id.DatasetId)6 IOException (java.io.IOException)6 BufferingTableTest (co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest)5