Search in sources :

Example 1 with ActiveTxnRecord

use of io.pravega.controller.store.stream.tables.ActiveTxnRecord in project pravega by pravega.

the class InMemoryStream method sealActiveTx.

@Override
CompletableFuture<Void> sealActiveTx(int epoch, UUID txId, boolean commit, ActiveTxnRecord txnRecord, int version) {
    Preconditions.checkNotNull(txId);
    CompletableFuture<Void> result = new CompletableFuture<>();
    synchronized (txnsLock) {
        if (!activeTxns.containsKey(txId.toString())) {
            result.completeExceptionally(StoreException.create(StoreException.Type.DATA_NOT_FOUND, "Stream: " + getName() + " Transaction: " + txId.toString()));
        } else {
            activeTxns.compute(txId.toString(), (x, y) -> {
                if (version != y.getVersion()) {
                    result.completeExceptionally(StoreException.create(StoreException.Type.WRITE_CONFLICT, "Stream: " + getName() + " Transaction: " + txId.toString()));
                    return y;
                } else {
                    ActiveTxnRecord previous = ActiveTxnRecord.parse(y.getData());
                    ActiveTxnRecord updated = new ActiveTxnRecord(previous.getTxCreationTimestamp(), previous.getLeaseExpiryTime(), previous.getMaxExecutionExpiryTime(), previous.getScaleGracePeriod(), commit ? TxnStatus.COMMITTING : TxnStatus.ABORTING);
                    result.complete(null);
                    return new Data<>(updated.toByteArray(), y.getVersion() + 1);
                }
            });
        }
    }
    return result;
}
Also used : ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord) CompletableFuture(java.util.concurrent.CompletableFuture) Data(io.pravega.controller.store.stream.tables.Data)

Example 2 with ActiveTxnRecord

use of io.pravega.controller.store.stream.tables.ActiveTxnRecord in project pravega by pravega.

the class PersistentStreamBase method pingTransaction.

@Override
public CompletableFuture<VersionedTransactionData> pingTransaction(final VersionedTransactionData txnData, final long lease) {
    // Update txn record with new lease value and return versioned tx data.
    final int epoch = txnData.getEpoch();
    final UUID txnId = txnData.getId();
    final int version = txnData.getVersion();
    final long creationTime = txnData.getCreationTime();
    final long maxExecutionExpiryTime = txnData.getMaxExecutionExpiryTime();
    final long scaleGracePeriod = txnData.getScaleGracePeriod();
    final TxnStatus status = txnData.getStatus();
    final ActiveTxnRecord newData = new ActiveTxnRecord(creationTime, System.currentTimeMillis() + lease, maxExecutionExpiryTime, scaleGracePeriod, status);
    final Data<Integer> data = new Data<>(newData.toByteArray(), version);
    return updateActiveTx(epoch, txnId, data).thenApply(x -> new VersionedTransactionData(epoch, txnId, version + 1, status, creationTime, maxExecutionExpiryTime, scaleGracePeriod));
}
Also used : ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord) Data(io.pravega.controller.store.stream.tables.Data) UUID(java.util.UUID)

Example 3 with ActiveTxnRecord

use of io.pravega.controller.store.stream.tables.ActiveTxnRecord in project pravega by pravega.

the class ZKStream method sealActiveTx.

@Override
CompletableFuture<Void> sealActiveTx(final int epoch, final UUID txId, final boolean commit, final ActiveTxnRecord previous, final int version) {
    final String activePath = getActiveTxPath(epoch, txId.toString());
    final ActiveTxnRecord updated = new ActiveTxnRecord(previous.getTxCreationTimestamp(), previous.getLeaseExpiryTime(), previous.getMaxExecutionExpiryTime(), previous.getScaleGracePeriod(), commit ? TxnStatus.COMMITTING : TxnStatus.ABORTING);
    final Data<Integer> data = new Data<>(updated.toByteArray(), version);
    return store.setData(activePath, data).thenApply(x -> cache.invalidateCache(activePath)).whenComplete((r, e) -> cache.invalidateCache(activePath));
}
Also used : ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord) CompletedTxnRecord(io.pravega.controller.store.stream.tables.CompletedTxnRecord) Getter(lombok.Getter) Exceptions(io.pravega.common.Exceptions) StreamTruncationRecord(io.pravega.controller.store.stream.tables.StreamTruncationRecord) Cache(io.pravega.controller.store.stream.tables.Cache) SerializationUtils(org.apache.commons.lang3.SerializationUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) State(io.pravega.controller.store.stream.tables.State) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) BitConverter(io.pravega.common.util.BitConverter) Collectors(java.util.stream.Collectors) ZKPaths(org.apache.curator.utils.ZKPaths) List(java.util.List) AccessLevel(lombok.AccessLevel) ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord) Data(io.pravega.controller.store.stream.tables.Data) Map(java.util.Map) Optional(java.util.Optional) TableHelper(io.pravega.controller.store.stream.tables.TableHelper) Futures(io.pravega.common.concurrent.Futures) Data(io.pravega.controller.store.stream.tables.Data)

Example 4 with ActiveTxnRecord

