From fa96015b220a2f8849c551e0fd0a606dbe5a43a0 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Tue, 28 Nov 2017 10:22:14 +0100 Subject: [PATCH 1/2] Update stage and failure message --- .../snapshots/IndexShardSnapshotStatus.java | 39 +++++++++++-------- .../blobstore/BlobStoreRepository.java | 3 +- .../snapshots/SnapshotShardsService.java | 3 +- .../snapshots/SnapshotsService.java | 6 +-- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java b/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java index 644caa7520be5..4dba81dc2688f 100644 --- a/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java +++ b/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java @@ -50,7 +50,7 @@ public enum Stage { FAILURE } - private Stage stage = Stage.INIT; + private volatile Stage stage = Stage.INIT; private long startTime; @@ -68,7 +68,7 @@ public enum Stage { private volatile boolean aborted; - private String failure; + private volatile String failure; /** * Returns current snapshot stage @@ -84,8 +84,27 @@ public Stage stage() { * * @param stage new snapshot stage */ - public void updateStage(Stage stage) { + public void updateStage(final Stage stage) { + updateStage(stage, null); + } + + /** + * Sets new snapshot stage and the reason for the failure if the snapshot + * is in the {@link IndexShardSnapshotStatus.Stage#FAILURE} state + * + * @param stage new snapshot stage + * @param failure the reason for the failure + */ + public synchronized void updateStage(final Stage stage, final String failure) { this.stage = stage; + this.failure = failure; + } + + /** + * Returns the reason for the failure if the snapshot is in the {@link IndexShardSnapshotStatus.Stage#FAILURE} state + */ + public String failure() { + return failure; } /** @@ -224,18 +243,4 @@ public void indexVersion(long indexVersion) { public long indexVersion() { return indexVersion; } - - /** - * Sets the reason for the failure if the snapshot is in the {@link IndexShardSnapshotStatus.Stage#FAILURE} state - */ - public void failure(String failure) { - this.failure = failure; - } - - /** - * Returns the reason for the failure if the snapshot is in the {@link IndexShardSnapshotStatus.Stage#FAILURE} state - */ - public String failure() { - return failure; - } } diff --git a/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java b/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java index 84d3d743f6402..b1f63295987ed 100644 --- a/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java +++ b/core/src/main/java/org/elasticsearch/repositories/blobstore/BlobStoreRepository.java @@ -825,8 +825,7 @@ public void snapshotShard(IndexShard shard, SnapshotId snapshotId, IndexId index snapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.DONE); } catch (Exception e) { snapshotStatus.time(System.currentTimeMillis() - snapshotStatus.startTime()); - snapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE); - snapshotStatus.failure(ExceptionsHelper.detailedMessage(e)); + snapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE,ExceptionsHelper.detailedMessage(e)); if (e instanceof IndexShardSnapshotFailedException) { throw (IndexShardSnapshotFailedException) e; } else { diff --git a/core/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java b/core/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java index 15f70e8b2c6fc..2290b6e4760c6 100644 --- a/core/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java +++ b/core/src/main/java/org/elasticsearch/snapshots/SnapshotShardsService.java @@ -228,7 +228,8 @@ private void processIndexShardSnapshots(ClusterChangedEvent event) { // running shards is missed, then the snapshot is removed is a subsequent cluster // state update, which is being processed here for (IndexShardSnapshotStatus snapshotStatus : entry.getValue().shards.values()) { - if (snapshotStatus.stage() == Stage.INIT || snapshotStatus.stage() == Stage.STARTED) { + Stage stage = snapshotStatus.stage(); + if (stage == Stage.INIT || stage == Stage.STARTED) { snapshotStatus.abort(); } } diff --git a/core/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java b/core/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java index 0804e69e46e23..efd8de23fbf18 100644 --- a/core/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java +++ b/core/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java @@ -596,8 +596,7 @@ public Map snapshotShards(final String reposi SnapshotShardFailure shardFailure = findShardFailure(snapshotInfo.shardFailures(), shardId); if (shardFailure != null) { IndexShardSnapshotStatus shardSnapshotStatus = new IndexShardSnapshotStatus(); - shardSnapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE); - shardSnapshotStatus.failure(shardFailure.reason()); + shardSnapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE, shardFailure.reason()); shardStatus.put(shardId, shardSnapshotStatus); } else { final IndexShardSnapshotStatus shardSnapshotStatus; @@ -610,8 +609,7 @@ public Map snapshotShards(final String reposi // a status for the shard to indicate that the shard snapshot // could not be taken due to partial being set to false. shardSnapshotStatus = new IndexShardSnapshotStatus(); - shardSnapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE); - shardSnapshotStatus.failure("skipped"); + shardSnapshotStatus.updateStage(IndexShardSnapshotStatus.Stage.FAILURE, "skipped"); } else { shardSnapshotStatus = repository.getShardSnapshotStatus( snapshotInfo.snapshotId(), From 2f4fb96fc2be4325edac8b80c7edfe649dcd5377 Mon Sep 17 00:00:00 2001 From: Tanguy Leroux Date: Tue, 28 Nov 2017 14:23:28 +0100 Subject: [PATCH 2/2] synchronized stage & failure --- .../index/snapshots/IndexShardSnapshotStatus.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java b/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java index 4dba81dc2688f..4fc8935ad0524 100644 --- a/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java +++ b/core/src/main/java/org/elasticsearch/index/snapshots/IndexShardSnapshotStatus.java @@ -75,7 +75,7 @@ public enum Stage { * * @return current snapshot stage */ - public Stage stage() { + public synchronized Stage stage() { return this.stage; } @@ -103,7 +103,7 @@ public synchronized void updateStage(final Stage stage, final String failure) { /** * Returns the reason for the failure if the snapshot is in the {@link IndexShardSnapshotStatus.Stage#FAILURE} state */ - public String failure() { + public synchronized String failure() { return failure; }