-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[storage]: refactor expon bm2 #2685
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
base: 5.4.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,10 +37,7 @@ | |
| import org.zstack.header.storage.primary.ImageCacheInventory; | ||
| import org.zstack.header.storage.primary.VolumeSnapshotCapability; | ||
| import org.zstack.header.storage.snapshot.VolumeSnapshotStats; | ||
| import org.zstack.header.volume.VolumeConstant; | ||
| import org.zstack.header.volume.VolumeInventory; | ||
| import org.zstack.header.volume.VolumeProtocol; | ||
| import org.zstack.header.volume.VolumeStats; | ||
| import org.zstack.header.volume.*; | ||
| import org.zstack.iscsi.IscsiUtils; | ||
| import org.zstack.iscsi.kvm.IscsiHeartbeatVolumeTO; | ||
| import org.zstack.iscsi.kvm.IscsiVolumeTO; | ||
|
|
@@ -63,6 +60,7 @@ | |
| import static org.zstack.core.Platform.operr; | ||
| import static org.zstack.expon.ExponIscsiHelper.*; | ||
| import static org.zstack.expon.ExponNameHelper.*; | ||
| import static org.zstack.iscsi.IscsiUtils.getGatewayMnIpFromInitiatorName; | ||
| import static org.zstack.iscsi.IscsiUtils.getHostMnIpFromInitiatorName; | ||
| import static org.zstack.storage.addon.primary.ExternalPrimaryStorageNameHelper.*; | ||
|
|
||
|
|
@@ -239,6 +237,10 @@ public List<IscsiSeverNode> getIscsiServers(String tianshuId) { | |
|
|
||
| private synchronized ActiveVolumeTO activeIscsiVolume(HostInventory h, BaseVolumeInfo vol, boolean shareable) { | ||
| String clientIqn = IscsiUtils.getHostInitiatorName(h.getUuid()); | ||
| if (h.getHypervisorType().equals("baremetal2")) { | ||
| VolumeVO volume = dbf.findByUuid(vol.getUuid(), VolumeVO.class); | ||
| clientIqn = String.format("iqn.2015-01.io.zstack:initiator.instance.%s", volume.getVmInstanceUuid()); | ||
| } | ||
| if (clientIqn == null) { | ||
| throw new RuntimeException(String.format("cannot get host[uuid:%s] initiator name", h.getUuid())); | ||
| } | ||
|
|
@@ -529,7 +531,7 @@ public List<ActiveVolumeClient> getActiveClients(String installPath, String prot | |
| ActiveVolumeClient c = new ActiveVolumeClient(); | ||
| if (it.contains("iqn")) { | ||
| c.setQualifiedName(it); | ||
| c.setManagerIp(getHostMnIpFromInitiatorName(it)); | ||
| c.setManagerIp(getHostMnIpFromInitiatorName(it) != null ? getHostMnIpFromInitiatorName(it) : getGatewayMnIpFromInitiatorName(it)); | ||
| } else { | ||
| c.setManagerIp(it); | ||
| } | ||
|
|
@@ -579,12 +581,16 @@ public void deactivate(String installPath, String protocol, HostInventory h, Com | |
| @Override | ||
| public void deactivate(String installPath, String protocol, ActiveVolumeClient client, Completion comp) { | ||
| HostVO host = Q.New(HostVO.class).eq(HostVO_.managementIp, client.getManagerIp()).find(); | ||
| if (host != null) { | ||
| deactivate(installPath, protocol, HostInventory.valueOf(host), comp); | ||
| } else { | ||
| if (host == null) { | ||
| comp.fail(operr("deactivate fail, cannot find host[ip:%s]", client.getManagerIp())); | ||
| return; | ||
| } | ||
| if (host.getHypervisorType().equals("baremetal2")) { | ||
| // bm instance InitiatorName | ||
| deactivateIscsi(installPath, client.getQualifiedName()); | ||
| comp.success(); | ||
| } else { | ||
| deactivate(installPath, protocol, HostInventory.valueOf(host), comp); | ||
| } | ||
|
Comment on lines
583
to
594
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 建议提取魔法值为常量 代码逻辑正确,但仍在多处使用魔法字符串 建议在类顶部定义常量: private static final String HYPERVISOR_TYPE_BAREMETAL2 = "baremetal2";然后在使用处替换: - if (host.getHypervisorType().equals("baremetal2")) {
+ if (HYPERVISOR_TYPE_BAREMETAL2.equals(host.getHypervisorType())) {这样可以提高代码的可维护性,并在多处使用时保持一致性。As per coding guidelines. 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
|
|
@@ -1327,6 +1333,11 @@ public long alignSize(long size) { | |
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public String getVolumeLunId(String volInstallPath) { | ||
| return apiHelper.getVolumeLunDetail(getVolIdFromPath(volInstallPath)).getLunId(); | ||
| } | ||
|
Comment on lines
+1336
to
+1339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少空值检查可能导致 NPE 该方法调用链 @Override
public String getVolumeLunId(String volInstallPath) {
- return apiHelper.getVolumeLunDetail(getVolIdFromPath(volInstallPath)).getLunId();
+ String volId = getVolIdFromPath(volInstallPath);
+ if (volId == null) {
+ return null;
+ }
+ VolumeDetail detail = apiHelper.getVolumeLunDetail(volId);
+ return detail != null ? detail.getLunId() : null;
}🤖 Prompt for AI Agents |
||
|
|
||
| private void retry(Runnable r) { | ||
| retry(r, 3); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ | |
| import org.zstack.header.storage.backup.*; | ||
| import org.zstack.header.tag.SystemTagVO; | ||
| import org.zstack.header.tag.SystemTagVO_; | ||
| import org.zstack.header.vm.VmInstanceVO; | ||
| import org.zstack.header.vm.VmInstanceVO_; | ||
| import org.zstack.storage.backup.BackupStorageSystemTags; | ||
|
|
||
| import java.util.Collections; | ||
|
|
@@ -38,6 +40,27 @@ public static String getHostMnIpFromInitiatorName(String initiatorName) { | |
| return null; | ||
| } | ||
|
|
||
| public static String getGatewayMnIpFromInitiatorName(String initiatorName) { | ||
| String requiredPrefix = "iqn.2015-01.io.zstack:initiator.instance."; | ||
|
|
||
| if (initiatorName == null || initiatorName.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!initiatorName.startsWith(requiredPrefix)) { | ||
| return null; | ||
| } | ||
|
|
||
| String uuid = initiatorName.substring(requiredPrefix.length()); | ||
| String hostUuid = Q.New(VmInstanceVO.class).eq(VmInstanceVO_.uuid, uuid) | ||
| .select(VmInstanceVO_.hostUuid) | ||
| .findValue(); | ||
| return Q.New(HostVO.class).eq(HostVO_.uuid, hostUuid) | ||
| .eq(HostVO_.hypervisorType, "baremetal2") | ||
| .select(HostVO_.managementIp) | ||
| .findValue(); | ||
| } | ||
|
Comment on lines
+43
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 存在空指针风险和魔法值使用问题 该方法存在以下问题:
建议应用以下修复: + private static final String BM2_INITIATOR_IQN_PREFIX = "iqn.2015-01.io.zstack:initiator.instance.";
+ private static final String HYPERVISOR_TYPE_BAREMETAL2 = "baremetal2";
+
public static String getGatewayMnIpFromInitiatorName(String initiatorName) {
- String requiredPrefix = "iqn.2015-01.io.zstack:initiator.instance.";
-
if (initiatorName == null || initiatorName.isEmpty()) {
return null;
}
- if (!initiatorName.startsWith(requiredPrefix)) {
+ if (!initiatorName.startsWith(BM2_INITIATOR_IQN_PREFIX)) {
return null;
}
- String uuid = initiatorName.substring(requiredPrefix.length());
+ String uuid = initiatorName.substring(BM2_INITIATOR_IQN_PREFIX.length());
String hostUuid = Q.New(VmInstanceVO.class).eq(VmInstanceVO_.uuid, uuid)
.select(VmInstanceVO_.hostUuid)
.findValue();
+ if (hostUuid == null) {
+ return null;
+ }
return Q.New(HostVO.class).eq(HostVO_.uuid, hostUuid)
- .eq(HostVO_.hypervisorType, "baremetal2")
+ .eq(HostVO_.hypervisorType, HYPERVISOR_TYPE_BAREMETAL2)
.select(HostVO_.managementIp)
.findValue();
}🤖 Prompt for AI Agents |
||
|
|
||
| private static String getBsMnIp(String bsUuid) { | ||
| CloudBus bus = Platform.getComponentLoader().getComponent(CloudBus.class); | ||
| GetBackupStorageManagerHostnameMsg msg = new GetBackupStorageManagerHostnameMsg(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.