Skip to content

Commit d2e6c21

Browse files
li4wangkezhuw
authored andcommitted
ZOOKEEPER-4858: Remove the lock contention between snapshotting and the sync operation
Reviewers: anmolnar, kezhuw Author: li4wang Closes #2185 from li4wang/ZOOKEEPER-4858 (cherry picked from commit c0e9241) Signed-off-by: Kezhu Wang <[email protected]>
1 parent 15423a4 commit d2e6c21

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
135135
public static final String CLOSE_SESSION_TXN_ENABLED = "zookeeper.closeSessionTxn.enabled";
136136
private static boolean closeSessionTxnEnabled = true;
137137
private volatile CountDownLatch restoreLatch;
138+
// exclusive lock for taking snapshot and restore
139+
private final Object snapshotAndRestoreLock = new Object();
138140

139141
static {
140142
LOG = LoggerFactory.getLogger(ZooKeeperServer.class);
@@ -565,11 +567,13 @@ public File takeSnapshot(boolean syncSnap) throws IOException {
565567
* @return file snapshot file object
566568
* @throws IOException
567569
*/
568-
public synchronized File takeSnapshot(boolean syncSnap, boolean isSevere) throws IOException {
570+
public File takeSnapshot(boolean syncSnap, boolean isSevere) throws IOException {
569571
long start = Time.currentElapsedTime();
570572
File snapFile = null;
571573
try {
572-
snapFile = txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap);
574+
synchronized (snapshotAndRestoreLock) {
575+
snapFile = txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap);
576+
}
573577
} catch (IOException e) {
574578
if (isSevere) {
575579
LOG.error("Severe unrecoverable error, exiting", e);
@@ -593,7 +597,7 @@ public synchronized File takeSnapshot(boolean syncSnap, boolean isSevere) throws
593597
* @Return last processed zxid
594598
* @throws IOException
595599
*/
596-
public synchronized long restoreFromSnapshot(final InputStream inputStream) throws IOException {
600+
public long restoreFromSnapshot(final InputStream inputStream) throws IOException {
597601
if (inputStream == null) {
598602
throw new IllegalArgumentException("InputStream can not be null when restoring from snapshot");
599603
}
@@ -618,9 +622,10 @@ public synchronized long restoreFromSnapshot(final InputStream inputStream) thro
618622
restoreLatch = new CountDownLatch(1);
619623

620624
try {
621-
// set to the new zkDatabase
622-
setZKDatabase(newZKDatabase);
623-
625+
synchronized (snapshotAndRestoreLock) {
626+
// set to the new zkDatabase
627+
setZKDatabase(newZKDatabase);
628+
}
624629
// re-create SessionTrack
625630
createSessionTracker();
626631
} finally {

0 commit comments

Comments
 (0)