|
| 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.document.Field; |
| 23 | +import org.apache.lucene.index.DirectoryReader; |
| 24 | +import org.apache.lucene.index.IndexCommit; |
| 25 | +import org.elasticsearch.common.bytes.BytesReference; |
| 26 | +import org.elasticsearch.index.mapper.ParseContext; |
| 27 | +import org.elasticsearch.index.mapper.ParsedDocument; |
| 28 | +import org.elasticsearch.index.mapper.SourceFieldMapper; |
| 29 | +import org.elasticsearch.index.seqno.SequenceNumbers; |
| 30 | +import org.elasticsearch.index.seqno.SequenceNumbersService; |
| 31 | +import org.elasticsearch.index.store.Store; |
| 32 | +import org.elasticsearch.index.translog.Translog; |
| 33 | +import org.junit.Before; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.concurrent.atomic.AtomicInteger; |
| 40 | +import java.util.concurrent.atomic.AtomicLong; |
| 41 | + |
| 42 | +import static org.hamcrest.Matchers.equalTo; |
| 43 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 44 | +import static org.hamcrest.Matchers.hasSize; |
| 45 | + |
| 46 | +public class KeepUntilGlobalCheckpointDeletionPolicyTests extends EngineTestCase { |
| 47 | + final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.UNASSIGNED_SEQ_NO); |
| 48 | + final AtomicInteger docId = new AtomicInteger(); |
| 49 | + |
| 50 | + @Before |
| 51 | + public void resetCounters() throws Exception { |
| 52 | + globalCheckpoint.set(SequenceNumbers.UNASSIGNED_SEQ_NO); |
| 53 | + docId.set(0); |
| 54 | + } |
| 55 | + |
| 56 | + public void testUnassignedGlobalCheckpoint() throws IOException { |
| 57 | + Path indexPath = createTempDir(); |
| 58 | + globalCheckpoint.set(SequenceNumbers.UNASSIGNED_SEQ_NO); |
| 59 | + try (Store store = createStore()) { |
| 60 | + int initDocs = scaledRandomIntBetween(10, 1000); |
| 61 | + int initCommits = 1; |
| 62 | + try (InternalEngine engine = newEngine(store, indexPath)) { |
| 63 | + for (int i = 0; i < initDocs; i++) { |
| 64 | + addDoc(engine); |
| 65 | + if (frequently()) { |
| 66 | + initCommits++; |
| 67 | + engine.flush(true, true); |
| 68 | + } |
| 69 | + if (rarely()) { |
| 70 | + engine.rollTranslogGeneration(); |
| 71 | + } |
| 72 | + } |
| 73 | + engine.flush(true, true); |
| 74 | + } |
| 75 | + assertThat(DirectoryReader.listCommits(store.directory()), hasSize(initCommits + 1)); |
| 76 | + try (InternalEngine engine = newEngine(store, indexPath)) { |
| 77 | + engine.refresh("test"); |
| 78 | + assertThat("Unassigned global checkpoint reserves all commits", DirectoryReader.listCommits(store.directory()), |
| 79 | + hasSize(initCommits + 1)); |
| 80 | + try (Translog.Snapshot snapshot = engine.getTranslog().newSnapshot()) { |
| 81 | + assertThat("Unassigned global checkpoint reserves all translog", snapshot.totalOperations(), equalTo(initDocs)); |
| 82 | + } |
| 83 | + int moreDocs = scaledRandomIntBetween(1, 100); |
| 84 | + int extraCommits = 0; |
| 85 | + for (int i = 0; i < moreDocs; i++) { |
| 86 | + addDoc(engine); |
| 87 | + if (frequently()) { |
| 88 | + engine.flush(true, true); |
| 89 | + extraCommits++; |
| 90 | + } |
| 91 | + } |
| 92 | + assertThat("Unassigned global checkpoint reserves all commits", DirectoryReader.listCommits(store.directory()), |
| 93 | + hasSize(initCommits + 1 + extraCommits)); |
| 94 | + try (Translog.Snapshot snapshot = engine.getTranslog().newSnapshot()) { |
| 95 | + assertThat("Unassigned global checkpoint reserves all translog", snapshot.totalOperations(), |
| 96 | + equalTo(initDocs + moreDocs)); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void testKeepUpGlobalCheckpoint() throws Exception { |
| 103 | + Path indexPath = createTempDir(); |
| 104 | + try (Store store = createStore()) { |
| 105 | + int initDocs = scaledRandomIntBetween(10, 1000); |
| 106 | + try (InternalEngine engine = newEngine(store, indexPath)) { |
| 107 | + for (int i = 0; i < initDocs; i++) { |
| 108 | + addDoc(engine); |
| 109 | + globalCheckpoint.set(engine.seqNoService().getLocalCheckpoint()); |
| 110 | + if (frequently()) { |
| 111 | + engine.flush(true, true); |
| 112 | + } |
| 113 | + } |
| 114 | + engine.flush(true, true); |
| 115 | + } |
| 116 | + assertThat(DirectoryReader.listCommits(store.directory()), hasSize(1)); |
| 117 | + try (InternalEngine engine = newEngine(store, indexPath)) { |
| 118 | + assertThat("OnInit deletes unreferenced commits", DirectoryReader.listCommits(store.directory()), hasSize(1)); |
| 119 | + int moreDocs = scaledRandomIntBetween(1, 100); |
| 120 | + for (int i = 0; i < moreDocs; i++) { |
| 121 | + addDoc(engine); |
| 122 | + globalCheckpoint.set(engine.seqNoService().getLocalCheckpoint()); |
| 123 | + if (frequently()) { |
| 124 | + engine.flush(true, true); |
| 125 | + assertThat("OnCommit deletes unreferenced commits", DirectoryReader.listCommits(store.directory()), hasSize(1)); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public void testLaggingGlobalCheckpoint() throws Exception { |
| 133 | + Path indexPath = createTempDir(); |
| 134 | + try (Store store = createStore()) { |
| 135 | + int initDocs = scaledRandomIntBetween(100, 1000); |
| 136 | + try (InternalEngine engine = newEngine(store, indexPath)) { |
| 137 | + for (int i = 0; i < initDocs; i++) { |
| 138 | + addDoc(engine); |
| 139 | + if (frequently()) { |
| 140 | + globalCheckpoint.set(engine.seqNoService().getLocalCheckpoint()); |
| 141 | + } |
| 142 | + if (frequently()) { |
| 143 | + engine.flush(true, true); |
| 144 | + } |
| 145 | + if (rarely()) { |
| 146 | + engine.rollTranslogGeneration(); |
| 147 | + } |
| 148 | + try (Translog.Snapshot snapshot = engine.getTranslog().newSnapshot()) { |
| 149 | + assertThat((long) snapshot.totalOperations(), greaterThanOrEqualTo(requiredOperations(i + 1))); |
| 150 | + } |
| 151 | + } |
| 152 | + engine.flush(true, true); |
| 153 | + } |
| 154 | + assertThat("Reserved commits should be 1", reservedCommits(), hasSize(1)); |
| 155 | + |
| 156 | + try (InternalEngine engine = newEngine(store, indexPath)) { |
| 157 | + assertThat("Reserved commits should always be 1", reservedCommits(), hasSize(1)); |
| 158 | + int moreDocs = scaledRandomIntBetween(1, 100); |
| 159 | + for (int i = 0; i < moreDocs; i++) { |
| 160 | + addDoc(engine); |
| 161 | + if (frequently()) { |
| 162 | + globalCheckpoint.set(engine.seqNoService().getLocalCheckpoint()); |
| 163 | + } |
| 164 | + if (frequently()) { |
| 165 | + engine.flush(true, true); |
| 166 | + assertThat("Reserved commits should be 1", reservedCommits(), hasSize(1)); |
| 167 | + } |
| 168 | + if (rarely()) { |
| 169 | + engine.rollTranslogGeneration(); |
| 170 | + } |
| 171 | + try (Translog.Snapshot snapshot = engine.getTranslog().newSnapshot()) { |
| 172 | + assertThat((long) snapshot.totalOperations(), greaterThanOrEqualTo(requiredOperations(initDocs + i + 1))); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + long requiredOperations(int processedOps) { |
| 180 | + return processedOps - Math.max(0, globalCheckpoint.get()); |
| 181 | + } |
| 182 | + |
| 183 | + List<IndexCommit> reservedCommits() throws IOException { |
| 184 | + List<IndexCommit> reservedCommits = new ArrayList<>(); |
| 185 | + List<IndexCommit> existingCommits = DirectoryReader.listCommits(store.directory()); |
| 186 | + for (IndexCommit commit : existingCommits) { |
| 187 | + if (Long.parseLong(commit.getUserData().get(SequenceNumbers.LOCAL_CHECKPOINT_KEY)) <= globalCheckpoint.get()) { |
| 188 | + reservedCommits.add(commit); |
| 189 | + } |
| 190 | + } |
| 191 | + return reservedCommits; |
| 192 | + } |
| 193 | + |
| 194 | + void addDoc(Engine engine) throws IOException { |
| 195 | + ParseContext.Document document = testDocumentWithTextField(); |
| 196 | + document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE)); |
| 197 | + ParsedDocument doc = testParsedDocument(Integer.toString(docId.getAndIncrement()), null, document, B_1, null); |
| 198 | + engine.index(indexForDoc(doc)); |
| 199 | + } |
| 200 | + |
| 201 | + InternalEngine newEngine(Store store, Path indexPath) throws IOException { |
| 202 | + return createEngine(defaultSettings, store, indexPath, newMergePolicy(), null, |
| 203 | + (config, seqNoStats) -> new SequenceNumbersService( |
| 204 | + config.getShardId(), |
| 205 | + config.getAllocationId(), |
| 206 | + config.getIndexSettings(), |
| 207 | + seqNoStats.getMaxSeqNo(), |
| 208 | + seqNoStats.getLocalCheckpoint(), |
| 209 | + seqNoStats.getGlobalCheckpoint()) { |
| 210 | + @Override |
| 211 | + public long getGlobalCheckpoint() { |
| 212 | + return globalCheckpoint.get(); |
| 213 | + } |
| 214 | + } |
| 215 | + ); |
| 216 | + } |
| 217 | +} |
0 commit comments