1010
1111import org .elasticsearch .action .ActionListener ;
1212import org .elasticsearch .action .ActionListenerResponseHandler ;
13+ import org .elasticsearch .action .ActionResponse ;
1314import org .elasticsearch .action .ActionType ;
1415import org .elasticsearch .action .support .ActionFilters ;
15- import org .elasticsearch .action .support .RefCountingListener ;
1616import org .elasticsearch .action .support .replication .BasicReplicationRequest ;
17+ import org .elasticsearch .action .support .replication .ReplicationOperation ;
1718import org .elasticsearch .action .support .replication .ReplicationResponse ;
1819import org .elasticsearch .action .support .replication .TransportReplicationAction ;
19- import org .elasticsearch .cluster .ClusterState ;
2020import org .elasticsearch .cluster .action .shard .ShardStateAction ;
21- import org .elasticsearch .cluster .node .DiscoveryNode ;
22- import org .elasticsearch .cluster .routing .ShardRouting ;
21+ import org .elasticsearch .cluster .routing .IndexShardRoutingTable ;
2322import org .elasticsearch .cluster .service .ClusterService ;
2423import org .elasticsearch .common .inject .Inject ;
2524import org .elasticsearch .common .io .stream .StreamInput ;
2827import org .elasticsearch .indices .IndicesService ;
2928import org .elasticsearch .logging .LogManager ;
3029import org .elasticsearch .logging .Logger ;
31- import org .elasticsearch .tasks .Task ;
3230import org .elasticsearch .threadpool .ThreadPool ;
33- import org .elasticsearch .transport .TransportRequestOptions ;
34- import org .elasticsearch .transport .TransportResponse ;
3531import org .elasticsearch .transport .TransportService ;
3632
3733import java .io .IOException ;
38- import java .util .function .Predicate ;
39- import java .util .stream .Collectors ;
4034
4135public class TransportShardRefreshAction extends TransportReplicationAction <
4236 BasicReplicationRequest ,
43- BasicReplicationRequest ,
37+ ShardRefreshReplicaRequest ,
4438 ReplicationResponse > {
4539
4640 private static final Logger logger = LogManager .getLogger (TransportShardRefreshAction .class );
@@ -69,10 +63,11 @@ public TransportShardRefreshAction(
6963 shardStateAction ,
7064 actionFilters ,
7165 BasicReplicationRequest ::new ,
72- BasicReplicationRequest ::new ,
66+ ShardRefreshReplicaRequest ::new ,
7367 ThreadPool .Names .REFRESH
7468 );
75- new TransportUnpromotableShardRefreshAction (transportService , actionFilters , indicesService );
69+ // registers the unpromotable version of shard refresh action
70+ new TransportUnpromotableShardRefreshAction (clusterService , transportService , actionFilters , indicesService );
7671 }
7772
7873 @ Override
@@ -84,53 +79,53 @@ protected ReplicationResponse newResponseInstance(StreamInput in) throws IOExcep
8479 protected void shardOperationOnPrimary (
8580 BasicReplicationRequest shardRequest ,
8681 IndexShard primary ,
87- ActionListener <PrimaryResult <BasicReplicationRequest , ReplicationResponse >> listener
82+ ActionListener <PrimaryResult <ShardRefreshReplicaRequest , ReplicationResponse >> listener
8883 ) {
89- try (var listeners = new RefCountingListener (listener .map (v -> new PrimaryResult <>(shardRequest , new ReplicationResponse ())))) {
90- var refreshResult = primary .refresh (SOURCE_API );
84+ ActionListener .completeWith (listener , () -> {
85+ ShardRefreshReplicaRequest replicaRequest = new ShardRefreshReplicaRequest (shardRequest .shardId (), primary .refresh (SOURCE_API ));
86+ replicaRequest .setParentTask (shardRequest .getParentTask ());
9187 logger .trace ("{} refresh request executed on primary" , primary .shardId ());
92-
93- // Forward the request to all nodes that hold unpromotable replica shards
94- final ClusterState clusterState = clusterService .state ();
95- final Task parentTaskId = taskManager .getTask (shardRequest .getParentTask ().getId ());
96- clusterState .routingTable ()
97- .shardRoutingTable (shardRequest .shardId ())
98- .assignedShards ()
99- .stream ()
100- .filter (Predicate .not (ShardRouting ::isPromotableToPrimary ))
101- .map (ShardRouting ::currentNodeId )
102- .collect (Collectors .toUnmodifiableSet ())
103- .forEach (nodeId -> {
104- final DiscoveryNode node = clusterState .nodes ().get (nodeId );
105- UnpromotableShardRefreshRequest request = new UnpromotableShardRefreshRequest (
106- primary .shardId (),
107- refreshResult .generation ()
108- );
109- logger .trace ("forwarding refresh request [{}] to node [{}]" , request , node );
110- transportService .sendChildRequest (
111- node ,
112- TransportUnpromotableShardRefreshAction .NAME ,
113- request ,
114- parentTaskId ,
115- TransportRequestOptions .EMPTY ,
116- new ActionListenerResponseHandler <>(
117- listeners .acquire (ignored -> {}),
118- (in ) -> TransportResponse .Empty .INSTANCE ,
119- ThreadPool .Names .REFRESH
120- )
121- );
122- });
123- } catch (Exception e ) {
124- listener .onFailure (e );
125- }
88+ return new PrimaryResult <>(replicaRequest , new ReplicationResponse ());
89+ });
12690 }
12791
12892 @ Override
129- protected void shardOperationOnReplica (BasicReplicationRequest request , IndexShard replica , ActionListener <ReplicaResult > listener ) {
93+ protected void shardOperationOnReplica (ShardRefreshReplicaRequest request , IndexShard replica , ActionListener <ReplicaResult > listener ) {
13094 ActionListener .completeWith (listener , () -> {
13195 replica .refresh (SOURCE_API );
13296 logger .trace ("{} refresh request executed on replica" , replica .shardId ());
13397 return new ReplicaResult ();
13498 });
13599 }
100+
101+ @ Override
102+ protected ReplicationOperation .Replicas <ShardRefreshReplicaRequest > newReplicasProxy () {
103+ return new UnpromotableReplicasRefreshProxy ();
104+ }
105+
106+ protected class UnpromotableReplicasRefreshProxy extends ReplicasProxy {
107+
108+ @ Override
109+ public void onPrimaryOperationComplete (
110+ ShardRefreshReplicaRequest replicaRequest ,
111+ IndexShardRoutingTable indexShardRoutingTable ,
112+ ActionListener <Void > listener
113+ ) {
114+ assert replicaRequest .primaryRefreshResult .refreshed () : "primary has not refreshed" ;
115+ UnpromotableShardRefreshRequest unpromotableReplicaRequest = new UnpromotableShardRefreshRequest (
116+ indexShardRoutingTable ,
117+ replicaRequest .primaryRefreshResult .generation ()
118+ );
119+ transportService .sendRequest (
120+ transportService .getLocalNode (),
121+ TransportUnpromotableShardRefreshAction .NAME ,
122+ unpromotableReplicaRequest ,
123+ new ActionListenerResponseHandler <>(
124+ listener .delegateFailure ((l , r ) -> l .onResponse (null )),
125+ (in ) -> ActionResponse .Empty .INSTANCE ,
126+ ThreadPool .Names .REFRESH
127+ )
128+ );
129+ }
130+ }
136131}
0 commit comments