Package org.jgroups.protocols
Class FlowControl
- java.lang.Object
-
- org.jgroups.stack.Protocol
-
- org.jgroups.protocols.FlowControl
-
- All Implemented Interfaces:
Lifecycle
public abstract class FlowControl extends Protocol
Simple flow control protocol based on a credit system. Each sender has a number of credits (bytes to send). When the credits have been exhausted, the sender blocks. Each receiver also keeps track of how many credits it has received from a sender. When credits for a sender fall below a threshold, the receiver sends more credits to the sender.- Author:
- Bela Ban
-
-
Field Summary
Fields Modifier and Type Field Description protected intfrag_sizeprotected longmax_block_timeMax time (in milliseconds) to block.protected longmax_creditsMax number of bytes to send per receiver until an ack must be received before continuing sendingprotected longmin_creditsComputed as max_credits times min_theshold.protected doublemin_thresholdIf we're down to (min_threshold * max_credits) bytes for P, we send more credits to P.protected longnum_credit_requests_receivedprotected longnum_credit_requests_sentprotected longnum_credit_responses_receivedprotected longnum_credit_responses_sentprotected longnum_msgs_droppedprotected java.util.Map<Address,Credit>receivedKeeps track of credits per member at the receiver.protected booleanrunningWhether FlowControl is still running, this is set to false when the protocol terminates (on stop())-
Fields inherited from class org.jgroups.stack.Protocol
after_creation_hook, down_prot, ergonomics, id, local_addr, log, policies, stack, stats, up_prot
-
-
Constructor Summary
Constructors Constructor Description FlowControl()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected longadjustCredit(java.util.Map<Address,Credit> map, Address sender, int length)Check whether sender has enough credits left.java.lang.Objectdown(Event evt)An event is to be sent down the stack.java.lang.Objectdown(Message msg)A message is sent down the stack.abstract doublegetAverageTimeBlocked()protected abstract HeadergetCreditRequestHeader()longgetMaxBlockTime()longgetMaxCredits()longgetMinCredits()doublegetMinThreshold()abstract intgetNumberOfBlockings()longgetNumberOfCreditRequestsReceived()Deprecated.longgetNumberOfCreditRequestsSent()Deprecated.longgetNumberOfCreditResponsesReceived()Deprecated.longgetNumberOfCreditResponsesSent()Deprecated.longgetReceiverCreditsFor(Address mbr)protected abstract HeadergetReplenishHeader()protected voidhandleConfigEvent(java.util.Map<java.lang.String,java.lang.Object> info)protected abstract voidhandleCredit(Address sender, long increase)protected voidhandleCreditRequest(java.util.Map<Address,Credit> map, Address sender, long requested_credits)protected abstract java.lang.ObjecthandleDownMessage(Message msg, int length)protected abstract booleanhandleMulticastMessage()Whether the protocol handles message with dest == null || dest.isMulticastAddress()protected voidhandleUpEvent(Message msg, FcHeader hdr)protected voidhandleViewChange(java.util.List<Address> mbrs)voidinit()Called after a protocol has been created and before the protocol is started.java.lang.StringprintCredits()protected static java.lang.StringprintMap(java.util.Map<Address,? extends Credit> m)java.lang.StringprintReceiverCredits()abstract java.lang.StringprintSenderCredits()voidresetStats()protected voidsendCredit(Address dest, long credits)protected voidsendCreditRequest(Address dest, long credits_needed)We cannot send this request as OOB message, as the credit request needs to queue up behind the regular messages; if a receiver cannot process the regular messages, that is a sign that the sender should be throttled !<T extends FlowControl>
TsetMaxBlockTime(long t)<T extends FlowControl>
TsetMaxCredits(long m)<T extends FlowControl>
TsetMinCredits(long m)<T extends FlowControl>
TsetMinThreshold(double m)voidstart()This method is called on aJChannel.connect(String); starts work.voidstop()Called on aJChannel.disconnect(); stops work (e.g.voidunblock()Allows to unblock all blocked senders from an external program, e.g.java.lang.Objectup(Event evt)An event was received from the protocol below.java.lang.Objectup(Message msg)A single message was received.voidup(MessageBatch batch)Sends up a multiple messages in aMessageBatch.-
Methods inherited from class org.jgroups.stack.Protocol
accept, addPolicy, addr, addr, afterCreationHook, destroy, down, enableStats, getAddress, getComponents, getDownProtocol, getDownServices, getId, getIdsAbove, getLevel, getLog, getName, getPolicies, getProtocolStack, getSocketFactory, getThreadFactory, getTransport, getUpProtocol, getUpServices, getValue, isErgonomics, level, parse, policies, providedDownServices, providedUpServices, removePolicy, requiredDownServices, requiredUpServices, resetStatistics, setAddress, setDownProtocol, setErgonomics, setId, setLevel, setPolicies, setProtocolStack, setSocketFactory, setUpProtocol, setValue, statsEnabled, toString
-
-
-
-
Field Detail
-
max_credits
protected long max_credits
Max number of bytes to send per receiver until an ack must be received before continuing sending
-
max_block_time
protected long max_block_time
Max time (in milliseconds) to block. If credit hasn't been received after max_block_time, we send a REPLENISHMENT request to the members from which we expect credits. A value <= 0 means to wait forever.
-
min_threshold
protected double min_threshold
If we're down to (min_threshold * max_credits) bytes for P, we send more credits to P. Example: if max_credits is 1'000'000, and min_threshold 0.25, then we send ca. 250'000 credits to P once we've got only 250'000 credits left for P (we've received 750'000 bytes from P).
-
min_credits
protected long min_credits
Computed as max_credits times min_theshold. If explicitly set, this will override the above computation
-
num_credit_requests_received
protected long num_credit_requests_received
-
num_credit_requests_sent
protected long num_credit_requests_sent
-
num_credit_responses_received
protected long num_credit_responses_received
-
num_credit_responses_sent
protected long num_credit_responses_sent
-
num_msgs_dropped
protected long num_msgs_dropped
-
received
protected final java.util.Map<Address,Credit> received
Keeps track of credits per member at the receiver. For each message, the credits for the sender are decremented by the size of the received message. When the credits fall below the threshold, we refill and send a REPLENISH message to the sender.
-
running
protected volatile boolean running
Whether FlowControl is still running, this is set to false when the protocol terminates (on stop())
-
frag_size
protected int frag_size
-
-
Method Detail
-
resetStats
public void resetStats()
- Overrides:
resetStatsin classProtocol
-
getNumberOfBlockings
public abstract int getNumberOfBlockings()
-
getAverageTimeBlocked
public abstract double getAverageTimeBlocked()
-
getMaxCredits
public long getMaxCredits()
-
setMaxCredits
public <T extends FlowControl> T setMaxCredits(long m)
-
getMinThreshold
public double getMinThreshold()
-
setMinThreshold
public <T extends FlowControl> T setMinThreshold(double m)
-
getMinCredits
public long getMinCredits()
-
setMinCredits
public <T extends FlowControl> T setMinCredits(long m)
-
getMaxBlockTime
public long getMaxBlockTime()
-
setMaxBlockTime
public <T extends FlowControl> T setMaxBlockTime(long t)
-
getNumberOfCreditRequestsReceived
@Deprecated public long getNumberOfCreditRequestsReceived()
Deprecated.Don't remove! https://issues.redhat.com/browse/JGRP-2814
-
getNumberOfCreditRequestsSent
@Deprecated public long getNumberOfCreditRequestsSent()
Deprecated.Don't remove! https://issues.redhat.com/browse/JGRP-2814
-
getNumberOfCreditResponsesReceived
@Deprecated public long getNumberOfCreditResponsesReceived()
Deprecated.Don't remove! https://issues.redhat.com/browse/JGRP-2814
-
getNumberOfCreditResponsesSent
@Deprecated public long getNumberOfCreditResponsesSent()
Deprecated.Don't remove! https://issues.redhat.com/browse/JGRP-2814
-
printSenderCredits
public abstract java.lang.String printSenderCredits()
-
printReceiverCredits
public java.lang.String printReceiverCredits()
-
getReceiverCreditsFor
public long getReceiverCreditsFor(Address mbr)
-
printCredits
public java.lang.String printCredits()
-
handleMulticastMessage
protected abstract boolean handleMulticastMessage()
Whether the protocol handles message with dest == null || dest.isMulticastAddress()
-
handleCredit
protected abstract void handleCredit(Address sender, long increase)
-
getReplenishHeader
protected abstract Header getReplenishHeader()
-
getCreditRequestHeader
protected abstract Header getCreditRequestHeader()
-
unblock
public void unblock()
Allows to unblock all blocked senders from an external program, e.g. JMX
-
init
public void init() throws java.lang.ExceptionDescription copied from class:ProtocolCalled after a protocol has been created and before the protocol is started. Attributes are already set. Other protocols are not yet connected and events cannot yet be sent.
-
start
public void start() throws java.lang.ExceptionDescription copied from class:ProtocolThis method is called on aJChannel.connect(String); starts work. Protocols are connected ready to receive events. Will be called from bottom to top.- Specified by:
startin interfaceLifecycle- Overrides:
startin classProtocol- Throws:
java.lang.Exception- Thrown if protocol cannot be started successfully. This will cause the ProtocolStack to fail, soJChannel.connect(String)will throw an exception
-
stop
public void stop()
Description copied from class:ProtocolCalled on aJChannel.disconnect(); stops work (e.g. by closing multicast socket). Will be called from top to bottom.
-
down
public java.lang.Object down(Event evt)
Description copied from class:ProtocolAn event is to be sent down the stack. A protocol may want to examine its type and perform some action on it, depending on the event's type. If the event is a message MSG, then the protocol may need to add a header to it (or do nothing at all) before sending it down the stack usingdown_prot.down().
-
down
public java.lang.Object down(Message msg)
Description copied from class:ProtocolA message is sent down the stack. Protocols may examine the message and do something (e.g. add a header) with it, before passing it down.
-
up
public java.lang.Object up(Event evt)
Description copied from class:ProtocolAn event was received from the protocol below. Usually the current protocol will want to examine the event type and - depending on its type - perform some computation (e.g. removing headers from a MSG event type, or updating the internal membership list when receiving a VIEW_CHANGE event). Finally, the event is either a) discarded, or b) an event is sent down the stack usingdown_prot.down()or c) the event (or another event) is sent up the stack usingup_prot.up().
-
up
public java.lang.Object up(Message msg)
Description copied from class:ProtocolA single message was received. Protocols may examine the message and do something (e.g. add a header) with it before passing it up.
-
up
public void up(MessageBatch batch)
Description copied from class:ProtocolSends up a multiple messages in aMessageBatch. The sender of the batch is always the same, and so is the destination (null == multicast messages). Messages in a batch can be OOB messages, regular messages, or mixed messages, although the transport itself will create initial MessageBatches that contain only either OOB or regular messages. The default processing below sends messages up the stack individually, based on a matching criteria (callingProtocol.accept(Message)), and - if true - callsProtocol.up(org.jgroups.Event)for that message and removes the message. If the batch is not empty, it is passed up, or else it is dropped. Subclasses should check if there are any messages destined for them (e.g. usingMessageBatch.iterator(Predicate)), then possibly remove and process them and finally pass the batch up to the next protocol. Protocols can also modify messages in place, e.g. ENCRYPT could decrypt all encrypted messages in the batch, not remove them, and pass the batch up when done.
-
handleConfigEvent
protected void handleConfigEvent(java.util.Map<java.lang.String,java.lang.Object> info)
-
handleDownMessage
protected abstract java.lang.Object handleDownMessage(Message msg, int length)
-
adjustCredit
protected long adjustCredit(java.util.Map<Address,Credit> map, Address sender, int length)
Check whether sender has enough credits left. If not, send it some more- Parameters:
map- The hashmap to usesender- The address of the senderlength- The number of bytes received by this message. We don't care about the size of the headers for the purpose of flow control- Returns:
- long Number of credits to be sent. Greater than 0 if credits needs to be sent, 0 otherwise
-
handleCreditRequest
protected void handleCreditRequest(java.util.Map<Address,Credit> map, Address sender, long requested_credits)
- Parameters:
map- The map to modifysender- The sender who requests creditsrequested_credits- Number of bytes that the sender has left to send messages to us
-
sendCredit
protected void sendCredit(Address dest, long credits)
-
sendCreditRequest
protected void sendCreditRequest(Address dest, long credits_needed)
We cannot send this request as OOB message, as the credit request needs to queue up behind the regular messages; if a receiver cannot process the regular messages, that is a sign that the sender should be throttled !- Parameters:
dest- The member to which we send the credit requestcredits_needed- The number of bytes (of credits) left for dest
-
handleViewChange
protected void handleViewChange(java.util.List<Address> mbrs)
-
-