From fc4a1d96ae12908acb0fc01164dbe52b6d4c16a0 Mon Sep 17 00:00:00 2001 From: Yannick Welsch Date: Thu, 17 Jan 2019 19:18:47 +0100 Subject: [PATCH] Fix assertion at end of forceRefreshes (#37559) This commit ensures that we only change refreshListeners to a list if we're actually adding something to the list. --- .../index/shard/RefreshListeners.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/shard/RefreshListeners.java b/server/src/main/java/org/elasticsearch/index/shard/RefreshListeners.java index b4b9e13f7e063..713563eb111ba 100644 --- a/server/src/main/java/org/elasticsearch/index/shard/RefreshListeners.java +++ b/server/src/main/java/org/elasticsearch/index/shard/RefreshListeners.java @@ -129,15 +129,12 @@ public boolean addOrNotify(Translog.Location location, Consumer listene return true; } synchronized (this) { - List>> listeners = refreshListeners; - if (listeners == null) { - if (closed) { - throw new IllegalStateException("can't wait for refresh on a closed index"); - } - listeners = new ArrayList<>(); - refreshListeners = listeners; + if (closed) { + throw new IllegalStateException("can't wait for refresh on a closed index"); } - if (refreshForcers == 0 && listeners.size() < getMaxRefreshListeners.getAsInt()) { + List>> listeners = refreshListeners; + final int maxRefreshes = getMaxRefreshListeners.getAsInt(); + if (refreshForcers == 0 && maxRefreshes > 0 && (listeners == null || listeners.size() < maxRefreshes)) { ThreadContext.StoredContext storedContext = threadContext.newStoredContext(true); Consumer contextPreservingListener = forced -> { try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { @@ -145,8 +142,12 @@ public boolean addOrNotify(Translog.Location location, Consumer listene listener.accept(forced); } }; + if (listeners == null) { + listeners = new ArrayList<>(); + } // We have a free slot so register the listener listeners.add(new Tuple<>(location, contextPreservingListener)); + refreshListeners = listeners; return false; } }