The Datatransferthrottler class is used to control data transfer rates when Datanode reads and writes data. This class is thread-safe and can be shared with multiple threads. It is used by first constructing the Datatransferthrottler object and setting the period period and bandwidth bandwidthpersec, and then calling the Datatransferthrottler.throttle () method before the data is actually read and written. If the I/O rate is too fast relative to the given bandwidth, the method will wait for the current thread.
Two constructors
- The two-parameter constructor allows you to set the period period and bandwidth bandwidthpersec.
Public Datatransferthrottler ( long period,long bandwidthpersec)
- Single parameter constructor, you can set the bandwidth bandwidthpersec, the period period by default is set to 500ms.
Public Datatransferthrottler ( long bandwidthpersec)
Important attributes
| Period |
Period, per millisecond |
| Periodextension |
Period expansion time, per millisecond |
| Bytesperperiod |
Total number of bytes that can be sent/received in a cycle |
| Curperiodstart |
Current cycle start time, per millisecond |
| Curreserve |
Number of bytes that can be sent/received in the current cycle |
| Bytesalreadyused |
Number of bytes already in use in the current cycle |
Bandwidth bandwidthperpersec = Bytesperperiod * period/1000
Datatransferthrottler.throttle () method
The method has two entry parameters:
- Numofbytes is the number of bytes that need to be sent/received since the last throttle was called to now;
- The Canceler is an external cancellation device that can be used to detect whether the flow control is canceled.
The Datatransferthrottler.throttle () method subtracts Curreserve from Numofbytes and then executes the following logic.
- If Curreserve is greater than 0, the current period is still surplus, and the throttle method returns directly.
- If Curreserve is less than or equal to 0, the current period has no margin and the following logic is executed.
> If the current time now is less than the current period end time Curperiodend, wait to the next cycle
> If the current time now is greater than the current period end time curperiodend and less than curperiodstart+periodextension, the description has entered the next cycle and throttle should not be used for a long time, The Curreserve is added to the bytesperperiod of the total number of bytes that can be transmitted in the next cycle, and the Curperiodstart is set to the beginning of the next cycle.
> If the current time now is greater than curperiodstart+periodextension, it may throttler for a long time, discarding the previous cycle.
Datatransferthrottler.throttle () method source code is as follows:
PublicsynchronizedvoidThrottle (LongNumofbytes, Canceler Canceler) {if(numofbytes <= 0) {return; }//Current period margin minus the number of bytes that need to be sent/received NumofbytesCurreserve-= numofbytes; bytesalreadyused + = Numofbytes;//If Curreserve is less than or equal to 0, the current period has no margin while(Curreserve <= 0) {//If a valid cancellation Canceler is passed in, and the cancellation state of the cancellation iscancelled is true, exit the while loop directlyif(Canceler! = null && canceler.iscancelled ()) {return; }Longnow = Monotonicnow ();//Calculates the current cycle end time and stores it in the Curperiodend variableLongCurperiodend = Curperiodstart + period;if(Now < Curperiodend) {//wait for the next cycle so that curreserve can increaseTry{Wait (curperiodend-now); }Catch(Interruptedexception e) {//Terminates the throttle and resets the interrupted state to ensure that the other interrupt processors in the call stack can execute correctlyThread. CurrentThread (). interrupt (); Break; } }//If the current time now is curperiodend late than the current end time and is less than curperiodstart+periodextension (3 times times the period), the next cycle is entered//and add Bytesperperiod to CurreserveElseif(Now < (Curperiodstart + periodextension)) {Curperiodstart = Curperiodend; Curreserve + = Bytesperperiod; }//If the current time now is greater than curperiodstart+periodextension, it may throttler for a long time without using, discarding the previous cycleElse{Curperiodstart = now; Curreserve = bytesperperiod-bytesalreadyused; }} bytesalreadyused-= Numofbytes;}
Summarize
The Datatransferthrottler class can control the average rate of data transfer over a period of time. However, the instantaneous rate may get out of control at the end of a cycle.
Resources
hadoop-release-2.5.0 Source
Reprint please attach original blog address: http://blog.csdn.net/jeff_fangji/article/details/44258827
Hadoop Source Analysis: HDFs data read/write traffic control (Datatransferthrottler Class)