Search in sources :

Example 1 with TestShardRouting.newShardRouting

use of org.opensearch.cluster.routing.TestShardRouting.newShardRouting in project OpenSearch by opensearch-project.

the class IndexShardTests method testClosedIndicesSkipSyncGlobalCheckpoint.

public void testClosedIndicesSkipSyncGlobalCheckpoint() throws Exception {
    ShardId shardId = new ShardId("index", "_na_", 0);
    IndexMetadata.Builder indexMetadata = IndexMetadata.builder("index").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 2)).state(IndexMetadata.State.CLOSE).primaryTerm(0, 1);
    ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, randomAlphaOfLength(8), true, ShardRoutingState.INITIALIZING, RecoverySource.EmptyStoreRecoverySource.INSTANCE);
    AtomicBoolean synced = new AtomicBoolean();
    IndexShard primaryShard = newShard(shardRouting, indexMetadata.build(), null, new InternalEngineFactory(), () -> synced.set(true), RetentionLeaseSyncer.EMPTY);
    recoverShardFromStore(primaryShard);
    IndexShard replicaShard = newShard(shardId, false);
    recoverReplica(replicaShard, primaryShard, true);
    int numDocs = between(1, 10);
    for (int i = 0; i < numDocs; i++) {
        indexDoc(primaryShard, "_doc", Integer.toString(i));
    }
    assertThat(primaryShard.getLocalCheckpoint(), equalTo(numDocs - 1L));
    primaryShard.updateLocalCheckpointForShard(replicaShard.shardRouting.allocationId().getId(), primaryShard.getLocalCheckpoint());
    long globalCheckpointOnReplica = randomLongBetween(SequenceNumbers.NO_OPS_PERFORMED, primaryShard.getLocalCheckpoint());
    primaryShard.updateGlobalCheckpointForShard(replicaShard.shardRouting.allocationId().getId(), globalCheckpointOnReplica);
    primaryShard.maybeSyncGlobalCheckpoint("test");
    assertFalse("closed indices should skip global checkpoint sync", synced.get());
    closeShards(primaryShard, replicaShard);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalEngineFactory(org.opensearch.index.engine.InternalEngineFactory) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 2 with TestShardRouting.newShardRouting

use of org.opensearch.cluster.routing.TestShardRouting.newShardRouting in project OpenSearch by opensearch-project.

the class IndexShardTests method testGlobalCheckpointSync.

public void testGlobalCheckpointSync() throws IOException {
    // create the primary shard with a callback that sets a boolean when the global checkpoint sync is invoked
    final ShardId shardId = new ShardId("index", "_na_", 0);
    final ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, randomAlphaOfLength(8), true, ShardRoutingState.INITIALIZING, RecoverySource.EmptyStoreRecoverySource.INSTANCE);
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 2).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).build();
    final IndexMetadata.Builder indexMetadata = IndexMetadata.builder(shardRouting.getIndexName()).settings(settings).primaryTerm(0, 1);
    final AtomicBoolean synced = new AtomicBoolean();
    final IndexShard primaryShard = newShard(shardRouting, indexMetadata.build(), null, new InternalEngineFactory(), () -> synced.set(true), RetentionLeaseSyncer.EMPTY);
    // add a replica
    recoverShardFromStore(primaryShard);
    final IndexShard replicaShard = newShard(shardId, false);
    recoverReplica(replicaShard, primaryShard, true);
    final int maxSeqNo = randomIntBetween(0, 128);
    for (int i = 0; i <= maxSeqNo; i++) {
        EngineTestCase.generateNewSeqNo(primaryShard.getEngine());
    }
    final long checkpoint = rarely() ? maxSeqNo - scaledRandomIntBetween(0, maxSeqNo) : maxSeqNo;
    // set up local checkpoints on the shard copies
    primaryShard.updateLocalCheckpointForShard(shardRouting.allocationId().getId(), checkpoint);
    final int replicaLocalCheckpoint = randomIntBetween(0, Math.toIntExact(checkpoint));
    final String replicaAllocationId = replicaShard.routingEntry().allocationId().getId();
    primaryShard.updateLocalCheckpointForShard(replicaAllocationId, replicaLocalCheckpoint);
    // initialize the local knowledge on the primary of the persisted global checkpoint on the replica shard
    final int replicaGlobalCheckpoint = randomIntBetween(Math.toIntExact(SequenceNumbers.NO_OPS_PERFORMED), Math.toIntExact(primaryShard.getLastKnownGlobalCheckpoint()));
    primaryShard.updateGlobalCheckpointForShard(replicaAllocationId, replicaGlobalCheckpoint);
    // initialize the local knowledge on the primary of the persisted global checkpoint on the primary
    primaryShard.updateGlobalCheckpointForShard(shardRouting.allocationId().getId(), primaryShard.getLastKnownGlobalCheckpoint());
    // simulate a background maybe sync; it should only run if the knowledge on the replica of the global checkpoint lags the primary
    primaryShard.maybeSyncGlobalCheckpoint("test");
    assertThat(synced.get(), equalTo(maxSeqNo == primaryShard.getLastKnownGlobalCheckpoint() && (replicaGlobalCheckpoint < checkpoint)));
    // simulate that the background sync advanced the global checkpoint on the replica
    primaryShard.updateGlobalCheckpointForShard(replicaAllocationId, primaryShard.getLastKnownGlobalCheckpoint());
    // reset our boolean so that we can assert after another simulated maybe sync
    synced.set(false);
    primaryShard.maybeSyncGlobalCheckpoint("test");
    // this time there should not be a sync since all the replica copies are caught up with the primary
    assertFalse(synced.get());
    closeShards(replicaShard, primaryShard);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalEngineFactory(org.opensearch.index.engine.InternalEngineFactory) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 3 with TestShardRouting.newShardRouting

