|
| 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 | + |
| 20 | +package org.elasticsearch.rollup; |
| 21 | + |
| 22 | +import org.apache.logging.log4j.LogManager; |
| 23 | +import org.apache.logging.log4j.Logger; |
| 24 | +import org.apache.lucene.index.Term; |
| 25 | +import org.apache.lucene.search.Query; |
| 26 | +import org.apache.lucene.search.QueryVisitor; |
| 27 | +import org.elasticsearch.cluster.metadata.IndexAbstraction; |
| 28 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 29 | +import org.elasticsearch.cluster.metadata.RollupGroup; |
| 30 | +import org.elasticsearch.cluster.metadata.RollupMetadata; |
| 31 | +import org.elasticsearch.common.Rounding; |
| 32 | +import org.elasticsearch.index.query.QueryBuilder; |
| 33 | +import org.elasticsearch.index.query.QueryShardContext; |
| 34 | +import org.elasticsearch.search.aggregations.AggregationBuilder; |
| 35 | +import org.elasticsearch.search.aggregations.AggregationInitializationException; |
| 36 | +import org.elasticsearch.search.aggregations.AggregatorFactories; |
| 37 | +import org.elasticsearch.search.aggregations.AggregatorFactory; |
| 38 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; |
| 39 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; |
| 40 | +import org.elasticsearch.search.aggregations.support.AggregationContext; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.time.ZoneId; |
| 44 | +import java.time.ZoneOffset; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.SortedMap; |
| 47 | + |
| 48 | +public class RollupShardDecider { |
| 49 | + private static final Logger logger = LogManager.getLogger(RollupShardDecider.class); |
| 50 | + |
| 51 | + public static boolean shouldMatchRollup(QueryShardContext context, |
| 52 | + QueryBuilder queryBuilder, |
| 53 | + AggregatorFactories.Builder aggFactoryBuilders, |
| 54 | + RollupMetadata rollupMetadata, |
| 55 | + Map<String, String> indexRollupMetadata, |
| 56 | + IndexMetadata requestIndexMetadata, |
| 57 | + String[] indices, |
| 58 | + SortedMap<String, IndexAbstraction> indexLookup) { |
| 59 | + // TODO(talevy): currently assumes that all indices part of the same rollup group are searched as well, since this is the case |
| 60 | + // for data-streams. should not be here in the non-datastream case... |
| 61 | + |
| 62 | + if (aggFactoryBuilders == null && indexRollupMetadata != null) { |
| 63 | + return false; |
| 64 | + } else if (aggFactoryBuilders != null && rollupMetadata != null && indexRollupMetadata != null && queryBuilder != null) { |
| 65 | + Query query = context.toQuery(queryBuilder).query(); |
| 66 | + try { |
| 67 | + AggregatorFactory[] factories = aggFactoryBuilders |
| 68 | + .build(new AggregationContext.ProductionAggregationContext(context, query), null) |
| 69 | + .factories(); |
| 70 | + } catch (IOException e) { |
| 71 | + throw new AggregationInitializationException("Failed to create aggregators, shard not supported", e); |
| 72 | + } |
| 73 | + |
| 74 | + if (queryShard(rollupMetadata, indices, requestIndexMetadata, aggFactoryBuilders) == false) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + // do something with query? |
| 78 | + query.visit(new QueryVisitor() { |
| 79 | + @Override |
| 80 | + public void consumeTerms(Query query, Term... terms) { |
| 81 | + super.consumeTerms(query, terms); |
| 82 | + } |
| 83 | + }); |
| 84 | + } else if (aggFactoryBuilders != null && rollupMetadata != null) { |
| 85 | + if (queryShard(rollupMetadata, indices, requestIndexMetadata, aggFactoryBuilders) == false) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + static boolean queryShard(RollupMetadata rollupMetadata, String[] indices, IndexMetadata requestIndexMetadata, |
| 94 | + AggregatorFactories.Builder aggFactoryBuilders) { |
| 95 | + String requestIndexName = requestIndexMetadata.getIndex().getName(); |
| 96 | + Map<String, String> indexRollupMetadata = requestIndexMetadata.getCustomData(RollupMetadata.TYPE); |
| 97 | + boolean isRollupIndex = requestIndexMetadata.getCustomData(RollupMetadata.TYPE) != null; |
| 98 | + final String originalIndexName; |
| 99 | + final RollupGroup rollupGroup; |
| 100 | + if (isRollupIndex) { |
| 101 | + // rollup is being searched |
| 102 | + originalIndexName = indexRollupMetadata.get(RollupMetadata.SOURCE_INDEX_NAME_META_FIELD); |
| 103 | + rollupGroup = rollupMetadata.rollupGroups().get(originalIndexName); |
| 104 | + } else if (rollupMetadata.contains(requestIndexName)) { |
| 105 | + originalIndexName = requestIndexName; |
| 106 | + rollupGroup = rollupMetadata.rollupGroups().get(requestIndexName); |
| 107 | + } else { |
| 108 | + // not part of a rollup group, search away! |
| 109 | + return true; |
| 110 | + } |
| 111 | + String bestIndexForAgg = bestIndexForAgg(originalIndexName, rollupGroup, aggFactoryBuilders); |
| 112 | + logger.warn("original index: " + originalIndexName); |
| 113 | + logger.warn("best index: " + bestIndexForAgg); |
| 114 | + return requestIndexName.equals(bestIndexForAgg); |
| 115 | + } |
| 116 | + |
| 117 | + static String bestIndexForAgg(String originalIndexName, RollupGroup rollupGroup, AggregatorFactories.Builder aggFactoryBuilders) { |
| 118 | + for (AggregationBuilder builder : aggFactoryBuilders.getAggregatorFactories()) { |
| 119 | + if (builder.getWriteableName().equals(DateHistogramAggregationBuilder.NAME)) { |
| 120 | + return bestIndexForDateHisto(originalIndexName, rollupGroup, (DateHistogramAggregationBuilder) builder); |
| 121 | + } |
| 122 | + } |
| 123 | + return originalIndexName; |
| 124 | + } |
| 125 | + |
| 126 | + static String bestIndexForDateHisto(String originalIndexName, RollupGroup rollupGroup, DateHistogramAggregationBuilder source) { |
| 127 | + DateHistogramInterval maxInterval = null; |
| 128 | + String bestIndex = originalIndexName; |
| 129 | + for (String rollupIndex : rollupGroup.getIndices()) { |
| 130 | + DateHistogramInterval thisInterval = rollupGroup.getDateInterval(rollupIndex); |
| 131 | + ZoneId thisTimezone = rollupGroup.getDateTimezone(rollupIndex).zoneId(); |
| 132 | + |
| 133 | + ZoneId sourceTimeZone = source.timeZone() == null ? ZoneOffset.UTC : ZoneId.of(source.timeZone().toString(), ZoneId.SHORT_IDS); |
| 134 | + DateHistogramInterval sourceInterval = source.getCalendarInterval(); |
| 135 | + |
| 136 | + if (thisTimezone.getRules().equals(sourceTimeZone.getRules()) == false) { |
| 137 | + continue; |
| 138 | + } |
| 139 | + if (validateCalendarInterval(sourceInterval, thisInterval)) { |
| 140 | + if (maxInterval == null) { |
| 141 | + bestIndex = rollupIndex; |
| 142 | + maxInterval = thisInterval; |
| 143 | + } else if (validateCalendarInterval(thisInterval, maxInterval)) { |
| 144 | + bestIndex = rollupIndex; |
| 145 | + maxInterval = thisInterval; |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return bestIndex; |
| 150 | + } |
| 151 | + |
| 152 | + static boolean validateCalendarInterval(DateHistogramInterval requestInterval, |
| 153 | + DateHistogramInterval configInterval) { |
| 154 | + if (requestInterval == null || configInterval == null) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + // The request must be gte the config. The CALENDAR_ORDERING map values are integers representing |
| 159 | + // relative orders between the calendar units |
| 160 | + Rounding.DateTimeUnit requestUnit = DateHistogramAggregationBuilder.DATE_FIELD_UNITS.get(requestInterval.toString()); |
| 161 | + if (requestUnit == null) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + Rounding.DateTimeUnit configUnit = DateHistogramAggregationBuilder.DATE_FIELD_UNITS.get(configInterval.toString()); |
| 165 | + if (configUnit == null) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + long requestOrder = requestUnit.getField().getBaseUnit().getDuration().toMillis(); |
| 170 | + long configOrder = configUnit.getField().getBaseUnit().getDuration().toMillis(); |
| 171 | + |
| 172 | + // All calendar units are multiples naturally, so we just care about gte |
| 173 | + return requestOrder >= configOrder; |
| 174 | + } |
| 175 | +} |
0 commit comments