7474import java .util .concurrent .CountDownLatch ;
7575import java .util .concurrent .CyclicBarrier ;
7676import java .util .concurrent .ExecutionException ;
77+ import java .util .concurrent .Future ;
7778import java .util .concurrent .Semaphore ;
7879import java .util .concurrent .TimeUnit ;
7980import java .util .concurrent .atomic .AtomicBoolean ;
8788import static org .hamcrest .Matchers .containsString ;
8889import static org .hamcrest .Matchers .empty ;
8990import static org .hamcrest .Matchers .equalTo ;
91+ import static org .hamcrest .Matchers .greaterThan ;
9092import static org .hamcrest .Matchers .hasToString ;
9193import static org .hamcrest .Matchers .instanceOf ;
94+ import static org .hamcrest .Matchers .lessThan ;
9295import static org .hamcrest .Matchers .not ;
9396import static org .hamcrest .Matchers .notNullValue ;
9497import static org .hamcrest .Matchers .nullValue ;
@@ -651,6 +654,74 @@ public void handleException(TransportException exp) {
651654 }
652655 }
653656
657+ public void testIndexingDataCompression () throws Exception {
658+ try (MockTransportService serviceC = buildService ("TS_C" , CURRENT_VERSION , Settings .EMPTY )) {
659+ String component = "cccccccccooooooooooooooommmmmmmmmmmppppppppppprrrrrrrreeeeeeeeeessssssssiiiiiiiiiibbbbbbbbllllllllleeeeee" ;
660+ StringBuilder builder = new StringBuilder ();
661+ for (int i = 0 ; i < 30 ; ++i ) {
662+ builder .append (component );
663+ }
664+ String text = builder .toString ();
665+ TransportRequestHandler <StringMessageRequest > handler = (request , channel , task ) -> {
666+ assertThat (text , equalTo (request .message ));
667+ try {
668+ channel .sendResponse (new StringMessageResponse ("" ));
669+ } catch (IOException e ) {
670+ logger .error ("Unexpected failure" , e );
671+ fail (e .getMessage ());
672+ }
673+ };
674+ serviceA .registerRequestHandler ("internal:sayHello" , ThreadPool .Names .GENERIC , StringMessageRequest ::new , handler );
675+ serviceC .registerRequestHandler ("internal:sayHello" , ThreadPool .Names .GENERIC , StringMessageRequest ::new , handler );
676+
677+ Settings settingsWithCompress = Settings .builder ()
678+ .put (TransportSettings .TRANSPORT_COMPRESS .getKey (), Compression .Enabled .INDEXING_DATA )
679+ .put (TransportSettings .TRANSPORT_COMPRESSION_SCHEME .getKey (),
680+ randomFrom (Compression .Scheme .DEFLATE , Compression .Scheme .LZ4 ))
681+ .build ();
682+ ConnectionProfile connectionProfile = ConnectionProfile .buildDefaultConnectionProfile (settingsWithCompress );
683+ serviceC .connectToNode (serviceA .getLocalDiscoNode (), connectionProfile );
684+ serviceA .connectToNode (serviceC .getLocalDiscoNode (), connectionProfile );
685+
686+ TransportResponseHandler <StringMessageResponse > responseHandler = new TransportResponseHandler <StringMessageResponse >() {
687+ @ Override
688+ public StringMessageResponse read (StreamInput in ) throws IOException {
689+ return new StringMessageResponse (in );
690+ }
691+
692+ @ Override
693+ public String executor () {
694+ return ThreadPool .Names .GENERIC ;
695+ }
696+
697+ @ Override
698+ public void handleResponse (StringMessageResponse response ) {
699+ }
700+
701+ @ Override
702+ public void handleException (TransportException exp ) {
703+ logger .error ("Unexpected failure" , exp );
704+ fail ("got exception instead of a response: " + exp .getMessage ());
705+ }
706+ };
707+
708+ Future <StringMessageResponse > compressed = serviceC .submitRequest (serviceA .getLocalDiscoNode (), "internal:sayHello" ,
709+ new StringMessageRequest (text , -1 , true ), responseHandler );
710+ Future <StringMessageResponse > uncompressed = serviceA .submitRequest (serviceC .getLocalDiscoNode (), "internal:sayHello" ,
711+ new StringMessageRequest (text , -1 , false ), responseHandler );
712+
713+ compressed .get ();
714+ uncompressed .get ();
715+ final long bytesLength ;
716+ try (BytesStreamOutput output = new BytesStreamOutput ()) {
717+ new StringMessageRequest (text , -1 ).writeTo (output );
718+ bytesLength = output .bytes ().length ();
719+ }
720+ assertThat (serviceA .transport ().getStats ().getRxSize ().getBytes (), lessThan (bytesLength ));
721+ assertThat (serviceC .transport ().getStats ().getRxSize ().getBytes (), greaterThan (bytesLength ));
722+ }
723+ }
724+
654725 public void testErrorMessage () {
655726 serviceA .registerRequestHandler ("internal:sayHelloException" , ThreadPool .Names .GENERIC , StringMessageRequest ::new ,
656727 (request , channel , task ) -> {
@@ -1188,14 +1259,20 @@ public void handleException(TransportException exp) {
11881259 }
11891260 }
11901261
1191- public static class StringMessageRequest extends TransportRequest {
1262+ public static class StringMessageRequest extends TransportRequest implements RawIndexingDataTransportRequest {
11921263
11931264 private String message ;
11941265 private long timeout ;
1266+ private boolean isRawIndexingData = false ;
11951267
11961268 StringMessageRequest (String message , long timeout ) {
1269+ this (message , timeout , false );
1270+ }
1271+
1272+ StringMessageRequest (String message , long timeout , boolean isRawIndexingData ) {
11971273 this .message = message ;
11981274 this .timeout = timeout ;
1275+ this .isRawIndexingData = isRawIndexingData ;
11991276 }
12001277
12011278 public StringMessageRequest (StreamInput in ) throws IOException {
@@ -1212,6 +1289,11 @@ public long timeout() {
12121289 return timeout ;
12131290 }
12141291
1292+ @ Override
1293+ public boolean isRawIndexingData () {
1294+ return isRawIndexingData ;
1295+ }
1296+
12151297 @ Override
12161298 public void writeTo (StreamOutput out ) throws IOException {
12171299 super .writeTo (out );
0 commit comments