<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<br>
<br>
<div class="gmail_quote">On Fri, Sep 30, 2011 at 4:28 AM, MORITA Kazutaka <span dir="ltr">
<<a href="mailto:morita.kazutaka@lab.ntt.co.jp">morita.kazutaka@lab.ntt.co.jp</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
Signed-off-by: MORITA Kazutaka <<a href="mailto:morita.kazutaka@lab.ntt.co.jp">morita.kazutaka@lab.ntt.co.jp</a>><br>
---<br>
sheep/Makefile.am | 2 +-<br>
sheep/cluster/corosync.c | 260 ++++++++++++++++++++++++++++++++++++++++++++++<br>
sheep/group.c | 11 ++<br>
3 files changed, 272 insertions(+), 1 deletions(-)<br>
create mode 100644 sheep/cluster/corosync.c<br>
<br>
diff --git a/sheep/Makefile.am b/sheep/Makefile.am<br>
index 2f51702..ad92ea0 100644<br>
--- a/sheep/Makefile.am<br>
+++ b/sheep/Makefile.am<br>
@@ -23,7 +23,7 @@ INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include $(libcpg_CFLAGS) $<br>
<br>
sbin_PROGRAMS = sheep<br>
<br>
-sheep_SOURCES = sheep.c group.c sdnet.c store.c vdi.c work.c<br>
+sheep_SOURCES = sheep.c group.c sdnet.c store.c vdi.c work.c cluster/corosync.c<br>
sheep_LDADD = $(libcpg_LIBS) $(libcfg_LIBS) ../lib/libsheepdog.a -lpthread<br>
sheep_DEPENDENCIES = ../lib/libsheepdog.a<br>
<br>
diff --git a/sheep/cluster/corosync.c b/sheep/cluster/corosync.c<br>
new file mode 100644<br>
index 0000000..735bee7<br>
--- /dev/null<br>
+++ b/sheep/cluster/corosync.c<br>
@@ -0,0 +1,260 @@<br>
+/*<br>
+ * Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.<br>
+ *<br>
+ * This program is free software; you can redistribute it and/or<br>
+ * modify it under the terms of the GNU General Public License version<br>
+ * 2 as published by the Free Software Foundation.<br>
+ *<br>
+ * You should have received a copy of the GNU General Public License<br>
+ * along with this program. If not, see <<a href="http://www.gnu.org/licenses/" target="_blank">http://www.gnu.org/licenses/</a>>.<br>
+ */<br>
+#include <stdio.h><br>
+#include <unistd.h><br>
+#include <corosync/cpg.h><br>
+#include <corosync/cfg.h><br>
+<br>
+#include "cluster.h"<br>
+<br>
+static cpg_handle_t cpg_handle;<br>
+static struct cpg_name cpg_group = { 9, "sheepdog" };<br>
+<br>
+static corosync_cfg_handle_t cfg_handle;<br>
+<br>
+static struct cdrv_handlers corosync_handlers;<br>
+<br>
+static int nodeid_to_addr(uint32_t nodeid, uint8_t *addr)<br>
+{<br>
+ int ret, nr;<br>
+ corosync_cfg_node_address_t caddr;<br>
+ struct sockaddr_storage *ss = (struct sockaddr_storage *)caddr.address;<br>
+ struct sockaddr_in *sin = (struct sockaddr_in *)caddr.address;<br>
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)caddr.address;<br>
+ void *saddr;<br>
+<br>
+ ret = corosync_cfg_get_node_addrs(cfg_handle, nodeid, 1, &nr, &caddr);<br>
+ if (ret != CS_OK) {<br>
+ vprintf(SDOG_ERR "failed to get addr %d\n", ret);<br>
+ return -1;<br>
+ }<br>
+<br>
+ if (!nr) {<br>
+ vprintf(SDOG_ERR "we got no address\n");<br>
+ return -1;<br>
+ }<br>
+<br>
+ if (ss->ss_family == AF_INET6) {<br>
+ saddr = &sin6->sin6_addr;<br>
+ memcpy(addr, saddr, 16);<br>
+ } else if (ss->ss_family == AF_INET) {<br>
+ saddr = &sin->sin_addr;<br>
+ memset(addr, 0, 16);<br>
+ memcpy(addr + 12, saddr, 4);<br>
+ } else {<br>
+ vprintf(SDOG_ERR "unknown protocol %d\n", ss->ss_family);<br>
+ return -1;<br>
+ }<br>
+<br>
+ return 0;<br>
+}<br>
+<br>
+static void cpg_addr_to_sheepid(const struct cpg_address *cpgs,<br>
+ struct sheepid *sheeps, size_t nr)<br>
+{<br>
+ int i;<br>
+<br>
+ for (i = 0; i < nr; i++) {<br>
+ nodeid_to_addr(cpgs[i].nodeid, sheeps[i].addr);<br>
+ sheeps[i].pid = cpgs[i].pid;<br>
+ }<br>
+}<br>
+<br>
+static void cdrv_cpg_deliver(cpg_handle_t handle,<br>
+ const struct cpg_name *group_name,<br>
+ uint32_t nodeid, uint32_t pid,<br>
+ void *msg, size_t msg_len)<br>
+{<br>
+ struct sheepid sender;<br>
+<br>
+ nodeid_to_addr(nodeid, sender.addr);<br>
+ sender.pid = pid;<br>
+<br>
+ corosync_handlers.notify_handler(&sender, msg, msg_len);<br>
+}<br>
+<br>
+static void cdrv_cpg_confchg(cpg_handle_t handle,<br>
+ const struct cpg_name *group_name,<br>
+ const struct cpg_address *member_list,<br>
+ size_t member_list_entries,<br>
+ const struct cpg_address *left_list,<br>
+ size_t left_list_entries,<br>
+ const struct cpg_address *joined_list,<br>
+ size_t joined_list_entries)<br>
+{<br>
+ int i;<br>
+ struct sheepid member_sheeps[SD_MAX_NODES];<br>
+ struct sheepid joined_sheeps[SD_MAX_NODES];<br>
+ struct sheepid left_sheeps[SD_MAX_NODES];<br>
+<br>
+ /* convert cpg_address to sheepid*/<br>
+ cpg_addr_to_sheepid(member_list, member_sheeps, member_list_entries);<br>
+ cpg_addr_to_sheepid(left_list, left_sheeps, left_list_entries);<br>
+ cpg_addr_to_sheepid(joined_list, joined_sheeps, joined_list_entries);<br>
+<br>
+ /* calculate a start member list */<br>
+ sheepid_del(member_sheeps, member_list_entries,<br>
+ joined_sheeps, joined_list_entries);<br>
+ member_list_entries -= joined_list_entries;<br>
+<br>
+ sheepid_add(member_sheeps, member_list_entries,<br>
+ left_sheeps, left_list_entries);<br>
+ member_list_entries += left_list_entries;<br>
+<br>
+ /* dispatch leave_handler */<br>
+ for (i = 0; i < left_list_entries; i++) {<br>
+ sheepid_del(member_sheeps, member_list_entries,<br>
+ left_sheeps + i, 1);<br>
+ member_list_entries--;<br>
+<br>
+ corosync_handlers.leave_handler(left_sheeps + i, member_sheeps,<br>
+ member_list_entries);<br>
+ }<br>
+<br>
+ /* dispatch join_handler */<br>
+ for (i = 0; i < joined_list_entries; i++) {<br>
+ sheepid_add(member_sheeps, member_list_entries,<br>
+ joined_sheeps, 1);<br>
+ member_list_entries++;<br>
+<br>
+ corosync_handlers.join_handler(joined_sheeps + i, member_sheeps,<br>
+ member_list_entries);<br>
+ }<br>
+}<br>
+<br>
+static int corosync_init(struct cdrv_handlers *handlers, struct sheepid *myid)<br>
+{<br>
+ int ret, fd;<br>
+ uint32_t nodeid;<br>
+ cpg_callbacks_t cb = {<br>
+ .cpg_deliver_fn = cdrv_cpg_deliver,<br>
+ .cpg_confchg_fn = cdrv_cpg_confchg<br>
+ };<br>
+<br>
+ corosync_handlers = *handlers;<br>
+<br>
+ ret = cpg_initialize(&cpg_handle, &cb);<br>
+ if (ret != CPG_OK) {<br>
+ eprintf("Failed to initialize cpg, %d\n", ret);<br>
+ eprintf("Is corosync running?\n");<br>
+ return -1;<br>
+ }<br>
+<br>
+ ret = corosync_cfg_initialize(&cfg_handle, NULL);<br>
+ if (ret != CS_OK) {<br>
+ vprintf(SDOG_ERR "failed to initiazize cfg %d\n", ret);<br>
+ return -1;<br>
+ }<br>
+<br>
+ ret = corosync_cfg_local_get(cfg_handle, &nodeid);<br>
+ if (ret != CS_OK) {<br>
+ vprintf(SDOG_ERR "failed to get nodeid %d\n", ret);<br>
+ return -1;<br>
+ }<br>
+<br>
+ ret = nodeid_to_addr(nodeid, myid->addr);<br>
+ if (ret < 0) {<br>
+ eprintf("failed to get local address\n");<br>
+ return -1;<br>
+ }<br>
+<br>
+ myid->pid = getpid();<br>
+<br>
+ ret = cpg_fd_get(cpg_handle, &fd);<br>
+ if (ret != CPG_OK) {<br>
+ eprintf("Failed to retrieve cpg file descriptor, %d\n", ret);<br>
+ return 1;<br>
</blockquote>
<div>here we can't return 1 anymore, the semanteme is changed </div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
+ }<br>
+<br>
+ return fd;<br>
+}<br>
+<br>
+static int corosync_join(void)<br>
+{<br>
+ int ret;<br>
+retry:<br>
+ ret = cpg_join(cpg_handle, &cpg_group);<br>
+ switch (ret) {<br>
+ case CPG_OK:<br>
+ break;<br>
+ case CPG_ERR_TRY_AGAIN:<br>
+ dprintf("Failed to join the sheepdog group, try again\n");<br>
+ sleep(1);<br>
+ goto retry;<br>
+ case CPG_ERR_SECURITY:<br>
+ eprintf("Permission error.\n");<br>
+ return -1;<br>
+ default:<br>
+ eprintf("Failed to join the sheepdog group, %d\n", ret);<br>
+ return -1;<br>
+ }<br>
+<br>
+ return 0;<br>
+}<br>
+<br>
+static int corosync_leave(void)<br>
+{<br>
+ int ret;<br>
+<br>
+ ret = cpg_leave(cpg_handle, &cpg_group);<br>
+ if (ret != CPG_OK) {<br>
+ eprintf("failed to leave the sheepdog group\n, %d", ret);<br>
+ return -1;<br>
+ }<br>
+<br>
+ return 0;<br>
+}<br>
+<br>
+static int corosync_notify(void *msg, size_t msg_len)<br>
+{<br>
+ struct iovec iov;<br>
+ int ret;<br>
+<br>
+ iov.iov_base = msg;<br>
+ iov.iov_len = msg_len;<br>
+retry:<br>
+ ret = cpg_mcast_joined(cpg_handle, CPG_TYPE_AGREED, &iov, 1);<br>
+ switch (ret) {<br>
+ case CPG_OK:<br>
+ break;<br>
+ case CPG_ERR_TRY_AGAIN:<br>
+ dprintf("failed to send message. try again\n");<br>
+ sleep(1);<br>
+ goto retry;<br>
+ default:<br>
+ eprintf("failed to send message, %d\n", ret);<br>
+ return -1;<br>
+ }<br>
+ return 0;<br>
+}<br>
+<br>
+static int corosync_dispatch(void)<br>
+{<br>
+ int ret;<br>
+<br>
+ ret = cpg_dispatch(cpg_handle, CPG_DISPATCH_ALL);<br>
+ if (ret != CPG_OK)<br>
+ return -1;<br>
+<br>
+ return 0;<br>
+}<br>
+<br>
+struct cluster_driver cdrv_corosync = {<br>
+ .name = "corosync",<br>
+<br>
+ .init = corosync_init,<br>
+ .join = corosync_join,<br>
+ .leave = corosync_leave,<br>
+ .notify = corosync_notify,<br>
+ .dispatch = corosync_dispatch,<br>
</blockquote>
<div>is 'dispatch' operation really essential?</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
+};<br>
+<br>
+cdrv_register(cdrv_corosync);<br>
diff --git a/sheep/group.c b/sheep/group.c<br>
index 95fc799..f6743f5 100644<br>
--- a/sheep/group.c<br>
+++ b/sheep/group.c<br>
@@ -1890,12 +1890,23 @@ oom:<br>
int create_cluster(int port, int64_t zone)<br>
{<br>
int fd, ret;<br>
+ struct cluster_driver *cdrv;<br>
struct cdrv_handlers handlers = {<br>
.join_handler = sd_join_handler,<br>
.leave_handler = sd_leave_handler,<br>
.notify_handler = sd_notify_handler,<br>
};<br>
<br>
+ if (!sys->cdrv) {<br>
+ FOR_EACH_CLUSTER_DRIVER(cdrv) {<br>
+ if (strcmp(cdrv->name, "corosync") == 0) {<br>
+ dprintf("use corosync driver as default\n");<br>
+ sys->cdrv = cdrv;<br>
</blockquote>
<div>i think we'd better do a NULL pointer check for the driver's member.</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
+ break;<br>
+ }<br>
+ }<br>
+ }<br>
+<br>
fd = sys->cdrv->init(&handlers, &sys->this_sheepid);</blockquote>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
if (fd < 0)<br>
return -1;<br>
--<br>
1.7.2.5<br>
<font color="#888888"><br>
--<br>
sheepdog mailing list<br>
<a href="mailto:sheepdog@lists.wpkg.org">sheepdog@lists.wpkg.org</a><br>
<a href="http://lists.wpkg.org/mailman/listinfo/sheepdog" target="_blank">http://lists.wpkg.org/mailman/listinfo/sheepdog</a><br>
</font></blockquote>
</div>
<br>
<br>
<hr>
<font face="Arial" color="Gray" size="1"><br>
This email (including any attachments) is confidential and may be legally privileged. If you received this email in error, please delete it immediately and do not copy it or use it for any purpose or disclose its contents to any other person. Thank you.<br>
<br>
本电邮(包括任何附件)可能含有机密资料并受法律保护。如您不是正确的收件人,请您立即删除本邮件。请不要将本电邮进行复制并用作任何其他用途、或透露本邮件之内容。谢谢。<br>
</font>
</body>
</html>