nick-boss-tech commented on code in PR #5301:
URL: https://github.com/apache/solr/pull/5301#discussion_r4636514094


##########
solr/core/src/test/org/apache/solr/cloud/TestConnectionManagerReconnectStress.java:
##########
@@ -0,0 +1,227 @@
+/*
+ * 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.solr.cloud;
+
+import java.net.URI;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.config.Property;
+import org.apache.lucene.tests.util.LuceneTestCase.Nightly;
+import org.apache.solr.SolrTestCase;
+import org.apache.solr.client.solrj.cloud.SocketProxy;
+import org.apache.solr.common.cloud.ConnectionManager;
+import org.apache.solr.common.cloud.SolrZkClient;
+import org.apache.solr.common.util.SuppressForbidden;
+import org.apache.solr.common.util.TimeSource;
+import org.apache.solr.util.TimeOut;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Drops the connection between a real {@link SolrZkClient} and ZooKeeper over 
and over, while
+ * slowing down the processing of {@code Disconnected} events the way a 
stalled callback thread
+ * would. {@link ConnectionManager} receives those events on an unbounded 
pool, so a slow {@code
+ * Disconnected} can be applied after the {@code SyncConnected} that followed 
it. The manager must
+ * still end up connected whenever ZooKeeper is. Every few disconnects the 
session is expired
+ * instead, which must also be recovered from.
+ */
+@Nightly
+@SuppressForbidden(reason = "needs log4j2 classes to delay a logging call")
+public class TestConnectionManagerReconnectStress extends SolrTestCase {
+
+  private static final int FLAPS = 25;
+  private static final int EXPIRE_EVERY = 5;
+  private static final int MAX_DISCONNECT_CALLBACK_DELAY_MS = 1500;
+  private static final int SETTLE_SECONDS = 10;
+  private static final int DRAIN_SECONDS = 30;
+
+  private ZkTestServer zkServer;
+  private SocketProxy proxy;
+  private SlowDisconnectAppender slowDisconnects;
+  private LoggerConfig loggerConfig;
+
+  /** Delays the log call that precedes the state change made for a {@code 
Disconnected} event. */
+  @SuppressForbidden(reason = "needs log4j2 classes to delay a logging call")
+  private static class SlowDisconnectAppender extends AbstractAppender {
+    final AtomicInteger delayed = new AtomicInteger();
+    final AtomicInteger inFlight = new AtomicInteger();
+    final AtomicInteger processed = new AtomicInteger();
+
+    SlowDisconnectAppender() {
+      super("slowDisconnects", null, null, true, Property.EMPTY_ARRAY);
+    }
+
+    @Override
+    public void append(LogEvent event) {
+      String message = event.getMessage().getFormattedMessage();
+      // Both messages follow the state update or rejection, unlike the 
initial warning below.
+      if (message.equals("zkClient has disconnected")
+          || message.startsWith("Ignoring stale disconnected event ")) {
+        processed.incrementAndGet();
+        return;
+      }
+      if (!message.startsWith("Watcher ") || 
!message.contains("state:Disconnected")) {
+        return;
+      }
+      delayed.incrementAndGet();
+      inFlight.incrementAndGet();
+      try {
+        Thread.sleep(random().nextInt(MAX_DISCONNECT_CALLBACK_DELAY_MS));
+      } catch (InterruptedException e) {
+        Thread.currentThread().interrupt();
+      } finally {
+        inFlight.decrementAndGet();
+      }
+    }
+  }
+
+  @Before
+  public void setUpServers() throws Exception {
+    zkServer = new ZkTestServer(createTempDir("zkData"));
+    zkServer.run();
+    proxy = new SocketProxy();
+    proxy.open(new URI("http://127.0.0.1:"; + zkServer.getPort()));
+
+    LoggerContext context = (LoggerContext) LogManager.getContext(false);
+    Configuration config = context.getConfiguration();
+    String loggerName = ConnectionManager.class.getName();
+    loggerConfig = new LoggerConfig(loggerName, Level.WARN, true);
+    slowDisconnects = new SlowDisconnectAppender();
+    slowDisconnects.start();
+    loggerConfig.addAppender(slowDisconnects, Level.WARN, null);

Review Comment:
   🤖 *AI text below* 🤖
   
   Could both the logger and appender be set to `INFO` here? The appender 
increments `delayed` from the initial `WARN`, but a correctly rejected 
disconnect now logs `Ignoring stale disconnected event` at `INFO`. That message 
never reaches the appender, so `processed` stays short and the drain can time 
out even when the sequence guard works.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to