2323import org .elasticsearch .action .admin .indices .stats .ShardStats ;
2424import org .elasticsearch .action .index .IndexRequest ;
2525import org .elasticsearch .cluster .metadata .IndexMetadata ;
26+ import org .elasticsearch .cluster .node .DiscoveryNodes ;
2627import org .elasticsearch .cluster .routing .ShardRouting ;
2728import org .elasticsearch .common .UUIDs ;
2829import org .elasticsearch .common .settings .Settings ;
4243import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
4344import static org .hamcrest .Matchers .greaterThan ;
4445
45- @ ESIntegTestCase .ClusterScope (scope = ESIntegTestCase .Scope .SUITE , numDataNodes = 2 )
46+ @ ESIntegTestCase .ClusterScope (scope = ESIntegTestCase .Scope .SUITE , numDataNodes = 2 , numClientNodes = 1 , transportClientRatio = 0.0D )
4647public class WriteMemoryLimitsIT extends ESIntegTestCase {
4748
49+ public static final String INDEX_NAME = "test" ;
50+
4851 @ Override
4952 protected Settings nodeSettings (int nodeOrdinal ) {
5053 return Settings .builder ()
@@ -69,15 +72,13 @@ protected int numberOfShards() {
6972 return 1 ;
7073 }
7174
72- @ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/58983" )
7375 public void testWriteBytesAreIncremented () throws Exception {
74- final String index = "test" ;
75- assertAcked (prepareCreate (index , Settings .builder ()
76+ assertAcked (prepareCreate (INDEX_NAME , Settings .builder ()
7677 .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
7778 .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 1 )));
78- ensureGreen (index );
79+ ensureGreen (INDEX_NAME );
7980
80- IndicesStatsResponse response = client ().admin ().indices ().prepareStats (index ).get ();
81+ IndicesStatsResponse response = client ().admin ().indices ().prepareStats (INDEX_NAME ).get ();
8182 String primaryId = Stream .of (response .getShards ())
8283 .map (ShardStats ::getShardRouting )
8384 .filter (ShardRouting ::primary )
@@ -90,8 +91,10 @@ public void testWriteBytesAreIncremented() throws Exception {
9091 .findAny ()
9192 .get ()
9293 .currentNodeId ();
93- String primaryName = client ().admin ().cluster ().prepareState ().get ().getState ().nodes ().get (primaryId ).getName ();
94- String replicaName = client ().admin ().cluster ().prepareState ().get ().getState ().nodes ().get (replicaId ).getName ();
94+ DiscoveryNodes nodes = client ().admin ().cluster ().prepareState ().get ().getState ().nodes ();
95+ String primaryName = nodes .get (primaryId ).getName ();
96+ String replicaName = nodes .get (replicaId ).getName ();
97+ String coordinatingOnlyNode = nodes .getCoordinatingOnlyNodes ().iterator ().next ().value .getName ();
9598
9699 final CountDownLatch replicationSendPointReached = new CountDownLatch (1 );
97100 final CountDownLatch latchBlockingReplicationSend = new CountDownLatch (1 );
@@ -118,7 +121,7 @@ public void testWriteBytesAreIncremented() throws Exception {
118121 final BulkRequest bulkRequest = new BulkRequest ();
119122 int totalRequestSize = 0 ;
120123 for (int i = 0 ; i < 80 ; ++i ) {
121- IndexRequest request = new IndexRequest (index ).id (UUIDs .base64UUID ())
124+ IndexRequest request = new IndexRequest (INDEX_NAME ).id (UUIDs .base64UUID ())
122125 .source (Collections .singletonMap ("key" , randomAlphaOfLength (50 )));
123126 totalRequestSize += request .ramBytesUsed ();
124127 assertTrue (request .ramBytesUsed () > request .source ().length ());
@@ -129,18 +132,19 @@ public void testWriteBytesAreIncremented() throws Exception {
129132 final long bulkShardRequestSize = totalRequestSize ;
130133
131134 try {
132- final ActionFuture <BulkResponse > successFuture = client (replicaName ).bulk (bulkRequest );
135+ final ActionFuture <BulkResponse > successFuture = client (coordinatingOnlyNode ).bulk (bulkRequest );
133136 replicationSendPointReached .await ();
134137
135138 WriteMemoryLimits primaryWriteLimits = internalCluster ().getInstance (WriteMemoryLimits .class , primaryName );
136139 WriteMemoryLimits replicaWriteLimits = internalCluster ().getInstance (WriteMemoryLimits .class , replicaName );
140+ WriteMemoryLimits coordinatingWriteLimits = internalCluster ().getInstance (WriteMemoryLimits .class , coordinatingOnlyNode );
137141
138- assertThat (primaryWriteLimits .getCoordinatingBytes (), greaterThan (bulkShardRequestSize ));
139- assertThat ( primaryWriteLimits .getPrimaryBytes (), greaterThan ( bulkShardRequestSize ));
140- assertEquals (0 , primaryWriteLimits . getReplicaBytes ());
141- assertEquals (bulkRequestSize , replicaWriteLimits .getCoordinatingBytes ());
142- assertEquals (0 , replicaWriteLimits . getPrimaryBytes ());
143- assertEquals (0 , replicaWriteLimits . getReplicaBytes ());
142+ assertThat (primaryWriteLimits .getWriteBytes (), greaterThan (bulkShardRequestSize ));
143+ assertEquals ( 0 , primaryWriteLimits .getReplicaWriteBytes ( ));
144+ assertEquals (0 , replicaWriteLimits . getWriteBytes ());
145+ assertEquals (0 , replicaWriteLimits .getReplicaWriteBytes ());
146+ assertEquals (bulkRequestSize , coordinatingWriteLimits . getWriteBytes ());
147+ assertEquals (0 , coordinatingWriteLimits . getReplicaWriteBytes ());
144148
145149 ThreadPool replicaThreadPool = replicaTransportService .getThreadPool ();
146150 // Block the replica Write thread pool
@@ -163,31 +167,45 @@ public void testWriteBytesAreIncremented() throws Exception {
163167 newActionsSendPointReached .await ();
164168 latchBlockingReplicationSend .countDown ();
165169
166- IndexRequest request = new IndexRequest (index ).id (UUIDs .base64UUID ())
170+ IndexRequest request = new IndexRequest (INDEX_NAME ).id (UUIDs .base64UUID ())
167171 .source (Collections .singletonMap ("key" , randomAlphaOfLength (50 )));
168172 final BulkRequest secondBulkRequest = new BulkRequest ();
169173 secondBulkRequest .add (request );
170174
171- ActionFuture <BulkResponse > secondFuture = client (replicaName ).bulk (secondBulkRequest );
175+ // Use the primary or the replica data node as the coordinating node this time
176+ boolean usePrimaryAsCoordinatingNode = randomBoolean ();
177+ final ActionFuture <BulkResponse > secondFuture ;
178+ if (usePrimaryAsCoordinatingNode ) {
179+ secondFuture = client (primaryName ).bulk (secondBulkRequest );
180+ } else {
181+ secondFuture = client (replicaName ).bulk (secondBulkRequest );
182+ }
172183
173184 final long secondBulkRequestSize = secondBulkRequest .ramBytesUsed ();
174185 final long secondBulkShardRequestSize = request .ramBytesUsed ();
175186
176- assertBusy (() -> assertEquals (bulkRequestSize + secondBulkRequestSize , replicaWriteLimits .getCoordinatingBytes ()));
177- assertBusy (() -> assertThat (replicaWriteLimits .getReplicaBytes (),
187+ if (usePrimaryAsCoordinatingNode ) {
188+ assertThat (primaryWriteLimits .getWriteBytes (), greaterThan (bulkShardRequestSize + secondBulkRequestSize ));
189+ assertEquals (0 , replicaWriteLimits .getWriteBytes ());
190+ } else {
191+ assertThat (primaryWriteLimits .getWriteBytes (), greaterThan (bulkShardRequestSize ));
192+ assertEquals (secondBulkRequestSize , replicaWriteLimits .getWriteBytes ());
193+ }
194+ assertEquals (bulkRequestSize , coordinatingWriteLimits .getWriteBytes ());
195+ assertBusy (() -> assertThat (replicaWriteLimits .getReplicaWriteBytes (),
178196 greaterThan (bulkShardRequestSize + secondBulkShardRequestSize )));
179197
180198 latchBlockingReplication .countDown ();
181199
182200 successFuture .actionGet ();
183201 secondFuture .actionGet ();
184202
185- assertEquals (0 , primaryWriteLimits .getCoordinatingBytes ());
186- assertEquals (0 , primaryWriteLimits .getPrimaryBytes ());
187- assertEquals (0 , primaryWriteLimits . getReplicaBytes ());
188- assertEquals (0 , replicaWriteLimits .getCoordinatingBytes ());
189- assertEquals (0 , replicaWriteLimits . getPrimaryBytes ());
190- assertEquals (0 , replicaWriteLimits . getReplicaBytes ());
203+ assertEquals (0 , primaryWriteLimits .getWriteBytes ());
204+ assertEquals (0 , primaryWriteLimits .getReplicaWriteBytes ());
205+ assertEquals (0 , replicaWriteLimits . getWriteBytes ());
206+ assertEquals (0 , replicaWriteLimits .getReplicaWriteBytes ());
207+ assertEquals (0 , coordinatingWriteLimits . getWriteBytes ());
208+ assertEquals (0 , coordinatingWriteLimits . getReplicaWriteBytes ());
191209 } finally {
192210 if (replicationSendPointReached .getCount () > 0 ) {
193211 replicationSendPointReached .countDown ();
0 commit comments