Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: Konstantin Osipov <kostja@tarantool.org>
Cc: tarantool-patches@freelists.org
Subject: Re: [PATCH 1/4] vinyl: fix EQ check in run iterator
Date: Tue, 15 May 2018 22:23:59 +0300	[thread overview]
Message-ID: <20180515192359.zvevt7wbswkgvat6@esperanza> (raw)
In-Reply-To: <20180515190512.GA24119@atlas>

On Tue, May 15, 2018 at 10:05:12PM +0300, Konstantin Osipov wrote:
> * Vladimir Davydov <vdavydov.dev@gmail.com> [18/05/15 17:10]:
> 
> I am pushing this as is now, please a couple of comments below.
> 
> > vy_run_iterator_seek() is supposed to check that the resulting statement
> > matches the search key in case of ITER_EQ, but if the search key lies at
> > the beginning of the slice, it doesn't. As a result, vy_point_lookup()
> > may fail to find an existing tuple as demonstrated below.
> > 
> > Suppose we are looking for key {10} in the primary index which consists
> > of an empty mem and two runs:
> > 
> >     run 1: DELETE{15}
> >     run 2: INSERT{10}
> > 
> > vy_run_iterator_next() returns DELETE{15} for run 1 because of the
> > missing EQ check and vy_point_lookup() stops at run 1 (since the
> > terminal statement is found) and mistakenly returns NULL.
> > 
> > The issue manifests itself as crash in vinyl_iterator_secondary_next(),
> > when we fail to find the tuple in the primary index corresponding to a
> > statement found in a secondary index.
> 
> I believe this explanation belongs to the code, not only to the
> changeset comment.

Well, all those ITER_EQ checks are scattered throughout iterator code
and none of them has a comment as they all are pretty self-explaining.
Adding a comment to just one of them looks pointless IMO.

Actually, I was thinking about extracting all the EQ checks out of
source iterators and moving them to vy_read_iterator as this would allow
to reduce the number of EQ comparisons, but I didn't get my hands on it
as there were some problems with the cache iterator. May be, later.

> 
> Ideally, this special case should be covered in a unit test, not
> just in the code or in CS comment.

After the last patch in the series is applied, vinyl/select_consistency
functional test triggers this bug in 100% cases. Anyway, I'll think
about a unit test.

> 
> > Part of #3393
> > ---
> >  src/box/vy_run.c | 13 ++++++++++++-
> >  1 file changed, 12 insertions(+), 1 deletion(-)
> > 
> > diff --git a/src/box/vy_run.c b/src/box/vy_run.c
> > index 587cb002..8c922895 100644
> > --- a/src/box/vy_run.c
> > +++ b/src/box/vy_run.c
> > @@ -1316,6 +1316,7 @@ vy_run_iterator_seek(struct vy_run_iterator *itr,
> >  {
> >  	const struct key_def *cmp_def = itr->cmp_def;
> >  	struct vy_slice *slice = itr->slice;
> > +	const struct tuple *check_eq_key = NULL;
> >  	int cmp;
> >  
> >  	if (slice->begin != NULL &&
> > @@ -1340,6 +1341,8 @@ vy_run_iterator_seek(struct vy_run_iterator *itr,
> >  			return 0;
> >  		}
> >  		if (cmp < 0 || (cmp == 0 && iterator_type != ITER_GT)) {
> > +			if (iterator_type == ITER_EQ)
> > +				check_eq_key = key;
> >  			iterator_type = ITER_GE;
> >  			key = slice->begin;
> >  		}
> > @@ -1365,7 +1368,15 @@ vy_run_iterator_seek(struct vy_run_iterator *itr,
> >  		}
> >  	}
> >  
> > -	return vy_run_iterator_do_seek(itr, iterator_type, key, ret);
> > +	if (vy_run_iterator_do_seek(itr, iterator_type, key, ret) != 0)
> > +		return -1;
> > +
> > +	if (check_eq_key != NULL && *ret != NULL &&
> > +	    vy_stmt_compare(check_eq_key, *ret, cmp_def) != 0) {
> > +		vy_run_iterator_stop(itr);
> > +		*ret = NULL;
> > +	}
> 
> As far as I understand the code flow, this adds an extra check for
> cases when key != slice->begin. This is 99.9% of cases. Can we
> avoid an extra check if it is not needed?

We do this extra tuple comparison only if cmp == 0 and the original
iterator_type is ITER_EQ. The (cmp < 0) case is excluded, because we
stop the iteration in this case for ITER_EQ - see several lines above:

> 		cmp = vy_stmt_compare_with_key(key, slice->begin, cmp_def);
> 		if (cmp < 0 && iterator_type == ITER_EQ) {
> 			vy_run_iterator_stop(itr);
> 			return 0;
> 		}
> 		if (cmp < 0 || (cmp == 0 && iterator_type != ITER_GT)) {
> 			if (iterator_type == ITER_EQ)
> 				check_eq_key = key;

				^^^^^^^^^^^^^^^^^^
cmp can't be < 0 if this assignment takes place

> 			iterator_type = ITER_GE;
> 			key = slice->begin;
> 		}

Looks rather messy, but then again I think we should try to clean up
this mess by extracting ITER_EQ check out of source iterators.

Thanks.

  reply	other threads:[~2018-05-15 19:23 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-15 14:08 [PATCH 0/4] vinyl: fix crash in vinyl_iterator_secondary_next() Vladimir Davydov
2018-05-15 14:08 ` [PATCH 1/4] vinyl: fix EQ check in run iterator Vladimir Davydov
2018-05-15 19:05   ` Konstantin Osipov
2018-05-15 19:23     ` Vladimir Davydov [this message]
2018-05-15 14:08 ` [PATCH 2/4] vinyl: fix lost key on dump completion Vladimir Davydov
2018-05-15 19:20   ` Konstantin Osipov
2018-05-15 19:27     ` Vladimir Davydov
2018-05-15 14:08 ` [PATCH 3/4] vinyl: do not panic if secondary index is inconsistent with primary Vladimir Davydov
2018-05-15 14:08 ` [PATCH 4/4] test: improve vinyl/select_consistency 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=20180515192359.zvevt7wbswkgvat6@esperanza \
    --to=vdavydov.dev@gmail.com \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH 1/4] vinyl: fix EQ check in run iterator' \
    /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