private static SubscriptionLink removeSubscriptionLink( final ArrayList<SubscriptionLink> subscriptionLinks, final long registrationId) { SubscriptionLink subscriptionLink = null; for (int i = 0, size = subscriptionLinks.size(); i < size; i++) { final SubscriptionLink subscription = subscriptionLinks.get(i); if (subscription.registrationId() == registrationId) { subscriptionLink = subscription; fastUnorderedRemove(subscriptionLinks, i); break; } } return subscriptionLink; }
/** * Removes element but instead of copying all elements to the left, moves into the same slot the last element. * This avoids the copy costs, but spoils the list order. If element is the last element then it is just removed. * * @param list to be modified. * @param e to be removed. * @param <T> element type. * @return true if found and removed, false otherwise. */ public static <T> boolean fastUnorderedRemove(final ArrayList<T> list, final T e) { for (int i = 0, size = list.size(); i < size; i++) { if (e == list.get(i)) { fastUnorderedRemove(list, i, size - 1); return true; } } return false; } }
/** * {@inheritDoc} */ public long onIdle(final long timeNs, final long senderLimit, final long senderPosition, final boolean isEos) { long minPosition = Long.MAX_VALUE; long minLimitPosition = Long.MAX_VALUE; final ArrayList<Receiver> receiverList = this.receiverList; for (int lastIndex = receiverList.size() - 1, i = lastIndex; i >= 0; i--) { final Receiver receiver = receiverList.get(i); if ((receiver.timeOfLastStatusMessageNs + RECEIVER_TIMEOUT) - timeNs < 0) { ArrayListUtil.fastUnorderedRemove(receiverList, i, lastIndex--); } else { minPosition = Math.min(minPosition, receiver.lastPosition); minLimitPosition = Math.min(minLimitPosition, receiver.lastPositionPlusWindow); } } if (isEos && shouldLinger) { if (0 == receiverList.size() || minPosition >= senderPosition) { shouldLinger = false; } } return receiverList.size() > 0 ? minLimitPosition : senderLimit; }
/** * {@inheritDoc} */ public long onIdle(final long timeNs, final long senderLimit, final long senderPosition, final boolean isEos) { long minPosition = Long.MAX_VALUE; long minLimitPosition = Long.MAX_VALUE; final ArrayList<Receiver> receiverList = this.receiverList; for (int lastIndex = receiverList.size() - 1, i = lastIndex; i >= 0; i--) { final Receiver receiver = receiverList.get(i); if ((receiver.timeOfLastStatusMessageNs + RECEIVER_TIMEOUT) - timeNs < 0) { ArrayListUtil.fastUnorderedRemove(receiverList, i, lastIndex--); } else { minPosition = Math.min(minPosition, receiver.lastPosition); minLimitPosition = Math.min(minLimitPosition, receiver.lastPositionPlusWindow); } } if (isEos && shouldLinger) { if (0 == receiverList.size() || minPosition >= senderPosition) { shouldLinger = false; } } return receiverList.size() > 0 ? minLimitPosition : senderLimit; }
int send( final DatagramChannel datagramChannel, final ByteBuffer buffer, final SendChannelEndpoint channelEndpoint, final int bytesToSend) { final long nowNs = nanoClock.nanoTime(); final ArrayList<Destination> destinations = this.destinations; final int position = buffer.position(); int minBytesSent = bytesToSend; for (int lastIndex = destinations.size() - 1, i = lastIndex; i >= 0; i--) { final Destination destination = destinations.get(i); if ((destination.timeOfLastActivityNs + destinationTimeoutNs) - nowNs < 0) { ArrayListUtil.fastUnorderedRemove(destinations, i, lastIndex--); } else { minBytesSent = Math.min( minBytesSent, send(datagramChannel, buffer, channelEndpoint, bytesToSend, position, destination.address)); } } return minBytesSent; }
private int checkLingeringResources(final long nowNs) { int workCount = 0; final ArrayList<ManagedResource> lingeringResources = this.lingeringResources; for (int lastIndex = lingeringResources.size() - 1, i = lastIndex; i >= 0; i--) { final ManagedResource resource = lingeringResources.get(i); if ((resource.timeOfLastStateChange() + ctx.resourceLingerDurationNs()) - nowNs < 0) { ArrayListUtil.fastUnorderedRemove(lingeringResources, i, lastIndex--); resource.delete(); workCount += 1; } } return workCount; }
public final int doWork() { int workDone = preWork(); final ArrayList<T> sessions = this.sessions; for (int lastIndex = sessions.size() - 1, i = lastIndex; i >= 0; i--) { final T session = sessions.get(i); workDone += session.doWork(); if (session.isDone()) { ArrayListUtil.fastUnorderedRemove(sessions, i, lastIndex--); closeSession(session); } } return workDone; }
void onRemovePublication(final long registrationId, final long correlationId) { PublicationLink publicationLink = null; final ArrayList<PublicationLink> publicationLinks = this.publicationLinks; for (int i = 0, size = publicationLinks.size(); i < size; i++) { final PublicationLink publication = publicationLinks.get(i); if (registrationId == publication.registrationId()) { publicationLink = publication; fastUnorderedRemove(publicationLinks, i); break; } } if (null == publicationLink) { throw new ControlProtocolException(UNKNOWN_PUBLICATION, "unknown publication: " + registrationId); } publicationLink.close(); clientProxy.operationSucceeded(correlationId); }
private int processRedirectSessions(final ArrayList<ClusterSession> redirectSessions, final long nowMs) { int workCount = 0; for (int lastIndex = redirectSessions.size() - 1, i = lastIndex; i >= 0; i--) { final ClusterSession session = redirectSessions.get(i); final EventCode eventCode = EventCode.REDIRECT; final int id = leaderMember.id(); if (egressPublisher.sendEvent(session, leadershipTermId, id, eventCode, clientFacingEndpoints) || nowMs > (session.timeOfLastActivityMs() + sessionTimeoutMs)) { ArrayListUtil.fastUnorderedRemove(redirectSessions, i, lastIndex--); session.close(); workCount++; } } return workCount; }
private int processRejectedSessions(final ArrayList<ClusterSession> rejectedSessions, final long nowMs) { int workCount = 0; for (int lastIndex = rejectedSessions.size() - 1, i = lastIndex; i >= 0; i--) { final ClusterSession session = rejectedSessions.get(i); String detail = ConsensusModule.Configuration.SESSION_LIMIT_MSG; EventCode eventCode = EventCode.ERROR; if (session.state() == REJECTED) { detail = ConsensusModule.Configuration.SESSION_REJECTED_MSG; eventCode = EventCode.AUTHENTICATION_REJECTED; } if (egressPublisher.sendEvent(session, leadershipTermId, leaderMember.id(), eventCode, detail) || nowMs > (session.timeOfLastActivityMs() + sessionTimeoutMs)) { ArrayListUtil.fastUnorderedRemove(rejectedSessions, i, lastIndex--); session.close(); workCount++; } } return workCount; }
void onRemoveCounter(final long registrationId, final long correlationId) { CounterLink counterLink = null; final ArrayList<CounterLink> counterLinks = this.counterLinks; for (int i = 0, size = counterLinks.size(); i < size; i++) { final CounterLink link = counterLinks.get(i); if (registrationId == link.registrationId()) { counterLink = link; fastUnorderedRemove(counterLinks, i); break; } } if (null == counterLink) { throw new ControlProtocolException(UNKNOWN_COUNTER, "unknown counter: " + registrationId); } clientProxy.operationSucceeded(correlationId); clientProxy.onUnavailableCounter(registrationId, counterLink.counterId()); counterLink.close(); }
private <T extends DriverManagedResource> void checkManagedResources( final ArrayList<T> list, final long nowNs, final long nowMs) { for (int lastIndex = list.size() - 1, i = lastIndex; i >= 0; i--) { final DriverManagedResource resource = list.get(i); resource.onTimeEvent(nowNs, nowMs, this); if (resource.hasReachedEndOfLife()) { if (resource.free()) { fastUnorderedRemove(list, i, lastIndex--); resource.close(); } else { ctx.systemCounters().get(FREE_FAILS).incrementOrdered(); } } } }
private void checkPendingSetupMessages(final long nowNs) { final ArrayList<PendingSetupMessageFromSource> pendingSetupMessages = this.pendingSetupMessages; for (int lastIndex = pendingSetupMessages.size() - 1, i = lastIndex; i >= 0; i--) { final PendingSetupMessageFromSource pending = pendingSetupMessages.get(i); if ((pending.timeOfStatusMessageNs() + PENDING_SETUPS_TIMEOUT_NS) - nowNs < 0) { if (!pending.isPeriodic()) { ArrayListUtil.fastUnorderedRemove(pendingSetupMessages, i, lastIndex--); pending.removeFromDataPacketDispatcher(); } else if (pending.shouldElicitSetupMessage()) { pending.channelEndpoint().sendSetupElicitingStatusMessage( pending.transportIndex(), pending.controlAddress(), pending.sessionId(), pending.streamId()); pending.timeOfStatusMessageNs(nowNs); } } } } }
ArrayListUtil.fastUnorderedRemove(pendingSessions, i, lastIndex--); session.timeOfLastActivityMs(nowMs); sessionByIdMap.put(session.id(), session); ArrayListUtil.fastUnorderedRemove(pendingSessions, i, lastIndex--); rejectedSessions.add(session); ArrayListUtil.fastUnorderedRemove(pendingSessions, i, lastIndex--); session.close(); ctx.timedOutClientCounter().incrementOrdered();
public int doWork() { int workCount = commandQueue.drain(Runnable::run, Configuration.COMMAND_DRAIN_LIMIT); final int bytesReceived = dataTransportPoller.pollTransports(); totalBytesReceived.getAndAddOrdered(bytesReceived); final long nowNs = nanoClock.nanoTime(); final ArrayList<PublicationImage> publicationImages = this.publicationImages; for (int lastIndex = publicationImages.size() - 1, i = lastIndex; i >= 0; i--) { final PublicationImage image = publicationImages.get(i); if (image.hasActivityAndNotEndOfStream(nowNs)) { workCount += image.sendPendingStatusMessage(); workCount += image.processPendingLoss(); workCount += image.initiateAnyRttMeasurements(nowNs); } else { ArrayListUtil.fastUnorderedRemove(publicationImages, i, lastIndex--); image.removeFromDispatcher(); } } checkPendingSetupMessages(nowNs); return workCount + bytesReceived; }
/** * Removes element but instead of copying all elements to the left, moves into the same slot the last element. * This avoids the copy costs, but spoils the list order. If element is the last element then it is just removed. * * @param list to be modified. * @param e to be removed. * @param <T> element type. * @return true if found and removed, false otherwise. */ public static <T> boolean fastUnorderedRemove(final ArrayList<T> list, final T e) { for (int i = 0, size = list.size(); i < size; i++) { if (e == list.get(i)) { fastUnorderedRemove(list, i, size - 1); return true; } } return false; } }
private int pollReplayerSessions() { final ArrayList<ReplayerSession> replayerSessions = this.replayerSessions; final int size = replayerSessions.size(); for (int lastIndex = size - 1, i = lastIndex; i >= 0; i--) { final ReplayerSession replayerSession = replayerSessions.get(i); if (replayerSession.attempReplay()) { fastUnorderedRemove(replayerSessions, i, lastIndex--); } } return size; }
private static SubscriptionLink removeSubscriptionLink( final ArrayList<SubscriptionLink> subscriptionLinks, final long registrationId) { SubscriptionLink subscriptionLink = null; for (int i = 0, size = subscriptionLinks.size(); i < size; i++) { final SubscriptionLink subscription = subscriptionLinks.get(i); if (subscription.registrationId() == registrationId) { subscriptionLink = subscription; fastUnorderedRemove(subscriptionLinks, i); break; } } return subscriptionLink; }
private static SubscriptionLink removeSubscriptionLink( final ArrayList<SubscriptionLink> subscriptionLinks, final long registrationId) { SubscriptionLink subscriptionLink = null; for (int i = 0, size = subscriptionLinks.size(); i < size; i++) { final SubscriptionLink subscription = subscriptionLinks.get(i); if (subscription.registrationId() == registrationId) { subscriptionLink = subscription; fastUnorderedRemove(subscriptionLinks, i); break; } } return subscriptionLink; }
/** * {@inheritDoc} */ public long onIdle(final long timeNs, final long senderLimit, final long senderPosition, final boolean isEos) { long minPosition = Long.MAX_VALUE; long minLimitPosition = Long.MAX_VALUE; final ArrayList<Receiver> receiverList = this.receiverList; for (int lastIndex = receiverList.size() - 1, i = lastIndex; i >= 0; i--) { final Receiver receiver = receiverList.get(i); if ((receiver.timeOfLastStatusMessageNs + RECEIVER_TIMEOUT) - timeNs < 0) { ArrayListUtil.fastUnorderedRemove(receiverList, i, lastIndex--); } else { minPosition = Math.min(minPosition, receiver.lastPosition); minLimitPosition = Math.min(minLimitPosition, receiver.lastPositionPlusWindow); } } if (isEos && shouldLinger) { if (0 == receiverList.size() || minPosition >= senderPosition) { shouldLinger = false; } } return receiverList.size() > 0 ? minLimitPosition : senderLimit; }