rg9975 commented on code in PR #7889:
URL: https://github.com/apache/cloudstack/pull/7889#discussion_r1353000509


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/MultipathSCSIAdapterBase.java:
##########
@@ -0,0 +1,616 @@
+// 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 com.cloud.hypervisor.kvm.storage;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Timer;
+import java.util.TimerTask;
+//import java.util.TimerTask;
+import java.util.UUID;
+
+import org.apache.cloudstack.utils.qemu.QemuImg;
+import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
+import org.apache.cloudstack.utils.qemu.QemuImgException;
+import org.apache.cloudstack.utils.qemu.QemuImgFile;
+import org.apache.log4j.Logger;
+
+import com.cloud.storage.Storage;
+import com.cloud.storage.StorageManager;
+import com.cloud.utils.PropertiesUtil;
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.cloud.utils.script.OutputInterpreter;
+import com.cloud.utils.script.Script;
+import org.apache.commons.lang3.StringUtils;
+import org.libvirt.LibvirtException;
+
+public abstract class MultipathSCSIAdapterBase implements StorageAdaptor {
+    static final Logger LOGGER = 
Logger.getLogger(MultipathSCSIAdapterBase.class);
+    static final Map<String, KVMStoragePool> MapStorageUuidToStoragePool = new 
HashMap<>();
+
+    /**
+     * A lock to avoid any possiblity of multiple requests for a scan
+     */
+    static byte[] CLEANUP_LOCK = new byte[0];
+
+    /**
+     * Property keys and defaults
+     */
+    static final Property<Integer> CLEANUP_FREQUENCY_SECS = new 
Property<Integer>("multimap.cleanup.frequency.secs", 60);
+    static final Property<Integer> CLEANUP_TIMEOUT_SECS = new 
Property<Integer>("multimap.cleanup.timeout.secs", 4);
+    static final Property<Boolean> CLEANUP_ENABLED = new 
Property<Boolean>("multimap.cleanup.enabled", true);
+    static final Property<String> CLEANUP_SCRIPT = new 
Property<String>("multimap.cleanup.script", "cleanStaleMaps.sh");
+    static final Property<String> CONNECT_SCRIPT = new 
Property<String>("multimap.connect.script", "connectVolume.sh");
+    static final Property<String> DISCONNECT_SCRIPT = new 
Property<String>("multimap.disconnect.script", "disconnectVolume.sh");
+    static final Property<Integer> DISK_WAIT_SECS = new 
Property<Integer>("multimap.disk.wait.secs", 240);
+    static final Property<String> STORAGE_SCRIPTS_DIR = new 
Property<String>("multimap.storage.scripts.dir", "scripts/storage/multipath");
+
+    static Timer cleanupTimer = new Timer();
+    private static int cleanupTimeoutSecs = 
CLEANUP_TIMEOUT_SECS.getFinalValue();
+    private static String connectScript = CONNECT_SCRIPT.getFinalValue();
+    private static String disconnectScript = DISCONNECT_SCRIPT.getFinalValue();
+    private static String cleanupScript = CLEANUP_SCRIPT.getFinalValue();
+    private static int diskWaitTimeSecs = DISK_WAIT_SECS.getFinalValue();
+
+    /**
+     * Initialize static program-wide configurations and background jobs
+     */
+    static {
+        long cleanupFrequency = CLEANUP_FREQUENCY_SECS.getFinalValue() * 1000;
+        boolean cleanupEnabled = CLEANUP_ENABLED.getFinalValue();
+
+
+        connectScript = Script.findScript(STORAGE_SCRIPTS_DIR.getFinalValue(), 
connectScript);
+        if (connectScript == null) {
+            throw new Error("Unable to find the connectVolume.sh script");
+        }
+
+        disconnectScript = 
Script.findScript(STORAGE_SCRIPTS_DIR.getFinalValue(), disconnectScript);
+        if (disconnectScript == null) {
+            throw new Error("Unable to find the disconnectVolume.sh script");
+        }
+
+        if (cleanupEnabled) {
+            cleanupScript = 
Script.findScript(STORAGE_SCRIPTS_DIR.getFinalValue(), cleanupScript);
+            if (cleanupScript == null) {
+                throw new Error("Unable to find the cleanStaleMaps.sh script 
and " + CLEANUP_ENABLED.getName() + " is true");
+            }
+
+            TimerTask task = new TimerTask() {
+                @Override
+                public void run() {
+                    try {
+                        MultipathSCSIAdapterBase.cleanupStaleMaps();
+                    } catch (Throwable e) {
+                        LOGGER.warn("Error running stale multipath map 
cleanup", e);
+                    }
+                }
+            };
+
+            cleanupTimer = new Timer("MultipathMapCleanupJob");
+            cleanupTimer.scheduleAtFixedRate(task, 0, cleanupFrequency);
+        }
+    }
+
+    @Override
+    public KVMStoragePool getStoragePool(String uuid, boolean refreshInfo) {
+        return getStoragePool(uuid);
+    }
+
+    public abstract String getName();
+
+    public abstract boolean isStoragePoolTypeSupported(Storage.StoragePoolType 
type);
+
+    public abstract AddressInfo parseAndValidatePath(String path);
+
+    @Override
+    public KVMPhysicalDisk getPhysicalDisk(String volumePath, KVMStoragePool 
pool) {
+        LOGGER.debug(String.format("getPhysicalDisk(volumePath,pool) called 
with args (%s,%s)", volumePath, pool));
+
+        if (StringUtils.isEmpty(volumePath) || pool == null) {
+            LOGGER.error("Unable to get physical disk, volume path or pool not 
specified");
+            return null;
+        }
+
+        // we expect WWN values in the volumePath so need to convert it to an 
actual physical path
+        AddressInfo address = parseAndValidatePath(volumePath);
+        return getPhysicalDisk(address, pool);
+    }
+
+    private KVMPhysicalDisk getPhysicalDisk(AddressInfo address, 
KVMStoragePool pool) {
+        LOGGER.debug(String.format("getPhysicalDisk(addressInfo,pool) called 
with args (%s,%s)", address.getPath(), pool));
+        KVMPhysicalDisk disk = new KVMPhysicalDisk(address.getPath(), 
address.toString(), pool);
+        disk.setFormat(QemuImg.PhysicalDiskFormat.RAW);
+
+        long diskSize = getPhysicalDiskSize(address.getPath());
+        disk.setSize(diskSize);
+        disk.setVirtualSize(diskSize);
+        LOGGER.debug("Physical disk " + disk.getPath() + " with format " + 
disk.getFormat() + " and size " + disk.getSize() + " provided");
+        return disk;
+    }
+
+    @Override
+    public KVMStoragePool createStoragePool(String uuid, String host, int 
port, String path, String userInfo, Storage.StoragePoolType type, Map<String, 
String> details) {
+        LOGGER.info(String.format("createStoragePool(uuid,host,port,path,type) 
called with args (%s, %s, %s, %s, %s)", uuid, host, ""+port, path, type));
+        MultipathSCSIPool storagePool = new MultipathSCSIPool(uuid, host, 
port, path, type, details, this);
+        MapStorageUuidToStoragePool.put(uuid, storagePool);
+        return storagePool;
+    }
+
+    @Override
+    public boolean deleteStoragePool(String uuid) {
+        return MapStorageUuidToStoragePool.remove(uuid) != null;
+    }
+
+   @Override
+    public boolean connectPhysicalDisk(String volumePath, KVMStoragePool pool, 
Map<String, String> details) {
+        LOGGER.info("connectPhysicalDisk called for [" + volumePath + "]");
+
+        if (StringUtils.isEmpty(volumePath)) {
+            LOGGER.error("Unable to connect physical disk due to insufficient 
data - volume path is undefined");
+            throw new CloudRuntimeException("Unable to connect physical disk 
due to insufficient data - volume path is underfined");
+        }
+
+        if (pool == null) {
+            LOGGER.error("Unable to connect physical disk due to insufficient 
data - pool is not set");
+            throw new CloudRuntimeException("Unable to connect physical disk 
due to insufficient data - pool is not set");
+        }
+
+        // we expect WWN values in the volumePath so need to convert it to an 
actual physical path
+        AddressInfo address = this.parseAndValidatePath(volumePath);
+        int waitTimeInSec = diskWaitTimeSecs;
+        if (details != null && 
details.containsKey(StorageManager.STORAGE_POOL_DISK_WAIT.toString())) {
+            String waitTime = 
details.get(StorageManager.STORAGE_POOL_DISK_WAIT.toString());
+            if (StringUtils.isNotEmpty(waitTime)) {
+                waitTimeInSec = Integer.valueOf(waitTime).intValue();
+            }
+        }
+        return waitForDiskToBecomeAvailable(address, pool, waitTimeInSec);
+    }
+
+    @Override
+    public boolean disconnectPhysicalDisk(String volumePath, KVMStoragePool 
pool) {
+        
LOGGER.debug(String.format("disconnectPhysicalDiskByPath(volumePath,pool) 
called with args (%s, %s) START", volumePath, pool.getUuid()));
+        AddressInfo address = this.parseAndValidatePath(volumePath);
+        String output = Script.runSimpleBashScript(String.format("%s %s", 
disconnectScript, address.getAddress().toLowerCase()), 60000);
+        if (LOGGER.isDebugEnabled()) LOGGER.debug("multipath flush output: " + 
output);
+        
LOGGER.debug(String.format("disconnectPhysicalDiskByPath(volumePath,pool) 
called with args (%s, %s) COMPLETE", volumePath, pool.getUuid()));
+        return true;
+    }
+
+    @Override
+    public boolean disconnectPhysicalDisk(Map<String, String> 
volumeToDisconnect) {
+        
LOGGER.debug(String.format("disconnectPhysicalDiskByPath(volumeToDisconnect) 
called with arg bag [not implemented]:") + " " + volumeToDisconnect);
+        return false;
+    }
+
+    @Override
+    public boolean disconnectPhysicalDiskByPath(String localPath) {
+        LOGGER.debug(String.format("disconnectPhysicalDiskByPath(localPath) 
called with args (%s) STARTED", localPath));
+        String output = Script.runSimpleBashScript(String.format("%s %s", 
disconnectScript, localPath.replace("/dev/mapper/3", "")), 60000);
+        if (LOGGER.isDebugEnabled()) LOGGER.debug("multipath flush output: " + 
output);
+        LOGGER.debug(String.format("disconnectPhysicalDiskByPath(localPath) 
called with args (%s) COMPLETE", localPath));
+        return true;
+    }
+
+    @Override
+    public boolean deletePhysicalDisk(String uuid, KVMStoragePool pool, 
Storage.ImageFormat format) {
+        LOGGER.info(String.format("deletePhysicalDisk(uuid,pool,format) called 
with args (%s, %s, %s) [not implemented]", uuid, pool.getUuid(), 
format.toString()));
+        return true;
+    }
+
+    @Override
+    public KVMPhysicalDisk createTemplateFromDisk(KVMPhysicalDisk disk, String 
name, QemuImg.PhysicalDiskFormat format, long size, KVMStoragePool destPool) {
+        
LOGGER.info(String.format("createTemplateFromDisk(disk,name,format,size,destPool)
 called with args (%s, %s, %s, %s, %s) [not implemented]", disk.getPath(), 
name, format.toString(), ""+size, destPool.getUuid()));
+        return null;
+    }
+
+    @Override
+    public List<KVMPhysicalDisk> listPhysicalDisks(String storagePoolUuid, 
KVMStoragePool pool) {
+        LOGGER.info(String.format("listPhysicalDisks(uuid,pool) called with 
args (%s, %s) [not implemented]", storagePoolUuid, pool.getUuid()));
+        return null;
+    }
+
+    @Override
+    public KVMPhysicalDisk copyPhysicalDisk(KVMPhysicalDisk disk, String name, 
KVMStoragePool destPool, int timeout) {
+        return copyPhysicalDisk(disk, name, destPool, timeout, null, null, 
null);
+    }
+
+    @Override
+    public boolean refresh(KVMStoragePool pool) {
+        LOGGER.info(String.format("refresh(pool) called with args (%s)", 
pool.getUuid()));
+        return true;
+    }
+
+    @Override
+    public boolean deleteStoragePool(KVMStoragePool pool) {
+        LOGGER.info(String.format("deleteStroagePool(pool) called with args 
(%s)", pool.getUuid()));
+        return deleteStoragePool(pool.getUuid());
+    }
+
+    @Override
+    public boolean createFolder(String uuid, String path) {
+        LOGGER.info(String.format("createFolder(uuid,path) called with args 
(%s, %s) [not implemented]", uuid, path));
+        return createFolder(uuid, path, null);
+    }
+
+    @Override
+    public boolean createFolder(String uuid, String path, String localPath) {
+        LOGGER.info(String.format("createFolder(uuid,path,localPath) called 
with args (%s, %s, %s) [not implemented]", uuid, path, localPath));
+        return true;
+    }
+
+
+    @Override
+    public KVMPhysicalDisk createTemplateFromDirectDownloadFile(String 
templateFilePath, String destTemplatePath, KVMStoragePool destPool, 
Storage.ImageFormat format, int timeout) {

Review Comment:
   Updated in upcoming commit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to