1616import org .elasticsearch .action .search .SearchRequest ;
1717import org .elasticsearch .common .logging .AccumulatingMockAppender ;
1818import org .elasticsearch .common .logging .Loggers ;
19+ import org .elasticsearch .common .logging .activity .ActivityLogProducer ;
1920import org .elasticsearch .common .logging .activity .QueryLogging ;
2021import org .elasticsearch .common .settings .Settings ;
2122import org .elasticsearch .index .query .MatchAllQueryBuilder ;
2930import java .util .Map ;
3031
3132import static org .elasticsearch .common .logging .activity .ActivityLogger .ACTIVITY_LOGGER_ENABLED ;
33+ import static org .elasticsearch .common .logging .activity .QueryLogging .QUERY_FIELD_INDICES ;
3234import static org .elasticsearch .common .logging .activity .QueryLogging .QUERY_FIELD_IS_CCS ;
35+ import static org .elasticsearch .common .logging .activity .QueryLogging .QUERY_FIELD_IS_REMOTE ;
3336import static org .elasticsearch .common .logging .activity .QueryLogging .QUERY_FIELD_REMOTE_COUNT ;
3437import static org .elasticsearch .test .ActivityLoggingUtils .assertMessageSuccess ;
3538import static org .elasticsearch .test .ActivityLoggingUtils .getMessageData ;
@@ -87,6 +90,13 @@ public void disableActivityLogger() {
8790 .setPersistentSettings (Settings .builder ().put (ACTIVITY_LOGGER_ENABLED .getKey (), (String ) null ))
8891 .get ()
8992 );
93+ assertAcked (
94+ client (REMOTE_CLUSTER ).admin ()
95+ .cluster ()
96+ .prepareUpdateSettings (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT )
97+ .setPersistentSettings (Settings .builder ().put (ACTIVITY_LOGGER_ENABLED .getKey (), (String ) null ))
98+ .get ()
99+ );
90100 }
91101
92102 /**
@@ -112,6 +122,8 @@ public void testCCSLoggingWithRemote() throws Exception {
112122 assertMessageSuccess (message , SearchLogContext .TYPE , "match_all" );
113123 assertThat (message .get (QUERY_FIELD_IS_CCS ), equalTo ("true" ));
114124 assertThat (message .get (QUERY_FIELD_REMOTE_COUNT ), equalTo ("1" ));
125+ assertThat (message .get (QUERY_FIELD_INDICES ), equalTo (localIndex + "," + REMOTE_CLUSTER + ":" + remoteIndex ));
126+ assertNull (message .get (QUERY_FIELD_IS_REMOTE ));
115127 }
116128 // Remote only
117129 {
@@ -128,6 +140,7 @@ public void testCCSLoggingWithRemote() throws Exception {
128140 assertMessageSuccess (message , SearchLogContext .TYPE , "match_all" );
129141 assertThat (message .get (QUERY_FIELD_IS_CCS ), equalTo ("true" ));
130142 assertThat (message .get (QUERY_FIELD_REMOTE_COUNT ), equalTo ("1" ));
143+ assertNull (message .get (QUERY_FIELD_IS_REMOTE ));
131144 }
132145 // Wildcard
133146 {
@@ -144,6 +157,8 @@ public void testCCSLoggingWithRemote() throws Exception {
144157 assertMessageSuccess (message , SearchLogContext .TYPE , "match_all" );
145158 assertThat (message .get (QUERY_FIELD_IS_CCS ), equalTo ("true" ));
146159 assertThat (message .get (QUERY_FIELD_REMOTE_COUNT ), equalTo ("1" ));
160+ assertThat (message .get (QUERY_FIELD_INDICES ), equalTo ("*:" + remoteIndex ));
161+ assertNull (message .get (QUERY_FIELD_IS_REMOTE ));
147162 }
148163 }
149164
@@ -167,4 +182,47 @@ public void testLocalOnlySearchDoesNotLogCcsFields() throws Exception {
167182 assertNull (message .get (QUERY_FIELD_IS_CCS ));
168183 assertNull (message .get (QUERY_FIELD_REMOTE_COUNT ));
169184 }
185+
186+ public void testCCSLoggingOnRemote () throws Exception {
187+ assertAcked (
188+ client (REMOTE_CLUSTER ).admin ()
189+ .cluster ()
190+ .prepareUpdateSettings (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT )
191+ .setPersistentSettings (Settings .builder ().put (ACTIVITY_LOGGER_ENABLED .getKey (), true ))
192+ .get ()
193+ );
194+ Map <String , Object > testClusterInfo = setupTwoClusters ();
195+ String localIndex = (String ) testClusterInfo .get ("local.index" );
196+ String remoteIndex = (String ) testClusterInfo .get ("remote.index" );
197+
198+ // Remote + local
199+ {
200+ SearchRequest searchRequest = new SearchRequest (localIndex , REMOTE_CLUSTER + ":" + remoteIndex );
201+ // remote only logs on MRT=true
202+ searchRequest .setCcsMinimizeRoundtrips (true );
203+ searchRequest .source (new SearchSourceBuilder ().query (new MatchAllQueryBuilder ()).size (10 ));
204+
205+ assertResponse (client (LOCAL_CLUSTER ).search (searchRequest ), Assert ::assertNotNull );
206+
207+ assertThat (appender .events .size (), equalTo (2 )); // logged on both sides!
208+ for (var event : appender .events ) {
209+ Map <String , String > message = getMessageData (event );
210+ assertMessageSuccess (message , SearchLogContext .TYPE , "match_all" );
211+ if (message .get (ActivityLogProducer .PARENT_TASK_ID_FIELD ) == null ) {
212+ // Local side
213+ assertThat (message .get (QUERY_FIELD_IS_CCS ), equalTo ("true" ));
214+ assertThat (message .get (QUERY_FIELD_REMOTE_COUNT ), equalTo ("1" ));
215+ assertThat (message .get (QUERY_FIELD_INDICES ), equalTo (localIndex + "," + REMOTE_CLUSTER + ":" + remoteIndex ));
216+ assertNull (message .get (QUERY_FIELD_IS_REMOTE ));
217+ } else {
218+ // Remote side
219+ assertNull (message .get (QUERY_FIELD_IS_CCS ));
220+ assertNull (message .get (QUERY_FIELD_REMOTE_COUNT ));
221+ assertThat (message .get (QUERY_FIELD_IS_REMOTE ), equalTo ("true" ));
222+ assertThat (message .get (QUERY_FIELD_INDICES ), equalTo (remoteIndex ));
223+ }
224+ }
225+ }
226+ }
227+
170228}
0 commit comments