Search in sources :

Example 1 with DeferFilter

use of org.apache.accumulo.tserver.ConditionalMutationSet.DeferFilter in project accumulo by apache.

the class RowLocks method acquireRowlocks.

List<RowLock> acquireRowlocks(Map<KeyExtent, List<ServerConditionalMutation>> updates, Map<KeyExtent, List<ServerConditionalMutation>> deferred) {
    ArrayList<RowLock> locks = new ArrayList<>();
    // assume that mutations are in sorted order to avoid deadlock
    synchronized (rowLocks) {
        for (List<ServerConditionalMutation> scml : updates.values()) {
            for (ServerConditionalMutation scm : scml) {
                locks.add(getRowLock(new ArrayByteSequence(scm.getRow())));
            }
        }
    }
    HashSet<ByteSequence> rowsNotLocked = null;
    // acquire as many locks as possible, not blocking on rows that are already locked
    if (locks.size() > 1) {
        for (RowLock rowLock : locks) {
            if (!rowLock.tryLock()) {
                if (rowsNotLocked == null)
                    rowsNotLocked = new HashSet<>();
                rowsNotLocked.add(rowLock.rowSeq);
            }
        }
    } else {
        // if there is only one lock, then wait for it
        locks.get(0).lock();
    }
    if (rowsNotLocked != null) {
        final HashSet<ByteSequence> rnlf = rowsNotLocked;
        // assume will get locks needed, do something expensive otherwise
        ConditionalMutationSet.defer(updates, deferred, new DeferFilter() {

            @Override
            public void defer(List<ServerConditionalMutation> scml, List<ServerConditionalMutation> okMutations, List<ServerConditionalMutation> deferred) {
                for (ServerConditionalMutation scm : scml) {
                    if (rnlf.contains(new ArrayByteSequence(scm.getRow())))
                        deferred.add(scm);
                    else
                        okMutations.add(scm);
                }
            }
        });
        ArrayList<RowLock> filteredLocks = new ArrayList<>();
        ArrayList<RowLock> locksToReturn = new ArrayList<>();
        for (RowLock rowLock : locks) {
            if (rowsNotLocked.contains(rowLock.rowSeq)) {
                locksToReturn.add(rowLock);
            } else {
                filteredLocks.add(rowLock);
            }
        }
        synchronized (rowLocks) {
            for (RowLock rowLock : locksToReturn) {
                returnRowLock(rowLock);
            }
        }
        locks = filteredLocks;
    }
    return locks;
}
Also used : DeferFilter(org.apache.accumulo.tserver.ConditionalMutationSet.DeferFilter) ServerConditionalMutation(org.apache.accumulo.tserver.data.ServerConditionalMutation) ArrayList(java.util.ArrayList) ArrayByteSequence(org.apache.accumulo.core.data.ArrayByteSequence) ByteSequence(org.apache.accumulo.core.data.ByteSequence) ArrayByteSequence(org.apache.accumulo.core.data.ArrayByteSequence) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ArrayByteSequence (org.apache.accumulo.core.data.ArrayByteSequence)1 ByteSequence (org.apache.accumulo.core.data.ByteSequence)1 DeferFilter (org.apache.accumulo.tserver.ConditionalMutationSet.DeferFilter)1 ServerConditionalMutation (org.apache.accumulo.tserver.data.ServerConditionalMutation)1