Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.service.cluster;
  
POJO used for exchanging node state information between servers; also stored in the local database.
 
 public final class ActiveNodeState extends NodeState
 {
    
Lazily calculated value
 
     protected int _hashCode;
 
     /*
     /**********************************************************************
     /* Instance creation, conversions
     /**********************************************************************
      */
 
     // just for (JSON) deserialization
     @SuppressWarnings("unused")
     private ActiveNodeState() {
         super();
         this. = null;
         this. = 0;
         this. = 0L;
         this. = null;
         this. = null;
         this. = null;
         this. = false;
         this. = 0L;
         this. = 0L;
         this. = 0L;
     }
    
    
Constructor used for constructing initial entry, when no persisted information is available. As such, will not contain valid sync range information, but just range information for node itself.
 
     public ActiveNodeState(NodeDefinition localNodelong creationTime)
     {
          = localNode.getAddress();
          = localNode.getIndex();
          = localNode.getActiveRange();
          = localNode.getPassiveRange();
         // can't calculate sync range without more info:
          = .withLength(0);
          = false// assume nodes start as active
          = creationTime;
          = 0L;
          = creationTime;
          = 0L;
     }
    
    
Constructor used for initializing state from definitions; used first time configurations are read.

Parameters:
localNode definition of local node (one on which service runs)
remoteNode definition of a peer node; node for which state is to be created (and which will be synced with local node)
updateTime Timestamp when last update was made; usually passed as 0L
 
     public ActiveNodeState(NodeState localNode,
             NodeDefinition remoteNodelong updateTime)
     {
          = remoteNode.getAddress();
          = remoteNode.getIndex();
          = remoteNode.getActiveRange();
          = remoteNode.getPassiveRange();
          = updateTime;
 
         // need to know total range of remove node
         KeyRange remoteRange = .union();
         KeyRange localRange = localNode.totalRange();
 
         // any overlap?
         if (remoteRange.overlapsWith(localRange)) { // yes!
              = remoteRange.union(localRange);
         } else { // nope; create empty sync range
              = remoteRange.withLength(0);
         }
          = false// assume nodes start as active
          = updateTime;        
          = 0L;
          = 0L;
     }

    
Constructor called when creating a peer node from information returned piggy-backed on Sync List response.
 
     public ActiveNodeState(NodeState localNode,
            NodeState remoteNodelong updateTime)
    {
        if (localNode.getAddress().equals(remoteNode.getAddress())) { // sanity check
            throw new IllegalArgumentException("Trying to create Peer with both end points as: "+localNode.getAddress());
        }
        
         = remoteNode.getAddress();
         = remoteNode.getIndex();
         = remoteNode.getRangeActive();
         = remoteNode.getRangePassive();
         = updateTime;
        // need to know total range of remove node
        KeyRange remoteRange = .union();
        KeyRange localRange = localNode.totalRange();
        // any overlap?
        if (remoteRange.overlapsWith(localRange)) { // yes!
             = remoteRange.union(localRange);
        } else { // nope; create empty sync range
             = remoteRange.withLength(0);
        }
         = remoteNode.isDisabled();
         = updateTime;
         = 0L;
         = 0L;
    }
    
    // used via fluent factory
    private ActiveNodeState(ActiveNodeState src,
            KeyRange newSyncRangelong newSyncedUpTo)
    {
         = src.address;
         = src.index;
         = src.lastUpdated;
         = src.rangeActive;
         = src.rangePassive;
         = newSyncRange;
         = src.disabled;
         = src.disabledUpdated;
         = src.lastSyncAttempt;
         = newSyncedUpTo;
    }
    // used via fluent factory
    private ActiveNodeState(ActiveNodeState src,
            long lastUpdatedlong lastSyncAttemptlong syncedUpTo)
    {
         = src.address;
         = src.index;
        this. = lastUpdated;
         = src.rangeActive;
         = src.rangePassive;
         = src.rangeSync;
         = src.disabled;
         = src.disabledUpdated;
        this. = lastSyncAttempt;
        this. = syncedUpTo;
    }
    // used via fluent factory
    private ActiveNodeState(ActiveNodeState srcint newIndex)
    {
         = src.address;
         = newIndex;
         = src.lastUpdated;
         = src.rangeActive;
         = src.rangePassive;
         = src.rangeSync;
         = src.disabled;
         = src.disabledUpdated;
         = src.lastSyncAttempt;
         = src.syncedUpTo;
    }
    // used via fluent factory
    private ActiveNodeState(ActiveNodeState srcboolean disabled,
            long disabledUpdated)
    {
         = src.address;
         = src.index;
         = src.lastUpdated;
         = src.rangeActive;
         = src.rangePassive;
         = src.rangeSync;
        this. = disabled;
        this. = disabledUpdated;
         = src.lastSyncAttempt;
         = src.syncedUpTo;
    }
    
    /*
    /**********************************************************************
    /* Fluent factories
    /**********************************************************************
     */
    public ActiveNodeState withSyncRange(KeyRange newRangelong newSyncedUpTo) {
        return new ActiveNodeState(thisnewRangenewSyncedUpTo);
    }

    
Fluent factory for creating new state with sync range calculated using specified local node state.
    public ActiveNodeState withSyncRange(ActiveNodeState localNode) {
        long syncTimestamp = ;
        KeyRange newSync = localNode.totalRange().intersection(totalRange());
        if (!.contains(newSync)) {
            syncTimestamp = 0L;
        }
        return new ActiveNodeState(thisnewSyncsyncTimestamp);
    }
    public ActiveNodeState withLastUpdated(long timestamp) {
    	// sanity check: MUST NOT use older timestamp
    	if (timestamp <= ) {
    	    if (timestamp == this.) {
    	        return this;
    	    }
    	    throw new IllegalArgumentException("Trying to set earlier 'lastUpdated': had "
    	            +this.+"; trying to set as "+timestamp);
    	}
        return new ActiveNodeState(thistimestamp);
    }
    public ActiveNodeState withLastSyncAttempt(long timestamp) {
        if ( == timestamp) {
            return this;
        }
        return new ActiveNodeState(thistimestamp);
    }
    public ActiveNodeState withSyncedUpTo(long timestamp) {
        if ( == timestamp) {
            return this;
        }
        return new ActiveNodeState(thistimestamp);
    }
    public ActiveNodeState withIndex(int newIndex) {
        if ( == newIndex) {
            return this;
        }
        return new ActiveNodeState(this);
    }
    public ActiveNodeState withDisabled(long timestampboolean state) {
        if ( == state && this. == timestamp) {
            return this;
        }
        return new ActiveNodeState(thisstatetimestamp);
    }
    /*
    /**********************************************************************
    /* Standard methods
    /**********************************************************************
     */
    @Override
    public String toString() {
        return String.valueOf();
    }

    
Equality is specifically defined to encompass just a subset of information:
  • Endpoint (ip+port)
  • Disabled?
  • Active Range
  • Passive Range
  • Index
    @Override
    public boolean equals(Object o)
    {
        if (o == thisreturn true;
        if (o == nullreturn false;
        if (o.getClass() != getClass()) return false;
        ActiveNodeState other = (ActiveNodeStateo;
        if (hashCode() != other.hashCode()) { // should be cheap(er)
            return false;
        }
        // but not enough: must verify they are equal...
        if ( != other.index
                ||  != other.disabled) {
            return false;
        }
        if (!.equals(other.address)) {
            return false;
        }
        if (!_rangesEqual(other.rangeActive)
                || !_rangesEqual(other.rangePassive)) {
            return false;
        }
        return true;
    }
    private final static boolean _rangesEqual(KeyRange r1KeyRange r2) {
        if (r1 == null) {
            return (r2 == null);
        }
        if (r2 == null) {
            return false;
        }
        return r1.equals(r2);
    }
    
    @Override
    public int hashCode()
    {
        int h = ;
        if (h == 0) {
            h =  ? 1 : -1;
            h += ;
            h ^= .hashCode();
            if ( != null) {
                h += .hashCode();
            }
            if ( != null) {
                h ^= .hashCode();
            }
             = h;
        }
        return h;
    }
New to GrepCode? Check out our FAQ X