6ae9ed
From 1b24ff19c5af366d8cac25c4cfecd45e77d1c239 Mon Sep 17 00:00:00 2001
6ae9ed
Message-Id: <1b24ff19c5af366d8cac25c4cfecd45e77d1c239@dist-git>
6ae9ed
From: Peter Krempa <pkrempa@redhat.com>
6ae9ed
Date: Tue, 2 Aug 2016 13:42:01 +0200
6ae9ed
Subject: [PATCH] storage: gluster: Support multiple hosts in backend functions
6ae9ed
6ae9ed
As gluster natively supports multiple hosts for failover reasons we can
6ae9ed
easily add the support to the storage driver code in libvirt.
6ae9ed
6ae9ed
Extract the code setting an individual host into a separate function and
6ae9ed
call them in a loop. The new code also tries to keep the debug log
6ae9ed
entries sane.
6ae9ed
6ae9ed
(cherry picked from commit f1bbc7df4a9959e09679486c769e12f82d443d9a)
6ae9ed
https://bugzilla.redhat.com/show_bug.cgi?id=1247521 [gluster multi-host]
6ae9ed
---
6ae9ed
 src/storage/storage_backend_gluster.c | 82 ++++++++++++++++++++++-------------
6ae9ed
 1 file changed, 51 insertions(+), 31 deletions(-)
6ae9ed
6ae9ed
diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_backend_gluster.c
6ae9ed
index eda060d..5bcbef4 100644
6ae9ed
--- a/src/storage/storage_backend_gluster.c
6ae9ed
+++ b/src/storage/storage_backend_gluster.c
6ae9ed
@@ -570,25 +570,55 @@ virStorageFileBackendGlusterDeinit(virStorageSourcePtr src)
6ae9ed
 }
6ae9ed
 
6ae9ed
 static int
6ae9ed
-virStorageFileBackendGlusterInit(virStorageSourcePtr src)
6ae9ed
+virStorageFileBackendGlusterInitServer(virStorageFileBackendGlusterPrivPtr priv,
6ae9ed
+                                       virStorageNetHostDefPtr host)
6ae9ed
 {
6ae9ed
-    virStorageFileBackendGlusterPrivPtr priv = NULL;
6ae9ed
-    virStorageNetHostDefPtr host = &(src->hosts[0]);
6ae9ed
-    const char *hostname;
6ae9ed
+    const char *transport = virStorageNetHostTransportTypeToString(host->transport);
6ae9ed
+    const char *hoststr = NULL;
6ae9ed
     int port = 0;
6ae9ed
 
6ae9ed
-    if (src->nhosts != 1) {
6ae9ed
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
6ae9ed
-                       _("Expected exactly 1 host for the gluster volume"));
6ae9ed
+    switch ((virStorageNetHostTransport) host->transport) {
6ae9ed
+    case VIR_STORAGE_NET_HOST_TRANS_RDMA:
6ae9ed
+    case VIR_STORAGE_NET_HOST_TRANS_TCP:
6ae9ed
+        hoststr = host->name;
6ae9ed
+
6ae9ed
+        if (host->port &&
6ae9ed
+            virStrToLong_i(host->port, NULL, 10, &port) < 0) {
6ae9ed
+            virReportError(VIR_ERR_INTERNAL_ERROR,
6ae9ed
+                           _("failed to parse port number '%s'"),
6ae9ed
+                           host->port);
6ae9ed
+            return -1;
6ae9ed
+        }
6ae9ed
+
6ae9ed
+        break;
6ae9ed
+
6ae9ed
+    case VIR_STORAGE_NET_HOST_TRANS_UNIX:
6ae9ed
+        hoststr = host->socket;
6ae9ed
+        break;
6ae9ed
+
6ae9ed
+    case VIR_STORAGE_NET_HOST_TRANS_LAST:
6ae9ed
+        break;
6ae9ed
+    }
6ae9ed
+
6ae9ed
+    VIR_DEBUG("adding gluster host for %p: transport=%s host=%s port=%d",
6ae9ed
+              priv, transport, hoststr, port);
6ae9ed
+
6ae9ed
+    if (glfs_set_volfile_server(priv->vol, transport, hoststr, port) < 0) {
6ae9ed
+        virReportSystemError(errno,
6ae9ed
+                             _("failed to set gluster volfile server '%s'"),
6ae9ed
+                             hoststr);
6ae9ed
         return -1;
6ae9ed
     }
6ae9ed
 
6ae9ed
-    hostname = host->name;
6ae9ed
+    return 0;
6ae9ed
+}
6ae9ed
 
