Getting a suitable address to advertise to other sheep is substancially
different functionality from initializing the cluster driver.  Split it
into a separate optional method that falls back to the getifaddrs loop
if not specified.

Signed-off-by: Christoph Hellwig <[email protected]>

diff --git a/sheep/cluster.h b/sheep/cluster.h
index 07e5f7b..c5dcedf 100644
--- a/sheep/cluster.h
+++ b/sheep/cluster.h
@@ -41,11 +41,19 @@ struct cluster_driver {
        /*
         * Initialize the cluster driver
         *
-        * On success, this function returns the file descriptor that
-        * may be used with the poll(2) to monitor cluster events.  On
-        * error, returns -1.
+        * Returns zero on success, -1 on error.
         */
-       int (*init)(const char *option, uint8_t *myaddr);
+       int (*init)(const char *option);
+
+       /*
+        * Get a node ID for this sheep.
+        *
+        * Gets and ID that is used in all communication with other sheep,
+        * which normally would be a string formatted IP address.
+        *
+        * Returns zero on success, -1 on error.
+        */
+       int (*get_local_addr)(uint8_t *myaddr);
 
        /*
         * Join the cluster
diff --git a/sheep/cluster/accord.c b/sheep/cluster/accord.c
index c3e0320..5fdad5f 100644
--- a/sheep/cluster/accord.c
+++ b/sheep/cluster/accord.c
@@ -528,7 +528,7 @@ out:
        pthread_mutex_unlock(&queue_lock);
 }
 
-static int accord_init(const char *option, uint8_t *myaddr)
+static int accord_init(const char *option)
 {
        int ret;
 
@@ -546,9 +546,6 @@ static int accord_init(const char *option, uint8_t *myaddr)
                return -1;
        }
 
-       if (get_local_addr(myaddr) < 0)
-               return -1;
-
        efd = eventfd(0, EFD_NONBLOCK);
        if (efd < 0) {
                eprintf("failed to create an event fd: %m\n");
diff --git a/sheep/cluster/corosync.c b/sheep/cluster/corosync.c
index e6655a0..f477b51 100644
--- a/sheep/cluster/corosync.c
+++ b/sheep/cluster/corosync.c
@@ -121,7 +121,7 @@ static inline void del_cpg_node(struct cpg_node *nodes, 
size_t nr_nodes,
        memmove(nodes + idx, nodes + idx + 1, sizeof(*nodes) * (nr_nodes - 
idx));
 }
 
-static int nodeid_to_addr(uint32_t nodeid, uint8_t *addr)
+static int corosync_get_local_addr(uint8_t *addr)
 {
        int ret, nr;
        corosync_cfg_node_address_t caddr;
@@ -130,7 +130,8 @@ static int nodeid_to_addr(uint32_t nodeid, uint8_t *addr)
        struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)caddr.address;
        void *saddr;
 
-       ret = corosync_cfg_get_node_addrs(cfg_handle, nodeid, 1, &nr, &caddr);
+       ret = corosync_cfg_get_node_addrs(cfg_handle, this_node.nodeid, 1,
+                                         &nr, &caddr);
        if (ret != CS_OK) {
                vprintf(SDOG_ERR, "failed to get node addresses (%d)\n", ret);
                return -1;
@@ -679,7 +680,7 @@ out:
        exit(1);
 }
 
-static int corosync_init(const char *option, uint8_t *myaddr)
+static int corosync_init(const char *option)
 {
        int ret, fd;
        uint32_t nodeid;
@@ -707,12 +708,6 @@ static int corosync_init(const char *option, uint8_t 
*myaddr)
                return -1;
        }
 
-       ret = nodeid_to_addr(nodeid, myaddr);
-       if (ret < 0) {
-               eprintf("failed to get local address\n");
-               return -1;
-       }
-
        this_node.nodeid = nodeid;
        this_node.pid = getpid();
 
@@ -733,14 +728,15 @@ static int corosync_init(const char *option, uint8_t 
*myaddr)
 }
 
 struct cluster_driver cdrv_corosync = {
-       .name       = "corosync",
-
-       .init       = corosync_init,
-       .join       = corosync_join,
-       .leave      = corosync_leave,
-       .notify     = corosync_notify,
-       .block      = corosync_block,
-       .unblock    = corosync_unblock,
+       .name           = "corosync",
+
+       .init           = corosync_init,
+       .get_local_addr = corosync_get_local_addr,
+       .join           = corosync_join,
+       .leave          = corosync_leave,
+       .notify         = corosync_notify,
+       .block          = corosync_block,
+       .unblock        = corosync_unblock,
 };
 
 cdrv_register(cdrv_corosync);
diff --git a/sheep/cluster/local.c b/sheep/cluster/local.c
index 4f2d8e8..d60f26b 100644
--- a/sheep/cluster/local.c
+++ b/sheep/cluster/local.c
@@ -420,7 +420,16 @@ out:
        shm_queue_unlock();
 }
 
-static int local_init(const char *option, uint8_t *myaddr)
+static int local_get_local_addr(uint8_t *myaddr)
+{
+       /* set 127.0.0.1 */
+       memset(myaddr, 0, 16);
+       myaddr[12] = 127;
+       myaddr[15] = 1;
+       return 0;
+}
+
+static int local_init(const char *option)
 {
        sigset_t mask;
        int ret;
@@ -432,11 +441,6 @@ static int local_init(const char *option, uint8_t *myaddr)
        if (option)
                shmfile = option;
 
-       /* set 127.0.0.1 */
-       memset(myaddr, 0, 16);
-       myaddr[12] = 127;
-       myaddr[15] = 1;
-
        shm_queue_init();
 
        sigemptyset(&mask);
@@ -461,14 +465,15 @@ static int local_init(const char *option, uint8_t *myaddr)
 }
 
 struct cluster_driver cdrv_local = {
-       .name       = "local",
-
-       .init       = local_init,
-       .join       = local_join,
-       .leave      = local_leave,
-       .notify     = local_notify,
-       .block      = local_block,
-       .unblock    = local_unblock,
+       .name           = "local",
+
+       .init           = local_init,
+       .get_local_addr = local_get_local_addr,
+       .join           = local_join,
+       .leave          = local_leave,
+       .notify         = local_notify,
+       .block          = local_block,
+       .unblock        = local_unblock,
 };
 
 cdrv_register(cdrv_local);
