|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.cache.common.tier; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.cluster.node.info.NodeInfo; |
| 12 | +import org.opensearch.action.admin.cluster.node.info.NodesInfoRequest; |
| 13 | +import org.opensearch.action.admin.cluster.node.info.NodesInfoResponse; |
| 14 | +import org.opensearch.action.admin.cluster.node.info.PluginsAndModules; |
| 15 | +import org.opensearch.action.search.SearchResponse; |
| 16 | +import org.opensearch.action.search.SearchType; |
| 17 | +import org.opensearch.client.Client; |
| 18 | +import org.opensearch.common.cache.CacheType; |
| 19 | +import org.opensearch.common.cache.ICache; |
| 20 | +import org.opensearch.common.cache.settings.CacheSettings; |
| 21 | +import org.opensearch.common.cache.store.OpenSearchOnHeapCache; |
| 22 | +import org.opensearch.common.settings.Settings; |
| 23 | +import org.opensearch.common.util.FeatureFlags; |
| 24 | +import org.opensearch.indices.IndicesRequestCache; |
| 25 | +import org.opensearch.plugins.CachePlugin; |
| 26 | +import org.opensearch.plugins.Plugin; |
| 27 | +import org.opensearch.plugins.PluginInfo; |
| 28 | +import org.opensearch.search.aggregations.bucket.histogram.DateHistogramInterval; |
| 29 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 30 | +import org.junit.Assert; |
| 31 | + |
| 32 | +import java.time.ZoneId; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.Collection; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.function.Function; |
| 38 | +import java.util.stream.Collectors; |
| 39 | +import java.util.stream.Stream; |
| 40 | + |
| 41 | +import static org.opensearch.search.aggregations.AggregationBuilders.dateHistogram; |
| 42 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 43 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse; |
| 44 | +import static org.hamcrest.Matchers.greaterThan; |
| 45 | + |
| 46 | +public class TieredSpilloverCacheIT extends OpenSearchIntegTestCase { |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 50 | + return Arrays.asList(TieredSpilloverCachePlugin.class, MockDiskCachePlugin.class); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected Settings featureFlagSettings() { |
| 55 | + return Settings.builder().put(super.featureFlagSettings()).put(FeatureFlags.PLUGGABLE_CACHE, "true").build(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 60 | + return Settings.builder() |
| 61 | + .put(super.nodeSettings(nodeOrdinal)) |
| 62 | + .put( |
| 63 | + CacheSettings.getConcreteStoreNameSettingForCacheType(CacheType.INDICES_REQUEST_CACHE).getKey(), |
| 64 | + TieredSpilloverCache.TieredSpilloverCacheFactory.TIERED_SPILLOVER_CACHE_NAME |
| 65 | + ) |
| 66 | + .put( |
| 67 | + TieredSpilloverCacheSettings.TIERED_SPILLOVER_ONHEAP_STORE_NAME.getConcreteSettingForNamespace( |
| 68 | + CacheType.INDICES_REQUEST_CACHE.getSettingPrefix() |
| 69 | + ).getKey(), |
| 70 | + OpenSearchOnHeapCache.OpenSearchOnHeapCacheFactory.NAME |
| 71 | + ) |
| 72 | + .put( |
| 73 | + TieredSpilloverCacheSettings.TIERED_SPILLOVER_DISK_STORE_NAME.getConcreteSettingForNamespace( |
| 74 | + CacheType.INDICES_REQUEST_CACHE.getSettingPrefix() |
| 75 | + ).getKey(), |
| 76 | + MockDiskCache.MockDiskCacheFactory.NAME |
| 77 | + ) |
| 78 | + .build(); |
| 79 | + } |
| 80 | + |
| 81 | + public void testPluginsAreInstalled() { |
| 82 | + NodesInfoRequest nodesInfoRequest = new NodesInfoRequest(); |
| 83 | + nodesInfoRequest.addMetric(NodesInfoRequest.Metric.PLUGINS.metricName()); |
| 84 | + NodesInfoResponse nodesInfoResponse = OpenSearchIntegTestCase.client().admin().cluster().nodesInfo(nodesInfoRequest).actionGet(); |
| 85 | + List<PluginInfo> pluginInfos = nodesInfoResponse.getNodes() |
| 86 | + .stream() |
| 87 | + .flatMap( |
| 88 | + (Function<NodeInfo, Stream<PluginInfo>>) nodeInfo -> nodeInfo.getInfo(PluginsAndModules.class).getPluginInfos().stream() |
| 89 | + ) |
| 90 | + .collect(Collectors.toList()); |
| 91 | + Assert.assertTrue( |
| 92 | + pluginInfos.stream() |
| 93 | + .anyMatch(pluginInfo -> pluginInfo.getName().equals("org.opensearch.cache.common" + ".tier.TieredSpilloverCachePlugin")) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + public void testSanityChecksWithIndicesRequestCache() throws InterruptedException { |
| 98 | + Client client = client(); |
| 99 | + assertAcked( |
| 100 | + client.admin() |
| 101 | + .indices() |
| 102 | + .prepareCreate("index") |
| 103 | + .setMapping("f", "type=date") |
| 104 | + .setSettings(Settings.builder().put(IndicesRequestCache.INDEX_CACHE_REQUEST_ENABLED_SETTING.getKey(), true).build()) |
| 105 | + .get() |
| 106 | + ); |
| 107 | + indexRandom( |
| 108 | + true, |
| 109 | + client.prepareIndex("index").setSource("f", "2014-03-10T00:00:00.000Z"), |
| 110 | + client.prepareIndex("index").setSource("f", "2014-05-13T00:00:00.000Z") |
| 111 | + ); |
| 112 | + ensureSearchable("index"); |
| 113 | + |
| 114 | + // This is not a random example: serialization with time zones writes shared strings |
| 115 | + // which used to not work well with the query cache because of the handles stream output |
| 116 | + // see #9500 |
| 117 | + final SearchResponse r1 = client.prepareSearch("index") |
| 118 | + .setSize(0) |
| 119 | + .setSearchType(SearchType.QUERY_THEN_FETCH) |
| 120 | + .addAggregation( |
| 121 | + dateHistogram("histo").field("f") |
| 122 | + .timeZone(ZoneId.of("+01:00")) |
| 123 | + .minDocCount(0) |
| 124 | + .dateHistogramInterval(DateHistogramInterval.MONTH) |
| 125 | + ) |
| 126 | + .get(); |
| 127 | + assertSearchResponse(r1); |
| 128 | + |
| 129 | + // The cached is actually used |
| 130 | + assertThat( |
| 131 | + client.admin().indices().prepareStats("index").setRequestCache(true).get().getTotal().getRequestCache().getMemorySizeInBytes(), |
| 132 | + greaterThan(0L) |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + public static class MockDiskCachePlugin extends Plugin implements CachePlugin { |
| 137 | + |
| 138 | + public MockDiskCachePlugin() {} |
| 139 | + |
| 140 | + @Override |
| 141 | + public Map<String, ICache.Factory> getCacheFactoryMap() { |
| 142 | + return Map.of(MockDiskCache.MockDiskCacheFactory.NAME, new MockDiskCache.MockDiskCacheFactory(0, 1000)); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public String getName() { |
| 147 | + return "mock_disk_plugin"; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments