-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-8913. ContainerManagerImpl: reduce processing while locked #4967
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -19,19 +19,18 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NavigableSet; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Random; | ||
| import java.util.Set; | ||
| import java.util.concurrent.TimeoutException; | ||
| import java.util.concurrent.locks.Lock; | ||
| import java.util.concurrent.locks.ReentrantLock; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Preconditions; | ||
|
|
@@ -144,55 +143,23 @@ public ContainerInfo getContainer(final ContainerID id) | |
| public List<ContainerInfo> getContainers(final ContainerID startID, | ||
| final int count) { | ||
| scmContainerManagerMetrics.incNumListContainersOps(); | ||
| final List<ContainerID> containersIds = | ||
| new ArrayList<>(containerStateManager.getContainerIDs()); | ||
| Collections.sort(containersIds); | ||
| List<ContainerInfo> containers; | ||
| lock.lock(); | ||
| try { | ||
| containers = containersIds.stream() | ||
| .filter(id -> id.compareTo(startID) >= 0).limit(count) | ||
| .map(containerStateManager::getContainer) | ||
| .collect(Collectors.toList()); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| return containers; | ||
| return toContainers(filterSortAndLimit(startID, count, | ||
| containerStateManager.getContainerIDs())); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ContainerInfo> getContainers(final LifeCycleState state) { | ||
| List<ContainerInfo> containers; | ||
| lock.lock(); | ||
| try { | ||
| containers = containerStateManager.getContainerIDs(state).stream() | ||
| .map(containerStateManager::getContainer) | ||
| .filter(Objects::nonNull).collect(Collectors.toList()); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| return containers; | ||
| scmContainerManagerMetrics.incNumListContainersOps(); | ||
| return toContainers(containerStateManager.getContainerIDs(state)); | ||
| } | ||
|
|
||
| @Override | ||
| public List<ContainerInfo> getContainers(final ContainerID startID, | ||
| final int count, | ||
| final LifeCycleState state) { | ||
| scmContainerManagerMetrics.incNumListContainersOps(); | ||
| final List<ContainerID> containersIds = | ||
| new ArrayList<>(containerStateManager.getContainerIDs(state)); | ||
| Collections.sort(containersIds); | ||
| List<ContainerInfo> containers; | ||
| lock.lock(); | ||
| try { | ||
| containers = containersIds.stream() | ||
| .filter(id -> id.compareTo(startID) >= 0).limit(count) | ||
| .map(containerStateManager::getContainer) | ||
| .collect(Collectors.toList()); | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| return containers; | ||
| return toContainers(filterSortAndLimit(startID, count, | ||
| containerStateManager.getContainerIDs(state))); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -431,18 +398,24 @@ public void notifyContainerReportProcessing(final boolean isFullReport, | |
| public void deleteContainer(final ContainerID cid) | ||
| throws IOException, TimeoutException { | ||
| HddsProtos.ContainerID protoId = cid.getProtobuf(); | ||
|
|
||
| final boolean found; | ||
| lock.lock(); | ||
| try { | ||
| if (containerExist(cid)) { | ||
| found = containerExist(cid); | ||
| if (found) { | ||
| containerStateManager.removeContainer(protoId); | ||
| scmContainerManagerMetrics.incNumSuccessfulDeleteContainers(); | ||
| } else { | ||
| scmContainerManagerMetrics.incNumFailureDeleteContainers(); | ||
| throwContainerNotFoundException(cid); | ||
| } | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
|
|
||
| if (found) { | ||
| scmContainerManagerMetrics.incNumSuccessfulDeleteContainers(); | ||
| } else { | ||
| scmContainerManagerMetrics.incNumFailureDeleteContainers(); | ||
| throwContainerNotFoundException(cid); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -471,4 +444,37 @@ public ContainerStateManager getContainerStateManager() { | |
| public SCMHAManager getSCMHAManager() { | ||
| return haManager; | ||
| } | ||
|
|
||
| private static List<ContainerID> filterSortAndLimit( | ||
|
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. I think this could be made more efficient. The current algorithm takes the set, forms a new list of all elements. Then filters, sorted and returns the range. Using an adaption of the priorityQueue technique here - https://www.baeldung.com/java-array-top-elements We could avoid the copy from set to list, and the potentially large sort. I think this would be more memory efficient on large container sets, and should perform better than the full sort
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. Thanks for the suggestion, I'll update the patch with it. FYI there is a bug in that solution due to
so It seems to work for (small?)
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. I wonder how many people have not noticed that issue, and just implemented the linked solution as is! |
||
| ContainerID startID, int count, Set<ContainerID> set) { | ||
|
|
||
| List<ContainerID> ids = new ArrayList<>(set); | ||
|
|
||
| // only filter if startID is not the minimum one | ||
| if (!startID.equals(ContainerID.MIN)) { | ||
| ids.removeIf(id -> id.compareTo(startID) < 0); | ||
| } | ||
|
|
||
| // sort after removing unnecessary items | ||
| Collections.sort(ids); | ||
|
|
||
| // limit | ||
| return ids.subList(0, Math.min(ids.size(), count)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a list of all containers identified by {@code ids}. | ||
| */ | ||
| private List<ContainerInfo> toContainers(Collection<ContainerID> ids) { | ||
| List<ContainerInfo> containers = new ArrayList<>(ids.size()); | ||
|
|
||
| for (ContainerID id : ids) { | ||
| ContainerInfo container = containerStateManager.getContainer(id); | ||
| if (container != null) { | ||
| containers.add(container); | ||
| } | ||
| } | ||
|
|
||
| return containers; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.