This is an automated email from the ASF dual-hosted git repository.
dsmiley pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 1209027537b SOLR-17094: Close objects contained inside an ObjectCache
(#2110)
1209027537b is described below
commit 1209027537bf929e5a0149eda92d76571ce936ac
Author: Vincent P <[email protected]>
AuthorDate: Mon Dec 4 17:53:08 2023 +0100
SOLR-17094: Close objects contained inside an ObjectCache (#2110)
Co-authored-by: Vincent Primault <[email protected]>
---
solr/CHANGES.txt | 2 +
.../org/apache/solr/common/util/ObjectCache.java | 22 ++++-
.../apache/solr/common/util/ObjectCacheTest.java | 109 +++++++++++++++++++++
3 files changed, 128 insertions(+), 5 deletions(-)
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 50154002e1a..c59ea23dd5d 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -115,6 +115,8 @@ Improvements
* SOLR-17050: Use compact JSON for Learning to Rank (LTR) feature and model
storage. (Florin Babes, Christine Poerschke, Alessandro Benedetti)
+* SOLR-17094: Close objects contained inside an ObjectCache. (Vincent
Primault)
+
Optimizations
---------------------
* SOLR-17084: LBSolrClient (used by CloudSolrClient) now returns the count of
core tracked as not live AKA zombies
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/ObjectCache.java
b/solr/solrj/src/java/org/apache/solr/common/util/ObjectCache.java
index be9ec69d926..2bbb6529c0f 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/ObjectCache.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/ObjectCache.java
@@ -16,22 +16,24 @@
*/
package org.apache.solr.common.util;
+import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import org.apache.solr.common.SolrCloseable;
/** Simple object cache with a type-safe accessor. */
public class ObjectCache extends MapBackedCache<String, Object> implements
SolrCloseable {
- private volatile boolean isClosed;
+ private final AtomicBoolean isClosed = new AtomicBoolean(false);
public ObjectCache() {
super(new ConcurrentHashMap<>());
}
private void ensureNotClosed() {
- if (isClosed) {
+ if (isClosed.get()) {
throw new RuntimeException("This ObjectCache is already closed.");
}
}
@@ -78,12 +80,22 @@ public class ObjectCache extends MapBackedCache<String,
Object> implements SolrC
@Override
public boolean isClosed() {
- return isClosed;
+ return isClosed.get();
}
@Override
public void close() throws IOException {
- isClosed = true;
- map.clear();
+ if (isClosed.compareAndSet(false, true)) {
+ // Close any Closeable object which may have been stored into this cache.
+ // This allows to tie some objects to the lifecycle of the object which
+ // owns this ObjectCache, which is useful for plugins to register objects
+ // which should be closed before being garbage-collected.
+ for (Object value : map.values()) {
+ if (value instanceof Closeable) {
+ ((Closeable) value).close();
+ }
+ }
+ map.clear();
+ }
}
}
diff --git
a/solr/solrj/src/test/org/apache/solr/common/util/ObjectCacheTest.java
b/solr/solrj/src/test/org/apache/solr/common/util/ObjectCacheTest.java
new file mode 100644
index 00000000000..a7bd478491c
--- /dev/null
+++ b/solr/solrj/src/test/org/apache/solr/common/util/ObjectCacheTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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.
+ */
+package org.apache.solr.common.util;
+
+import java.io.Closeable;
+import org.apache.solr.SolrTestCase;
+import org.junit.Test;
+
+/** Tests for {@link ObjectCache}. */
+public class ObjectCacheTest extends SolrTestCase {
+
+ private ObjectCache objectCache;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ objectCache = new ObjectCache();
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ objectCache.close();
+ super.tearDown();
+ }
+
+ @Test
+ public void testGetPutRemove() {
+ assertNull(objectCache.get("key"));
+
+ objectCache.put("key", "value");
+ assertEquals("value", objectCache.get("key"));
+
+ objectCache.remove("key");
+ assertNull(objectCache.get("key"));
+ }
+
+ @Test
+ public void testClear() {
+ objectCache.put("key1", "value1");
+ objectCache.put("key2", "value2");
+
+ objectCache.clear();
+ assertNull(objectCache.get("key1"));
+ assertNull(objectCache.get("key2"));
+ }
+
+ @Test
+ public void testGetTypeSafe() {
+ objectCache.put("string", "a string");
+ objectCache.put("integer", 42);
+
+ assertEquals("a string", objectCache.get("string", String.class));
+ assertEquals((Integer) 42, objectCache.get("integer", Integer.class));
+ }
+
+ @Test
+ public void testComputeIfAbsentTypeSafe() {
+ String returnValue = objectCache.computeIfAbsent("string", String.class, k
-> "a string");
+ assertEquals("a string", returnValue);
+ assertEquals("a string", objectCache.get("string"));
+
+ returnValue = objectCache.computeIfAbsent("string", String.class, k ->
"another string");
+ assertEquals("a string", returnValue);
+ assertEquals("a string", objectCache.get("string"));
+ }
+
+ @Test
+ public void testClose() throws Exception {
+ assertFalse(objectCache.isClosed());
+ objectCache.close();
+ assertTrue(objectCache.isClosed());
+ }
+
+ @Test
+ public void testCloseCloseableValues() throws Exception {
+ MyCloseable object1 = new MyCloseable();
+ MyCloseable object2 = new MyCloseable();
+ objectCache.put("object1", object1);
+ objectCache.put("object2", object2);
+ objectCache.put("string", "a string");
+ objectCache.close();
+
+ assertTrue(object1.closed);
+ assertTrue(object2.closed);
+ }
+
+ private static class MyCloseable implements Closeable {
+ private boolean closed = false;
+
+ @Override
+ public void close() {
+ closed = true;
+ }
+ }
+}