From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: kostja@tarantool.org
Cc: tarantool-patches@freelists.org
Subject: [RFC PATCH 04/12] xrow: add helper function for encoding vclock
Date: Wed, 6 Jun 2018 20:45:04 +0300 [thread overview]
Message-ID: <ba93ac8f4cdf0dcc0e23752bb1b17d0b1a2b7088.1528305232.git.vdavydov.dev@gmail.com> (raw)
In-Reply-To: <cover.1528305232.git.vdavydov.dev@gmail.com>
In-Reply-To: <cover.1528305232.git.vdavydov.dev@gmail.com>
So as not to duplicate the same code over and over again.
---
src/box/xrow.c | 58 +++++++++++++++++++++++++++-------------------------------
1 file changed, 27 insertions(+), 31 deletions(-)
diff --git a/src/box/xrow.c b/src/box/xrow.c
index b3f81a86..532e1296 100644
--- a/src/box/xrow.c
+++ b/src/box/xrow.c
@@ -43,6 +43,27 @@
#include "scramble.h"
#include "iproto_constants.h"
+static inline uint32_t
+mp_sizeof_vclock(const struct vclock *vclock)
+{
+ uint32_t size = vclock_size(vclock);
+ return mp_sizeof_map(size) + size * (mp_sizeof_uint(UINT32_MAX) +
+ mp_sizeof_uint(UINT64_MAX));
+}
+
+static inline char *
+mp_encode_vclock(char *data, const struct vclock *vclock)
+{
+ data = mp_encode_map(data, vclock_size(vclock));
+ struct vclock_iterator it;
+ vclock_iterator_init(&it, vclock);
+ vclock_foreach(&it, replica) {
+ data = mp_encode_uint(data, replica.id);
+ data = mp_encode_uint(data, replica.lsn);
+ }
+ return data;
+}
+
int
xrow_header_decode(struct xrow_header *header, const char **pos,
const char *end)
@@ -265,11 +286,8 @@ iproto_reply_request_vote(struct obuf *out, uint64_t sync,
uint32_t schema_version, const struct vclock *vclock,
bool read_only)
{
- uint32_t replicaset_size = vclock_size(vclock);
size_t max_size = IPROTO_HEADER_LEN + mp_sizeof_map(2) +
- mp_sizeof_uint(UINT32_MAX) + mp_sizeof_map(replicaset_size) +
- replicaset_size * (mp_sizeof_uint(UINT32_MAX) +
- mp_sizeof_uint(UINT64_MAX)) +
+ mp_sizeof_uint(UINT32_MAX) + mp_sizeof_vclock(vclock) +
mp_sizeof_uint(UINT32_MAX) + mp_sizeof_bool(true);
char *buf = obuf_reserve(out, max_size);
@@ -284,13 +302,7 @@ iproto_reply_request_vote(struct obuf *out, uint64_t sync,
data = mp_encode_uint(data, IPROTO_SERVER_IS_RO);
data = mp_encode_bool(data, read_only);
data = mp_encode_uint(data, IPROTO_VCLOCK);
- data = mp_encode_map(data, replicaset_size);
- struct vclock_iterator it;
- vclock_iterator_init(&it, vclock);
- vclock_foreach(&it, replica) {
- data = mp_encode_uint(data, replica.id);
- data = mp_encode_uint(data, replica.lsn);
- }
+ data = mp_encode_vclock(data, vclock);
size_t size = data - buf;
assert(size <= max_size);
@@ -806,9 +818,7 @@ xrow_encode_subscribe(struct xrow_header *row,
const struct vclock *vclock)
{
memset(row, 0, sizeof(*row));
- uint32_t replicaset_size = vclock_size(vclock);
- size_t size = XROW_BODY_LEN_MAX + replicaset_size *
- (mp_sizeof_uint(UINT32_MAX) + mp_sizeof_uint(UINT64_MAX));
+ size_t size = XROW_BODY_LEN_MAX + mp_sizeof_vclock(vclock);
char *buf = (char *) region_alloc(&fiber()->gc, size);
if (buf == NULL) {
diag_set(OutOfMemory, size, "region_alloc", "buf");
@@ -821,13 +831,7 @@ xrow_encode_subscribe(struct xrow_header *row,
data = mp_encode_uint(data, IPROTO_INSTANCE_UUID);
data = xrow_encode_uuid(data, instance_uuid);
data = mp_encode_uint(data, IPROTO_VCLOCK);
- data = mp_encode_map(data, replicaset_size);
- struct vclock_iterator it;
- vclock_iterator_init(&it, vclock);
- vclock_foreach(&it, replica) {
- data = mp_encode_uint(data, replica.id);
- data = mp_encode_uint(data, replica.lsn);
- }
+ data = mp_encode_vclock(data, vclock);
data = mp_encode_uint(data, IPROTO_SERVER_VERSION);
data = mp_encode_uint(data, tarantool_version_id());
assert(data <= buf + size);
@@ -971,9 +975,7 @@ xrow_encode_vclock(struct xrow_header *row, const struct vclock *vclock)
memset(row, 0, sizeof(*row));
/* Add vclock to response body */
- uint32_t replicaset_size = vclock_size(vclock);
- size_t size = 8 + replicaset_size *
- (mp_sizeof_uint(UINT32_MAX) + mp_sizeof_uint(UINT64_MAX));
+ size_t size = 8 + mp_sizeof_vclock(vclock);
char *buf = (char *) region_alloc(&fiber()->gc, size);
if (buf == NULL) {
diag_set(OutOfMemory, size, "region_alloc", "buf");
@@ -982,13 +984,7 @@ xrow_encode_vclock(struct xrow_header *row, const struct vclock *vclock)
char *data = buf;
data = mp_encode_map(data, 1);
data = mp_encode_uint(data, IPROTO_VCLOCK);
- data = mp_encode_map(data, replicaset_size);
- struct vclock_iterator it;
- vclock_iterator_init(&it, vclock);
- vclock_foreach(&it, replica) {
- data = mp_encode_uint(data, replica.id);
- data = mp_encode_uint(data, replica.lsn);
- }
+ data = mp_encode_vclock(data, vclock);
assert(data <= buf + size);
row->body[0].iov_base = buf;
row->body[0].iov_len = (data - buf);
--
2.11.0
next prev parent reply other threads:[~2018-06-06 17:45 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-06 17:45 [RFC PATCH 00/12] Replica rejoin Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 01/12] recovery: drop unused recovery_exit Vladimir Davydov
2018-06-08 4:13 ` Konstantin Osipov
2018-06-06 17:45 ` [RFC PATCH 02/12] recovery: constify vclock argument Vladimir Davydov
2018-06-08 4:14 ` Konstantin Osipov
2018-06-06 17:45 ` [RFC PATCH 03/12] applier: remove extra new line in log message printed on connect Vladimir Davydov
2018-06-08 4:15 ` Konstantin Osipov
2018-06-06 17:45 ` Vladimir Davydov [this message]
2018-06-08 4:16 ` [RFC PATCH 04/12] xrow: add helper function for encoding vclock Konstantin Osipov
2018-06-06 17:45 ` [RFC PATCH 05/12] box: retrieve instance uuid before starting local recovery Vladimir Davydov
2018-06-08 4:22 ` Konstantin Osipov
2018-06-06 17:45 ` [RFC PATCH 06/12] box: refactor hot standby recovery Vladimir Davydov
2018-06-08 4:40 ` Konstantin Osipov
2018-06-08 6:43 ` Vladimir Davydov
2018-06-08 13:15 ` Konstantin Osipov
2018-06-08 13:30 ` Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 07/12] box: retrieve end vclock before starting local recovery Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 08/12] box: open the port " Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 09/12] box: connect to remote peers " Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 10/12] box: factor out local recovery function Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 11/12] applier: inquire oldest vclock on connect Vladimir Davydov
2018-06-06 17:45 ` [RFC PATCH 12/12] replication: rebootstrap instance on startup if it fell behind Vladimir Davydov
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=ba93ac8f4cdf0dcc0e23752bb1b17d0b1a2b7088.1528305232.git.vdavydov.dev@gmail.com \
--to=vdavydov.dev@gmail.com \
--cc=kostja@tarantool.org \
--cc=tarantool-patches@freelists.org \
--subject='Re: [RFC PATCH 04/12] xrow: add helper function for encoding vclock' \
/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