[LIFECYCLE] Add before/afterIndexShardDelete callback
This commit allows code to be executed before or after a shards content is deleted from disk. This is only executed if the shard owns the content ie. on a shard file system only a primary shard will execute these operations.
This commit is contained in:
parent
a6897aa073
commit
d9be606c5e
|
@ -441,7 +441,12 @@ public class IndexService extends AbstractIndexComponent implements IndexCompone
|
|||
if (deleted.get()) { // we remove that shards content if this index has been deleted
|
||||
try {
|
||||
if (ownsShard) {
|
||||
indicesServices.deleteShardStore("delete index", lock, indexSettings);
|
||||
try {
|
||||
indicesLifecycle.beforeIndexShardDeleted(lock.getShardId(), indexSettings);
|
||||
} finally {
|
||||
indicesServices.deleteShardStore("delete index", lock, indexSettings);
|
||||
indicesLifecycle.afterIndexShardDeleted(lock.getShardId(), indexSettings);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
indicesServices.addPendingDelete(lock.getShardId(), indexSettings);
|
||||
|
|
|
@ -146,6 +146,27 @@ public interface IndicesLifecycle {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Called before the index shard gets deleted from disk
|
||||
* Note: this method is only executed on the first attempt of deleting the shard. Retries are will not invoke
|
||||
* this method.
|
||||
* @param shardId The shard id
|
||||
* @param indexSettings the shards index settings
|
||||
*/
|
||||
public void beforeIndexShardDeleted(ShardId shardId, Settings indexSettings) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after the index shard has been deleted from disk.
|
||||
*
|
||||
* Note: this method is only called if the deletion of the shard did finish without an exception
|
||||
*
|
||||
* @param shardId The shard id
|
||||
* @param indexSettings the shards index settings
|
||||
*/
|
||||
public void afterIndexShardDeleted(ShardId shardId, Settings indexSettings) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after a shard's {@link org.elasticsearch.index.shard.IndexShardState} changes.
|
||||
* The order of concurrent events is preserved. The execution must be lightweight.
|
||||
|
|
|
@ -212,6 +212,30 @@ public class InternalIndicesLifecycle extends AbstractComponent implements Indic
|
|||
}
|
||||
}
|
||||
|
||||
public void beforeIndexShardDeleted(ShardId shardId,
|
||||
@IndexSettings Settings indexSettings) {
|
||||
for (Listener listener : listeners) {
|
||||
try {
|
||||
listener.beforeIndexShardDeleted(shardId, indexSettings);
|
||||
} catch (Throwable t) {
|
||||
logger.warn("{} failed to invoke before shard deleted callback", t, shardId);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void afterIndexShardDeleted(ShardId shardId,
|
||||
@IndexSettings Settings indexSettings) {
|
||||
for (Listener listener : listeners) {
|
||||
try {
|
||||
listener.afterIndexShardDeleted(shardId, indexSettings);
|
||||
} catch (Throwable t) {
|
||||
logger.warn("{} failed to invoke after shard deleted callback", t, shardId);
|
||||
throw t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void indexShardStateChanged(IndexShard indexShard, @Nullable IndexShardState previousState, @Nullable String reason) {
|
||||
for (Listener listener : listeners) {
|
||||
try {
|
||||
|
|
|
@ -22,6 +22,7 @@ import org.elasticsearch.common.settings.Settings;
|
|||
import org.elasticsearch.index.Index;
|
||||
import org.elasticsearch.index.IndexService;
|
||||
import org.elasticsearch.index.settings.IndexSettings;
|
||||
import org.elasticsearch.index.shard.ShardId;
|
||||
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -48,7 +49,7 @@ public class IndicesLifecycleListenerSingleNodeTests extends ElasticsearchSingle
|
|||
getInstanceFromNode(IndicesLifecycle.class).addListener(new IndicesLifecycle.Listener() {
|
||||
@Override
|
||||
public void afterIndexClosed(Index index, @IndexSettings Settings indexSettings) {
|
||||
assertEquals(counter.get(), 3);
|
||||
assertEquals(counter.get(), 5);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
|
@ -60,18 +61,30 @@ public class IndicesLifecycleListenerSingleNodeTests extends ElasticsearchSingle
|
|||
|
||||
@Override
|
||||
public void afterIndexDeleted(Index index, @IndexSettings Settings indexSettings) {
|
||||
assertEquals(counter.get(), 4);
|
||||
assertEquals(counter.get(), 6);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeIndexDeleted(IndexService indexService) {
|
||||
public void beforeIndexDeleted(IndexService indexService) {
|
||||
assertEquals(counter.get(), 2);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeIndexShardDeleted(ShardId shardId, Settings indexSettings) {
|
||||
assertEquals(counter.get(), 3);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterIndexShardDeleted(ShardId shardId, Settings indexSettings) {
|
||||
assertEquals(counter.get(), 4);
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
});
|
||||
assertAcked(client().admin().indices().prepareDelete("test").get());
|
||||
assertEquals(5, counter.get());
|
||||
assertEquals(7, counter.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue