|
| 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 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.ccr.action; |
| 7 | + |
| 8 | +import org.elasticsearch.Version; |
| 9 | +import org.elasticsearch.action.ActionListener; |
| 10 | +import org.elasticsearch.action.support.replication.TransportWriteAction; |
| 11 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 12 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.unit.TimeValue; |
| 15 | +import org.elasticsearch.index.IndexSettings; |
| 16 | +import org.elasticsearch.index.engine.Engine.Operation.Origin; |
| 17 | +import org.elasticsearch.index.engine.EngineFactory; |
| 18 | +import org.elasticsearch.index.replication.ESIndexLevelReplicationTestCase; |
| 19 | +import org.elasticsearch.index.shard.IndexShard; |
| 20 | +import org.elasticsearch.index.shard.ShardId; |
| 21 | +import org.elasticsearch.index.translog.Translog; |
| 22 | +import org.elasticsearch.threadpool.ThreadPool; |
| 23 | +import org.elasticsearch.xpack.ccr.CcrSettings; |
| 24 | +import org.elasticsearch.xpack.ccr.action.bulk.BulkShardOperationsRequest; |
| 25 | +import org.elasticsearch.xpack.ccr.action.bulk.BulkShardOperationsResponse; |
| 26 | +import org.elasticsearch.xpack.ccr.action.bulk.TransportBulkShardOperationsAction; |
| 27 | +import org.elasticsearch.xpack.ccr.index.engine.FollowingEngineFactory; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 34 | +import java.util.function.BiConsumer; |
| 35 | +import java.util.function.Consumer; |
| 36 | +import java.util.function.LongConsumer; |
| 37 | + |
| 38 | +public class ShardFollowTaskReplicationTests extends ESIndexLevelReplicationTestCase { |
| 39 | + |
| 40 | + public void testSimpleCcrReplication() throws Exception { |
| 41 | + try (ReplicationGroup leaderGroup = createGroup(randomInt(2)); |
| 42 | + ReplicationGroup followerGroup = createFollowGroup(randomInt(2))) { |
| 43 | + leaderGroup.startAll(); |
| 44 | + int docCount = leaderGroup.appendDocs(randomInt(64)); |
| 45 | + leaderGroup.assertAllEqual(docCount); |
| 46 | + followerGroup.startAll(); |
| 47 | + ShardFollowNodeTask shardFollowTask = createShardFollowTask(leaderGroup, followerGroup); |
| 48 | + shardFollowTask.start(followerGroup.getPrimary().getGlobalCheckpoint()); |
| 49 | + docCount += leaderGroup.appendDocs(randomInt(128)); |
| 50 | + leaderGroup.syncGlobalCheckpoint(); |
| 51 | + |
| 52 | + leaderGroup.assertAllEqual(docCount); |
| 53 | + int expectedCount = docCount; |
| 54 | + assertBusy(() -> followerGroup.assertAllEqual(expectedCount)); |
| 55 | + shardFollowTask.markAsCompleted(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void testFailLeaderReplicaShard() throws Exception { |
| 60 | + try (ReplicationGroup leaderGroup = createGroup(1 + randomInt(1)); |
| 61 | + ReplicationGroup followerGroup = createFollowGroup(randomInt(2))) { |
| 62 | + leaderGroup.startAll(); |
| 63 | + followerGroup.startAll(); |
| 64 | + ShardFollowNodeTask shardFollowTask = createShardFollowTask(leaderGroup, followerGroup); |
| 65 | + shardFollowTask.start(followerGroup.getPrimary().getGlobalCheckpoint()); |
| 66 | + int docCount = 256; |
| 67 | + leaderGroup.appendDocs(1); |
| 68 | + Runnable task = () -> { |
| 69 | + try { |
| 70 | + leaderGroup.appendDocs(docCount - 1); |
| 71 | + leaderGroup.syncGlobalCheckpoint(); |
| 72 | + } catch (Exception e) { |
| 73 | + throw new AssertionError(e); |
| 74 | + } |
| 75 | + }; |
| 76 | + Thread thread = new Thread(task); |
| 77 | + thread.start(); |
| 78 | + |
| 79 | + // Remove and add a new replica |
| 80 | + IndexShard luckyReplica = randomFrom(leaderGroup.getReplicas()); |
| 81 | + leaderGroup.removeReplica(luckyReplica); |
| 82 | + luckyReplica.close("stop replica", false); |
| 83 | + luckyReplica.store().close(); |
| 84 | + leaderGroup.addReplica(); |
| 85 | + leaderGroup.startReplicas(1); |
| 86 | + thread.join(); |
| 87 | + |
| 88 | + leaderGroup.assertAllEqual(docCount); |
| 89 | + assertBusy(() -> followerGroup.assertAllEqual(docCount)); |
| 90 | + shardFollowTask.markAsCompleted(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected ReplicationGroup createGroup(int replicas, Settings settings) throws IOException { |
| 96 | + Settings newSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) |
| 97 | + .put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, replicas) |
| 98 | + .put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1) |
| 99 | + .put(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), true) |
| 100 | + .put(IndexSettings.INDEX_SOFT_DELETES_RETENTION_OPERATIONS_SETTING.getKey(), 10000) |
| 101 | + .put(settings) |
| 102 | + .build(); |
| 103 | + if (CcrSettings.CCR_FOLLOWING_INDEX_SETTING.get(newSettings)) { |
| 104 | + IndexMetaData metaData = buildIndexMetaData(replicas, newSettings, indexMapping); |
| 105 | + return new ReplicationGroup(metaData) { |
| 106 | + |
| 107 | + @Override |
| 108 | + protected EngineFactory getEngineFactory(ShardRouting routing) { |
| 109 | + return new FollowingEngineFactory(); |
| 110 | + } |
| 111 | + }; |
| 112 | + } else { |
| 113 | + return super.createGroup(replicas, newSettings); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private ReplicationGroup createFollowGroup(int replicas) throws IOException { |
| 118 | + Settings.Builder settingsBuilder = Settings.builder(); |
| 119 | + settingsBuilder.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true); |
| 120 | + return createGroup(replicas, settingsBuilder.build()); |
| 121 | + } |
| 122 | + |
| 123 | + private ShardFollowNodeTask createShardFollowTask(ReplicationGroup leaderGroup, ReplicationGroup followerGroup) { |
| 124 | + ShardFollowTask params = new ShardFollowTask(null, new ShardId("follow_index", "", 0), |
| 125 | + new ShardId("leader_index", "", 0), 1024, 1, Long.MAX_VALUE, 1, 10240, |
| 126 | + TimeValue.timeValueMillis(10), TimeValue.timeValueMillis(10), Collections.emptyMap()); |
| 127 | + |
| 128 | + BiConsumer<TimeValue, Runnable> scheduler = (delay, task) -> threadPool.schedule(delay, ThreadPool.Names.GENERIC, task); |
| 129 | + AtomicBoolean stopped = new AtomicBoolean(false); |
| 130 | + return new ShardFollowNodeTask(1L, "type", ShardFollowTask.NAME, "description", null, Collections.emptyMap(), params, scheduler) { |
| 131 | + @Override |
| 132 | + protected void innerUpdateMapping(LongConsumer handler, Consumer<Exception> errorHandler) { |
| 133 | + // noop, as mapping updates are not tested |
| 134 | + handler.accept(1L); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected void innerSendBulkShardOperationsRequest(List<Translog.Operation> operations, LongConsumer handler, |
| 139 | + Consumer<Exception> errorHandler) { |
| 140 | + Runnable task = () -> { |
| 141 | + BulkShardOperationsRequest request = new BulkShardOperationsRequest(params.getFollowShardId(), operations); |
| 142 | + ActionListener<BulkShardOperationsResponse> listener = |
| 143 | + ActionListener.wrap(r -> handler.accept(r.getGlobalCheckpoint()), errorHandler); |
| 144 | + new CCRAction(request, listener, followerGroup).execute(); |
| 145 | + }; |
| 146 | + threadPool.executor(ThreadPool.Names.GENERIC).execute(task); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void innerSendShardChangesRequest(long from, int maxOperationCount, Consumer<ShardChangesAction.Response> handler, |
| 151 | + Consumer<Exception> errorHandler) { |
| 152 | + Runnable task = () -> { |
| 153 | + List<IndexShard> indexShards = new ArrayList<>(leaderGroup.getReplicas()); |
| 154 | + indexShards.add(leaderGroup.getPrimary()); |
| 155 | + Collections.shuffle(indexShards, random()); |
| 156 | + |
| 157 | + Exception exception = null; |
| 158 | + for (IndexShard indexShard : indexShards) { |
| 159 | + long globalCheckpoint = indexShard.getGlobalCheckpoint(); |
| 160 | + try { |
| 161 | + Translog.Operation[] ops = ShardChangesAction.getOperations(indexShard, globalCheckpoint, from, |
| 162 | + maxOperationCount, params.getMaxBatchSizeInBytes()); |
| 163 | + // Hard code index metadata version, this is ok, as mapping updates are not tested here. |
| 164 | + handler.accept(new ShardChangesAction.Response(1L, globalCheckpoint, ops)); |
| 165 | + return; |
| 166 | + } catch (Exception e) { |
| 167 | + exception = e; |
| 168 | + } |
| 169 | + } |
| 170 | + assert exception != null; |
| 171 | + errorHandler.accept(exception); |
| 172 | + }; |
| 173 | + threadPool.executor(ThreadPool.Names.GENERIC).execute(task); |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + protected boolean isStopped() { |
| 178 | + return stopped.get(); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public void markAsCompleted() { |
| 183 | + stopped.set(true); |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public void markAsFailed(Exception e) { |
| 188 | + stopped.set(true); |
| 189 | + } |
| 190 | + |
| 191 | + }; |
| 192 | + } |
| 193 | + |
| 194 | + class CCRAction extends ReplicationAction<BulkShardOperationsRequest, BulkShardOperationsRequest, BulkShardOperationsResponse> { |
| 195 | + |
| 196 | + CCRAction(BulkShardOperationsRequest request, ActionListener<BulkShardOperationsResponse> listener, ReplicationGroup group) { |
| 197 | + super(request, listener, group, "ccr"); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + protected PrimaryResult performOnPrimary(IndexShard primary, BulkShardOperationsRequest request) throws Exception { |
| 202 | + TransportWriteAction.WritePrimaryResult<BulkShardOperationsRequest, BulkShardOperationsResponse> result = |
| 203 | + TransportBulkShardOperationsAction.shardOperationOnPrimary(primary.shardId(), request.getOperations(), |
| 204 | + primary, logger); |
| 205 | + return new PrimaryResult(result.replicaRequest(), result.finalResponseIfSuccessful); |
| 206 | + } |
| 207 | + |
| 208 | + @Override |
| 209 | + protected void performOnReplica(BulkShardOperationsRequest request, IndexShard replica) throws Exception { |
| 210 | + TransportBulkShardOperationsAction.applyTranslogOperations(request.getOperations(), replica, Origin.REPLICA); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments