|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.search; |
| 18 | + |
| 19 | +import java.lang.invoke.MethodHandles; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import org.apache.solr.client.solrj.SolrClient; |
| 25 | +import org.apache.solr.client.solrj.request.CollectionAdminRequest; |
| 26 | +import org.apache.solr.client.solrj.response.QueryResponse; |
| 27 | +import org.apache.solr.cloud.SolrCloudTestCase; |
| 28 | +import org.apache.solr.index.NoMergePolicyFactory; |
| 29 | +import org.apache.solr.util.LogLevel; |
| 30 | +import org.apache.solr.util.TestInjection; |
| 31 | +import org.apache.solr.util.ThreadCpuTimer; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.Assume; |
| 34 | +import org.junit.BeforeClass; |
| 35 | +import org.junit.Test; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +@LogLevel("org.apache.solr.search.MemAllowedLimit=DEBUG") |
| 40 | +public class TestMemAllowedLimit extends SolrCloudTestCase { |
| 41 | + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 42 | + |
| 43 | + private static final String COLLECTION = "test"; |
| 44 | + |
| 45 | + private static Path createConfigSet() throws Exception { |
| 46 | + Path configSet = createTempDir(); |
| 47 | + copyMinConf(configSet.toFile()); |
| 48 | + // insert an expensive search component |
| 49 | + Path solrConfig = configSet.resolve("conf/solrconfig.xml"); |
| 50 | + Files.writeString( |
| 51 | + solrConfig, |
| 52 | + Files.readString(solrConfig) |
| 53 | + .replace( |
| 54 | + "<requestHandler", |
| 55 | + "<searchComponent name=\"expensiveSearchComponent\"\n" |
| 56 | + + " class=\"org.apache.solr.search.ExpensiveSearchComponent\"/>\n" |
| 57 | + + "\n" |
| 58 | + + " <requestHandler") |
| 59 | + .replace( |
| 60 | + "class=\"solr.SearchHandler\">", |
| 61 | + "class=\"solr.SearchHandler\">\n" |
| 62 | + + " <arr name=\"first-components\">\n" |
| 63 | + + " <str>expensiveSearchComponent</str>\n" |
| 64 | + + " </arr>\n")); |
| 65 | + return configSet.resolve("conf"); |
| 66 | + } |
| 67 | + |
| 68 | + @BeforeClass |
| 69 | + public static void setup() throws Exception { |
| 70 | + // Using NoMergePolicy and 100 commits we should get 100 segments (across all shards). |
| 71 | + // At this point of writing MAX_SEGMENTS_PER_SLICE in lucene is 5, so we should be |
| 72 | + // ensured that any multithreaded testing will create 20 executable tasks for the |
| 73 | + // executor that was provided to index-searcher. |
| 74 | + systemSetPropertySolrTestsMergePolicyFactory(NoMergePolicyFactory.class.getName()); |
| 75 | + System.setProperty(ThreadCpuTimer.ENABLE_CPU_TIME, "true"); |
| 76 | + System.setProperty("metricsEnabled", "true"); |
| 77 | + Path configset = createConfigSet(); |
| 78 | + configureCluster(1).addConfig("conf", configset).configure(); |
| 79 | + SolrClient solrClient = cluster.getSolrClient(); |
| 80 | + CollectionAdminRequest.Create create = |
| 81 | + CollectionAdminRequest.createCollection(COLLECTION, "conf", 3, 2); |
| 82 | + create.process(solrClient); |
| 83 | + waitForState("active", COLLECTION, clusterShape(3, 6)); |
| 84 | + for (int j = 0; j < 100; j++) { |
| 85 | + solrClient.add(COLLECTION, sdoc("id", "id-" + j, "val_i", j % 5)); |
| 86 | + solrClient.commit(COLLECTION); // need to commit every doc to create many segments. |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @AfterClass |
| 91 | + public static void tearDownClass() { |
| 92 | + TestInjection.cpuTimerDelayInjectedNS = null; |
| 93 | + systemClearPropertySolrTestsMergePolicyFactory(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testLimit() throws Exception { |
| 98 | + Assume.assumeTrue("Thread memory monitoring is not available", MemAllowedLimit.isSupported()); |
| 99 | + long limitMs = 100000; |
| 100 | + // 1 MiB |
| 101 | + MemAllowedLimit memLimit = new MemAllowedLimit(1f); |
| 102 | + ArrayList<byte[]> data = new ArrayList<>(); |
| 103 | + long startNs = System.nanoTime(); |
| 104 | + int wakeups = 0; |
| 105 | + while (!memLimit.shouldExit()) { |
| 106 | + Thread.sleep(100); |
| 107 | + // allocate memory |
| 108 | + for (int i = 0; i < 20; i++) { |
| 109 | + data.add(new byte[5000]); |
| 110 | + } |
| 111 | + wakeups++; |
| 112 | + } |
| 113 | + long endNs = System.nanoTime(); |
| 114 | + assertTrue(data.size() > 1); |
| 115 | + long wallTimeDeltaMs = TimeUnit.MILLISECONDS.convert(endNs - startNs, TimeUnit.NANOSECONDS); |
| 116 | + log.info( |
| 117 | + "Time limit: {} ms, elapsed wall-clock: {} ms, wakeups: {}", |
| 118 | + limitMs, |
| 119 | + wallTimeDeltaMs, |
| 120 | + wakeups); |
| 121 | + assertTrue("Number of wakeups should be smaller than 100 but was " + wakeups, wakeups < 100); |
| 122 | + assertTrue( |
| 123 | + "Elapsed wall-clock time expected much smaller than 100ms but was " + wallTimeDeltaMs, |
| 124 | + limitMs > wallTimeDeltaMs); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testDistribLimit() throws Exception { |
| 129 | + Assume.assumeTrue("Thread memory monitoring is not available", MemAllowedLimit.isSupported()); |
| 130 | + SolrClient solrClient = cluster.getSolrClient(); |
| 131 | + // no limits set - should complete |
| 132 | + long dataSize = 150; // 150 KiB |
| 133 | + QueryResponse rsp = |
| 134 | + solrClient.query( |
| 135 | + COLLECTION, |
| 136 | + params("q", "id:*", "sort", "id desc", "dataSize", String.valueOf(dataSize))); |
| 137 | + assertEquals(rsp.getHeader().get("status"), 0); |
| 138 | + assertNull("should not have partial results", rsp.getHeader().get("partialResults")); |
| 139 | + |
| 140 | + // memAllowed set with large value, should return full results |
| 141 | + rsp = |
| 142 | + solrClient.query( |
| 143 | + COLLECTION, |
| 144 | + params( |
| 145 | + "q", |
| 146 | + "id:*", |
| 147 | + "sort", |
| 148 | + "id asc", |
| 149 | + "memLoadCount", |
| 150 | + String.valueOf(dataSize), |
| 151 | + "stages", |
| 152 | + "prepare,process", |
| 153 | + "memAllowed", |
| 154 | + "1.5")); |
| 155 | + assertNull("should have full results", rsp.getHeader().get("partialResults")); |
| 156 | + |
| 157 | + // memAllowed set, should return partial results |
| 158 | + rsp = |
| 159 | + solrClient.query( |
| 160 | + COLLECTION, |
| 161 | + params( |
| 162 | + "q", |
| 163 | + "id:*", |
| 164 | + "sort", |
| 165 | + "id asc", |
| 166 | + "memLoadCount", |
| 167 | + String.valueOf(dataSize), |
| 168 | + "stages", |
| 169 | + "prepare,process", |
| 170 | + "memAllowed", |
| 171 | + "0.2")); |
| 172 | + assertNotNull("should have partial results", rsp.getHeader().get("partialResults")); |
| 173 | + |
| 174 | + // multi-threaded search |
| 175 | + // memAllowed set, should return partial results |
| 176 | + rsp = |
| 177 | + solrClient.query( |
| 178 | + COLLECTION, |
| 179 | + params( |
| 180 | + "q", |
| 181 | + "id:*", |
| 182 | + "sort", |
| 183 | + "id asc", |
| 184 | + "memLoadCount", |
| 185 | + String.valueOf(dataSize), |
| 186 | + "stages", |
| 187 | + "prepare,process", |
| 188 | + "multiThreaded", |
| 189 | + "true", |
| 190 | + "memAllowed", |
| 191 | + "0.2")); |
| 192 | + assertNotNull("should have partial results", rsp.getHeader().get("partialResults")); |
| 193 | + } |
| 194 | +} |
0 commit comments