|
| 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.indices; |
| 21 | + |
| 22 | +import com.carrotsearch.hppc.cursors.ObjectCursor; |
| 23 | +import org.apache.logging.log4j.LogManager; |
| 24 | +import org.apache.logging.log4j.Logger; |
| 25 | +import org.apache.logging.log4j.message.ParameterizedMessage; |
| 26 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 27 | +import org.elasticsearch.cluster.ClusterChangedEvent; |
| 28 | +import org.elasticsearch.cluster.ClusterStateApplier; |
| 29 | +import org.elasticsearch.cluster.metadata.DataStream; |
| 30 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 31 | +import org.elasticsearch.cluster.metadata.Metadata; |
| 32 | +import org.elasticsearch.common.Nullable; |
| 33 | +import org.elasticsearch.common.component.AbstractLifecycleComponent; |
| 34 | +import org.elasticsearch.common.settings.Settings; |
| 35 | +import org.elasticsearch.common.util.concurrent.AbstractRunnable; |
| 36 | +import org.elasticsearch.common.util.concurrent.ConcurrentCollections; |
| 37 | +import org.elasticsearch.common.util.concurrent.EsExecutors; |
| 38 | +import org.elasticsearch.index.Index; |
| 39 | +import org.elasticsearch.index.IndexService; |
| 40 | +import org.elasticsearch.index.mapper.DateFieldMapper; |
| 41 | +import org.elasticsearch.index.mapper.MappedFieldType; |
| 42 | +import org.elasticsearch.index.mapper.MapperService; |
| 43 | +import org.elasticsearch.index.shard.IndexLongFieldRange; |
| 44 | +import org.elasticsearch.node.Node; |
| 45 | +import org.elasticsearch.threadpool.ThreadPool; |
| 46 | + |
| 47 | +import java.util.Map; |
| 48 | +import java.util.Objects; |
| 49 | +import java.util.concurrent.ExecutorService; |
| 50 | +import java.util.concurrent.TimeUnit; |
| 51 | + |
| 52 | +import static org.elasticsearch.common.util.concurrent.EsExecutors.daemonThreadFactory; |
| 53 | + |
| 54 | +/** |
| 55 | + * Tracks the mapping of the {@code @timestamp} field of immutable indices that expose their timestamp range in their index metadata. |
| 56 | + * Coordinating nodes do not have (easy) access to mappings for all indices, so we extract the type of this one field from the mapping here. |
| 57 | + */ |
| 58 | +public class TimestampFieldMapperService extends AbstractLifecycleComponent implements ClusterStateApplier { |
| 59 | + |
| 60 | + private static final Logger logger = LogManager.getLogger(TimestampFieldMapperService.class); |
| 61 | + |
| 62 | + private final IndicesService indicesService; |
| 63 | + private final ExecutorService executor; // single thread to construct mapper services async as needed |
| 64 | + |
| 65 | + /** |
| 66 | + * The type of the {@code @timestamp} field keyed by index. Futures may be completed with {@code null} to indicate that there is |
| 67 | + * no usable {@code @timestamp} field. |
| 68 | + */ |
| 69 | + private final Map<Index, PlainActionFuture<DateFieldMapper.DateFieldType>> fieldTypesByIndex = ConcurrentCollections.newConcurrentMap(); |
| 70 | + |
| 71 | + public TimestampFieldMapperService(Settings settings, ThreadPool threadPool, IndicesService indicesService) { |
| 72 | + this.indicesService = indicesService; |
| 73 | + |
| 74 | + final String nodeName = Objects.requireNonNull(Node.NODE_NAME_SETTING.get(settings)); |
| 75 | + final String threadName = "TimestampFieldMapperService#updateTask"; |
| 76 | + executor = EsExecutors.newScaling(nodeName + "/" + threadName, 0, 1, 0, TimeUnit.MILLISECONDS, |
| 77 | + daemonThreadFactory(nodeName, threadName), threadPool.getThreadContext()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void doStart() { |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + protected void doStop() { |
| 86 | + ThreadPool.terminate(executor, 10, TimeUnit.SECONDS); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected void doClose() { |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void applyClusterState(ClusterChangedEvent event) { |
| 95 | + final Metadata metadata = event.state().metadata(); |
| 96 | + |
| 97 | + // clear out mappers for indices that no longer exist or whose timestamp range is no longer known |
| 98 | + fieldTypesByIndex.keySet().removeIf(index -> hasUsefulTimestampField(metadata.index(index)) == false); |
| 99 | + |
| 100 | + // capture mappers for indices that do exist |
| 101 | + for (ObjectCursor<IndexMetadata> cursor : metadata.indices().values()) { |
| 102 | + final IndexMetadata indexMetadata = cursor.value; |
| 103 | + final Index index = indexMetadata.getIndex(); |
| 104 | + |
| 105 | + if (hasUsefulTimestampField(indexMetadata) && fieldTypesByIndex.containsKey(index) == false) { |
| 106 | + logger.trace("computing timestamp mapping for {}", index); |
| 107 | + final PlainActionFuture<DateFieldMapper.DateFieldType> future = new PlainActionFuture<>(); |
| 108 | + fieldTypesByIndex.put(index, future); |
| 109 | + |
| 110 | + final IndexService indexService = indicesService.indexService(index); |
| 111 | + if (indexService == null) { |
| 112 | + logger.trace("computing timestamp mapping for {} async", index); |
| 113 | + executor.execute(new AbstractRunnable() { |
| 114 | + @Override |
| 115 | + public void onFailure(Exception e) { |
| 116 | + logger.debug(new ParameterizedMessage("failed to compute mapping for {}", index), e); |
| 117 | + future.onResponse(null); // no need to propagate a failure to create the mapper service to searches |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + protected void doRun() throws Exception { |
| 122 | + try (MapperService mapperService = indicesService.createIndexMapperService(indexMetadata)) { |
| 123 | + mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_RECOVERY); |
| 124 | + future.onResponse(fromMapperService(mapperService)); |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + } else { |
| 129 | + logger.trace("computing timestamp mapping for {} using existing index service", index); |
| 130 | + try { |
| 131 | + future.onResponse(fromMapperService(indexService.mapperService())); |
| 132 | + } catch (Exception e) { |
| 133 | + assert false : e; |
| 134 | + future.onResponse(null); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static boolean hasUsefulTimestampField(IndexMetadata indexMetadata) { |
| 142 | + if (indexMetadata == null) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + final IndexLongFieldRange timestampMillisRange = indexMetadata.getTimestampMillisRange(); |
| 146 | + return timestampMillisRange.isComplete() && timestampMillisRange != IndexLongFieldRange.UNKNOWN; |
| 147 | + } |
| 148 | + |
| 149 | + private static DateFieldMapper.DateFieldType fromMapperService(MapperService mapperService) { |
| 150 | + final MappedFieldType mappedFieldType = mapperService.fieldType(DataStream.TimestampField.FIXED_TIMESTAMP_FIELD); |
| 151 | + if (mappedFieldType instanceof DateFieldMapper.DateFieldType) { |
| 152 | + return (DateFieldMapper.DateFieldType) mappedFieldType; |
| 153 | + } else { |
| 154 | + return null; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * @return the field type of the {@code @timestamp} field of the given index, or {@code null} if: |
| 160 | + * - the index is not found, |
| 161 | + * - the field is not found, |
| 162 | + * - the mapping is not known yet, or |
| 163 | + * - the field is not a timestamp field. |
| 164 | + */ |
| 165 | + @Nullable |
| 166 | + public DateFieldMapper.DateFieldType getTimestampFieldType(Index index) { |
| 167 | + final PlainActionFuture<DateFieldMapper.DateFieldType> future = fieldTypesByIndex.get(index); |
| 168 | + if (future == null || future.isDone() == false) { |
| 169 | + return null; |
| 170 | + } |
| 171 | + return future.actionGet(); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments