Search in sources :

Example 1 with AbstractBatchedModificationsCursor

use of org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor in project controller by opendaylight.

the class ShardCommitCoordinator method createForwardedBatchedModifications.

Collection<BatchedModifications> createForwardedBatchedModifications(final BatchedModifications from, final int maxModificationsPerBatch) {
    CohortEntry cohortEntry = cohortCache.remove(from.getTransactionId());
    if (cohortEntry == null || cohortEntry.getTransaction() == null) {
        return Collections.singletonList(from);
    }
    cohortEntry.applyModifications(from.getModifications());
    final LinkedList<BatchedModifications> newModifications = new LinkedList<>();
    cohortEntry.getTransaction().getSnapshot().applyToCursor(new AbstractBatchedModificationsCursor() {

        @Override
        protected BatchedModifications getModifications() {
            if (newModifications.isEmpty() || newModifications.getLast().getModifications().size() >= maxModificationsPerBatch) {
                newModifications.add(new BatchedModifications(from.getTransactionId(), from.getVersion()));
            }
            return newModifications.getLast();
        }
    });
    BatchedModifications last = newModifications.getLast();
    last.setDoCommitOnReady(from.isDoCommitOnReady());
    last.setReady(from.isReady());
    last.setTotalMessagesSent(newModifications.size());
    return newModifications;
}
Also used : LinkedList(java.util.LinkedList) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications) AbstractBatchedModificationsCursor(org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor)

Example 2 with AbstractBatchedModificationsCursor

use of org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor in project controller by opendaylight.

the class ShardCommitCoordinator method convertPendingTransactionsToMessages.

Collection<?> convertPendingTransactionsToMessages(final int maxModificationsPerBatch) {
    final Collection<VersionedExternalizableMessage> messages = new ArrayList<>();
    for (ShardDataTreeCohort cohort : dataTree.getAndClearPendingTransactions()) {
        CohortEntry cohortEntry = cohortCache.remove(cohort.getIdentifier());
        if (cohortEntry == null) {
            continue;
        }
        final Deque<BatchedModifications> newMessages = new ArrayDeque<>();
        cohortEntry.getDataTreeModification().applyToCursor(new AbstractBatchedModificationsCursor() {

            @Override
            protected BatchedModifications getModifications() {
                final BatchedModifications lastBatch = newMessages.peekLast();
                if (lastBatch != null && lastBatch.getModifications().size() >= maxModificationsPerBatch) {
                    return lastBatch;
                }
                // Allocate a new message
                final BatchedModifications ret = new BatchedModifications(cohortEntry.getTransactionId(), cohortEntry.getClientVersion());
                newMessages.add(ret);
                return ret;
            }
        });
        final BatchedModifications last = newMessages.peekLast();
        if (last != null) {
            final boolean immediate = cohortEntry.isDoImmediateCommit();
            last.setDoCommitOnReady(immediate);
            last.setReady(true);
            last.setTotalMessagesSent(newMessages.size());
            messages.addAll(newMessages);
            if (!immediate) {
                switch(cohort.getState()) {
                    case CAN_COMMIT_COMPLETE:
                    case CAN_COMMIT_PENDING:
                        messages.add(new CanCommitTransaction(cohortEntry.getTransactionId(), cohortEntry.getClientVersion()));
                        break;
                    case PRE_COMMIT_COMPLETE:
                    case PRE_COMMIT_PENDING:
                        messages.add(new CommitTransaction(cohortEntry.getTransactionId(), cohortEntry.getClientVersion()));
                        break;
                    default:
                        break;
                }
            }
        }
    }
    return messages;
}
Also used : VersionedExternalizableMessage(org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage) ArrayList(java.util.ArrayList) ArrayDeque(java.util.ArrayDeque) BatchedModifications(org.opendaylight.controller.cluster.datastore.messages.BatchedModifications) CommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CommitTransaction) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) CanCommitTransaction(org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction) AbstractBatchedModificationsCursor(org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor)

Aggregations

BatchedModifications (org.opendaylight.controller.cluster.datastore.messages.BatchedModifications)2 AbstractBatchedModificationsCursor (org.opendaylight.controller.cluster.datastore.utils.AbstractBatchedModificationsCursor)2 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CanCommitTransaction (org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction)1 CommitTransaction (org.opendaylight.controller.cluster.datastore.messages.CommitTransaction)1 VersionedExternalizableMessage (org.opendaylight.controller.cluster.datastore.messages.VersionedExternalizableMessage)1