lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a.@apache.org
Subject lucene-solr:master: SOLR-11642: Implement ObjectCache for SolrCloudManager.
Date Tue, 14 Nov 2017 12:38:39 GMT
Repository: lucene-solr
Updated Branches:
  refs/heads/master 7c54b2b11 -> fc678da2d


SOLR-11642: Implement ObjectCache for SolrCloudManager.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/fc678da2
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/fc678da2
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/fc678da2

Branch: refs/heads/master
Commit: fc678da2dd609adf64e10c3073539dbd65c2b4ab
Parents: 7c54b2b
Author: Andrzej Bialecki <ab@apache.org>
Authored: Tue Nov 14 13:21:26 2017 +0100
Committer: Andrzej Bialecki <ab@apache.org>
Committed: Tue Nov 14 13:21:26 2017 +0100

----------------------------------------------------------------------
 .../autoscaling/DelegatingCloudManager.java     |  6 ++
 .../cloud/autoscaling/SolrCloudManager.java     |  3 +
 .../solrj/impl/SolrClientCloudManager.java      | 10 +++
 .../apache/solr/common/util/MapBackedCache.java |  2 +-
 .../apache/solr/common/util/ObjectCache.java    | 91 ++++++++++++++++++++
 .../solrj/cloud/autoscaling/TestPolicy.java     |  7 ++
 6 files changed, 118 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc678da2/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/DelegatingCloudManager.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/DelegatingCloudManager.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/DelegatingCloudManager.java
index 05801b9..0987db4 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/DelegatingCloudManager.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/DelegatingCloudManager.java
@@ -23,6 +23,7 @@ import org.apache.solr.client.solrj.SolrRequest;
 import org.apache.solr.client.solrj.SolrResponse;
 import org.apache.solr.client.solrj.cloud.DistributedQueueFactory;
 import org.apache.solr.client.solrj.impl.ClusterStateProvider;
+import org.apache.solr.common.util.ObjectCache;
 
 /**
  * Base class for overriding some behavior of {@link SolrCloudManager}.
@@ -55,6 +56,11 @@ public class DelegatingCloudManager implements SolrCloudManager {
   }
 
   @Override
+  public ObjectCache getObjectCache() {
+    return delegate.getObjectCache();
+  }
+
+  @Override
   public SolrResponse request(SolrRequest req) throws IOException {
     return delegate.request(req);
   }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc678da2/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/SolrCloudManager.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/SolrCloudManager.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/SolrCloudManager.java
index 726c2c6..8a1f8f0 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/SolrCloudManager.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/SolrCloudManager.java
@@ -25,6 +25,7 @@ import org.apache.solr.client.solrj.SolrResponse;
 import org.apache.solr.client.solrj.cloud.DistributedQueueFactory;
 import org.apache.solr.client.solrj.impl.ClusterStateProvider;
 import org.apache.solr.common.SolrCloseable;
+import org.apache.solr.common.util.ObjectCache;
 
 /**
  * This interface abstracts the access to a SolrCloud cluster, including interactions with
Zookeeper, Solr
@@ -41,6 +42,8 @@ public interface SolrCloudManager extends SolrCloseable {
 
   DistributedQueueFactory getDistributedQueueFactory();
 
+  ObjectCache getObjectCache();
+
   // Solr-like methods
 
   SolrResponse request(SolrRequest req) throws IOException;

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc678da2/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientCloudManager.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientCloudManager.java
b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientCloudManager.java
index b8db4ab..c67cdc8 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientCloudManager.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrClientCloudManager.java
@@ -43,6 +43,8 @@ import org.apache.solr.client.solrj.cloud.autoscaling.NodeStateProvider;
 import org.apache.solr.client.solrj.cloud.autoscaling.SolrCloudManager;
 import org.apache.solr.common.cloud.SolrZkClient;
 import org.apache.solr.common.cloud.ZkStateReader;
+import org.apache.solr.common.util.IOUtils;
+import org.apache.solr.common.util.ObjectCache;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,6 +59,7 @@ public class SolrClientCloudManager implements SolrCloudManager {
   private final DistributedQueueFactory queueFactory;
   private final ZkStateReader zkStateReader;
   private final SolrZkClient zkClient;
+  private final ObjectCache objectCache;
   private volatile boolean isClosed;
 
   public SolrClientCloudManager(DistributedQueueFactory queueFactory, CloudSolrClient solrClient)
{
@@ -66,11 +69,13 @@ public class SolrClientCloudManager implements SolrCloudManager {
     this.zkClient = zkStateReader.getZkClient();
     this.stateManager = new ZkDistribStateManager(zkClient);
     this.isClosed = false;
+    this.objectCache = new ObjectCache();
   }
 
   @Override
   public void close() {
     isClosed = true;
+    IOUtils.closeQuietly(objectCache);
   }
 
   @Override
@@ -79,6 +84,11 @@ public class SolrClientCloudManager implements SolrCloudManager {
   }
 
   @Override
+  public ObjectCache getObjectCache() {
+    return objectCache;
+  }
+
+  @Override
   public ClusterStateProvider getClusterStateProvider() {
     return solrClient.getClusterStateProvider();
   }

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc678da2/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java b/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java
index 1aba20b..552dba6 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/MapBackedCache.java
@@ -24,7 +24,7 @@ import java.util.function.Function;
 // A cache backed by a map
 public class MapBackedCache<K, V> implements Cache<K, V> {
 
-  private final Map<K, V> map;
+  protected final Map<K, V> map;
 
   public MapBackedCache(Map<K, V> map) {
     this.map = map;

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc678da2/solr/solrj/src/java/org/apache/solr/common/util/ObjectCache.java
----------------------------------------------------------------------
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
new file mode 100644
index 0000000..72d40f9
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/common/util/ObjectCache.java
@@ -0,0 +1,91 @@
+/*
+ * 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.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+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;
+
+  public ObjectCache() {
+    super(new ConcurrentHashMap<>());
+  }
+
+  private void ensureNotClosed() {
+    if (isClosed) {
+      throw new RuntimeException("This ObjectCache is already closed.");
+    }
+  }
+
+  @Override
+  public Object put(String key, Object val) {
+    ensureNotClosed();
+    return super.put(key, val);
+  }
+
+  @Override
+  public Object get(String key) {
+    ensureNotClosed();
+    return super.get(key);
+  }
+
+  @Override
+  public Object remove(String key) {
+    ensureNotClosed();
+    return super.remove(key);
+  }
+
+  @Override
+  public void clear() {
+    ensureNotClosed();
+    super.clear();
+  }
+
+  public <T> T get(String key, Class<T> clazz) {
+    Object o = get(key);
+    if (o == null) {
+      return null;
+    } else {
+      return clazz.cast(o);
+    }
+  }
+
+  public <T> T computeIfAbsent(String key, Class<T> clazz, Function<String,
? extends T> mappingFunction) {
+    ensureNotClosed();
+    Object o = super.computeIfAbsent(key, mappingFunction);
+    return clazz.cast(o);
+  }
+
+  @Override
+  public boolean isClosed() {
+    return isClosed;
+  }
+
+  @Override
+  public void close() throws IOException {
+    isClosed = true;
+    map.clear();
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/fc678da2/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
----------------------------------------------------------------------
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
index 74f8aec..31e02f1 100644
--- a/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/cloud/autoscaling/TestPolicy.java
@@ -44,6 +44,7 @@ import org.apache.solr.common.cloud.ReplicaPosition;
 import org.apache.solr.common.cloud.ZkStateReader;
 import org.apache.solr.common.params.CollectionParams;
 import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.ObjectCache;
 import org.apache.solr.common.util.Pair;
 import org.apache.solr.common.util.Utils;
 import org.apache.solr.common.util.ValidatingJsonMap;
@@ -1130,6 +1131,12 @@ public class TestPolicy extends SolrTestCaseJ4 {
 
   private SolrCloudManager getSolrCloudManager(final Map<String, Map> nodeValues, String
clusterState) {
     return new SolrCloudManager() {
+      ObjectCache objectCache = new ObjectCache();
+
+      @Override
+      public ObjectCache getObjectCache() {
+        return objectCache;
+      }
       @Override
       public ClusterStateProvider getClusterStateProvider() {
         return new DelegatingClusterStateProvider(null) {


Mime
View raw message