-
Notifications
You must be signed in to change notification settings - Fork 977
BP-47 (task6): Direct I/O entrylogger support #3263
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
Merged
merlimat
merged 15 commits into
apache:master
from
hangc0276:chenhang/directio-part6-direct-entrylogger-support
Jun 23, 2022
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
49dc1e0
Direct I/O entry log support
73aae68
format code
hangc0276 0266494
format code
hangc0276 284d502
fix spotbugs check
hangc0276 4b27a8c
fix jnilib not found
hangc0276 f0ab4fb
fix jnilib not found
hangc0276 ae9e0e4
address comments
hangc0276 65e5bf7
fix so lib not found
hangc0276 46d61af
address comments
hangc0276 e197963
format code
hangc0276 660aee4
add compat test
hangc0276 2f758b7
add compat test
hangc0276 b221412
address comments
hangc0276 8a4d0da
fix findbugs failed
hangc0276 726d359
format code
hangc0276 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/EntryLogIds.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * | ||
| */ | ||
| package org.apache.bookkeeper.bookie.storage; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Generate unique entry log ids. | ||
| */ | ||
| public interface EntryLogIds { | ||
| /** | ||
| * Get the next available entry log ID. | ||
| */ | ||
| int nextId() throws IOException; | ||
| } |
158 changes: 158 additions & 0 deletions
158
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/EntryLogIdsImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /** | ||
| * | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * | ||
| */ | ||
| package org.apache.bookkeeper.bookie.storage; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import org.apache.bookkeeper.bookie.LedgerDirsManager; | ||
| import org.apache.bookkeeper.bookie.storage.directentrylogger.Events; | ||
| import org.apache.bookkeeper.slogger.Slogger; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
|
|
||
| /** | ||
| * EntryLogIdsImpl. | ||
| */ | ||
| public class EntryLogIdsImpl implements EntryLogIds { | ||
| public static final Pattern FILE_PATTERN = Pattern.compile("^([0-9a-fA-F]+)\\.log$"); | ||
| public static final Pattern COMPACTED_FILE_PATTERN = | ||
| Pattern.compile("^([0-9a-fA-F]+)\\.log\\.([0-9a-fA-F]+)\\.compacted$"); | ||
|
|
||
| private final LedgerDirsManager ledgerDirsManager; | ||
| private final Slogger slog; | ||
| private int nextId; | ||
| private int maxId; | ||
|
|
||
| public EntryLogIdsImpl(LedgerDirsManager ledgerDirsManager, | ||
| Slogger slog) throws IOException { | ||
| this.ledgerDirsManager = ledgerDirsManager; | ||
| this.slog = slog; | ||
| findLargestGap(); | ||
| } | ||
|
|
||
| @Override | ||
| public int nextId() throws IOException { | ||
| while (true) { | ||
| synchronized (this) { | ||
| int current = nextId; | ||
| nextId++; | ||
| if (nextId == maxId) { | ||
| findLargestGap(); | ||
| } else { | ||
| return current; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void findLargestGap() throws IOException { | ||
| long start = System.nanoTime(); | ||
| List<Integer> currentIds = new ArrayList<Integer>(); | ||
|
|
||
| for (File ledgerDir : ledgerDirsManager.getAllLedgerDirs()) { | ||
| currentIds.addAll(logIdsInDirectory(ledgerDir)); | ||
| currentIds.addAll(compactedLogIdsInDirectory(ledgerDir)); | ||
| } | ||
|
|
||
| Pair<Integer, Integer> gap = findLargestGap(currentIds); | ||
| nextId = gap.getLeft(); | ||
| maxId = gap.getRight(); | ||
| slog.kv("dirs", ledgerDirsManager.getAllLedgerDirs()) | ||
| .kv("nextId", nextId) | ||
| .kv("maxId", maxId) | ||
| .kv("durationMs", TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)) | ||
| .info(Events.ENTRYLOG_IDS_CANDIDATES_SELECTED); | ||
| } | ||
|
|
||
| /** | ||
| * O(nlogn) algorithm to find largest contiguous gap between | ||
| * integers in a passed list. n should be relatively small. | ||
| * Entry logs should be about 1GB in size, so even if the node | ||
|
Contributor
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. not guaranteed in case of entry log per ledger.
Contributor
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. Yes, it can only be guaranteed by |
||
| * stores a PB, there should be only 1000000 entry logs. | ||
| */ | ||
| static Pair<Integer, Integer> findLargestGap(List<Integer> currentIds) { | ||
| if (currentIds.isEmpty()) { | ||
| return Pair.of(0, Integer.MAX_VALUE); | ||
| } | ||
|
|
||
| Collections.sort(currentIds); | ||
|
|
||
| int nextIdCandidate = 0; | ||
| int maxIdCandidate = currentIds.get(0); | ||
| int maxGap = maxIdCandidate - nextIdCandidate; | ||
| for (int i = 0; i < currentIds.size(); i++) { | ||
| int gapStart = currentIds.get(i) + 1; | ||
| int j = i + 1; | ||
| int gapEnd = Integer.MAX_VALUE; | ||
| if (j < currentIds.size()) { | ||
| gapEnd = currentIds.get(j); | ||
| } | ||
| int gapSize = gapEnd - gapStart; | ||
| if (gapSize > maxGap) { | ||
| maxGap = gapSize; | ||
| nextIdCandidate = gapStart; | ||
| maxIdCandidate = gapEnd; | ||
| } | ||
| } | ||
| return Pair.of(nextIdCandidate, maxIdCandidate); | ||
| } | ||
|
|
||
| public static List<Integer> logIdsInDirectory(File directory) { | ||
| List<Integer> ids = new ArrayList<>(); | ||
| if (directory.exists() && directory.isDirectory()) { | ||
| File[] files = directory.listFiles(); | ||
| if (files != null && files.length > 0) { | ||
| for (File f : files) { | ||
| Matcher m = FILE_PATTERN.matcher(f.getName()); | ||
| if (m.matches()) { | ||
| int logId = Integer.parseUnsignedInt(m.group(1), 16); | ||
| ids.add(logId); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return ids; | ||
| } | ||
|
|
||
| private static List<Integer> compactedLogIdsInDirectory(File directory) { | ||
| List<Integer> ids = new ArrayList<>(); | ||
| if (directory.exists() && directory.isDirectory()) { | ||
| File[] files = directory.listFiles(); | ||
| if (files != null && files.length > 0) { | ||
| for (File f : files) { | ||
| Matcher m = COMPACTED_FILE_PATTERN.matcher(f.getName()); | ||
| if (m.matches()) { | ||
| int logId = Integer.parseUnsignedInt(m.group(1), 16); | ||
| ids.add(logId); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return ids; | ||
|
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why are we adding this check ?
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 there are not ledgers in ledgerMap, we just skip the following reading.
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.
so this case was not possible before this change ?
btw, it does no harm.
thanks for clarification