-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZOOKEEPER-2623: Fix database corruption caused by quorum check (#1988)
- Loading branch information
Showing
10 changed files
with
136 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
zookeeper-server/src/test/java/org/apache/zookeeper/test/CheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* 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.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import org.apache.zookeeper.KeeperException; | ||
import org.apache.zookeeper.TestableZooKeeper; | ||
import org.apache.zookeeper.ZooDefs; | ||
import org.apache.zookeeper.data.Stat; | ||
import org.apache.zookeeper.proto.CheckVersionRequest; | ||
import org.apache.zookeeper.proto.ReplyHeader; | ||
import org.apache.zookeeper.proto.RequestHeader; | ||
import org.apache.zookeeper.server.quorum.QuorumPeer; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
|
||
public class CheckTest extends ClientBase { | ||
|
||
@BeforeEach | ||
public void setUp(TestInfo testInfo) throws Exception { | ||
if (testInfo.getDisplayName().contains("Cluster")) { | ||
return; | ||
} | ||
super.setUp(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown(TestInfo testInfo) throws Exception { | ||
if (testInfo.getDisplayName().contains("Cluster")) { | ||
return; | ||
} | ||
super.tearDown(); | ||
} | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@Override | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
private static void checkVersion(TestableZooKeeper zk, String path, int version) throws Exception { | ||
RequestHeader header = new RequestHeader(); | ||
header.setType(ZooDefs.OpCode.check); | ||
CheckVersionRequest request = new CheckVersionRequest(path, version); | ||
ReplyHeader replyHeader = zk.submitRequest(header, request, null, null); | ||
if (replyHeader.getErr() != 0) { | ||
throw KeeperException.create(KeeperException.Code.get(replyHeader.getErr()), path); | ||
} | ||
} | ||
|
||
private void testOperations(TestableZooKeeper zk) throws Exception { | ||
Stat stat = new Stat(); | ||
zk.getData("/", false, stat); | ||
checkVersion(zk, "/", -1); | ||
checkVersion(zk, "/", stat.getVersion()); | ||
assertThrows(KeeperException.BadVersionException.class, () -> { | ||
checkVersion(zk, "/", stat.getVersion() + 1); | ||
}); | ||
assertThrows(KeeperException.NoNodeException.class, () -> { | ||
checkVersion(zk, "/no-node", Integer.MAX_VALUE); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testStandalone() throws Exception { | ||
TestableZooKeeper zk = createClient(); | ||
testOperations(zk); | ||
stopServer(); | ||
startServer(); | ||
createClient(); | ||
} | ||
|
||
@Test | ||
public void testCluster() throws Exception { | ||
QuorumBase qb = new QuorumBase(); | ||
try { | ||
qb.setUp(true, true); | ||
testOperations(qb.createClient(new CountdownWatcher(), QuorumPeer.ServerState.OBSERVING)); | ||
testOperations(qb.createClient(new CountdownWatcher(), QuorumPeer.ServerState.FOLLOWING)); | ||
testOperations(qb.createClient(new CountdownWatcher(), QuorumPeer.ServerState.LEADING)); | ||
int leaderIndex = qb.getLeaderIndex(); | ||
int leaderPort = qb.getLeaderClientPort(); | ||
qb.shutdown(qb.getLeaderQuorumPeer()); | ||
qb.setupServer(leaderIndex + 1); | ||
QuorumPeer quorumPeer = qb.getPeerList().get(leaderIndex); | ||
quorumPeer.start(); | ||
qb.createClient("localhost:" + leaderPort, 2 * CONNECTION_TIMEOUT); | ||
} finally { | ||
try { | ||
qb.tearDown(); | ||
} catch (Exception ignored) {} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters