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 405D76BD2D; Tue, 13 Apr 2021 16:26:11 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 405D76BD2D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1618320371; bh=huk/D1yWqzJH9oST3diQnBnT54K65rPm/4TOP0EjGrc=; h=To:Cc:References:Date:In-Reply-To:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=A+w+iGchuavfvsMGwQTNVHE4ny9RDyhE04zmwHCsNry1k99W6ffccjZkyhIw5i08x SJlMaD7O6M7UZEHsqteUr7qq6b1NZ6YaCkQRSbnbTF0FggfYH2ezfz1xFb0cujeD3h iXbLYHkOpLPf/xXoqvYD40xk4vLSZdQWRRPHkgiE= Received: from smtp51.i.mail.ru (smtp51.i.mail.ru [94.100.177.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 2756C6BD29 for ; Tue, 13 Apr 2021 16:26:10 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 2756C6BD29 Received: by smtp51.i.mail.ru with esmtpa (envelope-from ) id 1lWJ3Q-0004mn-Ql; Tue, 13 Apr 2021 16:26:09 +0300 To: Cyrill Gorcunov Cc: v.shpilevoy@tarantool.org, tarantool-patches@dev.tarantool.org References: <69fd9b54dcaf92116e08d3319a66c080ad578cea.1618163409.git.sergepetrenko@tarantool.org> Message-ID: <2ecf5ae6-abee-5d17-8620-0be4fc6a7899@tarantool.org> Date: Tue, 13 Apr 2021 16:26:07 +0300 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD92FFCB8E6708E74806859AC5FE18436AEED970E897805ADA4182A05F53808504091EFD08147188D04C9A939B80A84168EF3E9AA2825AD0678900AB83814462EA3 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7CE344C11F3296317EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AA32F0A5ADCF96E68638F802B75D45FF914D58D5BE9E6BC1A93B80C6DEB9DEE97C6FB206A91F05B242B5D44BC30DD1863BD073B37858666FE2F41FD49071CA2BD2E47CDBA5A96583C09775C1D3CA48CF17B107DEF921CE79117882F4460429724CE54428C33FAD30A8DF7F3B2552694AC26CFBAC0749D213D2E47CDBA5A9658378DA827A17800CE764603B5C71CE8B8F9FA2833FD35BB23DF004C90652538430302FCEF25BFAB3454AD6D5ED66289B5278DA827A17800CE76FD22633E8AECEADD32BA5DBAC0009BE395957E7521B51C20BC6067A898B09E4090A508E0FED6299176DF2183F8FC7C0BA46D620C74C8671CD04E86FAF290E2D7E9C4E3C761E06A71DD303D21008E298D5E8D9A59859A8B6B372FE9A2E580EFC725E5C173C3A84C3346FD2FB2ADF2DD735872C767BF85DA2F004C90652538430E4A6367B16DE6309 X-B7AD71C0: AC4F5C86D027EB782CDD5689AFBDA7A24209795067102C07E8F7B195E1C9783142177070C5D13C5A437CCAE968C07DD4 X-C1DE0DAB: 0D63561A33F958A588073DA732DCE4DA672B78F101DE1A895240D12E298110FCD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA7502E6951B79FF9A3F410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D340B06C327CE4A70E80D9CB1B9CFB9DB63F951318A69B3EBA607E6AD2C36870FD5921ADC9F219F52C21D7E09C32AA3244C1DD60E2758AF534D805644B230B7C704250262A5EE9971B0FACE5A9C96DEB163 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojnA7/qPBUIXEmfpB2qRGlag== X-Mailru-Sender: 583F1D7ACE8F49BDD2846D59FC20E9F86D25590A9589F10764D7E22BB92177FB8C7764B41FF7EAAB424AE0EB1F3D1D21E2978F233C3FAE6EE63DB1732555E4A8EE80603BA4A5B0BC112434F685709FCF0DA7A0AF5A3A8387 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH 1/9] wal: enrich row's meta information with sync replication flags 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: Serge Petrenko via Tarantool-patches Reply-To: Serge Petrenko Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 12.04.2021 16:06, Cyrill Gorcunov пишет: > On Sun, Apr 11, 2021 at 08:55:56PM +0300, Serge Petrenko wrote: >> diff --git a/src/box/iproto_constants.h b/src/box/iproto_constants.h >> index d3738c705..f7f46088f 100644 >> --- a/src/box/iproto_constants.h >> +++ b/src/box/iproto_constants.h >> @@ -49,9 +49,14 @@ enum { >> XLOG_FIXHEADER_SIZE = 19 >> }; >> >> +/** IPROTO_FLAGS bitfield constants. */ >> enum { >> /** Set for the last xrow in a transaction. */ >> IPROTO_FLAG_COMMIT = 0x01, >> + /** Set for the last row of a tx residing in limbo. */ >> + IPROTO_FLAG_WAIT_SYNC = 0x02, >> + /** Set for the last row of a synchronous tx. */ >> + IPROTO_FLAG_WAIT_ACK = 0x04, >> }; > Serge, is there some particular reason why cant we make them 1:1 > mapping to txn flags? Ie > > IPROTO_FLAG_WAIT_SYNC = 0x10, > IPROTO_FLAG_WAIT_ACK = 0x20, > > this would allow us to eliminate branching in code. While txn flags > are not part of API and iproto flags _are_ I don't expect the former > to be changed anyhow soon? > > Not insisting though, just share the idea. Thanks for the review! Please find my answers inline and the incremental diff below. Actually, this can be done. But it would require us to change the bitfield to something like struct {         bool is_commit : 1;         int unused : 3;         bool wait_sync : 1;         bool wait_ack: 1; }; Otherwise the mapping between opt_flags and the bit fields would be lost. I'd rather stick with your other idea (map between iproto_flags and txn_flags). > >> >> enum iproto_key { >> diff --git a/src/box/journal.h b/src/box/journal.h >> index 76c70c19f..9ab8af2c1 100644 >> --- a/src/box/journal.h >> +++ b/src/box/journal.h >> @@ -63,6 +63,7 @@ struct journal_entry { >> * A journal entry completion callback argument. >> */ >> void *complete_data; >> + uint8_t opt_flags; >> /** >> * Asynchronous write completion function. >> */ >> @@ -97,6 +98,7 @@ journal_entry_create(struct journal_entry *entry, size_t n_rows, >> entry->approx_len = approx_len; >> entry->n_rows = n_rows; >> entry->res = -1; >> + entry->opt_flags = 0; >> } > Please don't ruine alignment here. I know that Vlad prefer dense style > (which is actually hard to parse by eyes at least for me) but here we > have a block which ether should be whole formatted to dense or kept > aligned. Sure, thanks for noticing! > >> >> /** >> diff --git a/src/box/txn.c b/src/box/txn.c >> index 40061ff09..d65315f58 100644 >> --- a/src/box/txn.c >> +++ b/src/box/txn.c >> @@ -76,6 +76,7 @@ txn_add_redo(struct txn *txn, struct txn_stmt *stmt, struct request *request) >> row->lsn = 0; >> row->sync = 0; >> row->tm = 0; >> + row->opt_flags = 0; >> } >> /* >> * Group ID should be set both for requests not having a >> @@ -681,6 +682,10 @@ txn_journal_entry_new(struct txn *txn) >> --req->n_rows; >> } >> >> + req->opt_flags |= >> + (txn_has_flag(txn, TXN_WAIT_SYNC) ? IPROTO_FLAG_WAIT_SYNC : 0) | >> + (txn_has_flag(txn, TXN_WAIT_ACK) ? IPROTO_FLAG_WAIT_ACK : 0); >> + > When mentioning branching I meant this code. We could use 1:1 mapping > here if we define iproto flags to be the same as txn flags. > > Or say some explicit map could be provided (for readability sake) > > static const uint8_t opt_flags_map[] = { > [TXN_WAIT_SYNC] = IPROTO_FLAG_WAIT_SYNC, > [TXN_WAIT_ACK] = IPROTO_FLAG_WAIT_ACK, > }; > > req->opt_flags |= opt_flags_map[txn->flags & IPROTO_FLAG_WAIT_SYNC]; > req->opt_flags |= opt_flags_map[txn->flags & IPROTO_FLAG_WAIT_ACK]; > > Such technique is widely used when need some mapping between flags and > instead of number of `if` one simply use read-only memory. I like this variant, applied. > > On the other hands I'm fine with branching here because this is not > that hot code and even misprediction should not harm much. So just > to share the ideas. > >> return req; >> } >> >> diff --git a/src/box/wal.c b/src/box/wal.c >> index 34af0bda6..00fcb21b4 100644 >> --- a/src/box/wal.c >> +++ b/src/box/wal.c >> @@ -962,14 +962,14 @@ out: >> */ >> static void >> wal_assign_lsn(struct vclock *vclock_diff, struct vclock *base, >> - struct xrow_header **row, >> - struct xrow_header **end) >> + struct journal_entry *entry) >> { >> int64_t tsn = 0; >> - struct xrow_header **start = row; >> - struct xrow_header **first_glob_row = row; >> + struct xrow_header **start = entry->rows; >> + struct xrow_header **end = entry->rows + entry->n_rows; >> + struct xrow_header **first_glob_row = start; > There is no need to define another dependency, actually I think > the compiler will figure out that @first_glob_row and @start > are initialized from same @entry->rows still previously this > was more clear, iow I propose to leave `first_glob_row = entry->rows;` Ok, no problem. > >> /** Assign LSN to all local rows. */ >> - for ( ; row < end; row++) { >> + for (struct xrow_header **row = start; row < end; row++) { >> if ((*row)->replica_id == 0) { >> /* >> * All rows representing local space data >> @@ -996,7 +996,13 @@ wal_assign_lsn(struct vclock *vclock_diff, struct vclock *base, >> first_glob_row = row; >> } >> (*row)->tsn = tsn == 0 ? (*start)->lsn : tsn; >> - (*row)->is_commit = row == end - 1; >> + if (row < end - 1) >> + continue; >> + /* Tx meta is stored in the last tx row. */ >> + if (row == end - 1) { >> + (*row)->opt_flags = entry->opt_flags; >> + (*row)->is_commit = true; > Why can't we use > (*row)->opt_flags = entry->opt_flags | IPROTO_FLAG_COMMIT; > instead? WAL doesn't depend on IPROTO_CONSTANTS and I don't want to introduce such a dependency. OTOH, I don't want it to know of `wait_ack` and `wait_sync` properties of transactions/rows, that's why I have a generic opt_flags field, which WAL blindly assigns to the row, and it's txn_journal_entry_new()'s responsibility to set wait_ack and wait_sync bits. > >> + } >> } else { >> int64_t diff = (*row)->lsn - vclock_get(base, (*row)->replica_id); >> if (diff <= vclock_get(vclock_diff, >> @@ -1020,7 +1026,7 @@ wal_assign_lsn(struct vclock *vclock_diff, struct vclock *base, >> * the first global row. tsn was yet unknown when those >> * rows were processed. >> */ >> - for (row = start; row < first_glob_row; row++) >> + for (struct xrow_header **row = start; row < first_glob_row; row++) >> (*row)->tsn = tsn; >> } >> >> @@ -1098,8 +1104,7 @@ wal_write_to_disk(struct cmsg *msg) >> struct journal_entry *entry; >> struct stailq_entry *last_committed = NULL; >> stailq_foreach_entry(entry, &wal_msg->commit, fifo) { >> - wal_assign_lsn(&vclock_diff, &writer->vclock, >> - entry->rows, entry->rows + entry->n_rows); >> + wal_assign_lsn(&vclock_diff, &writer->vclock, entry); >> entry->res = vclock_sum(&vclock_diff) + >> vclock_sum(&writer->vclock); >> rc = xlog_write_entry(l, entry); >> @@ -1319,8 +1324,7 @@ wal_write_none_async(struct journal *journal, >> struct vclock vclock_diff; >> >> vclock_create(&vclock_diff); >> - wal_assign_lsn(&vclock_diff, &writer->vclock, entry->rows, >> - entry->rows + entry->n_rows); >> + wal_assign_lsn(&vclock_diff, &writer->vclock, entry); >> vclock_merge(&writer->vclock, &vclock_diff); >> vclock_copy(&replicaset.vclock, &writer->vclock); >> entry->res = vclock_sum(&writer->vclock); >> diff --git a/src/box/xrow.c b/src/box/xrow.c >> index bc06738ad..cc8e43ed4 100644 >> --- a/src/box/xrow.c >> +++ b/src/box/xrow.c >> @@ -183,7 +183,7 @@ error: >> break; >> case IPROTO_FLAGS: >> flags = mp_decode_uint(pos); >> - header->is_commit = flags & IPROTO_FLAG_COMMIT; >> + header->opt_flags = flags; >> break; >> default: >> /* unknown header */ >> @@ -299,6 +299,7 @@ xrow_header_encode(const struct xrow_header *header, uint64_t sync, >> * flag to find transaction boundary (last row in the >> * transaction stream). >> */ >> + uint8_t flags_to_encode = header->opt_flags & ~IPROTO_FLAG_COMMIT; >> if (header->tsn != 0) { >> if (header->tsn != header->lsn || !header->is_commit) { >> /* >> @@ -314,12 +315,14 @@ xrow_header_encode(const struct xrow_header *header, uint64_t sync, >> map_size++; >> } >> if (header->is_commit && header->tsn != header->lsn) { >> - /* Setup last row for multi row transaction. */ >> - d = mp_encode_uint(d, IPROTO_FLAGS); >> - d = mp_encode_uint(d, IPROTO_FLAG_COMMIT); >> - map_size++; >> + flags_to_encode |= IPROTO_FLAG_COMMIT; >> } >> } >> + if (flags_to_encode != 0) { >> + d = mp_encode_uint(d, IPROTO_FLAGS); >> + d = mp_encode_uint(d, flags_to_encode); >> + map_size++; >> + } >> assert(d <= data + XROW_HEADER_LEN_MAX); >> mp_encode_map(data, map_size); >> out->iov_len = d - (char *) out->iov_base; >> diff --git a/src/box/xrow.h b/src/box/xrow.h >> index fde8f9474..2a18733c0 100644 >> --- a/src/box/xrow.h >> +++ b/src/box/xrow.h >> @@ -80,14 +80,28 @@ struct xrow_header { >> * transaction. >> */ >> int64_t tsn; >> - /** >> - * True for the last row in a multi-statement transaction, >> - * or single-statement transaction. Is only encoded in the >> - * write ahead log for multi-statement transactions. >> - * Single-statement transactions do not encode >> - * tsn and is_commit flag to save space. >> - */ >> - bool is_commit; >> + /** Transaction meta flags set only in the last transaction row. */ >> + union { >> + uint8_t opt_flags; >> + struct { >> + /** >> + * Is only encoded in the write ahead log for >> + * multi-statement transactions. Single-statement >> + * transactions do not encode tsn and is_commit flag to >> + * save space. >> + */ >> + bool is_commit : 1; >> + /** >> + * True for a synchronous transaction. >> + */ >> + bool wait_sync : 1; >> + /** >> + * True for any transaction that would enter the limbo >> + * (not necessarily a synchronous one). >> + */ >> + bool wait_ack : 1; >> + }; >> + }; > Serge, I know that you mention bitfields in the mail in context of our > discussion and I assume you'll rework this otherwise this will be > just a hidden bug which might work for sometime but one day it will > trigger and we won't even be able to figure out why. As discussed verbally, let's leave this bitfield in place, and introduce some unit test which would check the assumptions I make about bit field order. Will the test help, though? What if a person builds tarantool and doesn't run the tests? Would a Cmake check be enough? > >> >> int bodycnt; >> uint32_t schema_version; >> -- >> 2.24.3 (Apple Git-128) >> > Cyrill Incremental diff: =============================== diff --git a/src/box/journal.h b/src/box/journal.h index 9ab8af2c1..3ce9c869e 100644 --- a/src/box/journal.h +++ b/src/box/journal.h @@ -98,7 +98,7 @@ journal_entry_create(struct journal_entry *entry, size_t n_rows,         entry->approx_len       = approx_len;         entry->n_rows           = n_rows;         entry->res              = -1; -       entry->opt_flags = 0; +       entry->opt_flags        = 0;  }  /** diff --git a/src/box/txn.c b/src/box/txn.c index d65315f58..34e8ce026 100644 --- a/src/box/txn.c +++ b/src/box/txn.c @@ -682,9 +682,13 @@ txn_journal_entry_new(struct txn *txn)                 --req->n_rows;         } -       req->opt_flags |= -               (txn_has_flag(txn, TXN_WAIT_SYNC) ? IPROTO_FLAG_WAIT_SYNC : 0) | -               (txn_has_flag(txn, TXN_WAIT_ACK) ? IPROTO_FLAG_WAIT_ACK : 0); +       static const uint8_t opt_flags_map[] = { +               [TXN_WAIT_SYNC] = IPROTO_FLAG_WAIT_SYNC, +               [TXN_WAIT_ACK] = IPROTO_FLAG_WAIT_ACK, +       }; + +       req->opt_flags |= opt_flags_map[txn->flags & TXN_WAIT_SYNC]; +       req->opt_flags |= opt_flags_map[txn->flags & TXN_WAIT_ACK];         return req;  } diff --git a/src/box/wal.c b/src/box/wal.c index 00fcb21b4..b7c69fc59 100644 --- a/src/box/wal.c +++ b/src/box/wal.c @@ -967,7 +967,7 @@ wal_assign_lsn(struct vclock *vclock_diff, struct vclock *base,         int64_t tsn = 0;         struct xrow_header **start = entry->rows;         struct xrow_header **end = entry->rows + entry->n_rows; -       struct xrow_header **first_glob_row = start; +       struct xrow_header **first_glob_row = entry->rows;         /** Assign LSN to all local rows. */         for (struct xrow_header **row = start; row < end; row++) {                 if ((*row)->replica_id == 0) { -- Serge Petrenko