Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
To: tarantool-patches@freelists.org
Cc: kostja@tarantool.org
Subject: [tarantool-patches] [PATCH 2/7] swim: encapsulate 'uint16' payload size inside swim.c
Date: Wed, 15 May 2019 02:06:20 +0300	[thread overview]
Message-ID: <ab61a8d60de105a00bbcb5b46603cc46f5c87f7d.1557875116.git.v.shpilevoy@tarantool.org> (raw)
In-Reply-To: <cover.1557875116.git.v.shpilevoy@tarantool.org>

uint16 was used in public SWIM C API as a type for payload size
to emphasize its small value. But it is not useful in Lua,
because Lua API should explicitly check if a number overflows
uint16 maximal value, and return the same error as in case it is
< uint16_max, but > payload_size_max.

So main motivation of the patch is to avoid unnecessary checks in
Lua and error message duplication. Internally payload size is
still uint16.
---
 src/lib/swim/swim.c         |  8 ++++----
 src/lib/swim/swim.h         |  4 ++--
 test/unit/swim.c            | 10 +++++-----
 test/unit/swim_test_utils.c | 12 ++++++------
 test/unit/swim_test_utils.h |  6 +++---
 5 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/src/lib/swim/swim.c b/src/lib/swim/swim.c
index 006c265b3..7a43fae1b 100644
--- a/src/lib/swim/swim.c
+++ b/src/lib/swim/swim.c
@@ -1824,10 +1824,10 @@ swim_is_configured(const struct swim *swim)
 }
 
 int
