-
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 2 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
60 changes: 60 additions & 0 deletions
60
common/src/main/java/org/astraea/common/admin/BrokerPath.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,60 @@ | ||
| /* | ||
| * 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.admin; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| public class BrokerPath { | ||
|
|
||
| private final int broker; | ||
| private final String path; | ||
|
|
||
| public static BrokerPath of(int broker, String path) { | ||
| 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) { | ||
| 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); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BrokerPath{" + "broker=" + broker + ", path='" + path + '\'' + '}'; | ||
| } | ||
| } | ||
151 changes: 151 additions & 0 deletions
151
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,151 @@ | ||
| /* | ||
| * 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.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
| import org.astraea.common.Configuration; | ||
| import org.astraea.common.DataSize; | ||
| import org.astraea.common.admin.BrokerPath; | ||
| import org.astraea.common.admin.ClusterBean; | ||
| import org.astraea.common.admin.ClusterInfo; | ||
| import org.astraea.common.admin.NodeInfo; | ||
| import org.astraea.common.admin.Replica; | ||
|
|
||
| public class BrokerDiskSpaceCost implements HasMoveCost { | ||
|
|
||
| public static final String BROKER_COST_LIMIT_KEY = "max.broker.disk.space"; | ||
| public static final String DISK_COST_LIMIT_KEY = "max.disk.space"; | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
| private final Configuration configuration; | ||
|
|
||
| public BrokerDiskSpaceCost(Configuration configuration) { | ||
| this.configuration = configuration; | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @Override | ||
| public MoveCost moveCost(ClusterInfo before, ClusterInfo after, ClusterBean clusterBean) { | ||
| var brokerCostLimit = brokerMoveCostLimit(); | ||
| var moveCost = | ||
| Stream.concat(before.nodes().stream(), after.nodes().stream()) | ||
| .map(NodeInfo::id) | ||
| .distinct() | ||
| .parallel() | ||
| .collect( | ||
| Collectors.toUnmodifiableMap( | ||
| Function.identity(), | ||
| id -> { | ||
| 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(); | ||
| return beforeSize + addedSize; | ||
| })); | ||
|
|
||
| var brokerOverflow = | ||
| brokerCostLimit.entrySet().stream() | ||
| .anyMatch( | ||
| brokerLimit -> | ||
| moveCost.getOrDefault(brokerLimit.getKey(), 0L) | ||
| > brokerLimit.getValue().bytes()); | ||
| if (brokerOverflow) return () -> brokerOverflow; | ||
|
qoo332001 marked this conversation as resolved.
Outdated
|
||
|
|
||
| var diskMoveCostLimit = diskMoveCostLimit(); | ||
| var pathCost = | ||
| Stream.concat( | ||
| before.brokerFolders().entrySet().stream(), | ||
| after.brokerFolders().entrySet().stream()) | ||
| .distinct() | ||
| .flatMap( | ||
| brokerPath -> | ||
| brokerPath.getValue().stream() | ||
| .map( | ||
| path -> { | ||
| var beforeSize = | ||
| before | ||
| .replicaStream(brokerPath.getKey()) | ||
| .filter(r -> r.path().equals(path)) | ||
| .mapToLong(Replica::size) | ||
| .sum(); | ||
| var addedSize = | ||
| (Long) | ||
| after | ||
| .replicaStream(brokerPath.getKey()) | ||
| .filter( | ||
| r -> | ||
| before | ||
| .replicaStream(brokerPath.getKey()) | ||
| .noneMatch(r::equals)) | ||
| .map(Replica::size) | ||
| .mapToLong(y -> y) | ||
| .sum(); | ||
| return Map.entry( | ||
| BrokerPath.of(brokerPath.getKey(), path), beforeSize + addedSize); | ||
| })) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
| var overflow = | ||
| pathCost.entrySet().stream() | ||
| .anyMatch( | ||
| costLimit -> | ||
| diskMoveCostLimit.getOrDefault(costLimit.getKey(), DataSize.ZERO).bytes() | ||
| > costLimit.getValue()); | ||
| return () -> overflow; | ||
| } | ||
|
|
||
| private Map<BrokerPath, DataSize> diskMoveCostLimit() { | ||
| return this.configuration | ||
| .string(DISK_COST_LIMIT_KEY) | ||
| .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]), brokerPath[1]), | ||
| DataSize.of(brokerPathAndLimit[1])); | ||
| }) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) | ||
| .orElse(Map.of()); | ||
| } | ||
|
|
||
| private Map<Integer, DataSize> brokerMoveCostLimit() { | ||
| return this.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()); | ||
| } | ||
| } | ||
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.