From 8285fbdd468d7b529431c4c055d801b11935b5e4 Mon Sep 17 00:00:00 2001 From: jaymode Date: Mon, 14 Nov 2016 10:33:47 -0500 Subject: [PATCH] address feedback Original commit: elastic/x-pack-elasticsearch@7f7f2c0e1f93889229b0c45945ff09e62d0b0e14 --- .../security/audit/index/IndexAuditTrail.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrail.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrail.java index 6002b0be9f1..87773392618 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrail.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/security/audit/index/IndexAuditTrail.java @@ -270,15 +270,19 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl client.admin().cluster().prepareState().execute(new ActionListener() { @Override public void onResponse(ClusterStateResponse clusterStateResponse) { - if (canStart(clusterStateResponse.getState(), master)) { - if (master) { + final boolean currentMaster = clusterService.state().getNodes().isLocalNodeElectedMaster(); + if (canStart(clusterStateResponse.getState(), currentMaster)) { + if (currentMaster) { putTemplate(customAuditIndexSettings(settings), ActionListener.wrap((v) -> innerStart(), (e) -> state.set(State.FAILED))); } else { innerStart(); } } else { - state.compareAndSet(State.STARTING, State.INITIALIZED); + if (state.compareAndSet(State.STARTING, State.INITIALIZED) == false) { + throw new IllegalStateException("state transition from starting to initialized failed, current value: " + + state.get()); + } } } @@ -287,13 +291,11 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl logger.error("failed to get remote cluster state", e); } }); + } else if (master) { + putTemplate(customAuditIndexSettings(settings), ActionListener.wrap((v) -> innerStart(), + (e) -> state.set(State.FAILED))); } else { - if (master) { - putTemplate(customAuditIndexSettings(settings), ActionListener.wrap((v) -> innerStart(), - (e) -> state.set(State.FAILED))); - } else { - innerStart(); - } + innerStart(); } } }