From 122f88ebf2a4328f1648acc2895a4f525233eceb Mon Sep 17 00:00:00 2001 From: Rishabh Rai Date: Mon, 30 Oct 2023 17:40:22 -0500 Subject: [PATCH] ZOOKEEPER-4766: Avoid unnecessary snapshots during leader election --- .../apache/zookeeper/server/ZooKeeperServer.java | 14 ++++++++++++-- .../org/apache/zookeeper/server/quorum/Leader.java | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java index 0725c388152..b30b65dd281 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java @@ -504,9 +504,17 @@ public void setZKDatabase(ZKDatabase zkDb) { } /** - * Restore sessions and data + * Restore sessions and data, takes a snapshot by default */ public void loadData() throws IOException, InterruptedException { + loadData(true); + } + + /** + * Restore sessions and data + * @param needSnapshot if true, take a snapshot after loading data + */ + public void loadData(boolean needSnapshot) throws IOException, InterruptedException { /* * When a new leader starts executing Leader#lead, it * invokes this method. The database, however, has been @@ -537,7 +545,9 @@ public void loadData() throws IOException, InterruptedException { .forEach(session -> killSession(session, zkDb.getDataTreeLastProcessedZxid())); // Make a clean snapshot - takeSnapshot(); + if (needSnapshot) { + takeSnapshot(); + } } public void takeSnapshot() { diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java index 8cb981237cf..384ae44545d 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Leader.java @@ -598,7 +598,7 @@ void lead() throws IOException, InterruptedException { try { self.setZabState(QuorumPeer.ZabState.DISCOVERY); self.tick.set(0); - zk.loadData(); + zk.loadData(false); leaderStateSummary = new StateSummary(self.getCurrentEpoch(), zk.getLastProcessedZxid());