forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Draft: <feature>[physicalServer]: SUG-1461 add assignment foundation #4690
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
ZStack-Robot
wants to merge
1
commit into
5.5.38
Choose a base branch
from
sync/jin.ma/fix/SUG-1461
base: 5.5.38
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
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
191 changes: 191 additions & 0 deletions
191
compute/src/main/java/org/zstack/compute/allocator/HostPhysicalServerCapacityProjection.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,191 @@ | ||
| package org.zstack.compute.allocator; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.cloudbus.CloudBus; | ||
| import org.zstack.core.cloudbus.EventCallback; | ||
| import org.zstack.core.cloudbus.EventFacade; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.header.Component; | ||
| import org.zstack.header.allocator.HostAllocatorConstant; | ||
| import org.zstack.header.allocator.HostCapacityStruct; | ||
| import org.zstack.header.allocator.HostCapacityVO; | ||
| import org.zstack.header.allocator.HostCpuOverProvisioningManager; | ||
| import org.zstack.header.allocator.ReportHostCapacityExtensionPoint; | ||
| import org.zstack.header.host.HostVO; | ||
| import org.zstack.header.host.HostVO_; | ||
| import org.zstack.header.host.RecalculateHostCapacityMsg; | ||
| import org.zstack.header.physicalserver.PhysicalServerCpuCapacityProjectionExtensionPoint; | ||
| import org.zstack.header.physicalserver.PhysicalServerCpuCapacitySnapshot; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class HostPhysicalServerCapacityProjection implements | ||
| PhysicalServerCpuCapacityProjectionExtensionPoint, | ||
| ReportHostCapacityExtensionPoint, | ||
| Component { | ||
| private static final String SYNC_PATH = "/physical-server/cpu-capacity-projection/changed"; | ||
|
|
||
| private final Map<String, String> serverByHost = new ConcurrentHashMap<>(); | ||
| private final Map<String, PhysicalServerCpuCapacitySnapshot> snapshots = | ||
| new ConcurrentHashMap<>(); | ||
|
|
||
| @Autowired | ||
| private HostCpuOverProvisioningManager cpuRatioMgr; | ||
| @Autowired | ||
| private CloudBus bus; | ||
| @Autowired | ||
| private EventFacade evtf; | ||
| private EventCallback<PhysicalServerCapacityProjectionSync> projectionChanged; | ||
|
|
||
| @Override | ||
| public HostCapacityVO reportHostCapacity(HostCapacityStruct struct) { | ||
| HostCapacityVO capacity = struct.getCapacityVO(); | ||
| String serverUuid = serverByHost.get(capacity.getUuid()); | ||
| if (serverUuid == null) { | ||
| return capacity; | ||
| } | ||
| PhysicalServerCpuCapacitySnapshot snapshot = snapshots.get(serverUuid); | ||
| if (snapshot == null) { | ||
| return capacity; | ||
| } | ||
| if (!snapshot.isEligible()) { | ||
| capacity.setTotalCpu(0); | ||
| capacity.setAvailableCpu(0); | ||
| return capacity; | ||
| } | ||
|
|
||
| int allocatablePhysicalCpu = Math.max( | ||
| 0, capacity.getCpuNum() - snapshot.getExcludedCpuCount()); | ||
| long totalCpu = cpuRatioMgr.calculateHostCpuByRatio( | ||
| capacity.getUuid(), allocatablePhysicalCpu); | ||
| capacity.setTotalCpu(totalCpu); | ||
| capacity.setAvailableCpu(Math.max(0, totalCpu - struct.getUsedCpu())); | ||
| return capacity; | ||
| } | ||
|
|
||
| @Override | ||
| public void refresh(String serverUuid, PhysicalServerCpuCapacitySnapshot snapshot) { | ||
| PhysicalServerCpuCapacitySnapshot previous = snapshots.put(serverUuid, snapshot); | ||
| publish(); | ||
| if (changed(previous, snapshot)) { | ||
| recalculateByServer(serverUuid); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void remove(String serverUuid) { | ||
| PhysicalServerCpuCapacitySnapshot previous = snapshots.remove(serverUuid); | ||
| publish(); | ||
| if (previous != null) { | ||
| recalculateByServer(serverUuid); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void synchronize(Map<String, PhysicalServerCpuCapacitySnapshot> incoming) { | ||
| Set<String> affected = new HashSet<>(snapshots.keySet()); | ||
| affected.addAll(incoming.keySet()); | ||
| Map<String, PhysicalServerCpuCapacitySnapshot> previous = new HashMap<>(snapshots); | ||
| snapshots.clear(); | ||
| snapshots.putAll(incoming); | ||
| publish(); | ||
| for (String serverUuid : affected) { | ||
| if (changed(previous.get(serverUuid), incoming.get(serverUuid))) { | ||
| recalculateByServer(serverUuid); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public void bindHost(String hostUuid, String serverUuid) { | ||
| String previous = serverUuid == null | ||
| ? serverByHost.remove(hostUuid) : serverByHost.put(hostUuid, serverUuid); | ||
| if (previous == null ? serverUuid != null : !previous.equals(serverUuid)) { | ||
| recalculate(hostUuid); | ||
| } | ||
| } | ||
|
|
||
| public void removeHost(String hostUuid) { | ||
| serverByHost.remove(hostUuid); | ||
| } | ||
|
|
||
| public PhysicalServerCpuCapacitySnapshot getSnapshot(String hostUuid) { | ||
| String serverUuid = serverByHost.get(hostUuid); | ||
| return serverUuid == null ? null : snapshots.get(serverUuid); | ||
| } | ||
|
|
||
| private void warmUpHostLinks() { | ||
| List<HostVO> hosts = Q.New(HostVO.class).notNull(HostVO_.serverUuid).list(); | ||
| serverByHost.clear(); | ||
| for (HostVO host : hosts) { | ||
| serverByHost.put(host.getUuid(), host.getServerUuid()); | ||
| } | ||
| } | ||
|
|
||
| private void publish() { | ||
| PhysicalServerCapacityProjectionSync sync = new PhysicalServerCapacityProjectionSync(); | ||
| sync.setSnapshots(new HashMap<>(snapshots)); | ||
| evtf.fire(SYNC_PATH, sync); | ||
| } | ||
|
|
||
| private void applyRemote(PhysicalServerCapacityProjectionSync sync) { | ||
| snapshots.clear(); | ||
| snapshots.putAll(sync.getSnapshots()); | ||
| } | ||
|
|
||
| private void recalculateByServer(String serverUuid) { | ||
| for (Map.Entry<String, String> entry : serverByHost.entrySet()) { | ||
| if (serverUuid.equals(entry.getValue())) { | ||
| recalculate(entry.getKey()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void recalculate(String hostUuid) { | ||
| RecalculateHostCapacityMsg msg = new RecalculateHostCapacityMsg(); | ||
| msg.setHostUuid(hostUuid); | ||
| bus.makeLocalServiceId(msg, HostAllocatorConstant.SERVICE_ID); | ||
| bus.send(msg); | ||
| } | ||
|
|
||
| private boolean changed( | ||
| PhysicalServerCpuCapacitySnapshot previous, | ||
| PhysicalServerCpuCapacitySnapshot next) { | ||
| if (previous == null || next == null) { | ||
| return previous != next; | ||
| } | ||
| return previous.isEligible() != next.isEligible() | ||
| || previous.getExcludedCpuCount() != next.getExcludedCpuCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean start() { | ||
| warmUpHostLinks(); | ||
| projectionChanged = new EventCallback<PhysicalServerCapacityProjectionSync>() { | ||
| @Override | ||
| protected void run( | ||
| Map<String, String> tokens, | ||
| PhysicalServerCapacityProjectionSync sync) { | ||
| if (!evtf.isFromThisManagementNode(tokens)) { | ||
| applyRemote(sync); | ||
| } | ||
| } | ||
| }; | ||
| evtf.on(SYNC_PATH, projectionChanged); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean stop() { | ||
| if (projectionChanged != null) { | ||
| evtf.off(projectionChanged); | ||
| } | ||
| serverByHost.clear(); | ||
| snapshots.clear(); | ||
| return true; | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
compute/src/main/java/org/zstack/compute/allocator/PhysicalServerCapacityProjectionSync.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,19 @@ | ||
| package org.zstack.compute.allocator; | ||
|
|
||
| import org.zstack.header.physicalserver.PhysicalServerCpuCapacitySnapshot; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| public class PhysicalServerCapacityProjectionSync implements Serializable { | ||
| private Map<String, PhysicalServerCpuCapacitySnapshot> snapshots = new HashMap<>(); | ||
|
|
||
| public Map<String, PhysicalServerCpuCapacitySnapshot> getSnapshots() { | ||
| return snapshots; | ||
| } | ||
|
|
||
| public void setSnapshots(Map<String, PhysicalServerCpuCapacitySnapshot> snapshots) { | ||
| this.snapshots = snapshots; | ||
| } | ||
| } |
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,84 @@ | ||
| CREATE TABLE IF NOT EXISTS `zstack`.`PhysicalServerVO` ( | ||
| `uuid` varchar(32) NOT NULL, | ||
| `zoneUuid` varchar(32) DEFAULT NULL, | ||
| `poolUuid` varchar(32) DEFAULT NULL, | ||
| `serialNumber` varchar(255) NOT NULL, | ||
| `createDate` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00', | ||
| `lastOpDate` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP, | ||
| PRIMARY KEY (`uuid`), | ||
| UNIQUE KEY `ukPhysicalServerSerialNumber` (`serialNumber`) | ||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS `zstack`.`PhysicalServerResourceAssignmentVO` ( | ||
| `uuid` varchar(32) NOT NULL, | ||
| `serverUuid` varchar(32) NOT NULL, | ||
| `resourceType` varchar(32) NOT NULL, | ||
| `assignmentType` varchar(64) NOT NULL, | ||
| `spec` text NOT NULL, | ||
| `specGeneration` bigint NOT NULL DEFAULT 1, | ||
| `status` text DEFAULT NULL, | ||
| `observedGeneration` bigint DEFAULT NULL, | ||
| `observedAt` timestamp NULL DEFAULT NULL, | ||
| `createDate` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00', | ||
| `lastOpDate` timestamp NOT NULL DEFAULT '2000-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP, | ||
| PRIMARY KEY (`uuid`), | ||
| UNIQUE KEY `ukPhysicalServerResourceAssignment` (`serverUuid`, `resourceType`, `assignmentType`), | ||
| CONSTRAINT `fkPhysicalServerResourceAssignmentServerUuid` | ||
| FOREIGN KEY (`serverUuid`) REFERENCES `zstack`.`PhysicalServerVO` (`uuid`) ON DELETE CASCADE | ||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
|
|
||
| CALL ADD_COLUMN('HostEO', 'serverUuid', 'VARCHAR(32)', 1, NULL); | ||
| CALL ADD_COLUMN('ManagementNodeVO', 'serverUuid', 'VARCHAR(32)', 1, NULL); | ||
|
|
||
| DROP PROCEDURE IF EXISTS addPhysicalServerIdentityUniqueKeys; | ||
| DELIMITER $$ | ||
| CREATE PROCEDURE addPhysicalServerIdentityUniqueKeys() | ||
| BEGIN | ||
| IF NOT EXISTS ( | ||
| SELECT 1 FROM information_schema.statistics | ||
| WHERE table_schema = 'zstack' | ||
| AND table_name = 'HostEO' | ||
| AND index_name = 'ukHostEOServerUuid' | ||
| ) THEN | ||
| ALTER TABLE `zstack`.`HostEO` | ||
| ADD UNIQUE KEY `ukHostEOServerUuid` (`serverUuid`); | ||
| END IF; | ||
|
|
||
| IF NOT EXISTS ( | ||
| SELECT 1 FROM information_schema.statistics | ||
| WHERE table_schema = 'zstack' | ||
| AND table_name = 'ManagementNodeVO' | ||
| AND index_name = 'ukManagementNodeVOServerUuid' | ||
| ) THEN | ||
| ALTER TABLE `zstack`.`ManagementNodeVO` | ||
| ADD UNIQUE KEY `ukManagementNodeVOServerUuid` (`serverUuid`); | ||
| END IF; | ||
| END $$ | ||
| DELIMITER ; | ||
| CALL addPhysicalServerIdentityUniqueKeys(); | ||
| DROP PROCEDURE IF EXISTS addPhysicalServerIdentityUniqueKeys; | ||
|
|
||
| CALL ADD_CONSTRAINT( | ||
| 'HostEO', | ||
| 'fkHostEOServerUuid', | ||
| 'serverUuid', | ||
| 'PhysicalServerVO', | ||
| 'uuid', | ||
| 'SET NULL' | ||
| ); | ||
|
|
||
| DROP VIEW IF EXISTS `zstack`.`HostVO`; | ||
| CREATE VIEW `zstack`.`HostVO` AS | ||
| SELECT uuid, zoneUuid, clusterUuid, name, description, managementIp, hypervisorType, | ||
| state, status, createDate, lastOpDate, architecture, serverUuid | ||
| FROM `zstack`.`HostEO` | ||
| WHERE deleted IS NULL; | ||
|
|
||
| CALL ADD_CONSTRAINT( | ||
| 'ManagementNodeVO', | ||
| 'fkManagementNodeVOServerUuid', | ||
| 'serverUuid', | ||
| 'PhysicalServerVO', | ||
| 'uuid', | ||
| 'SET NULL' | ||
| ); | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <service xmlns="http://zstack.org/schema/zstack"> | ||
| <id>physicalServer</id> | ||
| <interceptor>PhysicalServerApiInterceptor</interceptor> | ||
|
|
||
| <message> | ||
| <name>org.zstack.physicalserver.APIQueryPhysicalServerMsg</name> | ||
| <serviceId>query</serviceId> | ||
| </message> | ||
|
|
||
| <message> | ||
| <name>org.zstack.physicalserver.APIQueryPhysicalServerResourceAssignmentMsg</name> | ||
| <serviceId>query</serviceId> | ||
| </message> | ||
|
|
||
| <message> | ||
| <name>org.zstack.physicalserver.APIUpdatePhysicalServerCpuAssignmentMsg</name> | ||
| </message> | ||
|
|
||
| <message> | ||
| <name>org.zstack.physicalserver.APIRefreshPhysicalServerCpuIsolationMsg</name> | ||
| </message> | ||
| </service> |
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.
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.
🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 1433
🏁 Script executed:
Repository: MatheMatrix/zstack
Length of output: 16161
🌐 Web query:
MySQL 5.7 reference manual BLOB TEXT columns cannot have default values💡 Result:
According to the official MySQL 5.7 Reference Manual, it is correct that BLOB and TEXT columns cannot be assigned default values [1][2][3]. The documentation explicitly states in the sections regarding data type default values and BLOB/TEXT types that these columns cannot have DEFAULT values [1][2][4][3]. This restriction also applies to the GEOMETRY and JSON data types [1][4].
Citations:
删除
status的DEFAULT NULL。MySQL 5.7 不允许
TEXT列声明默认值。该定义会导致建表失败。省略DEFAULT NULL即可,status仍允许为NULL。建议修复
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Learnings