Skip to content

Commit

Permalink
ZOOKEEPER-4858: Remove the lock contention between snapshotting and t…
Browse files Browse the repository at this point in the history
…he 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]>
  • Loading branch information
li4wang authored and kezhuw committed Dec 21, 2024
1 parent 15423a4 commit d2e6c21
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
public static final String CLOSE_SESSION_TXN_ENABLED = "zookeeper.closeSessionTxn.enabled";
private static boolean closeSessionTxnEnabled = true;
private volatile CountDownLatch restoreLatch;
// exclusive lock for taking snapshot and restore
private final Object snapshotAndRestoreLock = new Object();

static {
LOG = LoggerFactory.getLogger(ZooKeeperServer.class);
Expand Down Expand Up @@ -565,11 +567,13 @@ public File takeSnapshot(boolean syncSnap) throws IOException {
* @return file snapshot file object
* @throws IOException
*/
public synchronized File takeSnapshot(boolean syncSnap, boolean isSevere) throws IOException {
public File takeSnapshot(boolean syncSnap, boolean isSevere) throws IOException {
long start = Time.currentElapsedTime();
File snapFile = null;
try {
snapFile = txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap);
synchronized (snapshotAndRestoreLock) {
snapFile = txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap);
}
} catch (IOException e) {
if (isSevere) {
LOG.error("Severe unrecoverable error, exiting", e);
Expand All @@ -593,7 +597,7 @@ public synchronized File takeSnapshot(boolean syncSnap, boolean isSevere) throws
* @Return last processed zxid
* @throws IOException
*/
public synchronized long restoreFromSnapshot(final InputStream inputStream) throws IOException {
public long restoreFromSnapshot(final InputStream inputStream) throws IOException {
if (inputStream == null) {
throw new IllegalArgumentException("InputStream can not be null when restoring from snapshot");
}
Expand All @@ -618,9 +622,10 @@ public synchronized long restoreFromSnapshot(final InputStream inputStream) thro
restoreLatch = new CountDownLatch(1);

try {
// set to the new zkDatabase
setZKDatabase(newZKDatabase);

synchronized (snapshotAndRestoreLock) {
// set to the new zkDatabase
setZKDatabase(newZKDatabase);
}
// re-create SessionTrack
createSessionTracker();
} finally {
Expand Down

0 comments on commit d2e6c21

Please sign in to comment.