Repository navigation
Feature/cstackex 350: Iscsicleanup monitor fix for not affecting luns which are provisioned other than by-path #141
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: main
Are you sure you want to change the base?
Changes from all commits
df1753e
36dd1d4
7da8828
dee1a0b
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 |
|---|---|---|
|
|
@@ -27,19 +27,26 @@ | |
| import org.libvirt.LibvirtException; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| public class IscsiStorageCleanupMonitor implements Runnable{ | ||
| protected Logger logger = LogManager.getLogger(getClass()); | ||
| protected static final Logger LOGGER = LogManager.getLogger(IscsiStorageCleanupMonitor.class); | ||
| protected Logger logger = LOGGER; | ||
| private static final int CLEANUP_INTERVAL_SEC = 60; // check every X seconds | ||
| private static final String ISCSI_PATH_PREFIX = "/dev/disk/by-path"; | ||
| private static final String KEYWORD_ISCSI = "iscsi"; | ||
| private static final String KEYWORD_IQN = "iqn"; | ||
| private static final String REGEX_PART = "\\S+part\\d+$"; | ||
| static final String ISCSI_PATH_PREFIX = "/dev/disk/by-path"; | ||
| static final String KEYWORD_ISCSI = "iscsi"; | ||
| static final String KEYWORD_IQN = "iqn"; | ||
| static final String REGEX_PART = "\\S+part\\d+$"; | ||
| static final String DEV_PREFIX = "/dev"; | ||
| static final String LUN_MARKER = "-lun-"; | ||
|
|
||
| private IscsiAdmStorageAdaptor iscsiStorageAdaptor; | ||
|
|
||
|
|
@@ -51,6 +58,68 @@ public IscsiStorageCleanupMonitor() { | |
| iscsiStorageAdaptor = new IscsiAdmStorageAdaptor(); | ||
| } | ||
|
|
||
| /** | ||
| * True if {@code path} is an iSCSI by-path symlink for a whole LUN (not a partition entry). | ||
| * udev creates a sibling {@code -partN} by-path entry for every partition on a LUN; because | ||
| * {@code iscsiadm --logout} is session-scoped, acting on a partition entry would tear down | ||
| * the whole LUN's session. The monitor must operate only on whole-disk entries. | ||
| * | ||
| * Visible for testing. | ||
| */ | ||
| static boolean isIscsiDisk(String path) { | ||
| return path != null | ||
| && path.startsWith(ISCSI_PATH_PREFIX) | ||
| && path.contains(KEYWORD_ISCSI) | ||
| && path.contains(KEYWORD_IQN) | ||
| && !path.matches(REGEX_PART); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a {@code /dev/disk/by-*} symlink (or any path under {@code /dev}) to its | ||
| * underlying kernel device absolute path (e.g. {@code /dev/sdb}). Returns {@code null} | ||
| * when the input is null, the symlink is dangling, the file is missing, or the real | ||
| * path is not under {@code /dev} (which filters out file-backed VM disks). | ||
| * | ||
| * Visible for testing. | ||
| */ | ||
| static String resolveToKernelDevice(String path) { | ||
| if (path == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| Path real = Paths.get(path).toRealPath(); | ||
| if (real.startsWith(DEV_PREFIX)) { | ||
| return real.toString(); | ||
| } | ||
| } catch (IOException e) { | ||
| LOGGER.debug("Could not resolve [{}] to a kernel device: {}", path, e.getMessage()); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the target identifier shared by all LUNs of a by-path iSCSI entry — everything up | ||
| * to the {@code -lun-} suffix (portal + target IQN). Returns {@code null} when {@code path} | ||
| * is not an iSCSI by-path for a specific LUN. | ||
| * | ||
| * <p>{@code iscsiadm --logout} is session-scoped (keyed on target IQN + portal), so logging | ||
| * out any one by-path tears down the session for every LUN on the same target. On shared- | ||
| * target backends (e.g. ONTAP SVMs, where many LUNs map to one IQN) the monitor must | ||
| * preserve a session whenever another LUN on the same target is still in use by a VM. | ||
| * | ||
| * <p>Visible for testing. | ||
| */ | ||
| static String extractTargetKey(String path) { | ||
| if (path == null) { | ||
| return null; | ||
| } | ||
| int idx = path.lastIndexOf(LUN_MARKER); | ||
| if (idx < 0) { | ||
| return null; | ||
| } | ||
| return path.substring(0, idx); | ||
| } | ||
|
|
||
|
|
||
| private class Monitor extends ManagedContextRunnable { | ||
|
|
||
|
|
@@ -81,18 +150,14 @@ protected void runInContext() { | |
| } | ||
| } | ||
|
|
||
| private boolean isIscsiDisk(String path) { | ||
| return path.startsWith(ISCSI_PATH_PREFIX) && path.contains(KEYWORD_ISCSI) && path.contains(KEYWORD_IQN); | ||
| } | ||
|
|
||
| /** | ||
| * for each volume if the volume is path is of type iscsi, add to diskstatusmap and set status to false. | ||
| * @param iscsiVolumes | ||
| */ | ||
| private void initializeDiskStatusMap(File[] iscsiVolumes){ | ||
| diskStatusMap.clear(); | ||
| for( File v : iscsiVolumes) { | ||
| if (isIscsiDisk(v.getAbsolutePath())) { | ||
| if (IscsiStorageCleanupMonitor.isIscsiDisk(v.getAbsolutePath())) { | ||
| logger.debug("found iscsi disk by cleanup thread, marking inactive: " + v.getAbsolutePath()); | ||
| diskStatusMap.put(v.getAbsolutePath(), false); | ||
| } | ||
|
|
@@ -104,6 +169,21 @@ private void initializeDiskStatusMap(File[] iscsiVolumes){ | |
| * @param conn | ||
| */ | ||
| private void updateDiskStatusMapWithInactiveIscsiSessions(Connect conn){ | ||
| // VM XML and diskStatusMap keys can reference the same iSCSI LUN through | ||
|
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. we need to watch for performance for this method in scale scenarios. For now lets have logger to trace the time taken in longer runs.
Author
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. This was an existing change. |
||
| // different udev symlinks: by-path (ip-...-iscsi-...-lun-N) vs by-id | ||
|
piyush5netapp marked this conversation as resolved.
Resolved
|
||
| // (wwn-0x... for storage providers that identify LUNs by NAA WWID, such as | ||
| // ONTAP). A raw string compare misses that match and the session gets | ||
| // disconnected while VMs are still using it. Resolve both sides to the | ||
| // underlying kernel block device (e.g. /dev/sdb) so the match works for | ||
| // any symlink form. | ||
| Map<String, String> kernelDeviceToByPath = new HashMap<>(); | ||
| for (String byPath : diskStatusMap.keySet()) { | ||
| String kernelDevice = IscsiStorageCleanupMonitor.resolveToKernelDevice(byPath); | ||
| if (kernelDevice != null) { | ||
| kernelDeviceToByPath.put(kernelDevice, byPath); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| int[] domains = conn.listDomains(); | ||
| logger.debug(String.format("found %d domains", domains.length)); | ||
|
|
@@ -116,9 +196,18 @@ private void updateDiskStatusMapWithInactiveIscsiSessions(Connect conn){ | |
|
|
||
| //check the volume map. If an entry exists change the status to True | ||
| for (final LibvirtVMDef.DiskDef disk : disks) { | ||
| if (diskStatusMap.containsKey(disk.getDiskPath())&&!disk.getDiskPath().matches(REGEX_PART)) { | ||
| diskStatusMap.put(disk.getDiskPath(), true); | ||
| logger.debug("active disk found by cleanup thread" + disk.getDiskPath()); | ||
| final String diskPath = disk.getDiskPath(); | ||
| if (diskPath == null || diskPath.matches(REGEX_PART)) { | ||
| continue; | ||
| } | ||
| String kernelDevice = IscsiStorageCleanupMonitor.resolveToKernelDevice(diskPath); | ||
| if (kernelDevice == null) { | ||
| continue; | ||
| } | ||
| String byPath = kernelDeviceToByPath.get(kernelDevice); | ||
| if (byPath != null) { | ||
| diskStatusMap.put(byPath, true); | ||
| logger.debug("active disk found by cleanup thread " + diskPath + " -> " + byPath); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -135,12 +224,33 @@ private void updateDiskStatusMapWithInactiveIscsiSessions(Connect conn){ | |
|
|
||
| * XXX: It is possible that someone had manually added an iSCSI volume. | ||
| * we would not be able to detect that | ||
| * | ||
| * {@code iscsiadm --logout} is target-scoped. On shared-target backends (e.g. ONTAP SVMs | ||
| * that expose many LUNs under one IQN) logging out one inactive LUN would also kill the | ||
| * session for sibling LUNs still in use by other VMs on this host. Before disconnecting | ||
| * an inactive by-path entry, confirm no active LUN remains on the same target. | ||
| */ | ||
| private void disconnectInactiveSessions(){ | ||
| Set<String> activeTargets = new HashSet<>(); | ||
| for (Map.Entry<String, Boolean> e : diskStatusMap.entrySet()) { | ||
| if (Boolean.TRUE.equals(e.getValue())) { | ||
| String targetKey = IscsiStorageCleanupMonitor.extractTargetKey(e.getKey()); | ||
| if (targetKey != null) { | ||
| activeTargets.add(targetKey); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (String diskPath : diskStatusMap.keySet()) { | ||
| if (!diskStatusMap.get(diskPath)) { | ||
| if (Files.exists(Paths.get(diskPath))) { | ||
| String targetKey = IscsiStorageCleanupMonitor.extractTargetKey(diskPath); | ||
| if (targetKey != null && activeTargets.contains(targetKey)) { | ||
| logger.debug("Preserving iSCSI session for inactive LUN [{}]: " + | ||
| "sibling LUN on target [{}] is still in use by a VM on this host", | ||
| diskPath, targetKey); | ||
| continue; | ||
| } | ||
| try { | ||
| logger.info("Cleaning up disk " + diskPath); | ||
| iscsiStorageAdaptor.disconnectPhysicalDiskByPath(diskPath); | ||
|
|
||
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.
what is the need of making it static with private?
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.
to make it Covered by UT.