|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.engine; |
| 11 | + |
| 12 | +import org.elasticsearch.index.mapper.DocumentParser; |
| 13 | +import org.elasticsearch.index.mapper.MappingLookup; |
| 14 | +import org.elasticsearch.index.shard.ShardId; |
| 15 | +import org.elasticsearch.index.translog.Translog; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * A utility class to assert that translog operations with the same sequence number |
| 23 | + * in the same generation are either identical or equivalent when synthetic sources are used. |
| 24 | + */ |
| 25 | +public abstract class TranslogOperationAsserter { |
| 26 | + public static final TranslogOperationAsserter DEFAULT = new TranslogOperationAsserter() { |
| 27 | + }; |
| 28 | + |
| 29 | + private TranslogOperationAsserter() { |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + public static TranslogOperationAsserter withEngineConfig(EngineConfig engineConfig) { |
| 34 | + return new TranslogOperationAsserter() { |
| 35 | + Translog.Index synthesizeSource(Translog.Index op) throws IOException { |
| 36 | + final ShardId shardId = engineConfig.getShardId(); |
| 37 | + MappingLookup mappingLookup = engineConfig.getMapperService().mappingLookup(); |
| 38 | + DocumentParser documentParser = engineConfig.getMapperService().documentParser(); |
| 39 | + try (var reader = new TranslogDirectoryReader(shardId, op, mappingLookup, documentParser, engineConfig, () -> {})) { |
| 40 | + final Engine.Searcher searcher = new Engine.Searcher( |
| 41 | + "assert_translog", |
| 42 | + reader, |
| 43 | + engineConfig.getSimilarity(), |
| 44 | + engineConfig.getQueryCache(), |
| 45 | + engineConfig.getQueryCachingPolicy(), |
| 46 | + () -> {} |
| 47 | + ); |
| 48 | + try ( |
| 49 | + LuceneSyntheticSourceChangesSnapshot snapshot = new LuceneSyntheticSourceChangesSnapshot( |
| 50 | + mappingLookup, |
| 51 | + searcher, |
| 52 | + LuceneSyntheticSourceChangesSnapshot.DEFAULT_BATCH_SIZE, |
| 53 | + Integer.MAX_VALUE, |
| 54 | + op.seqNo(), |
| 55 | + op.seqNo(), |
| 56 | + true, |
| 57 | + false, |
| 58 | + engineConfig.getIndexSettings().getIndexVersionCreated() |
| 59 | + ) |
| 60 | + ) { |
| 61 | + final Translog.Operation normalized = snapshot.next(); |
| 62 | + assert normalized != null : "expected one operation; got zero"; |
| 63 | + return (Translog.Index) normalized; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + boolean sameIndexOperation(Translog.Index o1, Translog.Index o2) throws IOException { |
| 70 | + return super.sameIndexOperation(o1, o2) |
| 71 | + || super.sameIndexOperation(synthesizeSource(o1), o2) |
| 72 | + || super.sameIndexOperation(o1, synthesizeSource(o2)); |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + boolean sameIndexOperation(Translog.Index o1, Translog.Index o2) throws IOException { |
| 78 | + // TODO: We haven't had timestamp for Index operations in Lucene yet, we need to loosen this check without timestamp. |
| 79 | + return Objects.equals(o1.id(), o2.id()) |
| 80 | + && Objects.equals(o1.source(), o2.source()) |
| 81 | + && Objects.equals(o1.routing(), o2.routing()) |
| 82 | + && o1.primaryTerm() == o2.primaryTerm() |
| 83 | + && o1.seqNo() == o2.seqNo() |
| 84 | + && o1.version() == o2.version(); |
| 85 | + } |
| 86 | + |
| 87 | + public boolean assertSameOperations( |
| 88 | + long seqNo, |
| 89 | + long generation, |
| 90 | + Translog.Operation newOp, |
| 91 | + Translog.Operation prvOp, |
| 92 | + Exception prevFailure |
| 93 | + ) throws IOException { |
| 94 | + final boolean sameOp; |
| 95 | + if (newOp instanceof final Translog.Index o2 && prvOp instanceof final Translog.Index o1) { |
| 96 | + sameOp = sameIndexOperation(o1, o2); |
| 97 | + } else if (newOp instanceof final Translog.Delete o1 && prvOp instanceof final Translog.Delete o2) { |
| 98 | + sameOp = Objects.equals(o1.id(), o2.id()) |
| 99 | + && o1.primaryTerm() == o2.primaryTerm() |
| 100 | + && o1.seqNo() == o2.seqNo() |
| 101 | + && o1.version() == o2.version(); |
| 102 | + } else { |
| 103 | + sameOp = false; |
| 104 | + } |
| 105 | + if (sameOp == false) { |
| 106 | + throw new AssertionError( |
| 107 | + "seqNo [" |
| 108 | + + seqNo |
| 109 | + + "] was processed twice in generation [" |
| 110 | + + generation |
| 111 | + + "], with different data. " |
| 112 | + + "prvOp [" |
| 113 | + + prvOp |
| 114 | + + "], newOp [" |
| 115 | + + newOp |
| 116 | + + "]", |
| 117 | + prevFailure |
| 118 | + ); |
| 119 | + } |
| 120 | + return true; |
| 121 | + } |
| 122 | +} |
0 commit comments