Skip to content

Commit baa3bcc

Browse files
author
gitlab
committed
Merge branch 'chore@@2' into '4.6.31'
<fix>[kvm]: move host os info to KVMHostVO See merge request zstackio/zstack!3833
2 parents fd5a1c7 + 6265a75 commit baa3bcc

File tree

20 files changed

+411
-199
lines changed

20 files changed

+411
-199
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,76 @@
11
package org.zstack.compute.allocator;
22

33
import org.springframework.beans.factory.annotation.Autowire;
4+
import org.springframework.beans.factory.annotation.Autowired;
45
import org.springframework.beans.factory.annotation.Configurable;
5-
import org.zstack.compute.host.HostSystemTags;
6+
import org.zstack.compute.host.HostManager;
67
import org.zstack.core.Platform;
8+
import org.zstack.core.db.Q;
79
import org.zstack.header.allocator.AbstractHostAllocatorFlow;
8-
import org.zstack.header.host.HostVO;
10+
import org.zstack.header.host.*;
11+
import org.zstack.header.vo.ResourceVO;
912

10-
import java.util.ArrayList;
11-
import java.util.HashMap;
12-
import java.util.List;
13-
import java.util.Map;
13+
import java.util.*;
1414
import java.util.Map.Entry;
15+
import java.util.stream.Collectors;
1516