use of io.pravega.controller.store.stream.tables.ActiveTxnRecord in project pravega by pravega.

the class ZKStream method createNewTransactionNode.

private CompletableFuture<Integer> createNewTransactionNode(final UUID txId, final long timestamp, final long leaseExpiryTime, final long maxExecutionExpiryTime, final long scaleGracePeriod) {
    return getLatestEpoch().thenCompose(pair -> {
        final String activePath = getActiveTxPath(pair.getKey(), txId.toString());
        final byte[] txnRecord = new ActiveTxnRecord(timestamp, leaseExpiryTime, maxExecutionExpiryTime, scaleGracePeriod, TxnStatus.OPEN).toByteArray();
        return store.createZNodeIfNotExist(activePath, txnRecord, false).thenApply(x -> cache.invalidateCache(activePath)).thenApply(y -> pair.getKey());
    });
}
Also used : ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord) CompletedTxnRecord(io.pravega.controller.store.stream.tables.CompletedTxnRecord) Getter(lombok.Getter) Exceptions(io.pravega.common.Exceptions) StreamTruncationRecord(io.pravega.controller.store.stream.tables.StreamTruncationRecord) Cache(io.pravega.controller.store.stream.tables.Cache) SerializationUtils(org.apache.commons.lang3.SerializationUtils) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) State(io.pravega.controller.store.stream.tables.State) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) BitConverter(io.pravega.common.util.BitConverter) Collectors(java.util.stream.Collectors) ZKPaths(org.apache.curator.utils.ZKPaths) List(java.util.List) AccessLevel(lombok.AccessLevel) ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord) Data(io.pravega.controller.store.stream.tables.Data) Map(java.util.Map) Optional(java.util.Optional) TableHelper(io.pravega.controller.store.stream.tables.TableHelper) Futures(io.pravega.common.concurrent.Futures)

Example 5 with ActiveTxnRecord

use of io.pravega.controller.store.stream.tables.ActiveTxnRecord in project pravega by pravega.

the class PersistentStreamBase method sealActiveTxn.

/**
 * Seal a transaction in OPEN/COMMITTING/ABORTING state. This method does CAS on the transaction data node if
 * the transaction is in OPEN state, optionally checking version of transaction data node, if required.
 *
 * @param epoch   transaction epoch.
 * @param txId    transaction identifier.
 * @param commit  boolean indicating whether to commit or abort the transaction.
 * @param version optional expected version of transaction node to validate before updating it.
 * @return        a pair containing transaction status and its epoch.
 */
private CompletableFuture<SimpleEntry<TxnStatus, Integer>> sealActiveTxn(final int epoch, final UUID txId, final boolean commit, final Optional<Integer> version) {
    return getActiveTx(epoch, txId).thenCompose(data -> {
        ActiveTxnRecord txnRecord = ActiveTxnRecord.parse(data.getData());
        int dataVersion = version.isPresent() ? version.get() : data.getVersion();
        TxnStatus status = txnRecord.getTxnStatus();
        switch(status) {
            case OPEN:
                return sealActiveTx(epoch, txId, commit, txnRecord, dataVersion).thenApply(y -> new SimpleEntry<>(commit ? TxnStatus.COMMITTING : TxnStatus.ABORTING, epoch));
            case COMMITTING:
            case COMMITTED:
                if (commit) {
                    return CompletableFuture.completedFuture(new SimpleEntry<>(status, epoch));
                } else {
                    throw StoreException.create(StoreException.Type.ILLEGAL_STATE, "Stream: " + getName() + " Transaction: " + txId.toString() + " State: " + status.name());
                }
            case ABORTING:
            case ABORTED:
                if (commit) {
                    throw StoreException.create(StoreException.Type.ILLEGAL_STATE, "Stream: " + getName() + " Transaction: " + txId.toString() + " State: " + status.name());
                } else {
                    return CompletableFuture.completedFuture(new SimpleEntry<>(status, epoch));
                }
            default:
                throw StoreException.create(StoreException.Type.DATA_NOT_FOUND, "Stream: " + getName() + " Transaction: " + txId.toString());
        }
    });
}
Also used : ActiveTxnRecord(io.pravega.controller.store.stream.tables.ActiveTxnRecord)

Aggregations

ActiveTxnRecord (io.pravega.controller.store.stream.tables.ActiveTxnRecord)5 Data (io.pravega.controller.store.stream.tables.Data)4 UUID (java.util.UUID)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)2 Exceptions (io.pravega.common.Exceptions)2 Futures (io.pravega.common.concurrent.Futures)2 BitConverter (io.pravega.common.util.BitConverter)2 Cache (io.pravega.controller.store.stream.tables.Cache)2 CompletedTxnRecord (io.pravega.controller.store.stream.tables.CompletedTxnRecord)2 State (io.pravega.controller.store.stream.tables.State)2 StreamTruncationRecord (io.pravega.controller.store.stream.tables.StreamTruncationRecord)2 TableHelper (io.pravega.controller.store.stream.tables.TableHelper)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 AccessLevel (lombok.AccessLevel)2 Getter (lombok.Getter)2