88
99import java .io .IOException ;
1010import java .io .OutputStreamWriter ;
11- import java .io .UnsupportedEncodingException ;
1211import java .io .Writer ;
1312import java .net .InetAddress ;
1413import java .net .Socket ;
1514import java .net .UnknownHostException ;
15+ import java .nio .charset .StandardCharsets ;
1616import java .util .ArrayList ;
1717import java .util .Collection ;
1818import java .util .List ;
4040import com .demandware .carbonj .service .db .model .DataPointValue ;
4141import com .demandware .carbonj .service .engine .DataPoint ;
4242import com .demandware .carbonj .service .engine .LineProtocolHandler ;
43- import com .google .common .base .Throwables ;
4443
4544public class CarbonJClient
4645 implements AutoCloseable
@@ -61,15 +60,15 @@ public CarbonJClient( String ip, int httpPort, int dataPort )
6160 {
6261 this .dataPort = dataPort ;
6362 this .httpPort = httpPort ;
64- log .info ("CarbonjClient is using Dataport as " + dataPort + " , HttpPort as " + httpPort );
63+ log .info ("CarbonjClient is using Dataport as {} , HttpPort as {}" , dataPort , httpPort );
6564
6665 try
6766 {
6867 this .ip = InetAddress .getByName ( ip );
6968 }
7069 catch ( UnknownHostException e )
7170 {
72- throw Throwables . propagate ( e );
71+ throw new RuntimeException ( e );
7372 }
7473 }
7574
@@ -82,7 +81,7 @@ public void close()
8281 {
8382 lineProtocolSock .close ();
8483 }
85- catch ( IOException e )
84+ catch ( IOException ignored )
8685 {
8786 }
8887 }
@@ -92,7 +91,7 @@ public void close()
9291 }
9392 catch ( IOException e )
9493 {
95- throw Throwables . propagate ( e );
94+ throw new RuntimeException ( e );
9695 }
9796 }
9897
@@ -108,7 +107,7 @@ private synchronized Socket openPlaintextStream()
108107 }
109108 catch ( IOException e )
110109 {
111- throw Throwables . propagate ( e );
110+ throw new RuntimeException ( e );
112111 }
113112 }
114113
@@ -125,7 +124,7 @@ public synchronized void send( Writer os, DataPoint... dps )
125124 }
126125 catch ( IOException e )
127126 {
128- throw Throwables . propagate ( e );
127+ throw new RuntimeException ( e );
129128 }
130129 }
131130
@@ -147,7 +146,7 @@ public void send( DataPoint dp )
147146 }
148147 catch ( IOException e )
149148 {
150- throw Throwables . propagate ( e );
149+ throw new RuntimeException ( e );
151150 }
152151 send ( wrt , dp );
153152 }
@@ -164,31 +163,23 @@ private String cjAdminUrl( String cmd )
164163
165164 public Collection <String > listMetrics ( String pattern )
166165 {
167- ArrayList <String > ret = new ArrayList <String >();
166+ ArrayList <String > ret = new ArrayList <>();
168167 // http://localhost:56787/_dw/rest/carbonj/listmetrics2/{pattern}
169- doRestCall ( "listmetrics2/" + pattern , e -> ret . add ( e ) );
168+ doRestCall ( "listmetrics2/" + pattern , ret :: add );
170169 return ret ;
171170 }
172171
173172 public String listPointsWithId ( String dbName , String id )
174173 {
175- ArrayList <String > ret = new ArrayList <String >();
176- doRestCall ( String .format ( "/listpointswithid/%s/%s" , dbName , id ), s -> ret . add ( s ) );
174+ ArrayList <String > ret = new ArrayList <>();
175+ doRestCall ( String .format ( "/listpointswithid/%s/%s" , dbName , id ), ret :: add );
177176 return ret .get ( 0 );
178177 }
179178
180179 private void doPost ( String url , String body )
181180 {
182181 HttpPost post = new HttpPost ( cjAdminUrl ( url ) );
183- HttpEntity entity ;
184- try
185- {
186- entity = new ByteArrayEntity ( body .getBytes ( "UTF-8" ) );
187- }
188- catch ( UnsupportedEncodingException e1 )
189- {
190- throw Throwables .propagate ( e1 );
191- }
182+ HttpEntity entity = new ByteArrayEntity ( body .getBytes (StandardCharsets .UTF_8 ) );
192183 post .setEntity ( entity );
193184 try (CloseableHttpResponse resp = httpClient .execute ( post ))
194185 {
@@ -199,7 +190,7 @@ private void doPost( String url, String body )
199190 HttpEntity e = resp .getEntity ();
200191 try
201192 {
202- System .out .println ( IOUtils .readLines ( e .getContent () ) );
193+ System .out .println ( IOUtils .readLines ( e .getContent (), java . nio . charset . StandardCharsets . UTF_8 ) );
203194 }
204195 finally
205196 {
@@ -208,7 +199,7 @@ private void doPost( String url, String body )
208199 }
209200 catch ( Exception e )
210201 {
211- throw Throwables . propagate ( e );
202+ throw new RuntimeException ( e );
212203 }
213204 }
214205
@@ -235,13 +226,13 @@ private void doCall( HttpRequestBase req, Consumer<String> respHandler )
235226 catch ( Exception e )
236227 {
237228 e .printStackTrace ();
238- throw Throwables . propagate ( e );
229+ throw new RuntimeException ( e );
239230 }
240231 }
241232
242233 private void doRestCall ( String url , Consumer <String > respHandler )
243234 {
244- log .info ("cjAdminUrl url is " + cjAdminUrl ( url ));
235+ log .info ("cjAdminUrl url is {}" , cjAdminUrl (url ));
245236 doCall ( new HttpGet ( cjAdminUrl ( url ) ), respHandler );
246237 }
247238
@@ -252,50 +243,50 @@ public Collection<String> dumpNames( String filter )
252243
253244 public Collection <String > dumpNames ( String filter , Integer startId , String startName , Integer count )
254245 {
255- ArrayList <String > ret = new ArrayList <String >();
246+ ArrayList <String > ret = new ArrayList <>();
256247 // http://localhost:56787/_dw/rest/carbonj/listmetrics2/{pattern}
257248 StringBuilder path = new StringBuilder ( "dumpnames?" );
258249 if ( null != filter )
259250 {
260- path .append ( "filter=" + filter + "&" );
251+ path .append ("filter=" ). append ( filter ). append ( "&" );
261252 }
262253 if ( null != startId )
263254 {
264- path .append ( "startId=" + startId + "&" );
255+ path .append ("startId=" ). append ( startId ). append ( "&" );
265256 }
266257 if ( null != startName )
267258 {
268- path .append ( "startName=" + startName + "&" );
259+ path .append ("startName=" ). append ( startName ). append ( "&" );
269260 }
270261 if ( null != count )
271262 {
272- path .append ( "count=" + count + "&" );
263+ path .append ("count=" ). append ( count ). append ( "&" );
273264 }
274265
275- doRestCall ( path .toString (), e -> ret . add ( e ) );
266+ doRestCall ( path .toString (), ret :: add );
276267 return ret ;
277268 }
278269
279270 public Collection <String > cleanSeries ( String from , String filter , String exclude , Integer count , boolean dryRun )
280271 {
281- ArrayList <String > ret = new ArrayList <String >();
272+ ArrayList <String > ret = new ArrayList <>();
282273 // http://localhost:56787/_dw/rest/carbonj/listmetrics2/{pattern}
283274 StringBuilder path = new StringBuilder ( "cleanseries?dryRun=" + dryRun );
284275 if ( null != from )
285276 {
286- path .append ( "&from=" + from );
277+ path .append ("&from=" ). append ( from );
287278 }
288279 if ( null != count )
289280 {
290- path .append ( "&count=" + count );
281+ path .append ("&count=" ). append ( count );
291282 }
292283 if ( null != filter )
293284 {
294- path .append ( "&filter=" + filter );
285+ path .append ("&filter=" ). append ( filter );
295286 }
296287 if ( null != exclude )
297288 {
298- path .append ( "&exclude=" + exclude );
289+ path .append ("&exclude=" ). append ( exclude );
299290 }
300291
301292 doCall ( new HttpPost ( cjAdminUrl ( path .toString () ) ), e -> ret .add ( StringUtils .substringBefore ( e , ":" ) ) );
@@ -305,12 +296,11 @@ public Collection<String> cleanSeries( String from, String filter, String exclud
305296 public Collection <DataPoint > dumpLines ( String dbName , String startName , String filter , int from , int to )
306297 {
307298 ArrayList <DataPoint > ret = new ArrayList <>();
308- StringBuilder sb = new StringBuilder ( "dumplines/" + dbName );
309- sb .append ( "?start=" ).append ( null != startName ? startName : "" );
310- sb .append ( "&filter=" ).append ( null != filter ? filter : "" );
311- sb .append ( "&from=" ).append ( from );
312- sb .append ( "&to=" ).append ( to );
313- doRestCall ( sb .toString (), i -> {
299+ String sb = "dumplines/" + dbName + "?start=" + (null != startName ? startName : "" ) +
300+ "&filter=" + (null != filter ? filter : "" ) +
301+ "&from=" + from +
302+ "&to=" + to ;
303+ doRestCall (sb , i -> {
314304 DataPoint dataPoint = LineProtocolHandler .parse (i );
315305 if (dataPoint != null ) {
316306 ret .add (dataPoint );
@@ -350,39 +340,33 @@ public DumpResult dumpSeries( String dbName, int cursor, int count, String filte
350340 public DumpResult dumpSeries ( String dbName , int cursor , int count , String filter , String exclude , int from , int to )
351341 {
352342 ArrayList <DataPoint > data = new ArrayList <>();
353- int next = dumpSeries ( dbName , cursor , count , filter , exclude , from , to , d -> data . addAll ( d ) );
343+ int next = dumpSeries ( dbName , cursor , count , filter , exclude , from , to , data :: addAll );
354344 return new DumpResult ( data , next );
355345 }
356346
357347 public int dumpSeries ( String dbName , int cursor , int count , String filter , String exclude , int from , int to ,
358348 Consumer <List <DataPoint >> dataHander )
359349 {
360- StringBuilder sb = new StringBuilder ( "dumpseries/" + dbName );
361- sb .append ( "?cursor=" ).append ( cursor );
362- sb .append ( "&count=" ).append ( count );
363- sb .append ( "&filter=" ).append ( null != filter ? filter : "" );
364- sb .append ( "&exclude=" ).append ( null != exclude ? exclude : "" );
365- sb .append ( "&from=" ).append ( from );
366- sb .append ( "&to=" ).append ( to );
350+ String sb = "dumpseries/" + dbName + "?cursor=" + cursor +
351+ "&count=" + count +
352+ "&filter=" + (null != filter ? filter : "" ) +
353+ "&exclude=" + (null != exclude ? exclude : "" ) +
354+ "&from=" + from +
355+ "&to=" + to ;
367356 AtomicInteger ret = new AtomicInteger ( Integer .MAX_VALUE );
368- final Consumer <String > dumpConsumer = new Consumer <String >()
369- {
370- @ Override
371- public void accept ( String i )
357+ final Consumer <String > dumpConsumer = i -> {
358+ List <DataPoint > dp = DumpFormat .parseSeries ( i );
359+ if ( i .startsWith ( "ignore.dumpseries.cursor" ) )
372360 {
373- List <DataPoint > dp = DumpFormat .parseSeries ( i );
374- if ( i .startsWith ( "ignore.dumpseries.cursor" ) )
375- {
376- // special cursor value marking end of the stream
377- ret .set ( (int ) dp .get ( 0 ).val );
378- }
379- else
380- {
381- dataHander .accept ( dp );
382- }
361+ // special cursor value marking end of the stream
362+ ret .set ( (int ) dp .get ( 0 ).val );
363+ }
364+ else
365+ {
366+ dataHander .accept ( dp );
383367 }
384368 };
385- doRestCall ( sb . toString () , dumpConsumer );
369+ doRestCall (sb , dumpConsumer );
386370 return ret .get ();
387371 }
388372
@@ -401,7 +385,7 @@ public void loadSeries( String dbName, int start, int step, Map<String, double[]
401385 StringBuilder body = new StringBuilder ();
402386 for ( Map .Entry <String , double []> e : vals .entrySet () )
403387 {
404- ArrayList <DataPointValue > v = new ArrayList <DataPointValue >();
388+ ArrayList <DataPointValue > v = new ArrayList <>();
405389 for ( int i = 0 ; i < e .getValue ().length ; i ++ )
406390 {
407391 v .add ( new DataPointValue ( start + i * step , e .getValue ()[i ] ) );
0 commit comments