Skip to content

Commit 26ca664

Browse files
OAK-11613: expose ELASTIC_ENABLED metric
1 parent 679a4c8 commit 26ca664

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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, Collections.emptyMap()).mark(enabled ? 1 : 0);
71+
}
72+
6273
/**
6374
* Tracks a new query using two metrics:
6475
* <ul>

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)