Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add before/afterIndexShardDelete callbacks to index lifecycle #10173

Merged
merged 2 commits into from Mar 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/org/elasticsearch/index/IndexService.java
Expand Up @@ -441,7 +441,12 @@ private void onShardClose(ShardLock lock, boolean ownsShard) {
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);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/elasticsearch/indices/IndicesLifecycle.java
Expand Up @@ -146,6 +146,27 @@ public void afterIndexShardClosed(ShardId shardId, @Nullable IndexShard indexSha

}

/**
* 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, @IndexSettings 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, @IndexSettings 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.
Expand Down
Expand Up @@ -212,6 +212,30 @@ public void afterIndexShardClosed(ShardId shardId, @Nullable IndexShard indexSha
}
}

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 {
Expand Down
Expand Up @@ -22,6 +22,7 @@
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;

Expand All @@ -48,7 +49,7 @@ public void testCloseDeleteCallback() throws Throwable {
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();
}

Expand All @@ -60,18 +61,30 @@ public void beforeIndexClosed(IndexService indexService) {

@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());
}

}