SOLR-11642: Implement ObjectCache for SolrCloudManager.

This commit is contained in:
Andrzej Bialecki 2017-11-14 13:21:26 +01:00
parent 7c54b2b116
commit fc678da2dd
6 changed files with 118 additions and 1 deletions

View File

@ -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}.
@ -54,6 +55,11 @@ public class DelegatingCloudManager implements SolrCloudManager {
return delegate.getDistributedQueueFactory();
}
@Override
public ObjectCache getObjectCache() {
return delegate.getObjectCache();
}
@Override
public SolrResponse request(SolrRequest req) throws IOException {
return delegate.request(req);

View File

@ -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;

View File

@ -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
@ -78,6 +83,11 @@ public class SolrClientCloudManager implements SolrCloudManager {
return isClosed;
}
@Override
public ObjectCache getObjectCache() {
return objectCache;
}
@Override
public ClusterStateProvider getClusterStateProvider() {
return solrClient.getClusterStateProvider();

View File

@ -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;

View File

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

View File

@ -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) {