Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.storm.utils;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import org.apache.storm.shade.com.google.common.annotations.VisibleForTesting;
import org.apache.storm.shade.org.apache.curator.RetryPolicy;
import org.apache.storm.shade.org.apache.curator.RetrySleeper;
import org.apache.storm.shade.org.apache.curator.framework.CuratorFramework;
import org.apache.storm.shade.org.apache.curator.framework.state.ConnectionState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A {@link RetryPolicy} wrapper that makes the Curator retry loop aware of the
* ZooKeeper connection state.
*
* <p>When the connection is {@link ConnectionState#SUSPENDED} or
* {@link ConnectionState#LOST}, instead of blindly sleeping and retrying (which
* races against the ZK client's {@code SendThread} reconnection), this policy
* calls {@link CuratorFramework#blockUntilConnected} to yield to the
* {@code SendThread} and wait for it to failover to another ensemble member.
*
* <p>Once the connection is re-established, the retry loop immediately retries
* the operation on the new connection. If the connection cannot be
* re-established within the session timeout, the retry is abandoned.
*
* <p>For all other connection states, this policy delegates to the wrapped
* delegate policy (typically a {@link StormBoundedExponentialBackoffRetry}).
*/
public class ConnectionAwareRetryPolicy implements RetryPolicy {

private static final Logger LOG = LoggerFactory.getLogger(ConnectionAwareRetryPolicy.class);

/**
* Maximum jitter (in ms) applied after a successful reconnection to avoid
* thundering-herd when many suspended clients retry in lockstep.
*/
private static final long RECONNECT_JITTER_MS = 100;

private final RetryPolicy delegate;
private final Supplier<CuratorFramework> zkSupplier;
private final int sessionTimeoutMs;
private final AtomicReference<ConnectionState> connectionState =
new AtomicReference<>(ConnectionState.CONNECTED);

/**
* @param delegate the underlying retry policy to delegate to for normal (connected) retries
* @param zkSupplier supplier for the {@link CuratorFramework}, used for late binding since
* the framework does not exist until {@code builder.build()} returns
* @param sessionTimeoutMs upper bound (in ms) for waiting on reconnection; typically
* {@code storm.zookeeper.session.timeout}
*/
public ConnectionAwareRetryPolicy(RetryPolicy delegate,
Supplier<CuratorFramework> zkSupplier,
int sessionTimeoutMs) {
this.delegate = delegate;
this.zkSupplier = zkSupplier;
this.sessionTimeoutMs = sessionTimeoutMs;
}

/**
* Register a {@link org.apache.storm.shade.org.apache.curator.framework.state.ConnectionStateListener}
* on the given framework to track the current connection state.
* Must be called after the {@link CuratorFramework} has been built.
*
* @param zk the built CuratorFramework
*/
public void bind(CuratorFramework zk) {
zk.getConnectionStateListenable().addListener((client, newState) -> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The state is updated asynchronously on the ConnectionStateManager thread, so a retry right after the disconnect can still see CONNECTED here (in my test I had to wait for this field to flip to SUSPENDED). zk.getZookeeperClient().isConnected() is updated synchronously and would also remove the need for the late-bound AtomicReference and bind() in CuratorUtils.

ConnectionState prev = connectionState.getAndSet(newState);
if (prev != newState) {
LOG.debug("ZK connection state changed: {} -> {}", prev, newState);
}
});
}

@VisibleForTesting
RetryPolicy getDelegate() {
return delegate;
}

@Override
public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleepSleeper) {
ConnectionState state = connectionState.get();

if (state == ConnectionState.SUSPENDED || state == ConnectionState.LOST) {
// Honour the configured retry budget even while suspended.
if (!delegate.allowRetry(retryCount, elapsedTimeMs, sleepSleeper)) {
return false;
}

CuratorFramework zk = zkSupplier.get();
if (zk == null) {
// Framework not yet available; delegate already approved the retry
return true;
}
Comment on lines +107 to +116

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core change: replace the budget check that calls the sleeping delegate with a pure retry-count cap, so the suspended path no longer reintroduces the exponential backoff sleep. Depends on the new maxRetries field/constructor arg and the CuratorUtils wiring in the patch above — apply those together or this won't compile.

Suggested change
// Honour the configured retry budget even while suspended.
if (!delegate.allowRetry(retryCount, elapsedTimeMs, sleepSleeper)) {
return false;
}
CuratorFramework zk = zkSupplier.get();
if (zk == null) {
// Framework not yet available; delegate already approved the retry
return true;
}
// Cap total attempts, but WITHOUT the delegate's exponential backoff sleep. On
// SUSPENDED/LOST this policy yields to the ZK SendThread via blockUntilConnected()
// instead of blind-sleeping; calling delegate.allowRetry() here would sleep the backoff
// as a side effect and reintroduce exactly the blind backoff this policy exists to avoid.
if (retryCount >= maxRetries) {
LOG.warn("ZK connection {} and retry budget ({}) exhausted on retry {}, abandoning retry",
state, maxRetries, retryCount);
return false;
}
CuratorFramework zk = zkSupplier.get();
if (zk == null) {
// Framework not yet available; within budget, so allow the retry.
return true;
}


LOG.info("ZK connection is {} on retry {}, waiting for reconnection (timeout {}ms)",
state, retryCount, sessionTimeoutMs);
try {
boolean reconnected = zk.blockUntilConnected(sessionTimeoutMs, TimeUnit.MILLISECONDS);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the blocking point from the summary: when allowRetry is called from the ZooKeeper EventThread (background operation retries via checkBackgroundRetry), this wait prevents the SyncConnected event from being processed, Curator stays SUSPENDED, and after the session timeout it injects a session expiration. In my test the server came back after 0.6 s, but the call blocked for the full 6 s session timeout, returned false, and the client went to LOST.

if (reconnected) {
LOG.info("ZK connection re-established (state: {}), retrying operation",
connectionState.get());
// Jitter to avoid thundering-herd on ensemble recovery.
long jitterMs = ThreadLocalRandom.current().nextLong(RECONNECT_JITTER_MS);
if (jitterMs > 0) {
sleepSleeper.sleepFor(jitterMs, TimeUnit.MILLISECONDS);
}
return true;
}
LOG.warn("ZK connection not re-established within {}ms, abandoning retry",
sessionTimeoutMs);
return false;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOG.warn("Interrupted while waiting for ZK reconnection", e);
return false;
}
}

// Connection is healthy (CONNECTED, RECONNECTED, or READ_ONLY) —
// delegate to the existing backoff policy for normal retry behaviour.
return delegate.allowRetry(retryCount, elapsedTimeMs, sleepSleeper);
}
}
20 changes: 19 additions & 1 deletion storm-client/src/jvm/org/apache/storm/utils/CuratorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import javax.naming.ConfigurationException;
import org.apache.storm.Config;
import org.apache.storm.shade.org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -61,6 +62,19 @@ public static CuratorFramework newCurator(Map<String, Object> conf, List<String>
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();

setupBuilder(builder, zkStr, conf, auth);

// Wrap with a connection-aware policy that yields to SendThread on SUSPENDED/LOST.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies to every client built by newCurator, not only the supervisor: Nimbus, Trident TransactionalState (which runs on executor threads) and BlobStoreUtils. With this policy a ZooKeeper call on any of those threads can block up to a session timeout per retry instead of failing. If the intent is to protect the supervisor, it would be worth either scoping it or calling the broader impact out in the description.

Nit: setupBuilder still builds a StormBoundedExponentialBackoffRetry that is immediately replaced here, and its new comment (line 101) sits above connectString rather than the retryPolicy call.

AtomicReference<CuratorFramework> zkRef = new AtomicReference<>();
int sessionTimeoutMs = ObjectReader.getInt(conf.get(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT));
ConnectionAwareRetryPolicy connectionAwarePolicy = new ConnectionAwareRetryPolicy(
new StormBoundedExponentialBackoffRetry(
ObjectReader.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL)),
ObjectReader.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL_CEILING)),
ObjectReader.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_TIMES))),
zkRef::get,
sessionTimeoutMs);
builder.retryPolicy(connectionAwarePolicy);

if (defaultAcl != null) {
builder.aclProvider(new ACLProvider() {
@Override
Expand All @@ -75,12 +89,16 @@ public List<ACL> getAclForPath(String s) {
});
}

return builder.build();
CuratorFramework framework = builder.build();
zkRef.set(framework);
connectionAwarePolicy.bind(framework);
return framework;
}

protected static void setupBuilder(CuratorFrameworkFactory.Builder builder, final String zkStr,
Map<String, Object> conf,
ZookeeperAuthInfo auth) {
// Default retry policy; overridden by newCurator() with ConnectionAwareRetryPolicy.
builder.connectString(zkStr);
builder
.connectionTimeoutMs(ObjectReader.getInt(conf
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.storm.utils;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.curator.test.TestingServer;
import org.apache.storm.Config;
import org.apache.storm.shade.org.apache.curator.RetryPolicy;
import org.apache.storm.shade.org.apache.curator.framework.CuratorFramework;
import org.apache.storm.shade.org.apache.curator.framework.api.CuratorEventType;
import org.apache.storm.shade.org.apache.curator.framework.state.ConnectionState;
import org.apache.storm.shade.org.apache.zookeeper.Watcher;
import org.junit.jupiter.api.Test;

/**
* Exercises {@link ConnectionAwareRetryPolicy} against a real ZooKeeper server that is stopped
* and restarted after a short outage.
*
* <p>Curator also consults the retry policy from the ZooKeeper EventThread, when it retries a
* background operation inside its async callback
* ({@code CuratorFrameworkImpl.checkBackgroundRetry}). That thread is the one that must deliver
* {@code SyncConnected}. If the policy blocks it in {@code blockUntilConnected}, Curator never
* sees {@code RECONNECTED}, and once the session timeout elapses while {@code SUSPENDED} it
* injects a session expiration, so a session that had already recovered is lost.
*/
public class ConnectionAwareRetryPolicyEventThreadTest {

private static final int SESSION_TIMEOUT_MS = 6000;
private static final int CONNECTION_TIMEOUT_MS = 3000;
private static final long OUTAGE_MS = 500;

/**
* Control case: off the EventThread the policy waits for the reconnection and allows the retry.
*/
@Test
public void allowRetryOffEventThreadSurvivesShortOutage() throws Exception {
Outcome outcome = run(false);
assertTrue(outcome.allowed, "retry should be allowed once the server is back: " + outcome);
assertFalse(outcome.lost(), "a short outage must not lose the session: " + outcome);
}

/**
* Fails with the current implementation: the policy blocks the EventThread for the whole
* session timeout, returns false, and Curator injects a session expiration (LOST).
*/
@Test
public void allowRetryOnEventThreadSurvivesShortOutage() throws Exception {
Outcome outcome = run(true);
assertTrue(outcome.allowed, "retry should be allowed once the server is back: " + outcome);
assertFalse(outcome.lost(), "a short outage must not lose the session: " + outcome);
}

private Outcome run(boolean onEventThread) throws Exception {
try (TestingServer server = new TestingServer(true)) {
Map<String, Object> conf = Utils.readDefaultConfig();
conf.put(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT, SESSION_TIMEOUT_MS);
conf.put(Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT, CONNECTION_TIMEOUT_MS);
String[] hostPort = server.getConnectString().split(":");
CuratorFramework zk = CuratorUtils.newCuratorStarted(conf, List.of(hostPort[0]),
Integer.parseInt(hostPort[1]), null, null);
try {
assertTrue(zk.blockUntilConnected(10, TimeUnit.SECONDS), "initial connection");
RetryPolicy policy = zk.getZookeeperClient().getRetryPolicy();
AtomicReference<ConnectionState> policyState = policyState(policy);

List<ConnectionState> states = new CopyOnWriteArrayList<>();
zk.getConnectionStateListenable().addListener((client, state) -> states.add(state));

CountDownLatch disconnected = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(1);
Outcome outcome = new Outcome(states);
Runnable callPolicy = () -> {
try {
// Same condition the policy checks: wait until its listener saw SUSPENDED.
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(3);
while (policyState.get() != ConnectionState.SUSPENDED
&& System.nanoTime() < deadline) {
Thread.sleep(5);
}
outcome.thread = Thread.currentThread().getName();
long start = System.nanoTime();
outcome.allowed = policy.allowRetry(0, 0, (time, unit) -> { });
outcome.blockedMs = (System.nanoTime() - start) / 1_000_000;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown();
}
};
// CuratorListener callbacks for connection events run on the ZooKeeper EventThread,
// the same thread that runs checkBackgroundRetry.
zk.getCuratorListenable().addListener((client, event) -> {
Watcher.Event.KeeperState state = event.getType() == CuratorEventType.WATCHED
? event.getWatchedEvent().getState() : null;
if (state == Watcher.Event.KeeperState.Disconnected
&& disconnected.getCount() > 0) {
disconnected.countDown();
if (onEventThread) {
callPolicy.run();
} else {
new Thread(callPolicy, "policy-caller").start();
}
}
});

server.stop();
assertTrue(disconnected.await(10, TimeUnit.SECONDS), "client saw the disconnect");
Thread.sleep(OUTAGE_MS);
server.restart();
assertTrue(done.await(30, TimeUnit.SECONDS), "policy call completed");
// Give Curator time to process the events queued behind a blocked EventThread.
Thread.sleep(1000);
assertNotNull(outcome.thread);
return outcome;
} finally {
zk.close();
}
}
}

@SuppressWarnings("unchecked")
private static AtomicReference<ConnectionState> policyState(RetryPolicy policy)
throws ReflectiveOperationException {
Field field = ConnectionAwareRetryPolicy.class.getDeclaredField("connectionState");
field.setAccessible(true);
return (AtomicReference<ConnectionState>) field.get(policy);
}

private static final class Outcome {
private final List<ConnectionState> states;
private volatile String thread;
private volatile boolean allowed;
private volatile long blockedMs;

Outcome(List<ConnectionState> states) {
this.states = states;
}

boolean lost() {
return states.contains(ConnectionState.LOST);
}

@Override
public String toString() {
return "thread=" + thread + " allowRetry=" + allowed + " blockedMs=" + blockedMs
+ " states=" + states;
}
}
}
Loading
Loading