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

Iterator: Always add key to txn.reads #1328

Merged
merged 11 commits into from
Jun 1, 2020
Merged

Conversation

jarifibrahim
Copy link
Contributor

@jarifibrahim jarifibrahim commented May 14, 2020

The SSI guarantees provided by badger are respected when multiple concurrent transactions try to update the same key at once using the txn.Get and txn.Set API

// txn.Get
// if not found; txn.Set and Commit

but the same guarantees fail when an iterator is used instead of the txn.Get API

// itr.Seek(key)
// if not itr.Valid(); txn.Set and Commit

If the code about is executed via multiple concurrent transactions, we might lose some updates.

The test TestConflict added in this PR fails on master but works fine on this PR.

Fixes #1289


This change is Reviewable

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

Be very careful.

Reviewable status: 0 of 3 files reviewed, all discussions resolved (waiting on @ashish-goswami and @manishrjain)

@swdee
Copy link

swdee commented May 16, 2020

I have tested your fix from branch ibrahim/itr-seek-conflict against the code from original bug report #1289 and confirm it now reports the conflict transaction as expected.

Output from test runs;

$ go run  bug-report.go
2020/05/16 15:32:52 insert error:Transaction Conflict. Please retry
gi= 15 NOT DUPLICATE
exit status 1

$ go run  bug-report.go
gi= 1 NOT DUPLICATE
gi= 6 was duplicate
gi= 4 was duplicate
gi= 7 was duplicate
gi= 5 was duplicate
gi= 13 was duplicate
gi= 9 was duplicate
gi= 8 was duplicate
gi= 2 was duplicate
gi= 0 was duplicate
gi= 14 was duplicate
gi= 10 was duplicate
gi= 15 was duplicate
gi= 3 was duplicate
gi= 11 was duplicate
gi= 12 was duplicate
done
badger 2020/05/16 15:32:54 INFO: Got compaction priority: {level:0 score:1.73 dropPrefix:[]}
badger 2020/05/16 15:32:54 INFO: Running for level: 0
badger 2020/05/16 15:32:54 INFO: LOG Compact 0->1, del 1 tables, add 1 tables, took 3.123552ms
badger 2020/05/16 15:32:54 INFO: Compaction for level: 0 DONE
badger 2020/05/16 15:32:54 INFO: Force compaction on level 0 done

$ go run  bug-report.go
gi= 15 NOT DUPLICATE
gi= 6 was duplicate
gi= 14 was duplicate
gi= 0 was duplicate
gi= 1 was duplicate
gi= 2 was duplicate
gi= 3 was duplicate
gi= 4 was duplicate
gi= 5 was duplicate
gi= 11 was duplicate
gi= 12 was duplicate
gi= 13 was duplicate
gi= 10 was duplicate
gi= 8 was duplicate
gi= 7 was duplicate
gi= 9 was duplicate
done
badger 2020/05/16 15:32:58 INFO: Got compaction priority: {level:0 score:1.73 dropPrefix:[]}
badger 2020/05/16 15:32:58 INFO: Running for level: 0
badger 2020/05/16 15:32:58 INFO: LOG Compact 0->1, del 1 tables, add 1 tables, took 2.958952ms
badger 2020/05/16 15:32:58 INFO: Compaction for level: 0 DONE
badger 2020/05/16 15:32:58 INFO: Force compaction on level 0 done

$ go run  bug-report.go
gi= 15 NOT DUPLICATE
gi= 1 was duplicate
gi= 0 was duplicate
gi= 2 was duplicate
gi= 4 was duplicate
gi= 3 was duplicate
gi= 5 was duplicate
gi= 6 was duplicate
gi= 7 was duplicate
gi= 8 was duplicate
gi= 9 was duplicate
gi= 10 was duplicate
gi= 11 was duplicate
gi= 12 was duplicate
gi= 14 was duplicate
gi= 13 was duplicate
done
badger 2020/05/16 15:33:00 INFO: Got compaction priority: {level:0 score:1.73 dropPrefix:[]}
badger 2020/05/16 15:33:00 INFO: Running for level: 0
badger 2020/05/16 15:33:00 INFO: LOG Compact 0->1, del 1 tables, add 1 tables, took 3.106956ms
badger 2020/05/16 15:33:00 INFO: Compaction for level: 0 DONE
badger 2020/05/16 15:33:00 INFO: Force compaction on level 0 done

