2525import java .util .Queue ;
2626import java .util .concurrent .ConcurrentHashMap ;
2727import java .util .concurrent .ConcurrentLinkedQueue ;
28- import java .util .concurrent .ExecutionException ;
2928import java .util .concurrent .ExecutorService ;
3029import java .util .concurrent .Executors ;
3130import java .util .concurrent .ScheduledExecutorService ;
4544import org .tron .common .overlay .server .SyncPool ;
4645import org .tron .common .utils .ExecutorLoop ;
4746import org .tron .common .utils .Sha256Hash ;
47+ import org .tron .common .utils .SlidingWindowCounter ;
4848import org .tron .common .utils .Time ;
4949import org .tron .core .capsule .BlockCapsule ;
5050import org .tron .core .capsule .BlockCapsule .BlockId ;
@@ -90,9 +90,14 @@ public class NodeImpl extends PeerConnectionDelegate implements Node {
9090 .maximumSize (10 ).expireAfterWrite (60 , TimeUnit .SECONDS )
9191 .recordStats ().build ();
9292
93- private Cache <Long , Long > fetchWaterLine = CacheBuilder .newBuilder ()
94- .expireAfterWrite (BLOCK_PRODUCED_INTERVAL / 1000 * MSG_CACHE_DURATION_IN_BLOCKS , TimeUnit .SECONDS )
95- .recordStats ().build ();
93+ // private Cache<Long, Long> fetchWaterLine = CacheBuilder.newBuilder()
94+ // .expireAfterWrite(BLOCK_PRODUCED_INTERVAL / 1000 * MSG_CACHE_DURATION_IN_BLOCKS, TimeUnit.SECONDS)
95+ // .recordStats().build();
96+
97+ private SlidingWindowCounter fetchWaterLine =
98+ new SlidingWindowCounter (BLOCK_PRODUCED_INTERVAL / 100 * MSG_CACHE_DURATION_IN_BLOCKS );
99+
100+
96101
97102 private int maxTrxsSize = 1_000_000 ;
98103
@@ -287,6 +292,9 @@ public Thread newThread(Runnable r) {
287292 private ScheduledExecutorService handleSyncBlockExecutor = Executors
288293 .newSingleThreadScheduledExecutor ();
289294
295+ private ScheduledExecutorService fetchWaterLineExecutor = Executors
296+ .newSingleThreadScheduledExecutor ();
297+
290298 private volatile boolean isHandleSyncBlockActive = false ;
291299
292300 private AtomicLong fetchSequenceCounter = new AtomicLong (0L );
@@ -473,6 +481,15 @@ private void activeTronPump() {
473481 logger .error ("Unhandled exception" , t );
474482 }
475483 }, 10 , 1 , TimeUnit .SECONDS );
484+
485+ //fetchWaterLine:
486+ fetchWaterLineExecutor .scheduleWithFixedDelay (() -> {
487+ try {
488+ fetchWaterLine .advance ();
489+ } catch (Throwable t ) {
490+ logger .error ("Unhandled exception" , t );
491+ }
492+ }, 1000 , 100 , TimeUnit .MILLISECONDS );
476493 }
477494
478495 private void consumerAdvObjToFetch () {
@@ -695,15 +712,16 @@ private void onHandleInventoryMessage(PeerConnection peer, InventoryMessage msg)
695712 if (msg .getInventoryType ().equals (InventoryType .TRX )
696713 && (peer .isAdvInvFull ()
697714 || isFlooded ())) {
698- logger .info ("A peer is flooding us, stop handle inv, the peer is:" + peer );
715+ logger .warn ("A peer is flooding us, stop handle inv, the peer is: " + peer );
699716 return ;
700717 }
701718
702719 peer .getAdvObjSpreadToUs ().put (id , System .currentTimeMillis ());
703720 if (!requested [0 ]) {
704721 if (!badAdvObj .containsKey (id )) {
705722 if (!advObjToFetch .contains (id )) {
706- addWaterLine ();
723+ fetchWaterLine .increase ();
724+ logger .info ("water line:" + fetchWaterLine .totalCount ());
707725 this .advObjToFetch .put (id , new PriorItem (new Item (id , msg .getInventoryType ()),
708726 fetchSequenceCounter .incrementAndGet ()));
709727 } else {
@@ -714,32 +732,27 @@ private void onHandleInventoryMessage(PeerConnection peer, InventoryMessage msg)
714732 }
715733 }
716734 }
717- logger .info ("Peer AdvObjSpreadToUs size: peer:" + peer .getNode ().getHost () + "::" + peer .getAdvObjSpreadToUs ().size ());
718- logger .info ("this advObjToFetch size:" + this .advObjToFetch .size ());
719- logger .info ("this advObjToRequest size" + this .advObjWeRequested .size ());
720735 }
721736
722737 private boolean isFlooded () {
723- try {
724- long value = fetchWaterLine .get (Time .getCurrentMillis () / 1000 , () -> 0L );
725- fetchWaterLine .put (Time .getCurrentMillis () / 1000 , value );
726- } catch (ExecutionException e ) {
727- e .printStackTrace ();
728- }
729-
730- return fetchWaterLine .asMap ().values ().stream ().mapToLong (Long ::longValue ).sum ()
738+ // try {
739+ // long value = fetchWaterLine.get(Time.getCurrentMillis() / 1000, () -> 0L);
740+ // fetchWaterLine.put(Time.getCurrentMillis() / 1000, value);
741+ // } catch (ExecutionException e) {
742+ // e.printStackTrace();
743+ // }
744+ return fetchWaterLine .totalCount ()
731745 > BLOCK_PRODUCED_INTERVAL * NET_MAX_TRX_PER_SECOND * MSG_CACHE_DURATION_IN_BLOCKS / 1000 ;
732746 }
733747
734- private void addWaterLine () {
735- try {
736- long value = fetchWaterLine .get (Time .getCurrentMillis () / 1000 , () -> 0L );
737- fetchWaterLine .put (Time .getCurrentMillis () / 1000 , ++value );
738- } catch (ExecutionException e ) {
739- e .printStackTrace ();
740- }
741- logger .info ("water line:" + fetchWaterLine .asMap ().values ().stream ().mapToLong (Long ::longValue ).sum ());
742- }
748+ // private void addWaterLine() {
749+ // try {
750+ // long value = fetchWaterLine.get(Time.getCurrentMillis() / 1000, () -> 0L);
751+ // fetchWaterLine.put(Time.getCurrentMillis() / 1000, ++value);
752+ // } catch (ExecutionException e) {
753+ // e.printStackTrace();
754+ // }
755+ // }
743756
744757 @ Override
745758 public void syncFrom (Sha256Hash myHeadBlockHash ) {
0 commit comments