-
Notifications
You must be signed in to change notification settings - Fork 58
Add broker disk space cost #1604
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
qoo332001
merged 18 commits into
opensource4you:main
from
qoo332001:addBrokerDiskSpaceCost
May 17, 2023
Merged
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6880f31
add new MoveCost to limit broker log size
qoo332001 64a52f6
update docs
qoo332001 b497ad6
Merge branch 'main' of https://github.com/skiptests/astraea into addB…
qoo332001 1e2edfc
fix issues
qoo332001 cd80a9f
fix issue
qoo332001 83f1f39
Merge branch 'main' of https://github.com/skiptests/astraea into addB…
qoo332001 7b9f3da
fix issue
qoo332001 27ba2ef
fix issue
qoo332001 e3dee28
Merge branch 'main' of https://github.com/skiptests/astraea into addB…
qoo332001 337c7df
update docs
qoo332001 54aa64d
Merge branch 'main' of https://github.com/skiptests/astraea into addB…
qoo332001 636feea
fix issues
qoo332001 277e7da
fix conflict
qoo332001 ff53cd8
fix issues
qoo332001 eed85ef
fix conflict
qoo332001 5b6dd26
fix issues
qoo332001 c2059df
update docs
qoo332001 58cb23c
fix issue
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
124 changes: 124 additions & 0 deletions
124
common/src/main/java/org/astraea/common/cost/BrokerDiskSpaceCost.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,124 @@ | ||
| /* | ||
| * 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.astraea.common.cost; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
| import org.astraea.common.Configuration; | ||
| import org.astraea.common.DataSize; | ||
| import org.astraea.common.admin.ClusterInfo; | ||
| import org.astraea.common.metrics.ClusterBean; | ||
|
|
||
| public class BrokerDiskSpaceCost implements HasMoveCost { | ||
|
|
||
| public static final String BROKER_COST_LIMIT_KEY = "max.broker.total.disk.space"; | ||
| public static final String BROKER_PATH_COST_LIMIT_KEY = "max.broker.path.disk.space"; | ||
| private final Map<Integer, DataSize> brokerMoveCostLimit; | ||
| private final Map<BrokerPath, DataSize> diskMoveCostLimit; | ||
|
|
||
| public BrokerDiskSpaceCost(Configuration configuration) { | ||
| this.diskMoveCostLimit = diskMoveCostLimit(configuration); | ||
| this.brokerMoveCostLimit = brokerMoveCostLimit(configuration); | ||
| } | ||
|
|
||
| @Override | ||
| public MoveCost moveCost(ClusterInfo before, ClusterInfo after, ClusterBean clusterBean) { | ||
| if (CostUtils.brokerDiskUsageSizeOverflow(before, after, brokerMoveCostLimit)) | ||
| return () -> true; | ||
| if (CostUtils.brokerPathDiskUsageSizeOverflow(before, after, diskMoveCostLimit)) | ||
| return () -> true; | ||
| return () -> false; | ||
| } | ||
|
|
||
| private Map<BrokerPath, DataSize> diskMoveCostLimit(Configuration configuration) { | ||
| return configuration | ||
| .string(BROKER_PATH_COST_LIMIT_KEY) | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
| .map( | ||
| s -> | ||
| Arrays.stream(s.split(",")) | ||
| .map( | ||
| idAndPath -> { | ||
| var brokerPathAndLimit = idAndPath.split(":"); | ||
| var brokerPath = brokerPathAndLimit[0].split("-"); | ||
| return Map.entry( | ||
| BrokerPath.of( | ||
| Integer.parseInt(brokerPath[0]), | ||
| IntStream.range(1, brokerPath.length) | ||
| .boxed() | ||
| .map(x -> brokerPath[x]) | ||
| .collect(Collectors.joining("-"))), | ||
| DataSize.of(brokerPathAndLimit[1])); | ||
| }) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) | ||
| .orElse(Map.of()); | ||
| } | ||
|
|
||
| private Map<Integer, DataSize> brokerMoveCostLimit(Configuration configuration) { | ||
| return configuration | ||
| .string(BROKER_COST_LIMIT_KEY) | ||
| .map( | ||
| s -> | ||
| Arrays.stream(s.split(",")) | ||
| .map( | ||
| idAndPath -> { | ||
| var brokerAndLimit = idAndPath.split(":"); | ||
| return Map.entry( | ||
| Integer.parseInt(brokerAndLimit[0]), DataSize.of(brokerAndLimit[1])); | ||
| }) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) | ||
| .orElse(Map.of()); | ||
| } | ||
|
|
||
| static class BrokerPath { | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
|
|
||
| private final int broker; | ||
| private final String path; | ||
|
|
||
| public static BrokerPath of(int broker, String path) { | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
| return new BrokerPath(broker, path); | ||
| } | ||
|
|
||
| public BrokerPath(int broker, String path) { | ||
| this.broker = broker; | ||
| this.path = path; | ||
| } | ||
|
|
||
| public int broker() { | ||
| return broker; | ||
| } | ||
|
|
||
| public String path() { | ||
| return path; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| BrokerPath that = (BrokerPath) o; | ||
| return broker == that.broker && Objects.equals(path, that.path); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(broker, path); | ||
| } | ||
| } | ||
| } | ||
121 changes: 121 additions & 0 deletions
121
common/src/main/java/org/astraea/common/cost/CostUtils.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,121 @@ | ||
| /* | ||
| * 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.astraea.common.cost; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.function.Predicate; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.astraea.common.DataSize; | ||
| import org.astraea.common.admin.ClusterInfo; | ||
| import org.astraea.common.admin.NodeInfo; | ||
| import org.astraea.common.admin.Replica; | ||
|
|
||
| final class CostUtils { | ||
|
|
||
| private CostUtils() {} | ||
|
|
||
| static boolean brokerDiskUsageSizeOverflow( | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
| ClusterInfo before, ClusterInfo after, Map<Integer, DataSize> brokerMoveCostLimit) { | ||
| for (var id : | ||
| Stream.concat(before.nodes().stream(), after.nodes().stream()) | ||
| .map(NodeInfo::id) | ||
| .parallel() | ||
| .collect(Collectors.toSet())) { | ||
|
|
||
| var beforeSize = (Long) before.replicaStream(id).map(Replica::size).mapToLong(y -> y).sum(); | ||
| var addedSize = | ||
| (Long) | ||
| after | ||
| .replicaStream(id) | ||
| .filter(r -> before.replicaStream(id).noneMatch(r::equals)) | ||
| .map(Replica::size) | ||
| .mapToLong(y -> y) | ||
| .sum(); | ||
| if ((beforeSize + addedSize) | ||
| > brokerMoveCostLimit.getOrDefault(id, DataSize.Byte.of(Long.MAX_VALUE)).bytes()) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static boolean brokerPathDiskUsageSizeOverflow( | ||
| ClusterInfo before, | ||
| ClusterInfo after, | ||
| Map<BrokerDiskSpaceCost.BrokerPath, DataSize> diskMoveCostLimit) { | ||
| for (var brokerPaths : | ||
| Stream.concat( | ||
| before.brokerFolders().entrySet().stream(), | ||
| after.brokerFolders().entrySet().stream()) | ||
| .collect(Collectors.toSet())) { | ||
| for (var path : brokerPaths.getValue()) { | ||
| var brokerPath = BrokerDiskSpaceCost.BrokerPath.of(brokerPaths.getKey(), path); | ||
| var beforeSize = | ||
| before | ||
| .replicaStream(brokerPaths.getKey()) | ||
| .filter(r -> r.path().equals(path)) | ||
| .mapToLong(Replica::size) | ||
| .sum(); | ||
| var addedSize = | ||
| (Long) | ||
| after | ||
| .replicaStream(brokerPaths.getKey()) | ||
| .filter(r -> before.replicaStream(brokerPaths.getKey()).noneMatch(r::equals)) | ||
| .map(Replica::size) | ||
| .mapToLong(y -> y) | ||
| .sum(); | ||
| if ((beforeSize + addedSize) | ||
| > diskMoveCostLimit.getOrDefault(brokerPath, DataSize.Byte.of(Long.MAX_VALUE)).bytes()) | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static boolean changedRecordSizeOverflow( | ||
| ClusterInfo before, ClusterInfo after, Predicate<Replica> predicate, long limit) { | ||
| var totalRemovedSize = 0L; | ||
| var totalAddedSize = 0L; | ||
| for (var id : | ||
| Stream.concat(before.nodes().stream(), after.nodes().stream()) | ||
| .map(NodeInfo::id) | ||
| .parallel() | ||
| .collect(Collectors.toSet())) { | ||
| var removed = | ||
| (int) | ||
| before | ||
| .replicaStream(id) | ||
| .filter(predicate) | ||
| .filter(r -> !after.replicas(r.topicPartition()).contains(r)) | ||
| .mapToLong(Replica::size) | ||
| .sum(); | ||
| var added = | ||
| (int) | ||
| after | ||
| .replicaStream(id) | ||
| .filter(predicate) | ||
| .filter(r -> !before.replicas(r.topicPartition()).contains(r)) | ||
| .mapToLong(Replica::size) | ||
| .sum(); | ||
| totalRemovedSize = totalRemovedSize + removed; | ||
| totalAddedSize = totalAddedSize + added; | ||
| // if migrate cost overflow, leave early and return true | ||
| if (totalRemovedSize > limit || totalAddedSize > limit) return true; | ||
| } | ||
| return Math.max(totalRemovedSize, totalAddedSize) > limit; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.