Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-8752: Add a test for SizeLimitedDistributedMap and improve javadocs

Shalin Shekhar Mangar06053fc01cf1
diff --git a/solr/core/src/java/org/apache/solr/cloud/SizeLimitedDistributedMap.java b/solr/core/src/java/org/apache/solr/cloud/SizeLimitedDistributedMap.java
index 3326dcae4a3..82ab1413010 100644
--- a/solr/core/src/java/org/apache/solr/cloud/SizeLimitedDistributedMap.java
+++ b/solr/core/src/java/org/apache/solr/cloud/SizeLimitedDistributedMap.java
@@ -27,7 +27,10 @@ import org.apache.zookeeper.data.Stat;
/**
* A size limited distributed map maintained in zk.
- * Oldest znodes (as per modification time) are evicted as newer ones come in.
+ * Oldest znodes (as per modification time) are evicted as newer ones come in.
+ *
+ * When the map hits the specified maximum size, the oldest <code>maxSize / 10</code> items
+ * are evicted on the next {@link #put(String, byte[])} invocation.
*/
public class SizeLimitedDistributedMap extends DistributedMap {
diff --git a/solr/core/src/test/org/apache/solr/cloud/TestSizeLimitedDistributedMap.java b/solr/core/src/test/org/apache/solr/cloud/TestSizeLimitedDistributedMap.java
new file mode 100644
index 00000000000..2131f48fafc
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/cloud/TestSizeLimitedDistributedMap.java
@@ -0,0 +1,58 @@
+package org.apache.solr.cloud;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.common.cloud.SolrZkClient;
+
+public class TestSizeLimitedDistributedMap extends SolrTestCaseJ4 {
+
+ public void testCleanup() throws Exception {
+ String zkDir = createTempDir("TestSizeLimitedDistributedMap").toFile().getAbsolutePath();
+
+ ZkTestServer server = new ZkTestServer(zkDir);
+ try {
+ server.run();
+
+ AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost());
+ AbstractZkTestCase.makeSolrZkNode(server.getZkHost());
+
+ try (SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), 10000)) {
+ DistributedMap map = Overseer.getCompletedMap(zkClient);
+ assertTrue(map instanceof SizeLimitedDistributedMap);
+ for (int i = 0; i < Overseer.NUM_RESPONSES_TO_STORE; i++) {
+ map.put("xyz_" + i, new byte[0]);
+ }
+
+ assertEquals("Number of items do not match", Overseer.NUM_RESPONSES_TO_STORE, map.size());
+ // add another to trigger cleanup
+ map.put("xyz_10000", new byte[0]);
+ assertEquals("Distributed queue was not cleaned up",
+ Overseer.NUM_RESPONSES_TO_STORE - (Overseer.NUM_RESPONSES_TO_STORE / 10) + 1, map.size());
+ for (int i = Overseer.NUM_RESPONSES_TO_STORE; i >= Overseer.NUM_RESPONSES_TO_STORE / 10; i--) {
+ assertTrue(map.contains("xyz_" + i));
+ }
+ for (int i = Overseer.NUM_RESPONSES_TO_STORE / 10 - 1; i >= 0; i--) {
+ assertFalse(map.contains("xyz_" + i));
+ }
+ }
+ } finally {
+ server.shutdown();
+ }
+ }
+}