Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZOOKEEPER-4766: Avoid unnecessary snapshots during leader election #2153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,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
Expand Down Expand Up @@ -545,8 +553,16 @@ public void loadData() throws IOException, InterruptedException {
.filter(session -> zkDb.getSessionWithTimeOuts().get(session) == null)
.forEach(session -> killSession(session, zkDb.getDataTreeLastProcessedZxid()));

// Make a clean snapshot
takeSnapshot();
// Make a clean snapshot if needed
/* A snapshot is not needed when loading data during leader election. A new snapshot is not needed to return to
* the current state because the current state was reached by loading the data tree using an old snapshot.
* During leader election, there are no ongoing transactions that could be lost either. If a snapshot is needed
* to send to a learner during leader election, that snapshot will be taken as part of leader election, so
* snapshotting does not need to be done here as well.
*/
Comment on lines +557 to +562
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should answer your question, right @horizonzy ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snapFile = txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap);

zookeeper's snapshot not only persists the dataTree but also persists the session and session expiration time, I'm concerned that if we don't take a snapshot here, the session data may not be accurate during the next restart.

@eolivelli /cc

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the info @horizonzy , is this session info used for something upon restart that is necessary for correctness? Is it mainly important for telemetry? If it is not needed for correctness could skipping this snapshotting step during leader election be made a configurable option?

if (needSnapshot) {
takeSnapshot();
}
}

public File takeSnapshot() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,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());

Expand Down