-swim_set_payload(struct swim *swim, const char *payload, uint16_t payload_size)
+swim_set_payload(struct swim *swim, const char *payload, int payload_size)
 {
-	if (payload_size > MAX_PAYLOAD_SIZE) {
-		diag_set(IllegalParams, "Payload should be <= %d",
+	if (payload_size > MAX_PAYLOAD_SIZE || payload_size < 0) {
+		diag_set(IllegalParams, "Payload should be <= %d and >= 0",
 			 MAX_PAYLOAD_SIZE);
 		return -1;
 	}
@@ -2070,7 +2070,7 @@ swim_member_incarnation(const struct swim_member *member)
 }
 
 const char *
-swim_member_payload(const struct swim_member *member, uint16_t *size)
+swim_member_payload(const struct swim_member *member, int *size)
 {
 	*size = member->payload_size;
 	return member->payload;
diff --git a/src/lib/swim/swim.h b/src/lib/swim/swim.h
index 044558dc8..8c4f8c3cf 100644
--- a/src/lib/swim/swim.h
+++ b/src/lib/swim/swim.h
@@ -102,7 +102,7 @@ swim_cfg(struct swim *swim, const char *uri, double heartbeat_rate,
 
 /** Set payload to disseminate over the cluster. */
 int
-swim_set_payload(struct swim *swim, const char *payload, uint16_t payload_size);
+swim_set_payload(struct swim *swim, const char *payload, int payload_size);
 
 /**
  * Stop listening and broadcasting messages, cleanup all internal
@@ -218,7 +218,7 @@ swim_member_incarnation(const struct swim_member *member);
 
 /** Member's payload. */
 const char *
-swim_member_payload(const struct swim_member *member, uint16_t *size);
+swim_member_payload(const struct swim_member *member, int *size);
 
 /**
  * Reference a member. The member memory will be valid until unref
diff --git a/test/unit/swim.c b/test/unit/swim.c
index 8b4a1f17a..3ec25c5d0 100644
--- a/test/unit/swim.c
+++ b/test/unit/swim.c
@@ -672,7 +672,7 @@ static void
 swim_test_payload_basic(void)
 {
 	swim_start_test(11);
-	uint16_t size, cluster_size = 3;
+	int size, cluster_size = 3;
 	struct swim_cluster *cluster = swim_cluster_new(cluster_size);
 	for (int i = 0; i < cluster_size; ++i) {
 		for (int j = i + 1; j < cluster_size; ++j)
@@ -685,7 +685,7 @@ swim_test_payload_basic(void)
 	ok(swim_error_check_match("Payload should be <="), "diag says too big");
 
 	const char *s0_payload = "S1 payload";
-	uint16_t s0_payload_size = strlen(s0_payload) + 1;
+	int s0_payload_size = strlen(s0_payload) + 1;
 	is(swim_cluster_member_set_payload(cluster, 0, s0_payload,
 					   s0_payload_size), 0,
 	   "payload is set");
@@ -732,7 +732,7 @@ static void
 swim_test_payload_refutation(void)
 {
 	swim_start_test(11);
-	uint16_t size, cluster_size = 3;
+	int size, cluster_size = 3;
 	struct swim_cluster *cluster = swim_cluster_new(cluster_size);
 	swim_cluster_set_ack_timeout(cluster, 1);
 	for (int i = 0; i < cluster_size; ++i) {
@@ -740,7 +740,7 @@ swim_test_payload_refutation(void)
 			swim_cluster_interconnect(cluster, i, j);
 	}
 	const char *s0_old_payload = "s0 payload";
-	uint16_t s0_old_payload_size = strlen(s0_old_payload) + 1;
+	int s0_old_payload_size = strlen(s0_old_payload) + 1;
 	fail_if(swim_cluster_member_set_payload(cluster, 0, s0_old_payload,
 						s0_old_payload_size) != 0);
 	fail_if(swim_cluster_wait_payload_everywhere(cluster, 0, s0_old_payload,
@@ -757,7 +757,7 @@ swim_test_payload_refutation(void)
 	 * lost, however ACKs work ok.
 	 */
 	const char *s0_new_payload = "s0 second payload";
-	uint16_t s0_new_payload_size = strlen(s0_new_payload);
+	int s0_new_payload_size = strlen(s0_new_payload);
 	fail_if(swim_cluster_member_set_payload(cluster, 0, s0_new_payload,
 						s0_new_payload_size) != 0);
 	int components[2] = {SWIM_DISSEMINATION, SWIM_ANTI_ENTROPY};
diff --git a/test/unit/swim_test_utils.c b/test/unit/swim_test_utils.c
index de1bef6e7..f55388055 100644
--- a/test/unit/swim_test_utils.c
+++ b/test/unit/swim_test_utils.c
@@ -332,7 +332,7 @@ swim_cluster_member_incarnation(struct swim_cluster *cluster, int node_id,
 
 const char *
 swim_cluster_member_payload(struct swim_cluster *cluster, int node_id,
-			    int member_id, uint16_t *size)
+			    int member_id, int *size)
 {
 	const struct swim_member *m =
 		swim_cluster_member_view(cluster, node_id, member_id);
@@ -345,7 +345,7 @@ swim_cluster_member_payload(struct swim_cluster *cluster, int node_id,
 
 int
 swim_cluster_member_set_payload(struct swim_cluster *cluster, int i,
-				const char *payload, uint16_t size)
+				const char *payload, int size)
 {
 	struct swim *s = swim_cluster_member(cluster, i);
 	return swim_set_payload(s, payload, size);
@@ -687,7 +687,7 @@ struct swim_member_template {
 	 */
 	bool need_check_payload;
 	const char *payload;
-	uint16_t payload_size;
+	int payload_size;
 };
 
 /** Build member template. No checks are set. */
@@ -730,7 +730,7 @@ swim_member_template_set_incarnation(struct swim_member_template *t,
  */
 static inline void
 swim_member_template_set_payload(struct swim_member_template *t,
-				 const char *payload, uint16_t payload_size)
+				 const char *payload, int payload_size)
 {
 	t->need_check_payload = true;
 	t->payload = payload;
@@ -747,7 +747,7 @@ swim_loop_check_member(struct swim_cluster *cluster, void *data)
 	enum swim_member_status status;
 	uint64_t incarnation;
 	const char *payload;
-	uint16_t payload_size;
+	int payload_size;
 	if (m != NULL) {
 		status = swim_member_status(m);
 		incarnation = swim_member_incarnation(m);
@@ -851,7 +851,7 @@ swim_cluster_wait_status_everywhere(struct swim_cluster *cluster, int member_id,
 int
 swim_cluster_wait_payload_everywhere(struct swim_cluster *cluster,
 				     int member_id, const char *payload,
-				     uint16_t payload_size, double timeout)
+				     int payload_size, double timeout)
 {
 	struct swim_member_template t;
 	swim_member_template_create(&t, -1, member_id);
diff --git a/test/unit/swim_test_utils.h b/test/unit/swim_test_utils.h
index c78894820..b786dfd79 100644
--- a/test/unit/swim_test_utils.h
+++ b/test/unit/swim_test_utils.h
@@ -154,11 +154,11 @@ swim_cluster_member_incarnation(struct swim_cluster *cluster, int node_id,
 
 const char *
 swim_cluster_member_payload(struct swim_cluster *cluster, int node_id,
-			    int member_id, uint16_t *size);
+			    int member_id, int *size);
 
 int
 swim_cluster_member_set_payload(struct swim_cluster *cluster, int i,
-				const char *payload, uint16_t size);
+				const char *payload, int size);
 
 /**
  * Check if in the cluster every instance knowns the about other
@@ -219,7 +219,7 @@ swim_cluster_wait_incarnation(struct swim_cluster *cluster, int node_id,
 int
 swim_cluster_wait_payload_everywhere(struct swim_cluster *cluster,
 				     int member_id, const char *payload,
-				     uint16_t payload_size, double timeout);
+				     int payload_size, double timeout);
 
 /** Process SWIM events for @a duration fake seconds. */
 void
-- 
2.20.1 (Apple Git-117)

  parent reply	other threads:[~2019-05-14 23:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-14 23:06 [tarantool-patches] [PATCH 0/7] swim lua preparation, again Vladislav Shpilevoy
2019-05-14 23:06 ` [tarantool-patches] [PATCH 1/7] swim: drop swim_info() function Vladislav Shpilevoy
2019-05-15  2:00   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` Vladislav Shpilevoy [this message]
2019-05-15  2:02   ` [tarantool-patches] Re: [PATCH 2/7] swim: encapsulate 'uint16' payload size inside swim.c Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 3/7] swim: do not rebind when new 'port' is 0 Vladislav Shpilevoy
2019-05-15  2:02   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 4/7] swim: set 'left' status in self on swim_quit() Vladislav Shpilevoy
2019-05-15  2:03   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 5/7] msgpack: allow to pass 'struct ibuf *' into encode() Vladislav Shpilevoy
2019-05-15  2:05   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 6/7] msgpack: allow to pass 'const char *' into decode() Vladislav Shpilevoy
2019-05-15  2:05   ` [tarantool-patches] " Konstantin Osipov
2019-05-14 23:06 ` [tarantool-patches] [PATCH 7/7] Drop an unused function and class Vladislav Shpilevoy
2019-05-15  2:06   ` [tarantool-patches] " Konstantin Osipov
2019-05-15 10:02 ` [tarantool-patches] Re: [PATCH 0/7] swim lua preparation, again Vladislav Shpilevoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ab61a8d60de105a00bbcb5b46603cc46f5c87f7d.1557875116.git.v.shpilevoy@tarantool.org \
    --to=v.shpilevoy@tarantool.org \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [tarantool-patches] [PATCH 2/7] swim: encapsulate '\''uint16'\'' payload size inside swim.c' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox