Search in sources :

Example 1 with CoreClosedListener

use of org.apache.lucene.index.LeafReader.CoreClosedListener in project elasticsearch by elastic.

the class ShardCoreKeyMap method add.

/**
     * Register a {@link LeafReader}. This is necessary so that the core cache
     * key of this reader can be found later using {@link #getCoreKeysForIndex(String)}.
     */
public void add(LeafReader reader) {
    final ShardId shardId = ShardUtils.extractShardId(reader);
    if (shardId == null) {
        throw new IllegalArgumentException("Could not extract shard id from " + reader);
    }
    final Object coreKey = reader.getCoreCacheKey();
    if (coreKeyToShard.containsKey(coreKey)) {
        // the time).
        return;
    }
    final String index = shardId.getIndexName();
    synchronized (this) {
        if (coreKeyToShard.containsKey(coreKey) == false) {
            Set<Object> objects = indexToCoreKey.get(index);
            if (objects == null) {
                objects = new HashSet<>();
                indexToCoreKey.put(index, objects);
            }
            final boolean added = objects.add(coreKey);
            assert added;
            CoreClosedListener listener = ownerCoreCacheKey -> {
                assert coreKey == ownerCoreCacheKey;
                synchronized (ShardCoreKeyMap.this) {
                    coreKeyToShard.remove(ownerCoreCacheKey);
                    final Set<Object> coreKeys = indexToCoreKey.get(index);
                    final boolean removed = coreKeys.remove(coreKey);
                    assert removed;
                    if (coreKeys.isEmpty()) {
                        indexToCoreKey.remove(index);
                    }
                }
            };
            boolean addedListener = false;
            try {
                reader.addCoreClosedListener(listener);
                addedListener = true;
                // Only add the core key to the map as a last operation so that
                // if another thread sees that the core key is already in the
                // map (like the check just before this synchronized block),
                // then it means that the closed listener has already been
                // registered.
                ShardId previous = coreKeyToShard.put(coreKey, shardId);
                assert previous == null;
            } finally {
                if (false == addedListener) {
                    try {
                        listener.onClose(coreKey);
                    } catch (IOException e) {
                        throw new RuntimeException("Blow up trying to recover from failure to add listener", e);
                    }
                }
            }
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) ShardId(org.elasticsearch.index.shard.ShardId) HashSet(java.util.HashSet) Collection(java.util.Collection) Map(java.util.Map) LeafReader(org.apache.lucene.index.LeafReader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CoreClosedListener(org.apache.lucene.index.LeafReader.CoreClosedListener) IOException(java.io.IOException) HashMap(java.util.HashMap) ShardUtils(org.elasticsearch.index.shard.ShardUtils) Collections(java.util.Collections) CoreClosedListener(org.apache.lucene.index.LeafReader.CoreClosedListener) HashSet(java.util.HashSet) Set(java.util.Set) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 LeafReader (org.apache.lucene.index.LeafReader)1 CoreClosedListener (org.apache.lucene.index.LeafReader.CoreClosedListener)1 ShardId (org.elasticsearch.index.shard.ShardId)1 ShardUtils (org.elasticsearch.index.shard.ShardUtils)1