|
20 | 20 | package org.elasticsearch.index.seqno;
|
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.ActionListener;
|
| 23 | +import org.elasticsearch.action.support.PlainActionFuture; |
| 24 | +import org.elasticsearch.action.support.replication.ReplicationResponse; |
23 | 25 | import org.elasticsearch.cluster.routing.AllocationId;
|
24 | 26 | import org.elasticsearch.common.collect.Tuple;
|
25 | 27 | import org.elasticsearch.common.settings.Settings;
|
@@ -247,6 +249,103 @@ public void testRemoveRetentionLease() {
|
247 | 249 | }
|
248 | 250 | }
|
249 | 251 |
|
| 252 | + public void testCloneRetentionLease() { |
| 253 | + final AllocationId allocationId = AllocationId.newInitializing(); |
| 254 | + final AtomicReference<ReplicationTracker> replicationTrackerRef = new AtomicReference<>(); |
| 255 | + final AtomicLong timeReference = new AtomicLong(); |
| 256 | + final AtomicBoolean synced = new AtomicBoolean(); |
| 257 | + final ReplicationTracker replicationTracker = new ReplicationTracker( |
| 258 | + new ShardId("test", "_na", 0), |
| 259 | + allocationId.getId(), |
| 260 | + IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), |
| 261 | + randomLongBetween(1, Long.MAX_VALUE), |
| 262 | + UNASSIGNED_SEQ_NO, |
| 263 | + value -> {}, |
| 264 | + timeReference::get, |
| 265 | + (leases, listener) -> { |
| 266 | + assertFalse(Thread.holdsLock(replicationTrackerRef.get())); |
| 267 | + assertTrue(synced.compareAndSet(false, true)); |
| 268 | + listener.onResponse(new ReplicationResponse()); |
| 269 | + }); |
| 270 | + replicationTrackerRef.set(replicationTracker); |
| 271 | + replicationTracker.updateFromMaster( |
| 272 | + randomNonNegativeLong(), |
| 273 | + Collections.singleton(allocationId.getId()), |
| 274 | + routingTable(Collections.emptySet(), allocationId)); |
| 275 | + replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED); |
| 276 | + |
| 277 | + final long addTime = randomLongBetween(timeReference.get(), Long.MAX_VALUE); |
| 278 | + timeReference.set(addTime); |
| 279 | + final long minimumRetainingSequenceNumber = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, Long.MAX_VALUE); |
| 280 | + final PlainActionFuture<ReplicationResponse> addFuture = new PlainActionFuture<>(); |
| 281 | + replicationTracker.addRetentionLease("source", minimumRetainingSequenceNumber, "test-source", addFuture); |
| 282 | + addFuture.actionGet(); |
| 283 | + assertTrue(synced.get()); |
| 284 | + synced.set(false); |
| 285 | + |
| 286 | + final long cloneTime = randomLongBetween(timeReference.get(), Long.MAX_VALUE); |
| 287 | + timeReference.set(cloneTime); |
| 288 | + final PlainActionFuture<ReplicationResponse> cloneFuture = new PlainActionFuture<>(); |
| 289 | + final RetentionLease clonedLease = replicationTracker.cloneRetentionLease("source", "target", cloneFuture); |
| 290 | + cloneFuture.actionGet(); |
| 291 | + assertTrue(synced.get()); |
| 292 | + synced.set(false); |
| 293 | + |
| 294 | + assertThat(clonedLease.id(), equalTo("target")); |
| 295 | + assertThat(clonedLease.retainingSequenceNumber(), equalTo(minimumRetainingSequenceNumber)); |
| 296 | + assertThat(clonedLease.timestamp(), equalTo(cloneTime)); |
| 297 | + assertThat(clonedLease.source(), equalTo("test-source")); |
| 298 | + |
| 299 | + assertThat(replicationTracker.getRetentionLeases().get("target"), equalTo(clonedLease)); |
| 300 | + } |
| 301 | + |
| 302 | + public void testCloneNonexistentRetentionLease() { |
| 303 | + final AllocationId allocationId = AllocationId.newInitializing(); |
| 304 | + final ReplicationTracker replicationTracker = new ReplicationTracker( |
| 305 | + new ShardId("test", "_na", 0), |
| 306 | + allocationId.getId(), |
| 307 | + IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), |
| 308 | + randomLongBetween(1, Long.MAX_VALUE), |
| 309 | + UNASSIGNED_SEQ_NO, |
| 310 | + value -> {}, |
| 311 | + () -> 0L, |
| 312 | + (leases, listener) -> { }); |
| 313 | + replicationTracker.updateFromMaster( |
| 314 | + randomNonNegativeLong(), |
| 315 | + Collections.singleton(allocationId.getId()), |
| 316 | + routingTable(Collections.emptySet(), allocationId)); |
| 317 | + replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED); |
| 318 | + |
| 319 | + assertThat(expectThrows(RetentionLeaseNotFoundException.class, |
| 320 | + () -> replicationTracker.cloneRetentionLease("nonexistent-lease-id", "target", ActionListener.wrap(() -> {}))).getMessage(), |
| 321 | + equalTo("retention lease with ID [nonexistent-lease-id] not found")); |
| 322 | + } |
| 323 | + |
| 324 | + public void testCloneDuplicateRetentionLease() { |
| 325 | + final AllocationId allocationId = AllocationId.newInitializing(); |
| 326 | + final ReplicationTracker replicationTracker = new ReplicationTracker( |
| 327 | + new ShardId("test", "_na", 0), |
| 328 | + allocationId.getId(), |
| 329 | + IndexSettingsModule.newIndexSettings("test", Settings.EMPTY), |
| 330 | + randomLongBetween(1, Long.MAX_VALUE), |
| 331 | + UNASSIGNED_SEQ_NO, |
| 332 | + value -> {}, |
| 333 | + () -> 0L, |
| 334 | + (leases, listener) -> { }); |
| 335 | + replicationTracker.updateFromMaster( |
| 336 | + randomNonNegativeLong(), |
| 337 | + Collections.singleton(allocationId.getId()), |
| 338 | + routingTable(Collections.emptySet(), allocationId)); |
| 339 | + replicationTracker.activatePrimaryMode(SequenceNumbers.NO_OPS_PERFORMED); |
| 340 | + |
| 341 | + replicationTracker.addRetentionLease("source", randomLongBetween(0L, Long.MAX_VALUE), "test-source", ActionListener.wrap(() -> {})); |
| 342 | + replicationTracker.addRetentionLease("exists", randomLongBetween(0L, Long.MAX_VALUE), "test-source", ActionListener.wrap(() -> {})); |
| 343 | + |
| 344 | + assertThat(expectThrows(RetentionLeaseAlreadyExistsException.class, |
| 345 | + () -> replicationTracker.cloneRetentionLease("source", "exists", ActionListener.wrap(() -> {}))).getMessage(), |
| 346 | + equalTo("retention lease with ID [exists] already exists")); |
| 347 | + } |
| 348 | + |
250 | 349 | public void testRemoveNotFound() {
|
251 | 350 | final AllocationId allocationId = AllocationId.newInitializing();
|
252 | 351 | long primaryTerm = randomLongBetween(1, Long.MAX_VALUE);
|
|
0 commit comments