Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.client.operation;
  
  import java.util.*;
  
Value class that is used as result type for content list operation. Unlike simple result classes like GetOperationResult, no calls are yet made when this object is constructed; rather, it is returned to be used for incrementally accessing contents to list. This is necessary as list operations may return large number of entries, and each individual operation can only return up to certain number of entries.

Parameters:
<K> Type of keys used for ClusterMate-based system
<T> Type of list items to return
 
 public class StoreEntryLister<K extends EntryKey,T>
 {
     public final static int DEFAULT_MAX_ENTRIES = 100;
     
     protected final StoreClientConfig<K,?> _clientConfig;
 
     protected final ClusterViewByClient<K> _cluster;
    
    
Prefix of entries to list.
 
     protected final K _prefix;
    
    
Type of items of the result list.
 
     protected final ListItemType _itemType;
 
     protected final ContentConverter<ListResponse<T>> _converter;

    
Id of the last entry that was iterated over.
 
     protected K _lastSeen;
     
     public StoreEntryLister(StoreClientConfig<K,?> configClusterViewByClient<K> cluster,
             K prefixListItemType itemTypeContentConverter<ListResponse<T>> converter,
             K initialLastSeen)
     {
          = config;
          = cluster;
          = prefix;
          = itemType;
          = converter;
          = initialLastSeen;
     }
 
     public ListOperationResult<T> listMore() throws InterruptedException
     {
         return listMore();
     }
 
     public ListOperationResult<T> listMore(int maxToListthrows InterruptedException
     {
         ListOperationResult<T> result = _listMore(maxToList);
         if (result != null) {
             StorableKey raw = result.getLastSeen();
             if (raw != null) { // should this error out?
                  = .getKeyConverter().rawToEntryKey(raw);
             }
         }
         return result;
     }
 
     protected ListOperationResult<T> _listMore(int maxToListthrows InterruptedException
     {
         final long startTime = System.currentTimeMillis();
 
         // First things first: find Server nodes to talk to:
         NodesForKey nodes = .getNodesFor();
         // then result
         ListOperationResult<T> result = new ListOperationResult<T>(.getOperationConfig());
         
         // One sanity check: if not enough server nodes to talk to, can't succeed...
         int nodeCount = nodes.size();
         if (nodeCount < 1) {
             return result// or Exception?
         }
         
         // Then figure out how long we have for the whole operation; use same timeout as GET
         final long endOfTime = startTime + .getOperationConfig().getGetOperationTimeoutMsecs();
         final long lastValidTime = endOfTime - .getCallConfig().getMinimumTimeoutMsecs();
 
         // Ok: first round; try List from every enabled store (or, if only one try, all)
         final boolean noRetries = !allowRetries();
        List<NodeFailureretries = null;
        for (int i = 0; i < nodeCount; ++i) {
            ClusterServerNode server = nodes.node(i);
            if (!server.isDisabled() || noRetries) {
                ListCallResult<T> gotten = server.entryLister().tryList(.getCallConfig(), endOfTime,
                        maxToList);
                if (gotten.failed()) {
                    CallFailure fail = gotten.getFailure();
                    if (fail.isRetriable()) {
                        retries = _add(retriesnew NodeFailure(serverfail));
                    } else {
                        result.withFailed(new NodeFailure(serverfail));
                    }
                    continue;
                }
                return result.setItems(servergotten);
            }
        }
        if (noRetries) { // if no retries, bail out quickly
            return result.withFailed(retries);
        }
        
        final long secondRoundStart = System.currentTimeMillis();
        // Do we need any delay in between?
        _doDelay(startTimesecondRoundStartendOfTime);
        
        // Otherwise: go over retry list first, and if that's not enough, try disabled
        if (retries == null) {
            retries = new LinkedList<NodeFailure>();
        } else {
            Iterator<NodeFailureit = retries.iterator();
            while (it.hasNext()) {
                NodeFailure retry = it.next();
                ClusterServerNode server = (ClusterServerNoderetry.getServer();
                ListCallResult<T> gotten = server.entryLister().tryList(.getCallConfig(), endOfTime,
                        maxToList);
                if (gotten.succeeded()) {
                    return result.withFailed(retries).setItems(servergotten);
                }
                CallFailure fail = gotten.getFailure();
                retry.addFailure(fail);
                if (!fail.isRetriable()) {
                    result.withFailed(retry);
                    it.remove();
                }
            }
        }
        // if no success, add disabled nodes in the mix
        for (int i = 0; i < nodeCount; ++i) {
            ClusterServerNode server = nodes.node(i);
            if (server.isDisabled()) {
                if (System.currentTimeMillis() >= lastValidTime) {
                    return result.withFailed(retries);
                }
                ListCallResult<T> gotten = server.entryLister().tryList(.getCallConfig(), endOfTime,
                        maxToList);
                if (gotten.succeeded()) {
                    return result.withFailed(retries).setItems(servergotten);
                }
                CallFailure fail = gotten.getFailure();
                if (fail.isRetriable()) {
                    retries.add(new NodeFailure(serverfail));
                } else {
                    result.withFailed(new NodeFailure(serverfail));
                }
            }
        }
        long prevStartTime = secondRoundStart;
        for (int i = 1; (i <= .) && !retries.isEmpty(); ++i) {
            final long currStartTime = System.currentTimeMillis();
            _doDelay(prevStartTimecurrStartTimeendOfTime);
            Iterator<NodeFailureit = retries.iterator();
            while (it.hasNext()) {
                if (System.currentTimeMillis() >= lastValidTime) {
                    return result.withFailed(retries);
                }
                NodeFailure retry = it.next();
                ClusterServerNode server = (ClusterServerNoderetry.getServer();
                ListCallResult<T> gotten = server.entryLister().tryList(.getCallConfig(), endOfTime,
                        maxToList);
                if (gotten.succeeded()) {
                    return result.withFailed(retries).setItems(servergotten);
                }
                CallFailure fail = gotten.getFailure();
                retry.addFailure(fail);
                if (!fail.isRetriable()) {
                    result.withFailed(retry);
                    it.remove();
                }
            }
        }
        // we are all done and this'll be a failure...
        return result.withFailed(retries);
    }
    /*
    /**********************************************************************
    /* Helper methods
    /**********************************************************************
     */
    protected boolean allowRetries() {
    }
    
    protected <T0> List<T0> _add(List<T0> list, T0 entry)
    {
        if (list == null) {
            list = new LinkedList<T0>();
        }
        list.add(entry);
        return list;
    }
    
    protected void _doDelay(long startTimelong currTimelong endTime)
        throws InterruptedException
    {
        long timeSpent = currTime - startTime;
        // only add delay if we have had quick failures (signaling overload)
        if (timeSpent < 1000L) {
            long timeLeft = endTime - currTime;
            // also, only wait if we still have some time; and then modest amount (250 mecs)
            if (timeLeft >= (4 * .)) {
                Thread.sleep(.);
            }
        }
    }
New to GrepCode? Check out our FAQ X