|
| 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.index.engine; |
| 21 | + |
| 22 | +import org.apache.lucene.index.DirectoryReader; |
| 23 | +import org.apache.lucene.index.IndexCommit; |
| 24 | +import org.apache.lucene.index.IndexWriter; |
| 25 | +import org.apache.lucene.index.IndexWriterConfig; |
| 26 | +import org.apache.lucene.index.NoMergePolicy; |
| 27 | +import org.apache.lucene.store.Directory; |
| 28 | +import org.elasticsearch.Assertions; |
| 29 | +import org.elasticsearch.common.UUIDs; |
| 30 | +import org.elasticsearch.index.seqno.SequenceNumbers; |
| 31 | +import org.elasticsearch.index.shard.ShardId; |
| 32 | +import org.elasticsearch.index.store.Store; |
| 33 | +import org.elasticsearch.index.translog.Translog; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * This class contains utility methods for mutating the shard lucene index and translog as a preparation to be opened. |
| 45 | + */ |
| 46 | +public abstract class EngineDiskUtils { |
| 47 | + |
| 48 | + /** |
| 49 | + * creates an empty lucene index and a corresponding empty translog. Any existing data will be deleted. |
| 50 | + */ |
| 51 | + public static void createEmpty(final Directory dir, final Path translogPath, final ShardId shardId) throws IOException { |
| 52 | + try (IndexWriter writer = newIndexWriter(true, dir)) { |
| 53 | + final String translogUuid = Translog.createEmptyTranslog(translogPath, SequenceNumbers.NO_OPS_PERFORMED, shardId); |
| 54 | + final Map<String, String> map = new HashMap<>(); |
| 55 | + map.put(Translog.TRANSLOG_GENERATION_KEY, "1"); |
| 56 | + map.put(Translog.TRANSLOG_UUID_KEY, translogUuid); |
| 57 | + map.put(Engine.HISTORY_UUID_KEY, UUIDs.randomBase64UUID()); |
| 58 | + map.put(SequenceNumbers.LOCAL_CHECKPOINT_KEY, Long.toString(SequenceNumbers.NO_OPS_PERFORMED)); |
| 59 | + map.put(SequenceNumbers.MAX_SEQ_NO, Long.toString(SequenceNumbers.NO_OPS_PERFORMED)); |
| 60 | + map.put(InternalEngine.MAX_UNSAFE_AUTO_ID_TIMESTAMP_COMMIT_ID, "-1"); |
| 61 | + updateCommitData(writer, map); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * Converts an existing lucene index and marks it with a new history uuid. Also creates a new empty translog file. |
| 68 | + * This is used to make sure no existing shard will recovery from this index using ops based recovery. |
| 69 | + */ |
| 70 | + public static void bootstrapNewHistoryFromLuceneIndex(final Directory dir, final Path translogPath, final ShardId shardId) |
| 71 | + throws IOException { |
| 72 | + try (IndexWriter writer = newIndexWriter(false, dir)) { |
| 73 | + final Map<String, String> userData = getUserData(writer); |
| 74 | + final long maxSeqNo = Long.parseLong(userData.get(SequenceNumbers.MAX_SEQ_NO)); |
| 75 | + final String translogUuid = Translog.createEmptyTranslog(translogPath, maxSeqNo, shardId); |
| 76 | + final Map<String, String> map = new HashMap<>(); |
| 77 | + map.put(Translog.TRANSLOG_GENERATION_KEY, "1"); |
| 78 | + map.put(Translog.TRANSLOG_UUID_KEY, translogUuid); |
| 79 | + map.put(Engine.HISTORY_UUID_KEY, UUIDs.randomBase64UUID()); |
| 80 | + map.put(SequenceNumbers.LOCAL_CHECKPOINT_KEY, Long.toString(maxSeqNo)); |
| 81 | + updateCommitData(writer, map); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Creates a new empty translog and associates it with an existing lucene index. |
| 87 | + */ |
| 88 | + public static void createNewTranslog(final Directory dir, final Path translogPath, long initialGlobalCheckpoint, final ShardId shardId) |
| 89 | + throws IOException { |
| 90 | + if (Assertions.ENABLED) { |
| 91 | + final List<IndexCommit> existingCommits = DirectoryReader.listCommits(dir); |
| 92 | + assert existingCommits.size() == 1 : "creating a translog translog should have one commit, commits[" + existingCommits + "]"; |
| 93 | + SequenceNumbers.CommitInfo commitInfo = Store.loadSeqNoInfo(existingCommits.get(0)); |
| 94 | + assert commitInfo.localCheckpoint >= initialGlobalCheckpoint : |
| 95 | + "trying to create a shard whose local checkpoint [" + commitInfo.localCheckpoint + "] is < global checkpoint [" |
| 96 | + + initialGlobalCheckpoint + "]"; |
| 97 | + } |
| 98 | + |
| 99 | + try (IndexWriter writer = newIndexWriter(false, dir)) { |
| 100 | + final String translogUuid = Translog.createEmptyTranslog(translogPath, initialGlobalCheckpoint, shardId); |
| 101 | + final Map<String, String> map = new HashMap<>(); |
| 102 | + map.put(Translog.TRANSLOG_GENERATION_KEY, "1"); |
| 103 | + map.put(Translog.TRANSLOG_UUID_KEY, translogUuid); |
| 104 | + updateCommitData(writer, map); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * Checks that the Lucene index contains a history uuid marker. If not, a new one is generated and committed. |
| 111 | + */ |
| 112 | + public static void ensureIndexHasHistoryUUID(final Directory dir) throws IOException { |
| 113 | + try (IndexWriter writer = newIndexWriter(false, dir)) { |
| 114 | + final Map<String, String> userData = getUserData(writer); |
| 115 | + if (userData.containsKey(Engine.HISTORY_UUID_KEY) == false) { |
| 116 | + updateCommitData(writer, Collections.singletonMap(Engine.HISTORY_UUID_KEY, UUIDs.randomBase64UUID())); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private static void updateCommitData(IndexWriter writer, Map<String, String> keysToUpdate) throws IOException { |
| 122 | + final Map<String, String> userData = getUserData(writer); |
| 123 | + userData.putAll(keysToUpdate); |
| 124 | + writer.setLiveCommitData(userData.entrySet()); |
| 125 | + writer.commit(); |
| 126 | + } |
| 127 | + |
| 128 | + private static Map<String, String> getUserData(IndexWriter writer) { |
| 129 | + final Map<String, String> userData = new HashMap<>(); |
| 130 | + writer.getLiveCommitData().forEach(e -> userData.put(e.getKey(), e.getValue())); |
| 131 | + return userData; |
| 132 | + } |
| 133 | + |
| 134 | + private static IndexWriter newIndexWriter(final boolean create, final Directory dir) throws IOException { |
| 135 | + IndexWriterConfig iwc = new IndexWriterConfig(null) |
| 136 | + .setCommitOnClose(false) |
| 137 | + // we don't want merges to happen here - we call maybe merge on the engine |
| 138 | + // later once we stared it up otherwise we would need to wait for it here |
| 139 | + // we also don't specify a codec here and merges should use the engines for this index |
| 140 | + .setMergePolicy(NoMergePolicy.INSTANCE) |
| 141 | + .setOpenMode(create ? IndexWriterConfig.OpenMode.CREATE : IndexWriterConfig.OpenMode.APPEND); |
| 142 | + return new IndexWriter(dir, iwc); |
| 143 | + } |
| 144 | +} |
0 commit comments