|
| 1 | +/** |
| 2 | + * Copyright (c) 2018, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: BSD-3-Clause |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | +package com.demandware.carbonj.service.db; |
| 8 | + |
| 9 | +import com.codahale.metrics.MetricRegistry; |
| 10 | +import com.demandware.carbonj.service.db.model.Metric; |
| 11 | +import com.demandware.carbonj.service.db.model.MetricIndex; |
| 12 | +import com.demandware.carbonj.service.db.model.RetentionPolicy; |
| 13 | +import com.demandware.carbonj.service.db.model.TooManyDatapointsFoundException; |
| 14 | +import com.demandware.carbonj.service.db.util.DatabaseMetrics; |
| 15 | +import com.demandware.carbonj.service.events.NoOpLogger; |
| 16 | +import com.demandware.carbonj.service.engine.Query; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileWriter; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.ThreadPoolExecutor; |
| 23 | + |
| 24 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 26 | +import static org.mockito.ArgumentMatchers.any; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 30 | + |
| 31 | +public class TimeSeriesStoreImplDatapointsLimitTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void incrementsMeterWhenTooManyDatapointsAreMatched() throws Exception { |
| 35 | + MetricRegistry metricRegistry = new MetricRegistry(); |
| 36 | + DatabaseMetrics dbMetrics = new DatabaseMetrics(metricRegistry); |
| 37 | + |
| 38 | + // Build a MetricIndex that returns 1 leaf metric with a long retention so estimated points is large |
| 39 | + MetricIndex nameIndex = mock(MetricIndex.class); |
| 40 | + Metric m = mock(Metric.class); |
| 41 | + when(m.isLeaf()).thenReturn(true); |
| 42 | + // Use 30m:2y retention so each series can be huge; we'll also set the threshold very low |
| 43 | + when(m.getRetentionPolicies()).thenReturn(RetentionPolicy.getPolicyList("30m:2y")); |
| 44 | + when(nameIndex.findMetrics(any(String.class), anyBoolean(), anyBoolean(), anyBoolean())) |
| 45 | + .thenReturn(List.of(m)); |
| 46 | + |
| 47 | + ThreadPoolExecutor main = TimeSeriesStoreImpl.newMainTaskQueue(1, 10); |
| 48 | + ThreadPoolExecutor heavy = TimeSeriesStoreImpl.newMainTaskQueue(1, 10); |
| 49 | + ThreadPoolExecutor serial = TimeSeriesStoreImpl.newSerialTaskQueue(10); |
| 50 | + |
| 51 | + // Create store with a non-existent config file, then override thresholds via reflection-free path: |
| 52 | + // Create a temp properties file with a very low datapoints threshold to force the exception |
| 53 | + File tmpProps = File.createTempFile("tsstore", ".properties"); |
| 54 | + try (FileWriter fw = new FileWriter(tmpProps)) { |
| 55 | + fw.write("metrics.store.maxDataPointsPerRequest=10\n"); |
| 56 | + } |
| 57 | + |
| 58 | + TimeSeriesStoreImpl store = new TimeSeriesStoreImpl(metricRegistry, nameIndex, new NoOpLogger<>(), |
| 59 | + main, heavy, serial, |
| 60 | + mock(com.demandware.carbonj.service.db.model.DataPointStore.class), dbMetrics, |
| 61 | + true, 10, false, new File("/tmp/idx.out"), 10, |
| 62 | + tmpProps.getAbsolutePath(), false); |
| 63 | + |
| 64 | + // Manually set thresholds via internal fields to avoid filesystem config |
| 65 | + // dataPointsThreshold is private, but enforced via selectThreadPoolExecutor; we can choose query that exceeds |
| 66 | + int now = (int) (System.currentTimeMillis() / 1000); |
| 67 | + Query q = new Query("foo", now - 365 * 24 * 3600, now, now, System.currentTimeMillis()); |
| 68 | + |
| 69 | + long before = metricRegistry.meter("db.datapointsLimitExceeded").getCount(); |
| 70 | + |
| 71 | + assertThrows(TooManyDatapointsFoundException.class, () -> store.fetchSeriesData(q)); |
| 72 | + |
| 73 | + long after = metricRegistry.meter("db.datapointsLimitExceeded").getCount(); |
| 74 | + // Ensure the meter was incremented once |
| 75 | + assertEquals(before + 1, after); |
| 76 | + |
| 77 | + // Cleanup executors |
| 78 | + main.shutdownNow(); |
| 79 | + heavy.shutdownNow(); |
| 80 | + serial.shutdownNow(); |
| 81 | + // cleanup temp |
| 82 | + //noinspection ResultOfMethodCallIgnored |
| 83 | + tmpProps.delete(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | + |
0 commit comments