12
12
import org .apache .logging .log4j .LogManager ;
13
13
import org .apache .logging .log4j .Logger ;
14
14
import org .apache .logging .log4j .core .LogEvent ;
15
+ import org .junit .After ;
15
16
import org .junit .AfterClass ;
16
17
import org .junit .BeforeClass ;
17
18
import org .opensearch .action .search .SearchShardTask ;
18
19
import org .opensearch .common .logging .Loggers ;
19
20
import org .opensearch .common .logging .MockAppender ;
21
+ import org .opensearch .common .settings .ClusterSettings ;
20
22
import org .opensearch .common .settings .Settings ;
23
+ import org .opensearch .common .unit .TimeValue ;
21
24
import org .opensearch .tasks .ResourceStats ;
22
25
import org .opensearch .tasks .ResourceStatsType ;
23
26
import org .opensearch .tasks .ResourceUsageMetric ;
26
29
27
30
import java .util .Collections ;
28
31
29
- import static org .opensearch .tasks .consumer .TopNSearchTasksLogger .LOG_TOP_QUERIES_FREQUENCY ;
30
- import static org .opensearch .tasks .consumer .TopNSearchTasksLogger .LOG_TOP_QUERIES_SIZE ;
32
+ import static org .opensearch .tasks .consumer .TopNSearchTasksLogger .LOG_TOP_QUERIES_SIZE_SETTING ;
33
+ import static org .opensearch .tasks .consumer .TopNSearchTasksLogger .LOG_TOP_QUERIES_FREQUENCY_SETTING ;
34
+ import static org .opensearch .test .hamcrest .OpenSearchAssertions .assertAcked ;
31
35
32
36
public class TopNSearchTasksLoggerTests extends OpenSearchSingleNodeTestCase {
33
37
static MockAppender appender ;
@@ -42,15 +46,34 @@ public static void init() throws IllegalAccessException {
42
46
Loggers .addAppender (searchLogger , appender );
43
47
}
44
48
49
+ @ After
50
+ public void cleanupAfterTest () {
51
+ assertAcked (
52
+ client ().admin ()
53
+ .cluster ()
54
+ .prepareUpdateSettings ()
55
+ .setPersistentSettings (Settings .builder ().putNull ("*" ))
56
+ .setTransientSettings (Settings .builder ().putNull ("*" ))
57
+ );
58
+ }
59
+
45
60
@ AfterClass
46
61
public static void cleanup () {
47
62
Loggers .removeAppender (searchLogger , appender );
48
63
appender .stop ();
49
64
}
50
65
51
66
public void testLoggerWithTasks () {
52
- final Settings settings = Settings .builder ().put (LOG_TOP_QUERIES_SIZE , 1 ).put (LOG_TOP_QUERIES_FREQUENCY , "0ms" ).build ();
53
- topNSearchTasksLogger = new TopNSearchTasksLogger (settings );
67
+ final Settings settings = Settings .builder ()
68
+ .put (LOG_TOP_QUERIES_SIZE_SETTING .getKey (), 1 )
69
+ .put (LOG_TOP_QUERIES_FREQUENCY_SETTING .getKey (), "60s" )
70
+ .build ();
71
+ topNSearchTasksLogger = new TopNSearchTasksLogger (
72
+ settings ,
73
+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
74
+ );
75
+ // This setting overrides is just for testing purpose
76
+ topNSearchTasksLogger .setTopQueriesLogFrequencyInNanos (TimeValue .timeValueMillis (0 ));
54
77
generateTasks (5 );
55
78
LogEvent logEvent = appender .getLastEventAndReset ();
56
79
assertNotNull (logEvent );
@@ -59,16 +82,28 @@ public void testLoggerWithTasks() {
59
82
}
60
83
61
84
public void testLoggerWithoutTasks () {
62
- final Settings settings = Settings .builder ().put (LOG_TOP_QUERIES_SIZE , 1 ).put (LOG_TOP_QUERIES_FREQUENCY , "500ms" ).build ();
63
- topNSearchTasksLogger = new TopNSearchTasksLogger (settings );
85
+ final Settings settings = Settings .builder ()
86
+ .put (LOG_TOP_QUERIES_SIZE_SETTING .getKey (), 1 )
87
+ .put (LOG_TOP_QUERIES_FREQUENCY_SETTING .getKey (), "60s" )
88
+ .build ();
89
+ topNSearchTasksLogger = new TopNSearchTasksLogger (
90
+ settings ,
91
+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
92
+ );
64
93
65
94
assertNull (appender .getLastEventAndReset ());
66
95
}
67
96
68
97
public void testLoggerWithHighFrequency () {
69
98
// setting the frequency to a really large value and confirming that nothing gets written to log file.
70
- final Settings settings = Settings .builder ().put (LOG_TOP_QUERIES_SIZE , 1 ).put (LOG_TOP_QUERIES_FREQUENCY , "10m" ).build ();
71
- topNSearchTasksLogger = new TopNSearchTasksLogger (settings );
99
+ final Settings settings = Settings .builder ()
100
+ .put (LOG_TOP_QUERIES_SIZE_SETTING .getKey (), 1 )
101
+ .put (LOG_TOP_QUERIES_FREQUENCY_SETTING .getKey (), "10m" )
102
+ .build ();
103
+ topNSearchTasksLogger = new TopNSearchTasksLogger (
104
+ settings ,
105
+ new ClusterSettings (Settings .EMPTY , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS )
106
+ );
72
107
generateTasks (5 );
73
108
generateTasks (2 );
74
109
0 commit comments