-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ZOOKEEPER-4712: Fix partially shutdown of ZooKeeperServer and its processors #2154
Changes from 8 commits
c4e7e42
8d055cc
d82213e
13001bf
5d7aa33
41e6e95
56e959d
c0682d7
3d6f619
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -900,26 +900,26 @@ boolean isRunning() { | |
} | ||
|
||
void closeSocket() { | ||
if (sock != null) { | ||
if (sockBeingClosed.compareAndSet(false, true)) { | ||
if (closeSocketAsync) { | ||
final Thread closingThread = new Thread(() -> closeSockSync(), "CloseSocketThread(sid:" + zk.getServerId()); | ||
closingThread.setDaemon(true); | ||
closingThread.start(); | ||
} else { | ||
closeSockSync(); | ||
} | ||
if (sockBeingClosed.compareAndSet(false, true)) { | ||
if (sock == null) { // Closing before establishing the connection is a noop | ||
return; | ||
} | ||
Comment on lines
+904
to
+906
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Why have you moved the null check inside the lock? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close could be called from different threads, and |
||
Socket socket = sock; | ||
sock = null; | ||
if (closeSocketAsync) { | ||
final Thread closingThread = new Thread(() -> closeSockSync(socket), "CloseSocketThread(sid:" + zk.getServerId()); | ||
closingThread.setDaemon(true); | ||
closingThread.start(); | ||
} else { | ||
closeSockSync(socket); | ||
} | ||
} | ||
} | ||
|
||
void closeSockSync() { | ||
private static void closeSockSync(Socket socket) { | ||
try { | ||
long startTime = Time.currentElapsedTime(); | ||
if (sock != null) { | ||
sock.close(); | ||
sock = null; | ||
} | ||
socket.close(); | ||
ServerMetrics.getMetrics().SOCKET_CLOSING_TIME.add(Time.currentElapsedTime() - startTime); | ||
} catch (IOException e) { | ||
LOG.warn("Ignoring error closing connection to leader", e); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ public void processRequest(Request si) { | |
learner.writePacket(qp, false); | ||
} catch (IOException e) { | ||
LOG.warn("Closing connection to leader, exception during packet send", e); | ||
learner.closeSockSync(); | ||
learner.closeSocket(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on this. This is the leftover of ZOOKEEPER-4409. We should respect |
||
} | ||
} | ||
} | ||
|
@@ -56,7 +56,7 @@ public void flush() throws IOException { | |
learner.writePacket(null, true); | ||
} catch (IOException e) { | ||
LOG.warn("Closing connection to leader, exception during packet send", e); | ||
learner.closeSockSync(); | ||
learner.closeSocket(); | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.zookeeper.server; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import org.apache.zookeeper.ZKTestCase; | ||
import org.apache.zookeeper.server.persistence.FileTxnSnapLog; | ||
import org.apache.zookeeper.server.quorum.Learner; | ||
import org.apache.zookeeper.server.quorum.LearnerZooKeeperServer; | ||
import org.apache.zookeeper.server.quorum.QuorumPeer; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
public class ZooKeeperServerShutdownTest extends ZKTestCase { | ||
|
||
static class ShutdownTrackRequestProcessor implements RequestProcessor { | ||
boolean shutdown = false; | ||
|
||
@Override | ||
public void processRequest(Request request) throws RequestProcessorException { | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
shutdown = true; | ||
} | ||
} | ||
|
||
public static class ShutdownTrackLearnerZooKeeperServer extends LearnerZooKeeperServer { | ||
public ShutdownTrackLearnerZooKeeperServer(FileTxnSnapLog logFactory, QuorumPeer self) throws IOException { | ||
super(logFactory, 2000, 2000, 2000, -1, new ZKDatabase(logFactory), self); | ||
} | ||
|
||
@Override | ||
protected void setupRequestProcessors() { | ||
firstProcessor = new ShutdownTrackRequestProcessor(); | ||
syncProcessor = new SyncRequestProcessor(this, null); | ||
syncProcessor.start(); | ||
} | ||
|
||
ShutdownTrackRequestProcessor getFirstProcessor() { | ||
return (ShutdownTrackRequestProcessor) firstProcessor; | ||
} | ||
|
||
SyncRequestProcessor getSyncRequestProcessor() { | ||
return syncProcessor; | ||
} | ||
|
||
@Override | ||
public Learner getLearner() { | ||
return null; | ||
} | ||
} | ||
|
||
@Test | ||
void testLearnerZooKeeperServerShutdown(@TempDir File tmpDir) throws Exception { | ||
File tmpFile = File.createTempFile("test", ".dir", tmpDir); | ||
tmpFile.delete(); | ||
FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpFile, tmpFile); | ||
ShutdownTrackLearnerZooKeeperServer zooKeeperServer = new ShutdownTrackLearnerZooKeeperServer(logFactory, new QuorumPeer()); | ||
zooKeeperServer.startup(); | ||
zooKeeperServer.shutdown(false); | ||
assertTrue(zooKeeperServer.getFirstProcessor().shutdown); | ||
assertFalse(zooKeeperServer.getSyncRequestProcessor().isAlive()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,7 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable { | |
} | ||
}); | ||
|
||
ZKDatabase database = new ZKDatabase(null); | ||
ZKDatabase database = new ZKDatabase(mock(FileTxnSnapLog.class)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise: NPE during fast-forward during shutdown. |
||
database.setlastProcessedZxid(2L); | ||
QuorumPeer quorumPeer = mock(QuorumPeer.class); | ||
FileTxnSnapLog logfactory = mock(FileTxnSnapLog.class); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw its access to metrics after
unregisterMetrics
. It probably be a good to order it beforeshutdownComponents
, that is assuming it require full functional server components.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The metrics used by the
FileTxnSnapLog
are not "owned" by theZooKeeperServer
, or its children, and not unregistered here, so that shouldn't be a problem.On the other hand, as the parent
ZooKeeperServer
is a dependency of its child classes, I would generally shut down all their components before the parent, and I consider thezkDb
to be owned by the parent in this case, which suggests the current order of shutdown is correct. That said, "shutdown is hard" 😂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is true that running multiple ZooKeeper instances in one JVM probably be hard. It is false that it unregisters metrics during shutdown.
I am ok to keep it unchanged.