From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladimir Davydov Subject: [PATCH 06/10] ddl: restore sequence value if drop is rolled back Date: Wed, 3 Jul 2019 22:30:08 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: To: kostja@tarantool.org Cc: tarantool-patches@freelists.org List-ID: A sequence isn't supposed to roll back to the old value if the transaction it was used in is aborted for some reason. However, if a sequence is dropped, we do want to restore the original value on rollback so that we don't lose it on an unsuccessful attempt to drop the sequence. --- src/box/alter.cc | 24 ++++++++++++++++++++++++ test/box/sequence.result | 21 +++++++++++++++++++++ test/box/sequence.test.lua | 10 ++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/box/alter.cc b/src/box/alter.cc index 89ab9c65..4f046ef6 100644 --- a/src/box/alter.cc +++ b/src/box/alter.cc @@ -3460,6 +3460,20 @@ on_replace_dd_sequence(struct trigger * /* trigger */, void *event) trigger_run_xc(&on_alter_sequence, seq); } +/** Restore the old sequence value on rollback. */ +static void +on_drop_sequence_data_rollback(struct trigger *trigger, void * /* event */) +{ + struct tuple *tuple = (struct tuple *)trigger->data; + uint32_t id = tuple_field_u32_xc(tuple, BOX_SEQUENCE_DATA_FIELD_ID); + int64_t val = tuple_field_i64_xc(tuple, BOX_SEQUENCE_DATA_FIELD_VALUE); + + struct sequence *seq = sequence_by_id(id); + assert(seq != NULL); + if (sequence_set(seq, val) != 0) + panic("Can't restore sequence value"); +} + /** * A trigger invoked on replace in space _sequence_data. * Used to update a sequence value. @@ -3483,6 +3497,16 @@ on_replace_dd_sequence_data(struct trigger * /* trigger */, void *event) if (sequence_set(seq, value) != 0) diag_raise(); } else { /* DELETE */ + /* + * A sequence isn't supposed to roll back to the old + * value if the transaction it was used in is aborted + * for some reason. However, if a sequence is dropped, + * we do want to restore the original sequence value + * on rollback. + */ + struct trigger *on_rollback = txn_alter_trigger_new( + on_drop_sequence_data_rollback, old_tuple); + txn_on_rollback(txn, on_rollback); sequence_reset(seq); } } diff --git a/test/box/sequence.result b/test/box/sequence.result index 99eed0ec..91795290 100644 --- a/test/box/sequence.result +++ b/test/box/sequence.result @@ -2249,3 +2249,24 @@ sq:drop() s:drop() --- ... +-- +-- Check that if a deletion from _sequence_data is rolled back, +-- the sequence state is restored. +-- +sq = box.schema.sequence.create('test') +--- +... +sq:next() -- 1 +--- +- 1 +... +box.begin() box.space._sequence_data:delete{sq.id} box.rollback() +--- +... +sq:next() -- 2 +--- +- 2 +... +sq:drop() +--- +... diff --git a/test/box/sequence.test.lua b/test/box/sequence.test.lua index 9615c447..b0ec020e 100644 --- a/test/box/sequence.test.lua +++ b/test/box/sequence.test.lua @@ -761,3 +761,13 @@ s:insert{box.NULL} -- ok s.index.pk:alter{sequence = false} sq:drop() s:drop() + +-- +-- Check that if a deletion from _sequence_data is rolled back, +-- the sequence state is restored. +-- +sq = box.schema.sequence.create('test') +sq:next() -- 1 +box.begin() box.space._sequence_data:delete{sq.id} box.rollback() +sq:next() -- 2 +sq:drop() -- 2.11.0