Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.client.cluster;
  
  import java.util.*;
  
Class that encapsulates view of the cluster, as whole, from client perspective.
 
 public class ClusterViewByClientImpl<K extends EntryKey>
     extends ClusterViewByClient<K>
 {
Reference to the underlying network client, so that we can construct paths for requests.
 
 	private final NetworkClient<K> _client;
 
 	private final KeySpace _keyspace;
     
 	private final EntryKeyConverter<K> _keyConverter;
     
Since we will need to iterate over server nodes, let's use pre-calculated array.
 
             new ClusterServerNode[0]);
 
     private final EntryAccessors<K> _entryAccessors;

    
Helper object that knows how to find node(s) to send requests to, for given key.
 
     private final HashRouter<K> _hashRouter;
     
     /*
     /**********************************************************************
     /* Construction
     /**********************************************************************
      */
    
    
Path segments for the root path
 
     private final String[] _rootPathSegments;
 
     public ClusterViewByClientImpl(StoreClientConfig<K,?> storeConfig,
             NetworkClient<K> clientKeySpace keyspace)
     {
          = keyspace;
         if (client == null) {
              = null;
              = null;
              = null;
         } else {
              = client;
              = client.getKeyConverter();
              = client.getEntryAccessors();
         }
         if (storeConfig == null) {
              = new String[0];
         } else {
              = storeConfig.getBasePath();
         }
          = new HashRouter<K>(keyspace);
     }
     
     public static <K extends EntryKeyClusterViewByClientImpl<K> forTesting(KeySpace keyspace)
     {
         return new ClusterViewByClientImpl<K>(nullnullkeyspace);
     }
 
     /*
     protected String[] _splitPath(String base)
     {
         base = base.trim();
         if (base.startsWith("/")) {
             base = base.substring(1);
         }
         if (base.endsWith("/")) {
             base = base.substring(0, base.length()-1);
         }
         if (base.length() == 0) {
             return new String[0];
         }
         return base.split("/");
     }
     */
     
     /*
    /**********************************************************************
    /* Accessors
    /**********************************************************************
     */
    @Override
    public int getServerCount() {
        return .size();
    }
    @Override
    public boolean isFullyAvailable() {
        return getCoverage() == .getLength();
    }
    @Override
    public int getCoverage() {
        return _getCoverage(.get());
    }
    // separate method for testing:
    protected int _getCoverage(ClusterServerNode[] states)
    {
        BitSet slices = new BitSet(.getLength());
        for (ClusterServerNode state : states) {
            state.getTotalRange().fill(slices);
        }
        return slices.cardinality();
    }
    @Override
    public NodesForKey getNodesFor(K key) {
        return .getNodesFor(key);
    }
    /*
    /**********************************************************************
    /* Updating state
    /**********************************************************************
     */
    
    
Method called to add information directly related to node that served the request.
    public synchronized void updateDirectState(IpAndPort byNodeNodeState stateInfo,
            long requestTimelong responseTime,
            long clusterInfoVersion)
    {
        ClusterServerNodeImpl localState = .get(byNode);
        if (localState == null) { // new info 
            localState = new ClusterServerNodeImpl(_rootPathFor(byNode),
            		byNodestateInfo.getRangeActive(), stateInfo.getRangePassive(),
                    );
            _addNode(byNodelocalState);
        }
        boolean needInvalidate = localState.updateRanges(stateInfo.getRangeActive(),
                stateInfo.getRangePassive());
        if (localState.updateDisabled(stateInfo.isDisabled())) {
            needInvalidate = true;
        }
        if (needInvalidate) {
            invalidateRouting();
        }
        localState.setLastRequestSent(requestTime);
        localState.setLastResponseReceived(responseTime);
        localState.setLastNodeUpdateFetched(stateInfo.getLastUpdated());
        localState.setLastClusterUpdateFetched(clusterInfoVersion);
    }
    
    
Method called to add information obtained indirectly; i.e. "gossip".
    public synchronized void updateIndirectState(IpAndPort byNodeNodeState stateInfo)
    {
        // First: ensure references are properly resolved (eliminate "localhost" if need be):
        IpAndPort ip = stateInfo.getAddress();
        if (ip.isLocalReference()) {
            ip = byNode.withPort(ip.getPort());
        }
        final long nodeInfoTimestamp = stateInfo.getLastUpdated();
        // otherwise pretty simple:
        ClusterServerNodeImpl state = .get(ip);
        if (state == null) { // new info 
            state = new ClusterServerNodeImpl(_rootPathFor(ip),
            		ipstateInfo.getRangeActive(), stateInfo.getRangePassive(),
                    );
            _addNode(ipstate);
        } else {
            // quick check to ensure info we get is newer: if not, skip
            if (nodeInfoTimestamp <= state.getLastNodeUpdateFetched()) {
                return;
            }
        }
        state.setLastNodeUpdateFetched(nodeInfoTimestamp);
        boolean needInvalidate = state.updateRanges(stateInfo.getRangeActive(),
                stateInfo.getRangePassive());
        if (state.updateDisabled(stateInfo.isDisabled())) {
            needInvalidate = true;
        }
        if (needInvalidate) {
            invalidateRouting();
        }
    }
    /*
    /**********************************************************************
    /* Test support
    /**********************************************************************
     */
    protected NodesForKey _calculateNodes(int versionKeyHash keyHash,
            ClusterServerNode[] allNodes)
    {
        return ._calculateNodes(versionkeyHashallNodes);
    }
    /*
    /**********************************************************************
    /* Internal methods
    /**********************************************************************
     */
    protected RequestPath _rootPathFor(IpAndPort serverAddress)
    {
        RequestPathBuilder builder = .pathBuilder(serverAddress);
        for (String component : ) {
            builder = builder.addPathSegment(component);
        }
        return builder.build();
    }
    
    private void _addNode(IpAndPort keyClusterServerNodeImpl state)
    {
        .put(keystate);
    }

    
Method called when server node state information changes in a way that may affect routing of requests.
    private final void invalidateRouting() {
    }
New to GrepCode? Check out our FAQ X