@@ -135,6 +135,8 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
135
135
public static final String CLOSE_SESSION_TXN_ENABLED = "zookeeper.closeSessionTxn.enabled" ;
136
136
private static boolean closeSessionTxnEnabled = true ;
137
137
private volatile CountDownLatch restoreLatch ;
138
+ // exclusive lock for taking snapshot and restore
139
+ private final Object snapshotAndRestoreLock = new Object ();
138
140
139
141
static {
140
142
LOG = LoggerFactory .getLogger (ZooKeeperServer .class );
@@ -565,11 +567,13 @@ public File takeSnapshot(boolean syncSnap) throws IOException {
565
567
* @return file snapshot file object
566
568
* @throws IOException
567
569
*/
568
- public synchronized File takeSnapshot (boolean syncSnap , boolean isSevere ) throws IOException {
570
+ public File takeSnapshot (boolean syncSnap , boolean isSevere ) throws IOException {
569
571
long start = Time .currentElapsedTime ();
570
572
File snapFile = null ;
571
573
try {
572
- snapFile = txnLogFactory .save (zkDb .getDataTree (), zkDb .getSessionWithTimeOuts (), syncSnap );
574
+ synchronized (snapshotAndRestoreLock ) {
575
+ snapFile = txnLogFactory .save (zkDb .getDataTree (), zkDb .getSessionWithTimeOuts (), syncSnap );
576
+ }
573
577
} catch (IOException e ) {
574
578
if (isSevere ) {
575
579
LOG .error ("Severe unrecoverable error, exiting" , e );
@@ -593,7 +597,7 @@ public synchronized File takeSnapshot(boolean syncSnap, boolean isSevere) throws
593
597
* @Return last processed zxid
594
598
* @throws IOException
595
599
*/
596
- public synchronized long restoreFromSnapshot (final InputStream inputStream ) throws IOException {
600
+ public long restoreFromSnapshot (final InputStream inputStream ) throws IOException {
597
601
if (inputStream == null ) {
598
602
throw new IllegalArgumentException ("InputStream can not be null when restoring from snapshot" );
599
603
}
@@ -618,9 +622,10 @@ public synchronized long restoreFromSnapshot(final InputStream inputStream) thro
618
622
restoreLatch = new CountDownLatch (1 );
619
623
620
624
try {
621
- // set to the new zkDatabase
622
- setZKDatabase (newZKDatabase );
623
-
625
+ synchronized (snapshotAndRestoreLock ) {
626
+ // set to the new zkDatabase
627
+ setZKDatabase (newZKDatabase );
628
+ }
624
629
// re-create SessionTrack
625
630
createSessionTracker ();
626
631
} finally {
0 commit comments