Discussion:
[leveldb] few questions about WriteBatch
Ren Z
2016-06-30 13:21:48 UTC
Permalink
I have few questions regarding WriteBatch:

1) Consider this:

one thread:

using (WriteBatch batch = new WriteBatch())
{
for(int i=0; i<100; i++)
{
batch.Put(i.ToString(), i.ToString())
}
leveld_db.Write(batch);
}



parallel thread:

using (WriteBatch batch = new WriteBatch())
{
for(int i=0; i<100; i++)
{
batch.Put(i.ToString(), (i+1000).ToString())
}
leveld_db.Write(batch);
}



Is there a guarantee that I won't end up with data like {10, 10} {11, 1011}
? (I.e. I expect that only one set or the other stays in db and not mixed,
is this assumption correct?)


2) Somehow related:

If I get to make a lot of changes through a WriteBatch and then write it
all is there a guarantee that another thread won't read in the middle of
writing to disk and end up with some new values and some old (which are
about to be modified) ?


Thanks in advance.
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Dhruba Borthakur
2016-06-30 14:01:03 UTC
Permalink
Post by Ren Z
Is there a guarantee that I won't end up with data like {10, 10} {11,
1011} ? (I.e. I expect that only one set or the other stays in db and not
mixed, is this assumption correct?)
Your assumption is correct.
Post by Ren Z
all is there a guarantee that another thread won't read in the middle of
writing to disk and end up with some new values and some old (which are
about to be modified) ?
Yes, it is guaranteed.

The way it is implemented internally is that all the writes inside a single
WriteBatch is assigned the same sequence number and is written as a single
entry in the write-ahead log. This ensure that the entire write batch makes
it to the DB or none at all.

Similarly, a Get/Scan call picks up all data upto a specified sequence
number, and since all the individual writes inside a writebatch has the
same seqno, all of them will show up in a Get call.
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Robert Escriva
2016-06-30 14:23:22 UTC
Permalink
Post by Ren Z
Is there a guarantee that I won't end up with data like {10, 10} {11, 1011}
? (I.e. I expect that only one set or the other stays in db and not mixed,
is this assumption correct?)
Your assumption is correct.
Not with the example code. Both write batches will be written because
they don't overlap. If there was overlap, it is guaranteed that the
keys in common will be those written by just one of the write batches.
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Mauricio Fernández
2016-07-06 09:07:30 UTC
Permalink
Post by Robert Escriva
Post by Ren Z
Is there a guarantee that I won't end up with data like {10, 10} {11, 1011}
? (I.e. I expect that only one set or the other stays in db and not mixed,
is this assumption correct?)
Your assumption is correct.
Not with the example code. Both write batches will be written because
they don't overlap. If there was overlap, it is guaranteed that the
keys in common will be those written by just one of the write batches.
Does that hold on system crash?
I seem to remember that LevelDB tried to recover incomplete log records on DB
open.
--
Mauricio Fernández
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
sel-fish
2016-06-30 15:44:35 UTC
Permalink
all the individual writes inside a writebatch has the same seqno
I'm afraid i can't agree with that as the code says :

WriteBatchInternal::SetSequence(updates, last_sequence + 1);
last_sequence += WriteBatchInternal::Count(updates);



圚 2016幎6月30日星期四 UTC+8䞋午10:01:06dhruba写道
Post by Ren Z
Is there a guarantee that I won't end up with data like {10, 10} {11,
1011} ? (I.e. I expect that only one set or the other stays in db and not
mixed, is this assumption correct?)
Your assumption is correct.
Post by Ren Z
all is there a guarantee that another thread won't read in the middle of
writing to disk and end up with some new values and some old (which are
about to be modified) ?
Yes, it is guaranteed.
The way it is implemented internally is that all the writes inside a
single WriteBatch is assigned the same sequence number and is written as a
single entry in the write-ahead log. This ensure that the entire write
batch makes it to the DB or none at all.
Similarly, a Get/Scan call picks up all data upto a specified sequence
number, and since all the individual writes inside a writebatch has the
same seqno, all of them will show up in a Get call.
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ren Z
2016-06-30 15:10:43 UTC
Permalink
Regarding 2) here http://stackoverflow.com/a/38125392/579026 are some
thoughts why it may not be guaranteed, could you comment on that?
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Robert Escriva
2016-06-30 15:50:43 UTC
Permalink
They didn't read the code well enough.

When doing a Get (or taking a snapshot), the code calls the
LastSequence() method of the version set to get a version number. Reads
will ignore anything after this number.

Writers set this number only after making every element in a write batch
visible. This guarantees the reader will atomically see all of the
write batch or none of it. While every element in a write batch will
not necessarily have the same sequence number, a reader will not be able
to observe a version that splits the write batch.
Regarding 2) here http://stackoverflow.com/a/38125392/579026 are some thoughts
why it may not be guaranteed, could you comment on that?
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
sel-fish
2016-07-01 03:18:12 UTC
Permalink
yes, @Robert Escriva is right, i didn't read the code well enough.
i fix my answer and thanks for the sharing.

圚 2016幎6月30日星期四 UTC+8䞋午11:50:35Robert Escriva写道
Post by Robert Escriva
They didn't read the code well enough.
When doing a Get (or taking a snapshot), the code calls the
LastSequence() method of the version set to get a version number. Reads
will ignore anything after this number.
Writers set this number only after making every element in a write batch
visible. This guarantees the reader will atomically see all of the
write batch or none of it. While every element in a write batch will
not necessarily have the same sequence number, a reader will not be able
to observe a version that splits the write batch.
Post by Ren Z
Regarding 2) here http://stackoverflow.com/a/38125392/579026 are some
thoughts
Post by Ren Z
why it may not be guaranteed, could you comment on that?
--
You received this message because you are subscribed to the Google
Groups
Post by Ren Z
"leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send
an email
Post by Ren Z
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "leveldb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leveldb+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...