1617
/**
18+
* Filter out hosts that do not match the operating system of the specific host
1719
*/
1820
@Configurable(preConstruction = true, autowire = Autowire.BY_TYPE)
1921
public class HostOsVersionAllocatorFlow extends AbstractHostAllocatorFlow {
22+
@Autowired
23+
private HostManager manager;
24+
2025
@Override
2126
public void allocate() {
2227
throwExceptionIfIAmTheFirstFlow();
2328

24-
Map<String, HostVO> hostMap = new HashMap<String, HostVO>();
29+
Map<String, HostVO> hostMap = new HashMap<>();
2530
for (HostVO h : candidates) {
2631
hostMap.put(h.getUuid(), h);
2732
}
2833

29-
List<String> hostUuids = new ArrayList<String>();
30-
hostUuids.addAll(hostMap.keySet());
34+
final String currentHostUuid = spec.getVmInstance().getHostUuid();
35+
List<HostVO> allHostList = new ArrayList<>(candidates);
36+
final HostVO currentHost = Q.New(HostVO.class)
37+
.eq(HostVO_.uuid, currentHostUuid)
38+
.find();
39+
allHostList.add(currentHost);
40+
Map<String, HostOperationSystem> hostOsMap = generateHostUuidOsMap(allHostList);
3141

32-
String distro = HostSystemTags.OS_DISTRIBUTION.getTokenByResourceUuid(spec.getVmInstance().getHostUuid(), HostSystemTags.OS_DISTRIBUTION_TOKEN);
33-
String release = HostSystemTags.OS_RELEASE.getTokenByResourceUuid(spec.getVmInstance().getHostUuid(), HostSystemTags.OS_RELEASE_TOKEN);
34-
String version = HostSystemTags.OS_VERSION.getTokenByResourceUuid(spec.getVmInstance().getHostUuid(), HostSystemTags.OS_VERSION_TOKEN);
35-
String currentVersion = String.format("%s;%s;%s", distro, release, version);
42+
final HostOperationSystem currentHostOs = hostOsMap.get(currentHostUuid);
43+
hostOsMap.remove(currentHostUuid);
3644

37-
Map<String, List<String>> distroMap = HostSystemTags.OS_DISTRIBUTION.getTags(hostUuids);
38-
Map<String, List<String>> releaseMap = HostSystemTags.OS_RELEASE.getTags(hostUuids);
39-
Map<String, List<String>> versionMap = HostSystemTags.OS_VERSION.getTags(hostUuids);
45+
List<HostVO> matchedHosts = hostOsMap.entrySet().stream()
46+
.filter(entry -> currentHostOs.equals(entry.getValue()))
47+
.map(Entry::getKey)
48+
.map(hostMap::get)
49+
.collect(Collectors.toList());
4050

41-
Map<String, String> candidateVersions = new HashMap<String, String>();
42-
for (String huuid : hostUuids) {
43-
List<String> ds = distroMap.get(huuid);
44-
String d = ds == null ? null : HostSystemTags.OS_DISTRIBUTION.getTokenByTag(ds.get(0), HostSystemTags.OS_DISTRIBUTION_TOKEN);
45-
List<String> rs = releaseMap.get(huuid);
46-
String r = rs == null ? null : HostSystemTags.OS_RELEASE.getTokenByTag(rs.get(0), HostSystemTags.OS_RELEASE_TOKEN);
47-
List<String> vs = versionMap.get(huuid);
48-
String v = vs == null ? null : HostSystemTags.OS_VERSION.getTokenByTag(vs.get(0), HostSystemTags.OS_VERSION_TOKEN);
49-
candidateVersions.put(huuid, String.format("%s;%s;%s", d, r, v));
51+
if (matchedHosts.isEmpty()) {
52+
fail(Platform.operr("no candidate host has version[%s]", currentHostOs));
53+
} else {
54+
next(matchedHosts);
5055
}
56+
}
5157

52-
List<HostVO> ret = new ArrayList<HostVO>();
53-
for (Entry<String, String> e : candidateVersions.entrySet()) {
54-
String huuid = e.getKey();
55-
String ver = e.getValue();
56-
if (ver.equals(currentVersion)) {
57-
ret.add(hostMap.get(huuid));
58-
}
59-
}
58+
private Map<String, HostOperationSystem> generateHostUuidOsMap(List<HostVO> hostList) {
59+
final Map<String, String> hostHypervisorTypeMap = hostList.stream()
60+
.collect(Collectors.toMap(ResourceVO::getUuid, HostAO::getHypervisorType));
61+
final Set<String> hypervisorTypeSet = new HashSet<>(hostHypervisorTypeMap.values());
6062

61-
if (ret.isEmpty()) {
62-
fail(Platform.operr("no candidate host has version[%s]", currentVersion));
63-
} else {
64-
next(ret);
63+
final Map<String, HostOperationSystem> results = new HashMap<>(hostList.size());
64+
for (String hypervisorTypeString : hypervisorTypeSet) {
65+
final HypervisorFactory factory = manager.getHypervisorFactory(
66+
HypervisorType.valueOf(hypervisorTypeString));
67+
final List<String> hostsWithThisHypervisorType = hostHypervisorTypeMap.entrySet().stream()
68+
.filter(entry -> hypervisorTypeString.equals(entry.getValue()))
69+
.map(Entry::getKey)
70+
.collect(Collectors.toList());
71+
results.putAll(factory.getHostOsMap(hostsWithThisHypervisorType));
6572
}
73+
74+
return results;
6675
}
6776
}

compute/src/main/java/org/zstack/compute/host/HostBase.java

-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.springframework.beans.factory.annotation.Autowire;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.beans.factory.annotation.Configurable;
6-
import org.springframework.beans.factory.annotation.Qualifier;
76
import org.zstack.compute.vm.VmSchedHistoryRecorder;
87
import org.zstack.core.upgrade.UpgradeGlobalConfig;
98
import org.zstack.core.asyncbatch.While;
@@ -52,13 +51,10 @@
5251
import org.zstack.utils.DebugUtils;
5352
import org.zstack.utils.Utils;
5453
import org.zstack.utils.function.ForEachFunction;
55-
import org.zstack.utils.function.Function;
56-
import org.zstack.utils.gson.JSONObjectUtil;
5754
import org.zstack.utils.logging.CLogger;
5855

5956
import java.util.*;
6057
import java.util.concurrent.TimeUnit;
61-
import java.util.function.Supplier;
6258

6359
import static org.zstack.core.Platform.err;
6460
import static org.zstack.core.Platform.operr;

compute/src/main/java/org/zstack/compute/host/HostManager.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
public interface HostManager {
99
HypervisorFactory getHypervisorFactory(HypervisorType type);
10-
11-
void handleMessage(Message msg);
1210

13-
HostBaseExtensionFactory getHostBaseExtensionFactory(Message msg);
11+
void handleMessage(Message msg);
12+
13+
HostBaseExtensionFactory getHostBaseExtensionFactory(Message msg);
1414
}

compute/src/main/java/org/zstack/compute/host/HostManagerImpl.java

+3-39
Original file line numberDiff line numberDiff line change
@@ -366,47 +366,11 @@ public void run(FlowTrigger trigger, Map data) {
366366

367367
@Override
368368
public void run(FlowTrigger trigger, Map data) {
369-
String distro = HostSystemTags.OS_DISTRIBUTION.getTokenByResourceUuid(vo.getUuid(), HostSystemTags.OS_DISTRIBUTION_TOKEN);
370-
String release = HostSystemTags.OS_RELEASE.getTokenByResourceUuid(vo.getUuid(), HostSystemTags.OS_RELEASE_TOKEN);
371-
String version = HostSystemTags.OS_VERSION.getTokenByResourceUuid(vo.getUuid(), HostSystemTags.OS_VERSION_TOKEN);
372-
373-
if (distro == null || release == null || version == null) {
374-
trigger.fail(operr("after connecting, host[name:%s, ip:%s] returns a null os version", vo.getName(), vo.getManagementIp()));
375-
return;
376-
}
377-
378-
SimpleQuery<HostVO> q = dbf.createQuery(HostVO.class);
379-
q.select(HostVO_.uuid);
380-
q.add(HostVO_.clusterUuid, Op.EQ, vo.getClusterUuid());
381-
q.add(HostVO_.uuid, Op.NOT_EQ, vo.getUuid());
382-
q.add(HostVO_.status, Op.NOT_EQ, HostStatus.Connecting);
383-
q.setLimit(1);
384-
List<String> huuids = q.listValue();
385-
if (huuids.isEmpty()) {
386-
// this the first host in cluster
387-
trigger.next();
388-
return;
389-
}
390-
391-
String otherHostUuid = huuids.get(0);
392-
String cdistro = HostSystemTags.OS_DISTRIBUTION.getTokenByResourceUuid(otherHostUuid, HostSystemTags.OS_DISTRIBUTION_TOKEN);
393-
String crelease = HostSystemTags.OS_RELEASE.getTokenByResourceUuid(otherHostUuid, HostSystemTags.OS_RELEASE_TOKEN);
394-
String cversion = HostSystemTags.OS_VERSION.getTokenByResourceUuid(otherHostUuid, HostSystemTags.OS_VERSION_TOKEN);
395-
if (cdistro == null || crelease == null || cversion == null) {
396-
// this the first host in cluster
397-
trigger.next();
369+
final ErrorCode errorCode = factory.checkNewAddedHost(vo);
370+
if (errorCode != null) {
371+
trigger.fail(errorCode);
398372
return;
399373
}
400-
401-
String mineVersion = String.format("%s;%s;%s", distro, release, version);
402-
String currentVersion = String.format("%s;%s;%s", cdistro, crelease, cversion);
403-
404-
if (!mineVersion.equals(currentVersion)) {
405-
trigger.fail(operr("cluster[uuid:%s] already has host with os version[%s], but new added host[name:%s ip:%s] has host os version[%s]",
406-
vo.getClusterUuid(), currentVersion, vo.getName(), vo.getManagementIp(), mineVersion));
407-
return;
408-
}
409-
410374
trigger.next();
411375
}
412376
}).then(new NoRollbackFlow() {

compute/src/main/java/org/zstack/compute/host/HostSystemTags.java

-9
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ public class HostSystemTags {
1717

1818
public static SystemTag LIVE_SNAPSHOT = new SystemTag("capability::liveSnapshot", HostVO.class);
1919

20-
public static String OS_DISTRIBUTION_TOKEN = "distribution";
21-
public static PatternedSystemTag OS_DISTRIBUTION = new PatternedSystemTag(String.format("os::distribution::{%s}", OS_DISTRIBUTION_TOKEN), HostVO.class);
22-
23-
public static String OS_RELEASE_TOKEN = "release";
24-
public static PatternedSystemTag OS_RELEASE = new PatternedSystemTag(String.format("os::release::{%s}", OS_RELEASE_TOKEN), HostVO.class);
25-
26-
public static String OS_VERSION_TOKEN = "version";
27-
public static PatternedSystemTag OS_VERSION = new PatternedSystemTag(String.format("os::version::{%s}", OS_VERSION_TOKEN), HostVO.class);
28-
2920
public static String EXTRA_IPS_TOKEN = "extraips";
3021
public static PatternedSystemTag EXTRA_IPS = new PatternedSystemTag(String.format("extraips::{%s}", EXTRA_IPS_TOKEN), HostVO.class);
3122

conf/db/upgrade/V4.6.31__schema.sql

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
ALTER TABLE `zstack`.`KVMHostVO` ADD COLUMN `osDistribution` varchar(64) DEFAULT NULL;
2+
ALTER TABLE `zstack`.`KVMHostVO` ADD COLUMN `osRelease` varchar(64) DEFAULT NULL;
3+
ALTER TABLE `zstack`.`KVMHostVO` ADD COLUMN `osVersion` varchar(64) DEFAULT NULL;
4+
5+
DELIMITER $$
6+
CREATE PROCEDURE moveOsInfoToKVMHostVO()
7+
BEGIN
8+
DECLARE hostUuid CHAR(32);
9+
DECLARE osInfo VARCHAR(64);
10+
DECLARE done INT DEFAULT FALSE;
11+
DECLARE curDistribution CURSOR FOR
12+
SELECT resourceUuid, SUBSTRING(tag, 19) FROM `zstack`.`SystemTagVO` WHERE tag LIKE 'os::distribution::%';
13+
DECLARE curRelease CURSOR FOR
14+
SELECT resourceUuid, SUBSTRING(tag, 14) FROM `zstack`.`SystemTagVO` WHERE tag LIKE 'os::release::%';
15+
DECLARE curVersion CURSOR FOR
16+
SELECT resourceUuid, SUBSTRING(tag, 14) FROM `zstack`.`SystemTagVO` WHERE tag LIKE 'os::version::%';
17+
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
18+
19+
OPEN curDistribution;
20+
read_loop1: LOOP
21+
FETCH curDistribution INTO hostUuid, osInfo;
22+
IF done THEN
23+
LEAVE read_loop1;
24+
END IF;
25+
26+
UPDATE `zstack`.`KVMHostVO` SET osDistribution = osInfo WHERE uuid = hostUuid;
27+
END LOOP;
28+
CLOSE curDistribution;
29+
SET done = FALSE;
30+
31+
OPEN curRelease;
32+
read_loop2: LOOP
33+
FETCH curRelease INTO hostUuid, osInfo;
34+
IF done THEN
35+
LEAVE read_loop2;
36+
END IF;
37+
38+
UPDATE `zstack`.`KVMHostVO` SET osRelease = osInfo WHERE uuid = hostUuid;
39+
END LOOP;
40+
CLOSE curRelease;
41+
SET done = FALSE;
42+
43+
OPEN curVersion;
44+
read_loop3: LOOP
45+
FETCH curVersion INTO hostUuid, osInfo;
46+
IF done THEN
47+
LEAVE read_loop3;
48+
END IF;
49+
50+
UPDATE `zstack`.`KVMHostVO` SET osVersion = osInfo WHERE uuid = hostUuid;
51+
END LOOP;
52+
CLOSE curVersion;
53+
SELECT CURTIME();
54+
END $$
55+
DELIMITER ;
56+
57+
call moveOsInfoToKVMHostVO();
58+
DROP PROCEDURE IF EXISTS moveOsInfoToKVMHostVO;
59+
60+
DELETE FROM `zstack`.`SystemTagVO` WHERE tag LIKE 'os::distribution::%';
61+
DELETE FROM `zstack`.`SystemTagVO` WHERE tag LIKE 'os::release::%';
62+
DELETE FROM `zstack`.`SystemTagVO` WHERE tag LIKE 'os::version::%';

conf/i18n/messages_en_US.properties

+11-11
Original file line numberDiff line numberDiff line change
@@ -1630,14 +1630,6 @@ there\ has\ been\ a\ host\ having\ managementIp[%s] = there has been a host havi
16301630
# args: msg.getClusterUuid()
16311631
cluster[uuid\:%s]\ is\ not\ existing = cluster[uuid:{0}] is not existing
16321632

1633-
# at: src/main/java/org/zstack/compute/host/HostManagerImpl.java:378
1634-
# args: vo.getName(),vo.getManagementIp()
1635-
after\ connecting,\ host[name\:%s,\ ip\:%s]\ returns\ a\ null\ os\ version = after connecting, host[name:{0}, ip:{1}] returns a null os version
1636-
1637-
# at: src/main/java/org/zstack/compute/host/HostManagerImpl.java:409
1638-
# args: vo.getClusterUuid(),currentVersion,vo.getName(),vo.getManagementIp(),mineVersion
1639-
cluster[uuid\:%s]\ already\ has\ host\ with\ os\ version[%s],\ but\ new\ added\ host[name\:%s\ ip\:%s]\ has\ host\ os\ version[%s] = cluster[uuid:{0}] already has host with os version[{1}], but new added host[name:{2} ip:{3}] has host os version[{4}]
1640-
16411633
# at: src/main/java/org/zstack/compute/host/HostManagerImpl.java:531
16421634
# args: msg.getCancellationApiId()
16431635
no\ running\ api[%s]\ task\ on\ hosts = no running api[{0}] task on hosts
@@ -3758,15 +3750,23 @@ The\ host[uuid\:%s]'s\ available\ memory\ capacity[%s]\ is\ lower\ than\ the\ re
37583750
# args: e.getMessage()
37593751
fail\ to\ load\ host\ info\ from\ file.\ because\n%s = fail to load host info from file. because\n{0}
37603752

3761-
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:329
3753+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:284
3754+
# args: os, vo.getName(), vo.getManagementIp()
3755+
the\ operation\ system[%s]\ of\ host[name\:%s,\ ip\:%s]\ is\ invalid = the operation system[{0}] of host[name:{1},ip:{2}] is invalid
3756+
3757+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:310
3758+
# args: vo.getClusterUuid(),thatOs,vo.getName(),vo.getManagementIp(),os
3759+
cluster[uuid\:%s]\ already\ has\ host\ with\ os\ version[%s],\ but\ new\ added\ host[name\:%s\ ip\:%s]\ has\ different\ host\ os\ version[%s] = cluster[uuid:{0}] already has host with os version[{1}], but new added host[name:{2} ip:{3}] has different host os version[{4}]
3760+
3761+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:482
37623762
# args: str.toString()
37633763
there\ are\ still\ hosts\ not\ have\ the\ same\ cpu\ model,\ details\:\ %s = there are still hosts not have the same cpu model, details: {0}
37643764

3765-
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:343
3765+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:496
37663766
# args: KVMSystemTags.VM_PREDEFINED_PCI_BRIDGE_NUM_TOKEN
37673767
pci\ bridge\ need\ a\ value\ greater\ than\ 0\ and\ lower\ than\ 32 = pci bridge need a value greater than 0 and lower than 32
37683768

3769-
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:364
3769+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:741
37703770
# args: hostUuid
37713771
host[uuid\:%s]\ does\ not\ have\ cpu\ model\ information,\ you\ can\ reconnect\ the\ host\ to\ fix\ it = host[uuid:{0}] does not have cpu model information, you can reconnect the host to fix it
37723772

conf/i18n/messages_zh_CN.properties

+11-11
Original file line numberDiff line numberDiff line change
@@ -1630,14 +1630,6 @@ there\ has\ been\ a\ host\ having\ managementIp[%s] = 已经存在一个管理IP
16301630
# args: msg.getClusterUuid()
16311631
cluster[uuid\:%s]\ is\ not\ existing =
16321632

1633-
# at: src/main/java/org/zstack/compute/host/HostManagerImpl.java:378
1634-
# args: vo.getName(),vo.getManagementIp()
1635-
after\ connecting,\ host[name\:%s,\ ip\:%s]\ returns\ a\ null\ os\ version = 在连接操作后,物理机[name:{0}, ip:{1}]没有返回操作系统版本信息
1636-
1637-
# at: src/main/java/org/zstack/compute/host/HostManagerImpl.java:409
1638-
# args: vo.getClusterUuid(),currentVersion,vo.getName(),vo.getManagementIp(),mineVersion
1639-
cluster[uuid\:%s]\ already\ has\ host\ with\ os\ version[%s],\ but\ new\ added\ host[name\:%s\ ip\:%s]\ has\ host\ os\ version[%s] = 集群[uuid:{0}]中物理机使用的操作系统版本是[{1}],但是新的物理机[name:{2} ip:{3}]的操作系统版本是 [{4}]
1640-
16411633
# at: src/main/java/org/zstack/compute/host/HostManagerImpl.java:531
16421634
# args: msg.getCancellationApiId()
16431635
no\ running\ api[%s]\ task\ on\ hosts =
@@ -3758,15 +3750,23 @@ The\ host[uuid\:%s]'s\ available\ memory\ capacity[%s]\ is\ lower\ than\ the\ re
37583750
# args: e.getMessage()
37593751
fail\ to\ load\ host\ info\ from\ file.\ because\n%s =
37603752

3761-
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:329
3753+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:284
3754+
# args: os, vo.getName(), vo.getManagementIp()
3755+
the\ operation\ system[%s]\ of\ host[name\:%s,\ ip\:%s]\ is\ invalid = 物理机[名称:{1},IP:{2}]的操作系统{0}是不合规的
3756+
3757+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:310
3758+
# args: vo.getClusterUuid(),thatOs,vo.getName(),vo.getManagementIp(),os
3759+
cluster[uuid\:%s]\ already\ has\ host\ with\ os\ version[%s],\ but\ new\ added\ host[name\:%s\ ip\:%s]\ has\ different\ host\ os\ version[%s] = 集群[uuid:{0}]中物理机使用的操作系统版本是[{1}],但是新的物理机[name:{2} ip:{3}]是不同的操作系统版本 [{4}]
3760+
3761+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:482
37623762
# args: str.toString()
37633763
there\ are\ still\ hosts\ not\ have\ the\ same\ cpu\ model,\ details\:\ %s = 仍存在host有不同的cpu模型,详细信息:{0}
37643764

3765-
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:343
3765+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:496
37663766
# args: KVMSystemTags.VM_PREDEFINED_PCI_BRIDGE_NUM_TOKEN
37673767
pci\ bridge\ need\ a\ value\ greater\ than\ 0\ and\ lower\ than\ 32 =
37683768

3769-
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:364
3769+
# at: src/main/java/org/zstack/kvm/KVMHostFactory.java:741
37703770
# args: hostUuid
37713771
host[uuid\:%s]\ does\ not\ have\ cpu\ model\ information,\ you\ can\ reconnect\ the\ host\ to\ fix\ it = 物理机[uuid:{0}]无cpu模型信息,你可以尝试重连来解决这个问题
37723772

0 commit comments

Comments
 (0)