From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 209926B944; Tue, 13 Apr 2021 17:16:02 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 209926B944 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618323362; bh=FPg/M0UaZhdvCkk5VmdZxgxO1ZJM454wO0KNGXzotE4=; h=Date:To:Cc:References:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=xIQtnU1UM53cgMA0ryQTJ12UwTVdGwLpt2GEImHyKugjL/X1u1nisFDKipi8OqX8b 8cr7gFu201yGA+AHAPwbMsvmtVONEbdvultzZNA58EvG6+77uApsvUNuZwDl37TwmX qJbLYIsDe166kSLmurELVu75bHOXRTJxYy23ZzXk= Received: from mail-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id D24A96BD2D for ; Tue, 13 Apr 2021 17:16:00 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D24A96BD2D Received: by mail-lj1-f179.google.com with SMTP id u4so19530782ljo.6 for ; Tue, 13 Apr 2021 07:16:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=6WZaGRQTseKHUtAUZixuv46doitA/5DUuYKM/SsYxC8=; b=lt7AhJ9wzDSO2yAW25Jq9kv/pnlIszT/3zm3XtpzoPyWWjz3j1IhbsBE+Bcf/s240p gqsuNuuU+4NEKbedUeSrSGCBVezi2NpxvzyJkW3W04Va377sjDxE3ZhCMQ7CMB+Hl7Hz 4Q84ChDSb+D6HENhzHox9enwIkCBR7jAB0CAsThV6RvFzi/U1a29/NB+l1oFA9OCG4W0 8mPme4I7D10FJ+B2Zb2Rzi8Y9DtLJtUCWopV9ZY0CMXywyVr3PXVOr5FofzOukROAThW JBi6mrpF55BYgJzUuYfPl04wSNrfI2X9JISFd5tbSUwAMaTgAVrNosaJcBXn5GnrBYZT lNLg== X-Gm-Message-State: AOAM533Kji0ElZLJ4E/t6sCBHHjH3cqUxohP7Mh3coEPPG/FqAN4LMiq ycwBHyNOdLG13s6z6o5c1pw4fl9+e6Q= X-Google-Smtp-Source: ABdhPJxxC/aS+6fWBeipMFvg8J3Feo5ua/4buw6zIdnZRcytpLWzC171rpNRZWSD4o1gY2Sz/h+7BQ== X-Received: by 2002:a05:651c:1023:: with SMTP id w3mr9842718ljm.303.1618323359218; Tue, 13 Apr 2021 07:15:59 -0700 (PDT) Received: from grain.localdomain ([5.18.199.94]) by smtp.gmail.com with ESMTPSA id a30sm496799lfi.235.2021.04.13.07.15.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Apr 2021 07:15:56 -0700 (PDT) Received: by grain.localdomain (Postfix, from userid 1000) id CBD1C560162; Tue, 13 Apr 2021 17:15:55 +0300 (MSK) Date: Tue, 13 Apr 2021 17:15:55 +0300 To: Serge Petrenko Cc: v.shpilevoy@tarantool.org, tarantool-patches@dev.tarantool.org Message-ID: References: <6641b0b1f98ff233ccb3a121833cb6363c45edcb.1618256019.git.sergepetrenko@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6641b0b1f98ff233ccb3a121833cb6363c45edcb.1618256019.git.sergepetrenko@tarantool.org> User-Agent: Mutt/2.0.5 (2021-01-21) Subject: Re: [Tarantool-patches] [PATCH v2 2/9] xrow: introduce a PROMOTE entry X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Cyrill Gorcunov via Tarantool-patches Reply-To: Cyrill Gorcunov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" On Mon, Apr 12, 2021 at 10:40:15PM +0300, Serge Petrenko wrote: > diff --git a/src/box/xrow.c b/src/box/xrow.c > index cc8e43ed4..70ba075f8 100644 > --- a/src/box/xrow.c > +++ b/src/box/xrow.c > @@ -890,11 +890,11 @@ xrow_encode_synchro(struct xrow_header *row, > const struct synchro_request *req) > { > /* > - * A map with two elements. We don't compress > + * A map with two or three elements. We don't compress > * numbers to have this structure constant in size, > * which allows us to preallocate it on stack. > */ > - body->m_body = 0x80 | 2; > + body->m_body = 0x80 | (req->type == IPROTO_PROMOTE ? 3 : 2); > body->k_replica_id = IPROTO_REPLICA_ID; > body->m_replica_id = 0xce; > body->v_replica_id = mp_bswap_u32(req->replica_id); > @@ -903,10 +903,24 @@ xrow_encode_synchro(struct xrow_header *row, > body->v_lsn = mp_bswap_u64(req->lsn); > > memset(row, 0, sizeof(*row)); > - > row->type = req->type; > - row->body[0].iov_base = (void *)body; > - row->body[0].iov_len = sizeof(*body); > + > + /* Promote body is longer. It has an additional IPROTO_TERM field. */ > + if (req->type == IPROTO_PROMOTE) { > + struct promote_body_bin *promote_body = > + (struct promote_body_bin *)body; > + > + promote_body->k_term = IPROTO_TERM; > + promote_body->m_term = 0xcf; > + promote_body->v_term = mp_bswap_u64(req->term); > + > + row->body[0].iov_base = (void *)promote_body; > + row->body[0].iov_len = sizeof(*promote_body); > + } else { > + row->body[0].iov_base = (void *)body; > + row->body[0].iov_len = sizeof(*body); > + } > + > row->bodycnt = 1; > } You know, while I understand that we're trying to reuse code flow here but I really don't like that this function unaware of type passed. IOW the function may easily overwire caller's stack if you occasionally pass synchro_body_bin instead of promote request. Actually I've sevaral options: 1) make the caller to provide a size and use assert() inside this encoder to make sure the caller passer proper amount of data from stack; 2) provide own helper for promote packet encoding (see below). Still both approaches somehow *ugly* I think. Since there only a few use of this encodings it is easy to remember where and what and don't make a mistake. So lets leave it as it now. --- diff --git a/src/box/txn_limbo.c b/src/box/txn_limbo.c index f119c35b6..87fa220ea 100644 --- a/src/box/txn_limbo.c +++ b/src/box/txn_limbo.c @@ -333,8 +333,7 @@ txn_limbo_write_synchro(struct txn_limbo *limbo, uint32_t type, int64_t lsn, * This is a synchronous commit so we can allocate everything on a * stack. Promote body includes synchro body. */ - struct promote_body_bin body; - struct synchro_body_bin *base = &body.base; + struct promote_body_bin body_bin; struct xrow_header row; char buf[sizeof(struct journal_entry) + @@ -343,7 +342,7 @@ txn_limbo_write_synchro(struct txn_limbo *limbo, uint32_t type, int64_t lsn, struct journal_entry *entry = (struct journal_entry *)buf; entry->rows[0] = &row; - xrow_encode_synchro(&row, base, &req); + xrow_encode_promote(&row, &body_bin, &req); journal_entry_create(entry, 1, xrow_approx_len(&row), txn_limbo_write_cb, fiber()); diff --git a/src/box/xrow.c b/src/box/xrow.c index 70ba075f8..df76b104b 100644 --- a/src/box/xrow.c +++ b/src/box/xrow.c @@ -884,16 +884,11 @@ xrow_encode_dml(const struct request *request, struct region *region, return iovcnt; } -void -xrow_encode_synchro(struct xrow_header *row, - struct synchro_body_bin *body, - const struct synchro_request *req) +static void +xrow_encode_synchro_common(struct xrow_header *row, + struct synchro_body_bin *body, + const struct synchro_request *req) { - /* - * A map with two or three elements. We don't compress - * numbers to have this structure constant in size, - * which allows us to preallocate it on stack. - */ body->m_body = 0x80 | (req->type == IPROTO_PROMOTE ? 3 : 2); body->k_replica_id = IPROTO_REPLICA_ID; body->m_replica_id = 0xce; @@ -904,24 +899,32 @@ xrow_encode_synchro(struct xrow_header *row, memset(row, 0, sizeof(*row)); row->type = req->type; + row->bodycnt = 1; +} - /* Promote body is longer. It has an additional IPROTO_TERM field. */ - if (req->type == IPROTO_PROMOTE) { - struct promote_body_bin *promote_body = - (struct promote_body_bin *)body; +void +xrow_encode_promote(struct xrow_header *row, struct promote_body_bin *body, + const struct synchro_request *req) +{ + xrow_encode_synchro_common(row, body, req); - promote_body->k_term = IPROTO_TERM; - promote_body->m_term = 0xcf; - promote_body->v_term = mp_bswap_u64(req->term); + promote_body->k_term = IPROTO_TERM; + promote_body->m_term = 0xcf; + promote_body->v_term = mp_bswap_u64(req->term); - row->body[0].iov_base = (void *)promote_body; - row->body[0].iov_len = sizeof(*promote_body); - } else { - row->body[0].iov_base = (void *)body; - row->body[0].iov_len = sizeof(*body); - } + row->body[0].iov_base = body; + row->body[0].iov_len = sizeof(*body); +} + +void +xrow_encode_synchro(struct xrow_header *row, struct synchro_body_bin *body, + const struct synchro_request *req) +{ + xrow_encode_synchro_common(row, body, req); + + row->body[0].iov_base = body; + row->body[0].iov_len = sizeof(*body); - row->bodycnt = 1; } int