|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.index.engine; |
| 20 | + |
| 21 | +import com.carrotsearch.hppc.ObjectLongHashMap; |
| 22 | +import com.carrotsearch.hppc.cursors.ObjectLongCursor; |
| 23 | +import org.apache.lucene.index.FieldInfo; |
| 24 | +import org.apache.lucene.index.LeafReader; |
| 25 | +import org.apache.lucene.index.LeafReaderContext; |
| 26 | +import org.apache.lucene.index.Terms; |
| 27 | +import org.apache.lucene.search.ReferenceManager; |
| 28 | +import org.apache.lucene.search.suggest.document.CompletionTerms; |
| 29 | +import org.elasticsearch.action.ActionListener; |
| 30 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 31 | +import org.elasticsearch.common.FieldMemoryStats; |
| 32 | +import org.elasticsearch.common.regex.Regex; |
| 33 | +import org.elasticsearch.search.suggest.completion.CompletionStats; |
| 34 | + |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | +import java.util.function.Supplier; |
| 37 | + |
| 38 | +class CompletionStatsCache implements ReferenceManager.RefreshListener { |
| 39 | + |
| 40 | + private final Supplier<Engine.Searcher> searcherSupplier; |
| 41 | + |
| 42 | + /** |
| 43 | + * Contains a future (i.e. non-null) if another thread is already computing stats, in which case wait for this computation to |
| 44 | + * complete. Contains null otherwise, in which case compute the stats ourselves and save them here for other threads to use. |
| 45 | + * Futures are eventually completed with stats that include all fields, requiring further filtering (see |
| 46 | + * {@link CompletionStatsCache#filterCompletionStatsByFieldName}). |
| 47 | + */ |
| 48 | + private final AtomicReference<PlainActionFuture<CompletionStats>> completionStatsFutureRef = new AtomicReference<>(); |
| 49 | + |
| 50 | + CompletionStatsCache(Supplier<Engine.Searcher> searcherSupplier) { |
| 51 | + this.searcherSupplier = searcherSupplier; |
| 52 | + } |
| 53 | + |
| 54 | + CompletionStats get(String... fieldNamePatterns) { |
| 55 | + final PlainActionFuture<CompletionStats> newFuture = new PlainActionFuture<>(); |
| 56 | + final PlainActionFuture<CompletionStats> oldFuture = completionStatsFutureRef.compareAndExchange(null, newFuture); |
| 57 | + |
| 58 | + if (oldFuture != null) { |
| 59 | + // we lost the race, someone else is already computing stats, so we wait for that to finish |
| 60 | + return filterCompletionStatsByFieldName(fieldNamePatterns, oldFuture.actionGet()); |
| 61 | + } |
| 62 | + |
| 63 | + // we won the race, nobody else is already computing stats, so it's up to us |
| 64 | + ActionListener.completeWith(newFuture, () -> { |
| 65 | + long sizeInBytes = 0; |
| 66 | + final ObjectLongHashMap<String> completionFields = new ObjectLongHashMap<>(); |
| 67 | + |
| 68 | + try (Engine.Searcher currentSearcher = searcherSupplier.get()) { |
| 69 | + for (LeafReaderContext atomicReaderContext : currentSearcher.getIndexReader().leaves()) { |
| 70 | + LeafReader atomicReader = atomicReaderContext.reader(); |
| 71 | + for (FieldInfo info : atomicReader.getFieldInfos()) { |
| 72 | + Terms terms = atomicReader.terms(info.name); |
| 73 | + if (terms instanceof CompletionTerms) { |
| 74 | + // TODO: currently we load up the suggester for reporting its size |
| 75 | + final long fstSize = ((CompletionTerms) terms).suggester().ramBytesUsed(); |
| 76 | + completionFields.addTo(info.name, fstSize); |
| 77 | + sizeInBytes += fstSize; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return new CompletionStats(sizeInBytes, new FieldMemoryStats(completionFields)); |
| 84 | + }); |
| 85 | + |
| 86 | + boolean success = false; |
| 87 | + final CompletionStats completionStats; |
| 88 | + try { |
| 89 | + completionStats = newFuture.actionGet(); |
| 90 | + success = true; |
| 91 | + } finally { |
| 92 | + if (success == false) { |
| 93 | + // invalidate the cache (if not already invalidated) so that future calls will retry |
| 94 | + completionStatsFutureRef.compareAndSet(newFuture, null); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return filterCompletionStatsByFieldName(fieldNamePatterns, completionStats); |
| 99 | + } |
| 100 | + |
| 101 | + private static CompletionStats filterCompletionStatsByFieldName(String[] fieldNamePatterns, CompletionStats fullCompletionStats) { |
| 102 | + final FieldMemoryStats fieldMemoryStats; |
| 103 | + if (fieldNamePatterns != null && fieldNamePatterns.length > 0) { |
| 104 | + final ObjectLongHashMap<String> completionFields = new ObjectLongHashMap<>(fieldNamePatterns.length); |
| 105 | + for (ObjectLongCursor<String> fieldCursor : fullCompletionStats.getFields()) { |
| 106 | + if (Regex.simpleMatch(fieldNamePatterns, fieldCursor.key)) { |
| 107 | + completionFields.addTo(fieldCursor.key, fieldCursor.value); |
| 108 | + } |
| 109 | + } |
| 110 | + fieldMemoryStats = new FieldMemoryStats(completionFields); |
| 111 | + } else { |
| 112 | + fieldMemoryStats = null; |
| 113 | + } |
| 114 | + return new CompletionStats(fullCompletionStats.getSizeInBytes(), fieldMemoryStats); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void beforeRefresh() { |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void afterRefresh(boolean didRefresh) { |
| 123 | + if (didRefresh) { |
| 124 | + completionStatsFutureRef.set(null); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments