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;
}
};
}
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);
}
}
}
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;
}
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;
}
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);
}
Aggregations