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

This commit is contained in:
Shalin Shekhar Mangar 2016-02-27 12:35:58 +05:30
parent c2bc93822d
commit 06053fc01c
2 changed files with 62 additions and 1 deletions

View File

@ -27,7 +27,10 @@ import org.apache.zookeeper.data.Stat;
/** /**
* A size limited distributed map maintained in zk. * 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 { public class SizeLimitedDistributedMap extends DistributedMap {

View File

@ -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();
}
}
}