Search in sources :

Example 11 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method txnPollReserve.

// TX Poll
/**
 * Tries to obtain an item by removing the head of the
 * queue or removing an item previously reserved by invoking
 * {@link #txnOfferReserve(UUID)} with {@code reservedOfferId}.
 * <p>
 * If the queue item does not have data in-memory it will load the
 * data from the queue store if the queue store is configured and enabled.
 *
 * @param reservedOfferId the ID of the reserved item to be returned if the queue is empty
 * @param transactionId   the transaction ID for which this poll is invoked
 * @return the head of the queue or a reserved item with the {@code reservedOfferId} if there is any
 */
public QueueItem txnPollReserve(long reservedOfferId, UUID transactionId) {
    QueueItem item = getItemQueue().peek();
    if (item == null) {
        TxQueueItem txItem = txMap.remove(reservedOfferId);
        if (txItem == null) {
            return null;
        }
        item = new QueueItem(this, txItem.getItemId(), txItem.getSerializedObject());
        return item;
    }
    if (store.isEnabled() && item.getSerializedObject() == null) {
        try {
            load(item);
        } catch (Exception e) {
            throw new HazelcastException(e);
        }
    }
    getItemQueue().poll();
    txMap.put(item.getItemId(), new TxQueueItem(item).setPollOperation(true).setTransactionId(transactionId));
    return item;
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TransactionException(com.hazelcast.transaction.TransactionException) HazelcastException(com.hazelcast.core.HazelcastException) IOException(java.io.IOException)

Example 12 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method txnRollbackPoll.

/**
 * Rolls back the effects of the {@link #txnPollReserve(long, UUID)}.
 * The {@code backup} parameter defines whether this item was stored
 * on a backup queue or a primary queue.
 * It will return the item to the queue or backup map if it wasn't
 * offered as a part of the transaction.
 * Cancels the queue eviction if one is scheduled.
 *
 * @param itemId the ID of the item which was polled in a transaction
 * @param backup if this is the primary or the backup replica for this queue
 * @return if there was any polled item with the {@code itemId} inside a transaction
 */
public boolean txnRollbackPoll(long itemId, boolean backup) {
    TxQueueItem item = txMap.remove(itemId);
    if (item == null) {
        return false;
    }
    if (backup) {
        getBackupMap().put(itemId, item);
    } else {
        addTxItemOrdered(item);
    }
    cancelEvictionIfExists();
    return true;
}
Also used : TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem)

Example 13 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method writeData.

@Override
public void writeData(ObjectDataOutput out) throws IOException {
    out.writeString(name);
    out.writeInt(getItemQueue().size());
    for (QueueItem item : getItemQueue()) {
        out.writeObject(item);
    }
    out.writeInt(txMap.size());
    for (TxQueueItem item : txMap.values()) {
        item.writeData(out);
    }
}
Also used : TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem) TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem)

Example 14 with TxQueueItem

use of com.hazelcast.collection.impl.txnqueue.TxQueueItem in project hazelcast by hazelcast.

the class QueueContainer method getItemQueue.

/**
 * Returns the item queue on the partition owner. This method
 * will also move the items from the backup map if this
 * member has been promoted from a backup replica to the
 * partition owner and clear the backup map.
 *
 * @return the item queue
 */
public Queue<QueueItem> getItemQueue() {
    if (itemQueue == null) {
        itemQueue = isPriorityQueue ? createPriorityQueue() : createLinkedList();
        if (!txMap.isEmpty()) {
            long maxItemId = Long.MIN_VALUE;
            for (TxQueueItem item : txMap.values()) {
                maxItemId = Math.max(maxItemId, item.itemId);
            }
            setId(maxItemId + ID_PROMOTION_OFFSET);
        }
    }
    return itemQueue;
}
Also used : TxQueueItem(com.hazelcast.collection.impl.txnqueue.TxQueueItem)

Aggregations

TxQueueItem (com.hazelcast.collection.impl.txnqueue.TxQueueItem)14 HazelcastException (com.hazelcast.core.HazelcastException)4 TransactionException (com.hazelcast.transaction.TransactionException)4 IOException (java.io.IOException)4 AddAllBackupOperation (com.hazelcast.collection.impl.queue.operations.AddAllBackupOperation)1 AddAllOperation (com.hazelcast.collection.impl.queue.operations.AddAllOperation)1 CheckAndEvictOperation (com.hazelcast.collection.impl.queue.operations.CheckAndEvictOperation)1 ClearBackupOperation (com.hazelcast.collection.impl.queue.operations.ClearBackupOperation)1 ClearOperation (com.hazelcast.collection.impl.queue.operations.ClearOperation)1 CompareAndRemoveBackupOperation (com.hazelcast.collection.impl.queue.operations.CompareAndRemoveBackupOperation)1 CompareAndRemoveOperation (com.hazelcast.collection.impl.queue.operations.CompareAndRemoveOperation)1 ContainsOperation (com.hazelcast.collection.impl.queue.operations.ContainsOperation)1 DrainBackupOperation (com.hazelcast.collection.impl.queue.operations.DrainBackupOperation)1 DrainOperation (com.hazelcast.collection.impl.queue.operations.DrainOperation)1 IsEmptyOperation (com.hazelcast.collection.impl.queue.operations.IsEmptyOperation)1 IteratorOperation (com.hazelcast.collection.impl.queue.operations.IteratorOperation)1 OfferBackupOperation (com.hazelcast.collection.impl.queue.operations.OfferBackupOperation)1 OfferOperation (com.hazelcast.collection.impl.queue.operations.OfferOperation)1 PeekOperation (com.hazelcast.collection.impl.queue.operations.PeekOperation)1 PollBackupOperation (com.hazelcast.collection.impl.queue.operations.PollBackupOperation)1