io.druid.server.lookup.namespace.cache.OnHeapNamespaceExtractionCacheManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of druid-lookups-cached-global Show documentation
Show all versions of druid-lookups-cached-global Show documentation
Extension to rename Druid dimension values using namespaces
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets 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 io.druid.server.lookup.namespace.cache;
import com.google.common.primitives.Chars;
import com.google.common.util.concurrent.Striped;
import com.google.inject.Inject;
import com.metamx.common.IAE;
import com.metamx.common.lifecycle.Lifecycle;
import com.metamx.common.logger.Logger;
import com.metamx.emitter.service.ServiceEmitter;
import com.metamx.emitter.service.ServiceMetricEvent;
import io.druid.query.lookup.namespace.ExtractionNamespace;
import io.druid.query.lookup.namespace.ExtractionNamespaceCacheFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
/**
*
*/
public class OnHeapNamespaceExtractionCacheManager extends NamespaceExtractionCacheManager
{
private static final Logger LOG = new Logger(OnHeapNamespaceExtractionCacheManager.class);
private final ConcurrentMap> mapMap = new ConcurrentHashMap<>();
private final Striped nsLocks = Striped.lock(32);
@Inject
public OnHeapNamespaceExtractionCacheManager(
final Lifecycle lifecycle,
final ServiceEmitter emitter,
final Map, ExtractionNamespaceCacheFactory>> namespaceFunctionFactoryMap
)
{
super(lifecycle, emitter, namespaceFunctionFactoryMap);
}
@Override
protected boolean swapAndClearCache(String namespaceKey, String cacheKey)
{
final Lock lock = nsLocks.get(namespaceKey);
lock.lock();
try {
ConcurrentMap cacheMap = mapMap.get(cacheKey);
if (cacheMap == null) {
throw new IAE("Extraction Cache [%s] does not exist", cacheKey);
}
ConcurrentMap prior = mapMap.put(namespaceKey, cacheMap);
mapMap.remove(cacheKey);
if (prior != null) {
// Old map will get GC'd when it is not used anymore
return true;
} else {
return false;
}
}
finally {
lock.unlock();
}
}
@Override
public ConcurrentMap getCacheMap(String namespaceOrCacheKey)
{
ConcurrentMap map = mapMap.get(namespaceOrCacheKey);
if (map == null) {
mapMap.putIfAbsent(namespaceOrCacheKey, new ConcurrentHashMap());
map = mapMap.get(namespaceOrCacheKey);
}
return map;
}
@Override
public boolean delete(final String namespaceKey)
{
// `super.delete` has a synchronization in it, don't call it in the lock.
if (!super.delete(namespaceKey)) {
return false;
}
final Lock lock = nsLocks.get(namespaceKey);
lock.lock();
try {
return mapMap.remove(namespaceKey) != null;
}
finally {
lock.unlock();
}
}
@Override
protected void monitor(ServiceEmitter serviceEmitter)
{
long numEntries = 0;
long size = 0;
for (Map.Entry> entry : mapMap.entrySet()) {
final ConcurrentMap map = entry.getValue();
if (map == null) {
LOG.debug("missing cache key for reporting [%s]", entry.getKey());
continue;
}
numEntries += map.size();
for (Map.Entry sEntry : map.entrySet()) {
final String key = sEntry.getKey();
final String value = sEntry.getValue();
if (key == null || value == null) {
LOG.debug("Missing entries for cache key [%s]", entry.getKey());
continue;
}
size += key.length() + value.length();
}
}
serviceEmitter.emit(ServiceMetricEvent.builder().build("namespace/cache/numEntries", numEntries));
serviceEmitter.emit(ServiceMetricEvent.builder().build("namespace/cache/heapSizeInBytes", size * Chars.BYTES));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy