From 0e5332f86c84a48206c71b481186ba0bc2f3b570 Mon Sep 17 00:00:00 2001 From: Igor Motov Date: Mon, 18 Aug 2014 16:26:01 -0400 Subject: [PATCH] Fix NPE in SnapshotsService on node shutdown Fixes #6506 --- .../snapshots/SnapshotsService.java | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java b/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java index 98cf72c9197f5..fac582903c336 100644 --- a/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java +++ b/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java @@ -762,42 +762,44 @@ private void processIndexShardSnapshots(SnapshotMetaData snapshotMetaData) { Map> newSnapshots = newHashMap(); // Now go through all snapshots and update existing or create missing final String localNodeId = clusterService.localNode().id(); - for (SnapshotMetaData.Entry entry : snapshotMetaData.entries()) { - if (entry.state() == State.STARTED) { - Map startedShards = newHashMap(); - SnapshotShards snapshotShards = shardSnapshots.get(entry.snapshotId()); - for (Map.Entry shard : entry.shards().entrySet()) { - // Add all new shards to start processing on - if (localNodeId.equals(shard.getValue().nodeId())) { - if (shard.getValue().state() == State.INIT && (snapshotShards == null || !snapshotShards.shards.containsKey(shard.getKey()))) { - logger.trace("[{}] - Adding shard to the queue", shard.getKey()); - startedShards.put(shard.getKey(), new IndexShardSnapshotStatus()); + if (snapshotMetaData != null) { + for (SnapshotMetaData.Entry entry : snapshotMetaData.entries()) { + if (entry.state() == State.STARTED) { + Map startedShards = newHashMap(); + SnapshotShards snapshotShards = shardSnapshots.get(entry.snapshotId()); + for (Map.Entry shard : entry.shards().entrySet()) { + // Add all new shards to start processing on + if (localNodeId.equals(shard.getValue().nodeId())) { + if (shard.getValue().state() == State.INIT && (snapshotShards == null || !snapshotShards.shards.containsKey(shard.getKey()))) { + logger.trace("[{}] - Adding shard to the queue", shard.getKey()); + startedShards.put(shard.getKey(), new IndexShardSnapshotStatus()); + } } } - } - if (!startedShards.isEmpty()) { - newSnapshots.put(entry.snapshotId(), startedShards); - if (snapshotShards != null) { - // We already saw this snapshot but we need to add more started shards - ImmutableMap.Builder shards = ImmutableMap.builder(); - // Put all shards that were already running on this node - shards.putAll(snapshotShards.shards); - // Put all newly started shards - shards.putAll(startedShards); - survivors.put(entry.snapshotId(), new SnapshotShards(shards.build())); - } else { - // Brand new snapshot that we haven't seen before - survivors.put(entry.snapshotId(), new SnapshotShards(ImmutableMap.copyOf(startedShards))); + if (!startedShards.isEmpty()) { + newSnapshots.put(entry.snapshotId(), startedShards); + if (snapshotShards != null) { + // We already saw this snapshot but we need to add more started shards + ImmutableMap.Builder shards = ImmutableMap.builder(); + // Put all shards that were already running on this node + shards.putAll(snapshotShards.shards); + // Put all newly started shards + shards.putAll(startedShards); + survivors.put(entry.snapshotId(), new SnapshotShards(shards.build())); + } else { + // Brand new snapshot that we haven't seen before + survivors.put(entry.snapshotId(), new SnapshotShards(ImmutableMap.copyOf(startedShards))); + } } - } - } else if (entry.state() == State.ABORTED) { - // Abort all running shards for this snapshot - SnapshotShards snapshotShards = shardSnapshots.get(entry.snapshotId()); - if (snapshotShards != null) { - for (Map.Entry shard : entry.shards().entrySet()) { - IndexShardSnapshotStatus snapshotStatus = snapshotShards.shards.get(shard.getKey()); - if (snapshotStatus != null) { - snapshotStatus.abort(); + } else if (entry.state() == State.ABORTED) { + // Abort all running shards for this snapshot + SnapshotShards snapshotShards = shardSnapshots.get(entry.snapshotId()); + if (snapshotShards != null) { + for (Map.Entry shard : entry.shards().entrySet()) { + IndexShardSnapshotStatus snapshotStatus = snapshotShards.shards.get(shard.getKey()); + if (snapshotStatus != null) { + snapshotStatus.abort(); + } } } }