$ go run  bug-report.go
gi= 15 NOT DUPLICATE
2020/05/16 15:33:03 insert error:Transaction Conflict. Please retry
gi= 11 was duplicate
gi= 14 was duplicate
gi= 0 was duplicate
gi= 1 was duplicate
exit status 1

So things look good to me now thank you.

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: 0 of 3 files reviewed, 1 unresolved discussion (waiting on @ashish-goswami and @jarifibrahim)


txn.go, line 488 at r2 (raw file):

func (txn *Txn) addReadKey(key []byte) {
	if txn.update {
		fp := z.MemHash(key)

avoid changes.

Copy link
Contributor Author

@jarifibrahim jarifibrahim left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 2 files reviewed, 1 unresolved discussion (waiting on @ashish-goswami and @manishrjain)


txn.go, line 488 at r2 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

avoid changes.

Done.

@jarifibrahim jarifibrahim merged commit da80eb9 into master Jun 1, 2020
@jarifibrahim jarifibrahim deleted the ibrahim/itr-seek-conflict branch June 1, 2020 10:28
jarifibrahim pushed a commit that referenced this pull request Jul 8, 2020
This PR adds the following changes from **Master branch to the dgraph-maintenance branch**.
```
25fd0ef Update ristretto to commit f66de99 (#1391)
fac972f Update head while replaying value log (#1372)
e5fd05a Rework DB.DropPrefix (#1381)
adeb842 Fix assert in background compression and encryption. (#1366)
```

The **master and dgraph-maintenace branch** have following difference 
`+`  (in green) means commit missing on dgraph-maintenance
 `-` (in red) means the commit exists.
```diff
+ 079f5ae DefaultOptions: Set KeepL0InMemory to false (#1345)
+ 7e19cac Add immudb to the project list (#1341)
+ da80eb9 Iterator: Always add key to txn.reads (#1328)
+ a7e239e StreamWriter: Close head writer (#1347)
+ 543f353 Fix build on golang tip (#1355)
+ fd89894 Compaction: Expired keys and delete markers are never purged (#1354)
+ 056d859 Support disabling conflict detection (#1344)
+ b762832 Tests: Do not leave behind state goroutines (#1349)
+ b2267c2 Restore: Account for value size as well (#1358)
+ 14386ac GC: Consider size of value while rewriting (#1357)
+ c45d966 Fix assert in background compression and encryption. (#1366)
+ dd332b0 Avoid panic in filltables() (#1365)
+ 3f4761d Force KeepL0InMemory to be true when InMemory is true (#1375)
+ d37ce36 Tests: Use t.Parallel in TestIteratePrefix tests  (#1377)
+ 158d927 Remove second initialization of writech in Open (#1382)
+ 675efcd Increase default valueThreshold from 32B to 1KB (#1346)
+ 3042e37 pre allocate cache key for the block cache and the bloom filter cache (#1371)
+ e013bfd Rework DB.DropPrefix (#1381)
+ 509de73 Update head while replaying value log (#1372)
+ 09dfa66 Update ristretto to commit f66de99 (#1391)
+ 717b89c Enable cross-compiled 32bit tests on TravisCI (#1392)
```

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/dgraph-io/badger/1398)
<!-- Reviewable:end -->
jarifibrahim pushed a commit that referenced this pull request Oct 2, 2020
The SSI guarantees provided by badger are respected when 
multiple concurrent transactions try to update the same key at 
once using the txn.Get and txn.Set API
```
// txn.Get
// if not found; txn.Set and Commit
```
but the same guarantees fail when an `iterator` is used instead 
of the `txn.Get` API
```
// itr.Seek(key)
// if not itr.Valid(); txn.Set and Commit
```
If the code above is executed via multiple concurrent transactions,
we might overwrite the first update.

The test `TestConflict` added in this PR fails on master but works fine
on this PR.

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

Successfully merging this pull request may close these issues.

Question: Insert on Duplicate Transaction
3 participants