/** Moves contents of matrix num_rows down. Avoids a System.arraycopy(). Caller must hold the lock. */ @GuardedBy("lock") protected void move(int num_rows) { if(num_rows <= 0 || num_rows > matrix.length) return; int target_index=0; for(int i=num_rows; i < matrix.length; i++) matrix[target_index++]=matrix[i]; for(int i=matrix.length - num_rows; i < matrix.length; i++) matrix[i]=null; num_moves++; }
/** Computes and returns the row index for seqno. The caller must hold the lock. */ // Note that seqno-offset is never > Integer.MAX_VALUE and thus doesn't overflow into a negative long, // as offset is always adjusted in resize() or compact(). Even if it was negative, callers of computeRow() will // ignore the result or throw an ArrayIndexOutOfBound exception @GuardedBy("lock") protected int computeRow(long seqno) { int diff=(int)(seqno-offset); if(diff < 0) return diff; return diff / elements_per_row; }
@GuardedBy("lock") protected boolean decrement(long credits) { if(credits_left - credits >= 0) { credits_left-=credits; return true; } return false; } }
@GuardedBy("deliverySet") private View getAndSetView(View newView) { View oldView = currentView; currentView = newView; return oldView; }
/** Computes and returns the index within a row for seqno */ // Note that seqno-offset is never > Integer.MAX_VALUE and thus doesn't overflow into a negative long, // as offset is always adjusted in resize() or compact(). Even if it was negative, callers of computeIndex() will // ignore the result or throw an ArrayIndexOutOfBound exception @GuardedBy("lock") protected int computeIndex(long seqno) { int diff=(int)(seqno - offset); if(diff < 0) return diff; return diff & (elements_per_row - 1); // same as mod, but (apparently, I'm told) more efficient }
@GuardedBy("lock") protected boolean setProcessing(boolean flag) { boolean do_signal=processing && !flag; processing=flag; if(do_signal) processing_done.signalAll(); return flag; }
@GuardedBy("lock") protected List<PingData> toList() { List<PingData> retval=new ArrayList<>(); retval.addAll(Arrays.asList(ping_rsps).subList(0, index)); return retval; }
@GuardedBy("deliverySet") private void notifyIfNeeded() { if (!deliverySet.isEmpty() && deliverySet.first().isReadyToDeliver()) { deliverySet.notify(); } }
@GuardedBy("lock") protected Responses _done() { if(!done) { done=true; cond.signal(true); } return this; }
@GuardedBy("this") public void replaceConnection(Address address, Connection conn) { Connection previous=conns.put(address, conn); Util.close(previous); // closes previous connection (if present) }
@GuardedBy("lock") protected boolean block(long seqno) { while(running && seqno - low > capacity()) { try { buffer_full.await(); } catch(InterruptedException e) { } } return running; }
@GuardedBy("lock") protected void addMessage(Message msg, long size) { Address dest=msg.getDest(); List<Message> tmp=msgs.computeIfAbsent(dest, k -> new ArrayList<>(5)); tmp.add(msg); count+=size; } }
@GuardedBy("lock") protected void advanceLow() { while(low < high) { int index=index(low); if(buffer[index] != null) break; low++; } }
/** Iterate from low to hr and add up non-null values. Caller must hold the lock. */ @GuardedBy("lock") public int computeSize() { return (int)stream().filter(Objects::nonNull).count(); }
@GuardedBy("lock") protected int find(PingData data) { if(data == null) return -1; for(int i=0; i < index; i++) { if(data.equals(ping_rsps[i])) return i; } return -1; }
@GuardedBy("lock") protected void _grow(long new_capacity) { int new_cap=Util.getNextHigherPowerOfTwo((int)Math.max(buffer.length, new_capacity)); if(new_cap == buffer.length) return; _copy(new_cap); }
@GuardedBy("lock") protected void add(PingData data) { if(index >= ping_rsps.length) resize(newLength(ping_rsps.length)); ping_rsps[index++]=data; }
@GuardedBy("deliverSet") private long internalGetViewId() { return currentView == null ? -1 : currentView.getViewId().getId(); }
/** * @return {@code true} if the member who sent the message left the cluster and the message isn't ready to be deliver. */ @GuardedBy("deliverySet") private boolean removeMessage(MessageInfo messageInfo) { if (currentView.containsMember(messageInfo.getMessage().getSrc()) || messageInfo.isReadyToDeliver()) { return false; } else { messageCache.remove(messageInfo.messageID); return true; } }
@GuardedBy("lock") protected boolean responsesComplete() { if(isDone()) return true; final int num_total=rsps.size(); switch(options.mode()) { case GET_FIRST: return num_valid >= 1 || num_received >= num_total; case GET_ALL: return num_valid >= num_total || num_received >= num_total; case GET_NONE: return true; } return false; }