|
19 | 19 |
|
20 | 20 | package org.elasticsearch.test; |
21 | 21 |
|
| 22 | +import com.carrotsearch.hppc.ObjectLongMap; |
22 | 23 | import com.carrotsearch.randomizedtesting.RandomizedContext; |
23 | 24 | import com.carrotsearch.randomizedtesting.annotations.TestGroup; |
24 | 25 | import com.carrotsearch.randomizedtesting.generators.RandomNumbers; |
|
49 | 50 | import org.elasticsearch.action.admin.indices.segments.IndexShardSegments; |
50 | 51 | import org.elasticsearch.action.admin.indices.segments.IndicesSegmentResponse; |
51 | 52 | import org.elasticsearch.action.admin.indices.segments.ShardSegments; |
| 53 | +import org.elasticsearch.action.admin.indices.stats.IndexShardStats; |
| 54 | +import org.elasticsearch.action.admin.indices.stats.IndexStats; |
| 55 | +import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; |
| 56 | +import org.elasticsearch.action.admin.indices.stats.ShardStats; |
52 | 57 | import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequestBuilder; |
53 | 58 | import org.elasticsearch.action.bulk.BulkRequestBuilder; |
54 | 59 | import org.elasticsearch.action.bulk.BulkResponse; |
|
69 | 74 | import org.elasticsearch.cluster.metadata.IndexMetaData; |
70 | 75 | import org.elasticsearch.cluster.metadata.MappingMetaData; |
71 | 76 | import org.elasticsearch.cluster.metadata.MetaData; |
| 77 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
72 | 78 | import org.elasticsearch.cluster.routing.IndexRoutingTable; |
73 | 79 | import org.elasticsearch.cluster.routing.IndexShardRoutingTable; |
74 | 80 | import org.elasticsearch.cluster.routing.ShardRouting; |
|
114 | 120 | import org.elasticsearch.index.codec.CodecService; |
115 | 121 | import org.elasticsearch.index.engine.Segment; |
116 | 122 | import org.elasticsearch.index.mapper.DocumentMapper; |
| 123 | +import org.elasticsearch.index.seqno.SeqNoStats; |
| 124 | +import org.elasticsearch.index.seqno.SequenceNumbers; |
| 125 | +import org.elasticsearch.index.shard.IndexShard; |
117 | 126 | import org.elasticsearch.index.translog.Translog; |
118 | 127 | import org.elasticsearch.indices.IndicesQueryCache; |
119 | 128 | import org.elasticsearch.indices.IndicesRequestCache; |
|
161 | 170 | import java.util.List; |
162 | 171 | import java.util.Locale; |
163 | 172 | import java.util.Map; |
| 173 | +import java.util.Optional; |
164 | 174 | import java.util.Random; |
165 | 175 | import java.util.Set; |
166 | 176 | import java.util.concurrent.Callable; |
|
191 | 201 | import static org.hamcrest.Matchers.equalTo; |
192 | 202 | import static org.hamcrest.Matchers.is; |
193 | 203 | import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 204 | +import static org.hamcrest.Matchers.not; |
194 | 205 | import static org.hamcrest.Matchers.notNullValue; |
195 | 206 | import static org.hamcrest.Matchers.startsWith; |
196 | 207 |
|
@@ -2194,4 +2205,44 @@ public static Index resolveIndex(String index) { |
2194 | 2205 | String uuid = getIndexResponse.getSettings().get(index).get(IndexMetaData.SETTING_INDEX_UUID); |
2195 | 2206 | return new Index(index, uuid); |
2196 | 2207 | } |
| 2208 | + |
| 2209 | + protected void assertSeqNos() throws Exception { |
| 2210 | + assertBusy(() -> { |
| 2211 | + IndicesStatsResponse stats = client().admin().indices().prepareStats().clear().get(); |
| 2212 | + for (IndexStats indexStats : stats.getIndices().values()) { |
| 2213 | + for (IndexShardStats indexShardStats : indexStats.getIndexShards().values()) { |
| 2214 | + Optional<ShardStats> maybePrimary = Stream.of(indexShardStats.getShards()) |
| 2215 | + .filter(s -> s.getShardRouting().active() && s.getShardRouting().primary()) |
| 2216 | + .findFirst(); |
| 2217 | + if (maybePrimary.isPresent() == false) { |
| 2218 | + continue; |
| 2219 | + } |
| 2220 | + ShardStats primary = maybePrimary.get(); |
| 2221 | + final SeqNoStats primarySeqNoStats = primary.getSeqNoStats(); |
| 2222 | + final ShardRouting primaryShardRouting = primary.getShardRouting(); |
| 2223 | + assertThat(primaryShardRouting + " should have set the global checkpoint", |
| 2224 | + primarySeqNoStats.getGlobalCheckpoint(), not(equalTo(SequenceNumbers.UNASSIGNED_SEQ_NO))); |
| 2225 | + final DiscoveryNode node = clusterService().state().nodes().get(primaryShardRouting.currentNodeId()); |
| 2226 | + final IndicesService indicesService = |
| 2227 | + internalCluster().getInstance(IndicesService.class, node.getName()); |
| 2228 | + final IndexShard indexShard = indicesService.getShardOrNull(primaryShardRouting.shardId()); |
| 2229 | + final ObjectLongMap<String> globalCheckpoints = indexShard.getInSyncGlobalCheckpoints(); |
| 2230 | + for (ShardStats shardStats : indexShardStats) { |
| 2231 | + final SeqNoStats seqNoStats = shardStats.getSeqNoStats(); |
| 2232 | + assertThat(shardStats.getShardRouting() + " local checkpoint mismatch", |
| 2233 | + seqNoStats.getLocalCheckpoint(), equalTo(primarySeqNoStats.getLocalCheckpoint())); |
| 2234 | + assertThat(shardStats.getShardRouting() + " global checkpoint mismatch", |
| 2235 | + seqNoStats.getGlobalCheckpoint(), equalTo(primarySeqNoStats.getGlobalCheckpoint())); |
| 2236 | + assertThat(shardStats.getShardRouting() + " max seq no mismatch", |
| 2237 | + seqNoStats.getMaxSeqNo(), equalTo(primarySeqNoStats.getMaxSeqNo())); |
| 2238 | + // the local knowledge on the primary of the global checkpoint equals the global checkpoint on the shard |
| 2239 | + assertThat( |
| 2240 | + seqNoStats.getGlobalCheckpoint(), |
| 2241 | + equalTo(globalCheckpoints.get(shardStats.getShardRouting().allocationId().getId()))); |
| 2242 | + } |
| 2243 | + } |
| 2244 | + } |
| 2245 | + }); |
| 2246 | + } |
| 2247 | + |
2197 | 2248 | } |
0 commit comments