6ae9ed
-    VIR_DEBUG("initializing gluster storage file %p (gluster://%s:%s/%s%s)[%u:%u]",
6ae9ed
-              src, hostname, host->port ? host->port : "0",
6ae9ed
-              NULLSTR(src->volume), src->path,
6ae9ed
-              (unsigned int)src->drv->uid, (unsigned int)src->drv->gid);
6ae9ed
+
6ae9ed
+static int
6ae9ed
+virStorageFileBackendGlusterInit(virStorageSourcePtr src)
6ae9ed
+{
6ae9ed
+    virStorageFileBackendGlusterPrivPtr priv = NULL;
6ae9ed
+    size_t i;
6ae9ed
 
6ae9ed
     if (!src->volume) {
6ae9ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
6ae9ed
@@ -600,35 +630,25 @@ virStorageFileBackendGlusterInit(virStorageSourcePtr src)
6ae9ed
     if (VIR_ALLOC(priv) < 0)
6ae9ed
         return -1;
6ae9ed
 
6ae9ed
-    if (host->port &&
6ae9ed
-        virStrToLong_i(host->port, NULL, 10, &port) < 0) {
6ae9ed
-        virReportError(VIR_ERR_INTERNAL_ERROR,
6ae9ed
-                       _("failed to parse port number '%s'"),
6ae9ed
-                       host->port);
6ae9ed
-        goto error;
6ae9ed
-    }
6ae9ed
-
6ae9ed
-    if (host->transport == VIR_STORAGE_NET_HOST_TRANS_UNIX)
6ae9ed
-        hostname = host->socket;
6ae9ed
+    VIR_DEBUG("initializing gluster storage file %p "
6ae9ed
+              "(priv='%p' volume='%s' path='%s') as [%u:%u]",
6ae9ed
+              src, priv, src->volume, src->path,
6ae9ed
+              (unsigned int)src->drv->uid, (unsigned int)src->drv->gid);
6ae9ed
 
6ae9ed
     if (!(priv->vol = glfs_new(src->volume))) {
6ae9ed
         virReportOOMError();
6ae9ed
         goto error;
6ae9ed
     }
6ae9ed
 
6ae9ed
-    if (glfs_set_volfile_server(priv->vol,
6ae9ed
-                                virStorageNetHostTransportTypeToString(host->transport),
6ae9ed
-                                hostname, port) < 0) {
6ae9ed
-        virReportSystemError(errno,
6ae9ed
-                             _("failed to set gluster volfile server '%s'"),
6ae9ed
-                             hostname);
6ae9ed
-        goto error;
6ae9ed
+    for (i = 0; i < src->nhosts; i++) {
6ae9ed
+        if (virStorageFileBackendGlusterInitServer(priv, src->hosts + i) < 0)
6ae9ed
+            goto error;
6ae9ed
     }
6ae9ed
 
6ae9ed
     if (glfs_init(priv->vol) < 0) {
6ae9ed
         virReportSystemError(errno,
6ae9ed
-                             _("failed to initialize gluster connection to "
6ae9ed
-                               "server: '%s'"), hostname);
6ae9ed
+                             _("failed to initialize gluster connection "
6ae9ed
+                               "(src=%p priv=%p)"), src, priv);
6ae9ed
         goto error;
6ae9ed
     }
6ae9ed
 
6ae9ed
-- 
6ae9ed
2.9.2
6ae9ed