[PATCH v4 12/12] [RAW] swim: allow to use broadcast tasks to send pings

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Thu Jan 31 00:28:30 MSK 2019


Part of #3234
---
 src/lib/swim/swim.c | 15 +++++++++++++++
 src/lib/swim/swim.h |  7 +++++++
 src/lua/swim.c      | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git a/src/lib/swim/swim.c b/src/lib/swim/swim.c
index b377f154f..f67acc5b3 100644
--- a/src/lib/swim/swim.c
+++ b/src/lib/swim/swim.c
@@ -1626,6 +1626,21 @@ swim_probe_member(struct swim *swim, const char *uri)
 	return 0;
 }
 
+int
+swim_broadcast(struct swim *swim, int port)
+{
+	const char *msg_pref = "swim.broadcast:";
+	if (swim_check_is_configured(swim, msg_pref) != 0)
+		return -1;
+	if (port < 0)
+		port = ntohs(swim->self->addr.sin_port);
+	struct swim_bcast_task *t = swim_bcast_task_new(port);
+	if (t == NULL)
+		return -1;
+	swim_send_ping(swim, &t->base, &t->base.dst, NULL);
+	return 0;
+}
+
 void
 swim_info(struct swim *swim, struct info_handler *info)
 {
diff --git a/src/lib/swim/swim.h b/src/lib/swim/swim.h
index 24f3a4b33..8f30d58d0 100644
--- a/src/lib/swim/swim.h
+++ b/src/lib/swim/swim.h
@@ -93,6 +93,13 @@ swim_remove_member(struct swim *swim, const struct tt_uuid *uuid);
 int
 swim_probe_member(struct swim *swim, const char *uri);
 
+/**
+ * Broadcast a ping to all interfaces on a specified port. If a
+ * port is < 0, then a port of the SWIM instance is used.
+ */
+int
+swim_broadcast(struct swim *swim, int port);
+
 /** Dump member statuses into @a info. */
 void
 swim_info(struct swim *swim, struct info_handler *info);
diff --git a/src/lua/swim.c b/src/lua/swim.c
index 7df4e5c85..1be8e7595 100644
--- a/src/lua/swim.c
+++ b/src/lua/swim.c
@@ -403,6 +403,44 @@ lua_swim_quit(struct lua_State *L)
 	return 0;
 }
 
+/**
+ * Broadcast a ping over all network interfaces with a speicifed
+ * port. Port is optional and in a case of absence it is set to
+ * a port of the current instance. The Lua stack should contain a
+ * SWIM instance to broadcast from, and optionally a port.
+ * @param L Lua state.
+ * @retval 1 True.
+ * @retval 2 Nil and an error object. On invalid Lua parameters
+ *         and OOM it throws.
+ */
+static int
+lua_swim_broadcast(struct lua_State *L)
+{
+	struct swim *swim = lua_swim_ptr(L, 1);
+	if (swim == NULL)
+		return luaL_error(L, "Usage: swim:broadcast([port])");
+	int port = -1;
+	if (lua_gettop(L) > 1) {
+		if (! lua_isnumber(L, 2)) {
+			return luaL_error(L, "swim.broadcast: port should be "\
+					  "a number");
+		}
+		double dport = lua_tonumber(L, 2);
+		port = dport;
+		if (dport != (double) port) {
+			return luaL_error(L, "swim.broadcast: port should be "\
+					  "an integer");
+		}
+	}
+	if (swim_broadcast(swim, port) != 0) {
+		lua_pushnil(L);
+		luaT_pusherror(L, diag_last_error(diag_get()));
+		return 2;
+	}
+	lua_pushboolean(L, true);
+	return 1;
+}
+
 void
 tarantool_lua_swim_init(struct lua_State *L)
 {
@@ -415,6 +453,7 @@ tarantool_lua_swim_init(struct lua_State *L)
 		{"info", lua_swim_info},
 		{"probe_member", lua_swim_probe_member},
 		{"quit", lua_swim_quit},
+		{"broadcast", lua_swim_broadcast},
 		{NULL, NULL}
 	};
 	luaL_register_module(L, "swim", lua_swim_methods);
-- 
2.17.2 (Apple Git-113)




More information about the Tarantool-patches mailing list