use of org.opensearch.cluster.routing.TestShardRouting.newShardRouting in project OpenSearch by opensearch-project.

the class IndexShardTestCase method recoverShardFromSnapshot.

/**
 * Recover a shard from a snapshot using a given repository *
 */
protected void recoverShardFromSnapshot(final IndexShard shard, final Snapshot snapshot, final Repository repository) {
    final Version version = Version.CURRENT;
    final ShardId shardId = shard.shardId();
    final IndexId indexId = new IndexId(shardId.getIndex().getName(), shardId.getIndex().getUUID());
    final DiscoveryNode node = getFakeDiscoNode(shard.routingEntry().currentNodeId());
    final RecoverySource.SnapshotRecoverySource recoverySource = new RecoverySource.SnapshotRecoverySource(UUIDs.randomBase64UUID(), snapshot, version, indexId);
    final ShardRouting shardRouting = TestShardRouting.newShardRouting(shardId, node.getId(), true, ShardRoutingState.INITIALIZING, recoverySource);
    shard.markAsRecovering("from snapshot", new RecoveryState(shardRouting, node, null));
    final PlainActionFuture<Void> future = PlainActionFuture.newFuture();
    repository.restoreShard(shard.store(), snapshot.getSnapshotId(), indexId, shard.shardId(), shard.recoveryState(), future);
    future.actionGet();
}
Also used : IndexId(org.opensearch.repositories.IndexId) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Version(org.opensearch.Version) TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) RecoveryState(org.opensearch.indices.recovery.RecoveryState) RecoverySource(org.opensearch.cluster.routing.RecoverySource)

Example 4 with TestShardRouting.newShardRouting

use of org.opensearch.cluster.routing.TestShardRouting.newShardRouting in project OpenSearch by opensearch-project.

the class IndexShardTestCase method newShard.

/**
 * Creates a new initializing shard. The shard will have its own unique data path.
 *
 * @param primary       indicates whether to a primary shard (ready to recover from an empty store) or a replica (ready to recover from
 *                      another shard)
 * @param settings      the settings to use for this shard
 * @param engineFactory the engine factory to use for this shard
 */
protected IndexShard newShard(boolean primary, Settings settings, EngineFactory engineFactory) throws IOException {
    final RecoverySource recoverySource = primary ? RecoverySource.EmptyStoreRecoverySource.INSTANCE : RecoverySource.PeerRecoverySource.INSTANCE;
    final ShardRouting shardRouting = TestShardRouting.newShardRouting(new ShardId("index", "_na_", 0), randomAlphaOfLength(10), primary, ShardRoutingState.INITIALIZING, recoverySource);
    return newShard(shardRouting, settings, engineFactory);
}
Also used : TestShardRouting.newShardRouting(org.opensearch.cluster.routing.TestShardRouting.newShardRouting) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) RecoverySource(org.opensearch.cluster.routing.RecoverySource)

Aggregations

ShardRouting (org.opensearch.cluster.routing.ShardRouting)4 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)4 TestShardRouting.newShardRouting (org.opensearch.cluster.routing.TestShardRouting.newShardRouting)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 RecoverySource (org.opensearch.cluster.routing.RecoverySource)2 InternalEngineFactory (org.opensearch.index.engine.InternalEngineFactory)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 Version (org.opensearch.Version)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 IndexScopedSettings (org.opensearch.common.settings.IndexScopedSettings)1 Settings (org.opensearch.common.settings.Settings)1 IndexSettings (org.opensearch.index.IndexSettings)1 RecoveryState (org.opensearch.indices.recovery.RecoveryState)1 IndexId (org.opensearch.repositories.IndexId)1