Search in sources :

Example 1 with IndexRemovalReason

use of org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason in project OpenSearch by opensearch-project.

the class IndicesLifecycleListenerSingleNodeTests method testStartDeleteIndexEventCallback.

public void testStartDeleteIndexEventCallback() throws Throwable {
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    assertAcked(client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)));
    ensureGreen();
    Index idx = resolveIndex("test");
    IndexMetadata metadata = indicesService.indexService(idx).getMetadata();
    ShardRouting shardRouting = indicesService.indexService(idx).getShard(0).routingEntry();
    final AtomicInteger counter = new AtomicInteger(1);
    IndexEventListener countingListener = new IndexEventListener() {

        @Override
        public void beforeIndexCreated(Index index, Settings indexSettings) {
            assertEquals("test", index.getName());
            assertEquals(1, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void afterIndexCreated(IndexService indexService) {
            assertEquals("test", indexService.index().getName());
            assertEquals(2, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void beforeIndexShardCreated(ShardId shardId, Settings indexSettings) {
            assertEquals(3, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void afterIndexShardCreated(IndexShard indexShard) {
            assertEquals(4, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void afterIndexShardStarted(IndexShard indexShard) {
            assertEquals(5, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void beforeIndexRemoved(IndexService indexService, IndexRemovalReason reason) {
            assertEquals(DELETED, reason);
            assertEquals(6, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void beforeIndexShardDeleted(ShardId shardId, Settings indexSettings) {
            assertEquals(7, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void afterIndexShardDeleted(ShardId shardId, Settings indexSettings) {
            assertEquals(8, counter.get());
            counter.incrementAndGet();
        }

        @Override
        public void afterIndexRemoved(Index index, IndexSettings indexSettings, IndexRemovalReason reason) {
            assertEquals(DELETED, reason);
            assertEquals(9, counter.get());
            counter.incrementAndGet();
        }
    };
    indicesService.removeIndex(idx, DELETED, "simon says");
    try {
        IndexService index = indicesService.createIndex(metadata, Arrays.asList(countingListener), false);
        assertEquals(3, counter.get());
        idx = index.index();
        ShardRouting newRouting = shardRouting;
        String nodeId = newRouting.currentNodeId();
        UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "boom");
        newRouting = newRouting.moveToUnassigned(unassignedInfo).updateUnassigned(unassignedInfo, RecoverySource.EmptyStoreRecoverySource.INSTANCE);
        newRouting = ShardRoutingHelper.initialize(newRouting, nodeId);
        IndexShard shard = index.createShard(newRouting, s -> {
        }, RetentionLeaseSyncer.EMPTY);
        IndexShardTestCase.updateRoutingEntry(shard, newRouting);
        assertEquals(5, counter.get());
        final DiscoveryNode localNode = new DiscoveryNode("foo", buildNewFakeTransportAddress(), emptyMap(), emptySet(), Version.CURRENT);
        shard.markAsRecovering("store", new RecoveryState(newRouting, localNode, null));
        IndexShardTestCase.recoverFromStore(shard);
        newRouting = ShardRoutingHelper.moveToStarted(newRouting);
        IndexShardTestCase.updateRoutingEntry(shard, newRouting);
        assertEquals(6, counter.get());
    } finally {
        indicesService.removeIndex(idx, DELETED, "simon says");
    }
    assertEquals(10, counter.get());
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) IndexService(org.opensearch.index.IndexService) UnassignedInfo(org.opensearch.cluster.routing.UnassignedInfo) IndexShard(org.opensearch.index.shard.IndexShard) IndexSettings(org.opensearch.index.IndexSettings) Index(org.opensearch.index.Index) ShardId(org.opensearch.index.shard.ShardId) IndexEventListener(org.opensearch.index.shard.IndexEventListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexRemovalReason(org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting) RecoveryState(org.opensearch.indices.recovery.RecoveryState) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 2 with IndexRemovalReason

use of org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason in project OpenSearch by opensearch-project.

the class IndexModuleTests method testOtherServiceBound.

public void testOtherServiceBound() throws IOException {
    final AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    final IndexEventListener eventListener = new IndexEventListener() {

        @Override
        public void beforeIndexRemoved(IndexService indexService, IndexRemovalReason reason) {
            atomicBoolean.set(true);
        }
    };
    IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings);
    IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry);
    module.addIndexEventListener(eventListener);
    IndexService indexService = newIndexService(module);
    IndexSettings x = indexService.getIndexSettings();
    assertEquals(x.getSettings(), indexSettings.getSettings());
    assertEquals(x.getIndex(), index);
    indexService.getIndexEventListener().beforeIndexRemoved(null, null);
    assertTrue(atomicBoolean.get());
    indexService.close("simon says", false);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexEventListener(org.opensearch.index.shard.IndexEventListener) IndexRemovalReason(org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason)

Aggregations

IndexEventListener (org.opensearch.index.shard.IndexEventListener)2 IndexRemovalReason (org.opensearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 ShardRouting (org.opensearch.cluster.routing.ShardRouting)1 UnassignedInfo (org.opensearch.cluster.routing.UnassignedInfo)1 Settings (org.opensearch.common.settings.Settings)1 Index (org.opensearch.index.Index)1 IndexService (org.opensearch.index.IndexService)1 IndexSettings (org.opensearch.index.IndexSettings)1 IndexShard (org.opensearch.index.shard.IndexShard)1 ShardId (org.opensearch.index.shard.ShardId)1 RecoveryState (org.opensearch.indices.recovery.RecoveryState)1