blueflood-core/src/main/java/com/rackspacecloud/blueflood/service/ZKShardLockManager.java
@VisibleForTesting void prefetchLocks() {

        if (!connected) {
            log.warn("Cannot connect to Zookeeper; will not perform initial lock acquisition");
            for (Lock lock : locks.values()) {
                lock.connectionLost();
            }
        } else {
            log.info("Pre-fetching zookeeper locks for shards");
            boolean isManagingAllShards = locks.size() >= Constants.NUMBER_OF_SHARDS;
            int maxLocksToPrefetch = moreThanHalf();

            if (isManagingAllShards) {
                maxLocksToPrefetch = Constants.NUMBER_OF_SHARDS;
            }

            List<Integer> shards = new ArrayList<Integer>(locks.keySet());
            Collections.shuffle(shards);

            int locksObtained = 0;
            for (int shard : shards) {
                try {
                    log.debug("Initial lock attempt for shard={}", shard);
                    final Lock lock = locks.get(shard);
                    lock.acquire().get();

                    if (lock.isHeld() && ++locksObtained >= maxLocksToPrefetch) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    log.warn("Thread exception while acquiring initial locks: " + ex.getMessage(), ex);
                } catch (ExecutionException ex) {
                    log.error("Problem acquiring lock " + shard + " " + ex.getCause().getMessage(), ex.getCause());
                }
            }
            log.info("Finished pre-fetching zookeeper locks");
        }
    }
@VisibleForTesting void prefetchLocks() {

        if (!connected) {
            log.warn("Cannot connect to Zookeeper; will not perform initial lock acquisition");
            for (Lock lock : locks.values()) {
                lock.connectionLost();
            }
        } else {
            log.info("Pre-fetching zookeeper locks for shards");
            boolean isManagingAllShards = locks.size() >= Constants.NUMBER_OF_SHARDS;
            int maxLocksToPrefetch = moreThanHalf();

            if (isManagingAllShards) {
                maxLocksToPrefetch = Constants.NUMBER_OF_SHARDS;
            }

            List<Integer> shards = new ArrayList<Integer>(locks.keySet());
            Collections.shuffle(shards);

            int locksObtained = 0;
            for (int shard : shards) {
                try {
                    log.debug("Initial lock attempt for shard={}", shard);
                    final Lock lock = locks.get(shard);
                    lock.acquire().get();

                    if (lock.isHeld() && ++locksObtained >= maxLocksToPrefetch) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    log.warn("Thread exception while acquiring initial locks: " + ex.getMessage(), ex);
                } catch (ExecutionException ex) {
                    log.error("Problem acquiring lock " + shard + " " + ex.getCause().getMessage(), ex.getCause());
                }
            }
            log.info("Finished pre-fetching zookeeper locks");
        }
    }
@ VisibleForTesting void prefetchLocks ( ) { if ( ! connected ) { log . warn ( "Cannot connect to Zookeeper; will not perform initial lock acquisition" ) ; for ( Lock lock : locks . values ( ) ) { lock . connectionLost ( ) ; } } else { log . info ( "Pre-fetching zookeeper locks for shards" ) ; boolean isManagingAllShards = locks . size ( ) >= Constants . NUMBER_OF_SHARDS ; int maxLocksToPrefetch = moreThanHalf ( ) ; if ( isManagingAllShards ) { maxLocksToPrefetch = Constants . NUMBER_OF_SHARDS ; } List < Integer > shards = new ArrayList < Integer > ( locks . keySet ( ) ) ; Collections . shuffle ( shards ) ; int locksObtained = 0 ; for ( int shard : shards ) { try { log . debug ( "Initial lock attempt for shard={}" , shard ) ; final Lock lock = locks . get ( shard ) ; lock . acquire ( ) . get ( ) ; if ( lock . isHeld ( ) && ++ locksObtained >= maxLocksToPrefetch ) { break ; } } catch ( InterruptedException ex ) { log . warn ( "Thread exception while acquiring initial locks: " + ex . getMessage ( ) , ex ) ; } catch ( ExecutionException ex ) { log . error ( "Problem acquiring lock " + shard + " " + ex . getCause ( ) . getMessage ( ) , ex . getCause ( ) ) ; } } log . info ( "Finished pre-fetching zookeeper locks" ) ; } }
Only called from {@link #init(TimeValue)}.

Try to achieve each lock. This will give us a good initial state where each lock is either held or unheld.
The purpose is to avoid a situation at startup where a node assumes it can schedule anything (until a lock
is held).
Only called from { @link #init ( TimeValue ) } .