Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WritePrepared Txn: Duplicate Keys, Memtable part #3418

Closed

Conversation

maysamyabandeh
Copy link
Contributor

Currently DB does not accept duplicate keys (keys with the same user key and the same sequence number). If Memtable returns false when receiving such keys, we can benefit from this signal to properly increase the sequence number in the rare cases when we have a duplicate key in the write batch written to DB under WritePrepared transactions.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maysamyabandeh has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

db/dbformat.h Outdated
@@ -162,6 +162,8 @@ class InternalKeyComparator

virtual const char* Name() const override;
virtual int Compare(const Slice& a, const Slice& b) const override;
// Same as Compare except that it excludes teh value type from comparison
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: the

db/dbformat.h Outdated
@@ -162,6 +162,8 @@ class InternalKeyComparator

virtual const char* Name() const override;
virtual int Compare(const Slice& a, const Slice& b) const override;
// Same as Compare except that it excludes teh value type from comparison
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where else is using Compare()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare() the is essential part of Comparator. I guess you mean where else we use InternalKeyComparator to which the answer would be everywhere :) It is all over the source code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering whether it is possible to make Compare() not taking types into account, although it would be very risky to do so.

db/memtable.cc Outdated
@@ -473,7 +473,10 @@ void MemTable::Add(SequenceNumber s, ValueType type,
Slice prefix = insert_with_hint_prefix_extractor_->Transform(key_slice);
table->InsertWithHint(handle, &insert_hints_[prefix]);
} else {
table->Insert(handle);
bool res = table->Insert(handle);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should do the same for InsertWithHint().

db/memtable.cc Outdated
@@ -815,7 +820,12 @@ void MemTable::Update(SequenceNumber seq,
}

// key doesn't exist
Add(seq, kTypeValue, key, value);
#ifndef NDEBUG
bool add_res =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use __attribute__((__unused__)) is cleaner than use NDEBUG macro.

db/memtable.cc Outdated
@@ -793,6 +797,7 @@ void MemTable::Update(SequenceNumber seq,
ValueType type;
SequenceNumber unused;
UnPackSequenceAndType(tag, &unused, &type);
assert(unused != seq);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename unused to something else (e.g. existing_seq)?

@@ -81,7 +81,7 @@ class MemTableRep {
// single buffer and pass that in as the parameter to Insert).
// REQUIRES: nothing that compares equal to key is currently in the
// collection, and no concurrent modifications to the table in progress
virtual void Insert(KeyHandle handle) = 0;
virtual bool Insert(KeyHandle handle) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Comment what's return means
  • InsertWithHint and InsertConcurrent should also return duplicate key error.
  • Shall we handle duplicate keys for other memtable types if that's not too difficult to do? Or alternatively, shall we add a method (e.g. MemTableRep::CanHandleDuplicatedKey() ?) to denote the behavior difference?

@facebook-github-bot
Copy link
Contributor

@maysamyabandeh has updated the pull request. View: changes, changes since last import

batch_cnt = 1

count batches for CommitBatchInternal

commit multiple batches in WriteBatch

exclude type from memtable key compare

Replace Collapse with DuplicateKeysCnt

handle out-of-order duplicates

rollback with duplicate keys

commititmewritebatch with duplicatae keys

test dup key with different cf

fix compile problems

rollback Update return status

move the test to db_memtable_test
@facebook-github-bot
Copy link
Contributor

@maysamyabandeh has updated the pull request.

@facebook-github-bot
Copy link
Contributor

@maysamyabandeh has updated the pull request. View: changes, changes since last import

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maysamyabandeh has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@yiwu-arbug yiwu-arbug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maysamyabandeh is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants