2727
2828public class JavaAgent implements Agent {
2929
30- private static final int MAX_BUCKET_SIZE = 1024 * 1024 ;
30+ private static final int MAX_BUCKET_SIZE = 2 * 1024 * 1024 ;
3131 private Map <String , Bucket > buckets = new HashMap <String , Bucket >();
3232 private Set <Receiver > receivers = new HashSet <Receiver >();
3333 private int collectionInterval = 2 ;
@@ -44,33 +44,52 @@ public JavaAgent() {
4444
4545 private void init () {
4646 exec = Executors .newSingleThreadScheduledExecutor ();
47- exec .scheduleAtFixedRate (this ::empty , collectionInterval , collectionInterval , TimeUnit .SECONDS );
47+ exec .scheduleAtFixedRate (this ::drain , collectionInterval , collectionInterval , TimeUnit .SECONDS );
4848 }
4949
5050 public void pushData (String type , String data ) {
5151 synchronized (buckets ) {
5252 Bucket bucket = buckets .get (type );
5353 if (bucket == null ) {
54- bucket = new StringDataBucket ();
54+ bucket = new StringDataBucket (MAX_BUCKET_SIZE );
5555 buckets .put (type , bucket );
5656 }
57-
58- if (( bucket . getSize () + data . length ()) > MAX_BUCKET_SIZE ) {
59- emit ( type , bucket .empty ());
57+ if (! bucket . addData ( data )) {
58+ // System.err.println("Javametrics: data dropped. Bucket size="
59+ // + bucket.getSize ());
6060 }
61-
62- bucket .pushData (data );
6361 }
6462 }
6563
66- private void empty () {
64+ private void drain () {
6765 synchronized (buckets ) {
6866 buckets .forEach ((name , bucket ) -> {
69- emit (name , bucket . empty () );
67+ drainBatched (name , bucket );
7068 });
7169 }
7270 }
7371
72+ @ SuppressWarnings ("unused" )
73+ private void drain (String type , Bucket bucket ) {
74+ String data = bucket .getNext ();
75+ while (data != null ) {
76+ emit (type , data );
77+ data = bucket .getNext ();
78+ }
79+ }
80+
81+ private void drainBatched (String type , Bucket bucket ) {
82+ StringBuffer sb = new StringBuffer ();
83+ String data = bucket .getNext ();
84+ while (data != null ) {
85+ sb .append (data );
86+ data = bucket .getNext ();
87+ }
88+ if (sb .length () > 0 ) {
89+ emit (type , sb .toString ());
90+ }
91+ }
92+
7493 private void emit (String type , String data ) {
7594 if (data != null ) {
7695 receivers .forEach ((receiver ) -> {
0 commit comments