Skip to content

Commit ca83044

Browse files
OAK-11613: expose ELASTIC_ENABLED metric (#2192)
* OAK-11613: expose ELASTIC_ENABLED metric * OAK-11613: (improvement) use Map.of() instead Collections.singletonMap() + mark index as @NotNull
1 parent 679a4c8 commit ca83044

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderService.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,22 @@ public class ElasticIndexProviderService {
161161

162162
@Activate
163163
private void activate(BundleContext bundleContext, Config config) {
164+
metricHandler = new ElasticMetricHandler(statisticsProvider);
164165
boolean disabled = Boolean.parseBoolean(System.getProperty(OAK_ELASTIC_PREFIX + PROP_DISABLED, Boolean.toString(config.disabled())));
165166
if (disabled) {
166167
LOG.info("Component disabled by configuration");
168+
metricHandler.markEnabled(false);
167169
return;
168170
}
169171

172+
elasticConnection = getElasticConnection(config);
173+
boolean isElasticAvailable = elasticConnection.isAvailable();
174+
metricHandler.markEnabled(isElasticAvailable);
175+
170176
whiteboard = new OsgiWhiteboard(bundleContext);
171177

172178
//initializeTextExtractionDir(bundleContext, config);
173179
//initializeExtractedTextCache(config, statisticsProvider);
174-
175-
elasticConnection = getElasticConnection(config);
176-
metricHandler = new ElasticMetricHandler(statisticsProvider);
177180
indexTracker = new ElasticIndexTracker(elasticConnection, metricHandler);
178181

179182
// register observer needed for index tracking
@@ -195,7 +198,11 @@ private void activate(BundleContext bundleContext, Config config) {
195198

196199
registerIndexProvider(bundleContext);
197200
registerIndexEditor(bundleContext);
198-
registerIndexCleaner(config);
201+
if (isElasticAvailable) {
202+
registerIndexCleaner(config);
203+
} else {
204+
LOG.warn("The Elastic cluster at {} is not reachable. The index cleaner job has not been enabled", elasticConnection);
205+
}
199206
}
200207

201208
@Deactivate
@@ -216,10 +223,6 @@ private void deactivate() {
216223
}
217224

218225
private void registerIndexCleaner(Config contextConfig) {
219-
if (!elasticConnection.isAvailable()) {
220-
LOG.warn("The Elastic cluster at {} is not reachable. The index cleaner job has not been enabled", elasticConnection);
221-
return;
222-
}
223226
if (contextConfig.remoteIndexCleanupFrequency() <= 0) {
224227
LOG.info("Index Cleaner disabled by configuration");
225228
return;

oak-search-elastic/src/main/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticMetricHandler.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.apache.jackrabbit.oak.stats.MeterStats;
2424
import org.apache.jackrabbit.oak.stats.StatisticsProvider;
2525
import org.apache.jackrabbit.oak.stats.TimerStats;
26+
import org.jetbrains.annotations.NotNull;
2627

27-
import java.util.Collections;
2828
import java.util.Map;
2929
import java.util.concurrent.TimeUnit;
3030
import java.util.function.BiFunction;
@@ -34,6 +34,8 @@
3434
*/
3535
public class ElasticMetricHandler {
3636

37+
private static final String ENABLED = "ELASTIC_ENABLED";
38+
3739
private static final String QUERY_RATE = "ELASTIC_QUERY_RATE";
3840
private static final String QUERY_INTERNAL_RATE = "ELASTIC_QUERY_INTERNAL_RATE";
3941

@@ -59,6 +61,15 @@ public ElasticMetricHandler(StatisticsProvider sp) {
5961
timer = statsProviderUtil.getTimerStats();
6062
}
6163

64+
/**
65+
* Marks the Elastic metric as enabled or disabled.
66+
*
67+
* @param enabled a boolean indicating whether the Elastic metric is enabled (true) or disabled (false)
68+
*/
69+
public void markEnabled(boolean enabled) {
70+
meter.apply(ENABLED, Map.of()).mark(enabled ? 1 : 0);
71+
}
72+
6273
/**
6374
* Tracks a new query using two metrics:
6475
* <ul>
@@ -69,8 +80,8 @@ public ElasticMetricHandler(StatisticsProvider sp) {
6980
* @param index the index passed as metric label
7081
* @param isRootQuery if {@code false} only {@code QUERY_INTERNAL_RATE} gets incremented
7182
*/
72-
public void markQuery(String index, boolean isRootQuery) {
73-
Map<String, String> labels = Collections.singletonMap("index", index);
83+
public void markQuery(@NotNull String index, boolean isRootQuery) {
84+
Map<String, String> labels = Map.of("index", index);
7485
if (isRootQuery) {
7586
meter.apply(QUERY_RATE, labels).mark();
7687
}
@@ -87,8 +98,8 @@ public void markQuery(String index, boolean isRootQuery) {
8798
* @param timedOut Elastic could time out while returning partial results. When {@code true} these
8899
* occurrences get tracked
89100
*/
90-
public void measureQuery(String index, int hits, long serverTimeMs, long totalTimeMs, boolean timedOut) {
91-
Map<String, String> labels = Collections.singletonMap("index", index);
101+
public void measureQuery(@NotNull String index, int hits, long serverTimeMs, long totalTimeMs, boolean timedOut) {
102+
Map<String, String> labels = Map.of("index", index);
92103
histogram.apply(QUERY_HITS, labels).update(hits);
93104
timer.apply(QUERY_SERVER_TIME, labels).update(serverTimeMs, TimeUnit.MILLISECONDS);
94105
timer.apply(QUERY_TOTAL_TIME, labels).update(totalTimeMs, TimeUnit.MILLISECONDS);
@@ -103,8 +114,8 @@ public void measureQuery(String index, int hits, long serverTimeMs, long totalTi
103114
* @param index the index passed as metric label
104115
* @param totalTimeMs the total execution time
105116
*/
106-
public void measureFailedQuery(String index, long totalTimeMs) {
107-
Map<String, String> labels = Collections.singletonMap("index", index);
117+
public void measureFailedQuery(@NotNull String index, long totalTimeMs) {
118+
Map<String, String> labels = Map.of("index", index);
108119
meter.apply(QUERY_FAILED_RATE, labels).mark();
109120
timer.apply(QUERY_TOTAL_TIME, labels).update(totalTimeMs, TimeUnit.MILLISECONDS);
110121
}
@@ -115,8 +126,8 @@ public void measureFailedQuery(String index, long totalTimeMs) {
115126
* @param index the index passed as metric label
116127
* @param numDocs the current number of documents. Only top level documents are tracked
117128
*/
118-
public void markDocuments(String index, long numDocs) {
119-
Map<String, String> labels = Collections.singletonMap("index", index);
129+
public void markDocuments(@NotNull String index, long numDocs) {
130+
Map<String, String> labels = Map.of("index", index);
120131
histogram.apply(INDEX_DOCUMENTS, labels).update(numDocs);
121132
}
122133

@@ -127,8 +138,8 @@ public void markDocuments(String index, long numDocs) {
127138
* @param primarySize the primary shards size
128139
* @param storeSize the total size in bytes. The value includes potential replicas
129140
*/
130-
public void markSize(String index, long primarySize, long storeSize) {
131-
Map<String, String> labels = Collections.singletonMap("index", index);
141+
public void markSize(@NotNull String index, long primarySize, long storeSize) {
142+
Map<String, String> labels = Map.of("index", index);
132143
histogram.apply(INDEX_SIZE, labels).update(primarySize);
133144
histogram.apply(INDEX_WITH_REPLICAS_SIZE, labels).update(storeSize);
134145
}

oak-search-elastic/src/test/java/org/apache/jackrabbit/oak/plugins/index/elastic/ElasticIndexProviderServiceTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.jackrabbit.oak.spi.state.NodeStore;
2727
import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
2828
import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils;
29+
import org.apache.jackrabbit.oak.stats.MeterStats;
2930
import org.apache.jackrabbit.oak.stats.StatisticsProvider;
3031
import org.apache.sling.testing.mock.osgi.MockOsgi;
3132
import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
@@ -51,7 +52,13 @@
5152
import static org.junit.Assert.assertEquals;
5253
import static org.junit.Assert.assertNotNull;
5354
import static org.junit.Assert.assertNull;
55+
import static org.mockito.ArgumentMatchers.any;
56+
import static org.mockito.ArgumentMatchers.eq;
5457
import static org.mockito.Mockito.mock;
58+
import static org.mockito.Mockito.reset;
59+
import static org.mockito.Mockito.spy;
60+
import static org.mockito.Mockito.verify;
61+
import static org.mockito.Mockito.when;
5562

5663
public class ElasticIndexProviderServiceTest {
5764

@@ -68,12 +75,17 @@ public class ElasticIndexProviderServiceTest {
6875

6976
private Whiteboard wb;
7077

78+
private final MeterStats spyElasticEnabledMetric = spy(MeterStats.class);
79+
7180
@Before
7281
public void setUp() {
82+
reset(spyElasticEnabledMetric);
83+
StatisticsProvider spyStatsProvider = mock(StatisticsProvider.class);
84+
when(spyStatsProvider.getMeter(eq("ELASTIC_ENABLED"), any())).thenReturn(spyElasticEnabledMetric);
7385
MountInfoProvider mip = Mounts.newBuilder().build();
7486
context.registerService(MountInfoProvider.class, mip);
7587
context.registerService(NodeStore.class, new MemoryNodeStore());
76-
context.registerService(StatisticsProvider.class, StatisticsProvider.NOOP);
88+
context.registerService(StatisticsProvider.class, spyStatsProvider);
7789
context.registerService(AsyncIndexInfoService.class, mock(AsyncIndexInfoService.class));
7890

7991
wb = new OsgiWhiteboard(context.bundleContext());
@@ -84,6 +96,8 @@ public void setUp() {
8496
public void defaultSetup() {
8597
MockOsgi.activate(service, context.bundleContext());
8698

99+
verify(spyElasticEnabledMetric).mark(eq(0L));
100+
87101
assertNotNull(context.getService(QueryIndexProvider.class));
88102
assertNotNull(context.getService(IndexEditorProvider.class));
89103

@@ -98,6 +112,8 @@ public void defaultSetup() {
98112
public void withElasticSetup() {
99113
MockOsgi.activate(service, context.bundleContext(), getElasticConfig());
100114

115+
verify(spyElasticEnabledMetric).mark(eq(1L));
116+
101117
assertNotNull(context.getService(QueryIndexProvider.class));
102118
assertNotNull(context.getService(IndexEditorProvider.class));
103119

@@ -112,6 +128,8 @@ public void withIndexCleanerSetup() {
112128
props.put("remoteIndexCleanupFrequency", 600);
113129
MockOsgi.activate(service, context.bundleContext(), props);
114130

131+
verify(spyElasticEnabledMetric).mark(eq(1L));
132+
115133
assertNotNull(context.getService(QueryIndexProvider.class));
116134
assertNotNull(context.getService(IndexEditorProvider.class));
117135

@@ -124,6 +142,8 @@ public void withIndexCleanerSetup() {
124142
public void disabled() {
125143
MockOsgi.activate(service, context.bundleContext(), Map.of(PROP_DISABLED, true));
126144

145+
verify(spyElasticEnabledMetric).mark(eq(0L));
146+
127147
assertNull(context.getService(QueryIndexProvider.class));
128148
assertNull(context.getService(IndexEditorProvider.class));
129149

0 commit comments

Comments
 (0)