-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[Refactor] optimize code structure about collector and job within Manager #3675
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
Open
Calvin979
wants to merge
6
commits into
master
Choose a base branch
from
refactor_manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d95df3
[refactor] optimize code structure about collector and job within Man…
Calvin979 553bc1e
Merge branch 'master' into refactor_manager
tomsun28 2276937
Update hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/p…
Calvin979 8a705c9
[refactor] update class and method comments
Calvin979 22897e4
[refactor] optimize code
Calvin979 b696129
[refactor] optimize code
Calvin979 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
25 changes: 25 additions & 0 deletions
25
hertzbeat-common/src/main/java/org/apache/hertzbeat/common/constants/CollectorStatus.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,25 @@ | ||
| /* | ||
| * 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.hertzbeat.common.constants; | ||
|
|
||
| /** | ||
| * Enum representing the possible statuses of a collector. | ||
| */ | ||
| public enum CollectorStatus { | ||
| ONLINE, OFFLINE; | ||
| } |
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
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
156 changes: 156 additions & 0 deletions
156
hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/pojo/CollectorNode.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,156 @@ | ||
| /* | ||
| * 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.hertzbeat.manager.pojo; | ||
|
|
||
| import lombok.Data; | ||
| import org.apache.hertzbeat.common.constants.CollectorStatus; | ||
| import org.apache.hertzbeat.manager.scheduler.AssignJobs; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * Collector Node | ||
| */ | ||
| @Data | ||
| public class CollectorNode { | ||
| /** | ||
| * Default number of VM nodes | ||
| */ | ||
| private static final byte VIRTUAL_NODE_DEFAULT_SIZE = 10; | ||
|
|
||
| /** | ||
| * collector identity | ||
| */ | ||
| private final String identity; | ||
|
|
||
| /** | ||
| * collector mode: public or private | ||
| */ | ||
| private String mode; | ||
|
|
||
| /** | ||
| * ip | ||
| */ | ||
| private String ip; | ||
|
|
||
| /** | ||
| * collector On-line time stamp | ||
| */ | ||
| private long uptime; | ||
|
|
||
| /** | ||
| * collector's own performance service quality score 0 - 127 | ||
| * The number of virtual nodes will be calculated based on this service quality score | ||
| * | ||
| */ | ||
| private Byte quality; | ||
|
|
||
| private CollectorStatus collectorStatus; | ||
|
|
||
| /** | ||
| * use this collector's collect job ID list | ||
| * jobId,jobVersion | ||
| */ | ||
| private AssignJobs assignJobs; | ||
|
|
||
| /** | ||
| * the collection task ID list mapped by each virtual node corresponding to this node | ||
| * Long[] [0]-jobId, [1]-dispatchHash | ||
| */ | ||
| private Map<Integer, Set<Long[]>> virtualNodeMap; | ||
|
|
||
| public CollectorNode(String identity, String mode, String ip, long uptime, Byte quality) { | ||
| this.identity = identity; | ||
| this.mode = mode; | ||
| this.ip = ip; | ||
| this.uptime = uptime; | ||
| this.quality = quality; | ||
| assignJobs = new AssignJobs(); | ||
| virtualNodeMap = new ConcurrentHashMap<>(VIRTUAL_NODE_DEFAULT_SIZE); | ||
| } | ||
|
|
||
| public synchronized void addJob(Integer virtualNodeKey, Integer dispatchHash, Long jobId, boolean isFlushed) { | ||
| if (virtualNodeMap == null) { | ||
| virtualNodeMap = new ConcurrentHashMap<>(VIRTUAL_NODE_DEFAULT_SIZE); | ||
| } | ||
| if (assignJobs == null) { | ||
| assignJobs = new AssignJobs(); | ||
| } | ||
| Set<Long[]> virtualNodeJob = virtualNodeMap.computeIfAbsent(virtualNodeKey, k -> ConcurrentHashMap.newKeySet(16)); | ||
| virtualNodeJob.add(new Long[]{jobId, dispatchHash.longValue()}); | ||
| if (isFlushed) { | ||
| assignJobs.addAssignJob(jobId); | ||
| } else { | ||
| assignJobs.addAddingJob(jobId); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * obtain the collection task routed by the specified virtual node according to virtualNodeKey | ||
| * @param virtualNodeKey virtualNodeKey | ||
| * @return collection task | ||
| */ | ||
| public Set<Long[]> clearVirtualNodeJobs(Integer virtualNodeKey) { | ||
| if (virtualNodeMap == null || virtualNodeMap.isEmpty()) { | ||
| return null; | ||
| } | ||
| Set<Long[]> virtualNodeJobs = virtualNodeMap.remove(virtualNodeKey); | ||
| virtualNodeMap.put(virtualNodeKey, ConcurrentHashMap.newKeySet(16)); | ||
| return virtualNodeJobs; | ||
| } | ||
|
|
||
| public void addVirtualNodeJobs(Integer virtualHashKey, Set<Long[]> reDispatchJobs) { | ||
| if (reDispatchJobs == null) { | ||
| return; | ||
| } | ||
| if (virtualNodeMap == null) { | ||
| virtualNodeMap = new ConcurrentHashMap<>(16); | ||
| } | ||
| virtualNodeMap.computeIfPresent(virtualHashKey, (k, v) -> { | ||
| reDispatchJobs.addAll(v); | ||
| return v; | ||
| }); | ||
| virtualNodeMap.put(virtualHashKey, reDispatchJobs); | ||
| } | ||
|
|
||
| public void removeVirtualNodeJob(Long jobId) { | ||
| if (jobId == null || virtualNodeMap == null) { | ||
| return; | ||
| } | ||
| for (Set<Long[]> jobSet : virtualNodeMap.values()) { | ||
| Optional<Long[]> optional = jobSet.stream().filter(item -> Objects.equals(item[0], jobId)).findFirst(); | ||
| if (optional.isPresent()) { | ||
| jobSet.remove(optional.get()); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void destroy() { | ||
| if (assignJobs != null) { | ||
| assignJobs.clear(); | ||
| } | ||
| if (virtualNodeMap != null) { | ||
| virtualNodeMap.clear(); | ||
| } | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/pojo/JobCache.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,53 @@ | ||
| /* | ||
| * 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.hertzbeat.manager.pojo; | ||
|
|
||
| import org.apache.hertzbeat.common.entity.job.Job; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * Utility class for caching {@link Job} objects in memory. | ||
| * <p> | ||
| * This class provides static methods to store, retrieve, and remove {@code Job} instances | ||
| * using a thread-safe {@link ConcurrentHashMap}. It is intended to be used as a simple | ||
| * in-memory cache for job data within the manager component. | ||
| * <p> | ||
| * Usage: | ||
| * <pre> | ||
| * JobCache.put(job); | ||
| * Job job = JobCache.get(jobId); | ||
| * JobCache.remove(jobId); | ||
| * </pre> | ||
| */ | ||
| public class JobCache { | ||
| private static final Map<Long, Job> jobContentCache = new ConcurrentHashMap<>(16); | ||
|
|
||
| public static Job get(Long jobId) { | ||
| return jobContentCache.get(jobId); | ||
| } | ||
|
|
||
| public static void put(Job job) { | ||
| jobContentCache.put(job.getId(), job); | ||
| } | ||
|
|
||
| public static void remove(Long jobId) { | ||
| jobContentCache.remove(jobId); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.