diff --git a/sheep/cluster/zookeeper.c b/sheep/cluster/zookeeper.c
index 8499cb4..273b34d 100644
--- a/sheep/cluster/zookeeper.c
+++ b/sheep/cluster/zookeeper.c
@@ -786,7 +786,7 @@ out:
        return;
 }
 
-static int zk_init(const char *option, uint8_t *myaddr)
+static int zk_init(const char *option)
 {
        int ret;
 
@@ -807,9 +807,6 @@ static int zk_init(const char *option, uint8_t *myaddr)
                "negotiated session timeout:%dms\n",
                SESSION_TIMEOUT, zoo_recv_timeout(zhandle));
 
-       if (get_local_addr(myaddr) < 0)
-               return -1;
-
        zk_queue_init(zhandle);
 
        efd = eventfd(0, EFD_NONBLOCK);
diff --git a/sheep/group.c b/sheep/group.c
index ebfa12e..71c64ca 100644
--- a/sheep/group.c
+++ b/sheep/group.c
@@ -1046,7 +1046,14 @@ int create_cluster(int port, int64_t zone, int nr_vnodes)
                }
        }
 
-       ret = sys->cdrv->init(sys->cdrv_option, sys->this_node.addr);
+       ret = sys->cdrv->init(sys->cdrv_option);
+       if (ret < 0)
+               return -1;
+       
+       if (sys->cdrv->get_local_addr)
+               ret = sys->cdrv->get_local_addr(sys->this_node.addr);
+       else
+               ret = get_local_addr(sys->this_node.addr);
        if (ret < 0)
                return -1;
 
-- 
sheepdog mailing list
[email protected]
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to