2222import  org .elasticsearch .cluster .node .DiscoveryNode ;
2323import  org .elasticsearch .cluster .node .DiscoveryNodeUtils ;
2424import  org .elasticsearch .cluster .node .DiscoveryNodes ;
25+ import  org .elasticsearch .common .bytes .ReleasableBytesReference ;
2526import  org .elasticsearch .common .component .Lifecycle ;
27+ import  org .elasticsearch .common .compress .CompressorFactory ;
28+ import  org .elasticsearch .common .io .Streams ;
2629import  org .elasticsearch .common .io .stream .BytesStreamOutput ;
2730import  org .elasticsearch .common .io .stream .NamedWriteableAwareStreamInput ;
2831import  org .elasticsearch .common .io .stream .NamedWriteableRegistryTests ;
32+ import  org .elasticsearch .common .io .stream .OutputStreamStreamOutput ;
2933import  org .elasticsearch .common .io .stream .StreamOutput ;
3034import  org .elasticsearch .common .settings .Settings ;
3135import  org .elasticsearch .common .util .concurrent .AbstractRunnable ;
3842import  org .elasticsearch .test .transport .MockTransport ;
3943import  org .elasticsearch .threadpool .TestThreadPool ;
4044import  org .elasticsearch .threadpool .ThreadPool ;
45+ import  org .elasticsearch .transport .BytesTransportRequest ;
4146import  org .elasticsearch .transport .CloseableConnection ;
4247import  org .elasticsearch .transport .RemoteTransportException ;
4348import  org .elasticsearch .transport .TestTransportChannel ;
4954import  org .elasticsearch .transport .TransportService ;
5055import  org .elasticsearch .xcontent .ToXContent ;
5156
57+ import  java .io .IOException ;
5258import  java .util .ArrayList ;
5359import  java .util .Collections ;
5460import  java .util .Iterator ;
@@ -155,6 +161,7 @@ public void doRun() {
155161            final  var  joinValidationService  = new  JoinValidationService (
156162                settings ,
157163                transportService ,
164+                 writableRegistry (),
158165                () -> usually () ? clusterState  : null ,
159166                clusterState ::metadata ,
160167                List .of ()
@@ -286,7 +293,14 @@ public void writeTo(StreamOutput out) {}
286293        );
287294
288295        // registers request handler 
289-         new  JoinValidationService (Settings .EMPTY , joiningNodeTransportService , () -> clusterState , clusterState ::metadata , List .of ());
296+         new  JoinValidationService (
297+             Settings .EMPTY ,
298+             joiningNodeTransportService ,
299+             writableRegistry (),
300+             () -> clusterState ,
301+             clusterState ::metadata ,
302+             List .of ()
303+         );
290304
291305        joiningNodeTransportService .start ();
292306        joiningNodeTransportService .acceptIncomingRequests ();
@@ -325,6 +339,7 @@ protected void onSendRequest(long requestId, String action, TransportRequest req
325339        final  var  joinValidationService  = new  JoinValidationService (
326340            Settings .EMPTY ,
327341            masterTransportService ,
342+             writableRegistry (),
328343            () -> clusterState ,
329344            clusterState ::metadata ,
330345            List .of ()
@@ -349,7 +364,7 @@ protected void onSendRequest(long requestId, String action, TransportRequest req
349364        }
350365    }
351366
352-     public  void  testJoinValidationRejectsMismatchedClusterUUID () {
367+     public  void  testJoinValidationRejectsMismatchedClusterUUID () throws   IOException   {
353368        final  var  deterministicTaskQueue  = new  DeterministicTaskQueue ();
354369        final  var  mockTransport  = new  MockTransport ();
355370        final  var  localNode  = DiscoveryNodeUtils .create ("node0" );
@@ -371,7 +386,14 @@ public void testJoinValidationRejectsMismatchedClusterUUID() {
371386        final  var  settings  = Settings .builder ().put (Environment .PATH_DATA_SETTING .getKey (), dataPath ).build ();
372387
373388        // registers request handler 
374-         new  JoinValidationService (settings , transportService , () -> localClusterState , localClusterState ::metadata , List .of ());
389+         new  JoinValidationService (
390+             settings ,
391+             transportService ,
392+             writableRegistry (),
393+             () -> localClusterState ,
394+             localClusterState ::metadata ,
395+             List .of ()
396+         );
375397
376398        transportService .start ();
377399        transportService .acceptIncomingRequests ();
@@ -384,7 +406,7 @@ public void testJoinValidationRejectsMismatchedClusterUUID() {
384406        transportService .sendRequest (
385407            localNode ,
386408            JoinValidationService .JOIN_VALIDATE_ACTION_NAME ,
387-             new   ValidateJoinRequest (otherClusterState ),
409+             serializeClusterState (otherClusterState ),
388410            new  ActionListenerResponseHandler <>(future , in  -> TransportResponse .Empty .INSTANCE , TransportResponseHandler .TRANSPORT_WORKER )
389411        );
390412        deterministicTaskQueue .runAllTasks ();
@@ -401,6 +423,22 @@ public void testJoinValidationRejectsMismatchedClusterUUID() {
401423        );
402424    }
403425
426+     private  static  BytesTransportRequest  serializeClusterState (ClusterState  clusterState ) {
427+         try  (
428+             var  bytesStream  = new  BytesStreamOutput ();
429+             var  compressedStream  = new  OutputStreamStreamOutput (
430+                 CompressorFactory .COMPRESSOR .threadLocalOutputStream (Streams .flushOnCloseStream (bytesStream ))
431+             )
432+         ) {
433+             compressedStream .setTransportVersion (TransportVersion .current ());
434+             clusterState .writeTo (compressedStream );
435+             compressedStream .flush ();
436+             return  new  BytesTransportRequest (ReleasableBytesReference .wrap (bytesStream .bytes ()), TransportVersion .current ());
437+         } catch  (Exception  e ) {
438+             throw  new  AssertionError (e );
439+         }
440+     }
441+ 
404442    public  void  testJoinValidationRunsJoinValidators () {
405443        final  var  deterministicTaskQueue  = new  DeterministicTaskQueue ();
406444        final  var  mockTransport  = new  MockTransport ();
@@ -420,11 +458,12 @@ public void testJoinValidationRunsJoinValidators() {
420458        new  JoinValidationService (
421459            Settings .EMPTY ,
422460            transportService ,
461+             writableRegistry (),
423462            () -> localClusterState ,
424463            localClusterState ::metadata ,
425464            List .of ((node , state ) -> {
426465                assertSame (node , localNode );
427-                 assertSame (state , stateForValidation );
466+                 assertEquals (state . stateUUID () , stateForValidation . stateUUID () );
428467                throw  new  IllegalStateException ("simulated validation failure" );
429468            })
430469        ); // registers request handler 
@@ -435,7 +474,7 @@ public void testJoinValidationRunsJoinValidators() {
435474        transportService .sendRequest (
436475            localNode ,
437476            JoinValidationService .JOIN_VALIDATE_ACTION_NAME ,
438-             new   ValidateJoinRequest (stateForValidation ),
477+             serializeClusterState (stateForValidation ),
439478            new  ActionListenerResponseHandler <>(future , in  -> TransportResponse .Empty .INSTANCE , TransportResponseHandler .TRANSPORT_WORKER )
440479        );
441480        deterministicTaskQueue .runAllTasks ();
@@ -467,9 +506,16 @@ protected void onSendRequest(long requestId, String action, TransportRequest req
467506            null ,
468507            Collections .emptySet ()
469508        );
470-         final  var  joinValidationService  = new  JoinValidationService (Settings .EMPTY , masterTransportService , () -> null , () -> {
471-             throw  new  AssertionError ("should not be called" );
472-         }, List .of ());
509+         final  var  joinValidationService  = new  JoinValidationService (
510+             Settings .EMPTY ,
511+             masterTransportService ,
512+             writableRegistry (),
513+             () -> null ,
514+             () -> {
515+                 throw  new  AssertionError ("should not be called" );
516+             },
517+             List .of ()
518+         );
473519        masterTransportService .start ();
474520        masterTransportService .acceptIncomingRequests ();
475521
0 commit comments