-
Notifications
You must be signed in to change notification settings - Fork 13
[LIZK-3903] Optimizing snapshot creation #125
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
base: branch-3.6
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| package org.apache.zookeeper.server; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
|
|
||
| public class SegmentedList<E> { | ||
|
|
||
| private final int segmentCount; | ||
| private final List<E>[] segments; | ||
| private final ReentrantLock[] locks; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public SegmentedList(int segmentCount) { | ||
| this.segmentCount = segmentCount; | ||
| this.segments = (List<E>[]) new List[segmentCount]; | ||
| this.locks = new ReentrantLock[segmentCount]; | ||
|
|
||
| for (int i = 0; i < segmentCount; i++) { | ||
| segments[i] = new ArrayList<>(); | ||
| locks[i] = new ReentrantLock(); | ||
| } | ||
| } | ||
|
|
||
| private int segmentIndex(Object element) { | ||
| return (element == null ? 0 : (element.hashCode() & 0x7fffffff) % segmentCount); | ||
| } | ||
|
|
||
| public void add(E element) { | ||
| int seg = segmentIndex(element); | ||
| locks[seg].lock(); | ||
| try { | ||
| segments[seg].add(element); | ||
| } finally { | ||
| locks[seg].unlock(); | ||
| } | ||
| } | ||
|
|
||
| public List<E> toListSnapshot() { | ||
| // snapshot copied under all locks to ensure consistency | ||
| List<E> snapshot = new ArrayList<>(); | ||
| for (int i = 0; i < segmentCount; i++) { | ||
| locks[i].lock(); | ||
| } | ||
| try { | ||
| for (List<E> segment : segments) { | ||
| snapshot.addAll(segment); | ||
| } | ||
| } finally { | ||
| for (int i = segmentCount - 1; i >= 0; i--) { | ||
| locks[i].unlock(); | ||
| } | ||
| } | ||
| return snapshot; | ||
| } | ||
|
|
||
| public int size() { | ||
| int size = 0; | ||
| for (int i = 0; i < segmentCount; i++) { | ||
| locks[i].lock(); | ||
| } | ||
| try { | ||
| for (List<E> segment : segments) { | ||
| size += segment.size(); | ||
| } | ||
| } finally { | ||
| for (int i = segmentCount - 1; i >= 0; i--) { | ||
| locks[i].unlock(); | ||
| } | ||
| } | ||
| return size; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -530,17 +530,17 @@ public void loadData() throws IOException, InterruptedException { | |
| } | ||
|
|
||
| // Make a clean snapshot | ||
| takeSnapshot(); | ||
| takeSnapshot(true); | ||
| } | ||
|
|
||
| public void takeSnapshot() { | ||
| takeSnapshot(false); | ||
| public void takeSnapshot(boolean isLeaderBootupSnapshot) { | ||
| takeSnapshot(false, isLeaderBootupSnapshot); | ||
| } | ||
|
|
||
| public void takeSnapshot(boolean syncSnap) { | ||
| public void takeSnapshot(boolean syncSnap, boolean isLeaderBootupSnapshot) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, add a config to determine whether parallel serialization is enabled. In case not it should use regular snapshot mechanism
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, parallel serilazation will be config driven, along with the number of threads. |
||
| long start = Time.currentElapsedTime(); | ||
| try { | ||
| txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap); | ||
| txnLogFactory.save(zkDb.getDataTree(), zkDb.getSessionWithTimeOuts(), syncSnap, isLeaderBootupSnapshot); | ||
| } catch (IOException e) { | ||
| LOG.error("Severe unrecoverable error, exiting", e); | ||
| // This is a severe error that we cannot recover from, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's separate API changes to a separate PR. We can have core snapshot changes in this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to check just the logic change, you can checkout the older commit - d6474f8