Repository: yorkie-team/yorkie
Pull request #297 (2022-01-26 06:18:42)

Fix snapshot interval to make them trigger properly in memdb
<!--  Thanks for sending a pull request! -->

**What this PR does / why we need it**:

Fix snapshot interval to make them trigger properly in memdb

```
```

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

**Additional documentation**:

<!--
This section can be blank if this pull request does not require a release note.

Please use the following format for linking documentation:
- [Usage]: <link>
- [Other doc]: <link>
-->
```docs

```

**Checklist**:
- [x] Added relevant tests or not required
- [x] Didn't break anything


codecov[bot]: # [Codecov](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) Report
> Merging [#297](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) (621f994) into [main](https://codecov.io/gh/yorkie-team/yorkie/commit/77892ce956ca925203957b715974b39e7f39dd6a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) (77892ce) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/yorkie-team/yorkie/pull/297/graphs/tree.svg?width=650&height=150&src=pr&token=P6COB1ZNIC&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team)](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team)

```diff
@@           Coverage Diff           @@
##             main     #297   +/-   ##
=======================================
  Coverage   48.95%   48.95%           
=======================================
  Files          59       59           
  Lines        4764     4764           
=======================================
  Hits         2332     2332           
  Misses       2170     2170           
  Partials      262      262           
```


| [Impacted Files](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) | Coverage Δ | |
|---|---|---|
| [yorkie/backend/db/memory/db.go](https://codecov.io/gh/yorkie-team/yorkie/pull/297/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team#diff-eW9ya2llL2JhY2tlbmQvZGIvbWVtb3J5L2RiLmdv) | `47.83% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team). Last update [77892ce...621f994](https://codecov.io/gh/yorkie-team/yorkie/pull/297?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team).


Patch:
diff --git a/yorkie/backend/db/memory/db.go b/yorkie/backend/db/memory/db.go
index 02b64a2e..f578daa9 100644
--- a/yorkie/backend/db/memory/db.go
+++ b/yorkie/backend/db/memory/db.go
@@ -19,6 +19,7 @@ package memory
 import (
 	"context"
 	"fmt"
+	"math"
 	gotime "time"
 
 	"github.com/hashicorp/go-memdb"
@@ -420,13 +421,11 @@ func (d *DB) FindLastSnapshotInfo(
 	txn := d.db.Txn(false)
 	defer txn.Abort()
 
-	// TODO(hackerwins): I couldn't find a efficient way to get the snapshotInfo
-	// with the last serverSeq of the given docID.
 	iterator, err := txn.ReverseLowerBound(
 		tblSnapshots,
 		"doc_id_server_seq",
 		docID.String(),
-		uint64(0),
+		uint64(math.MaxUint64),
 	)
 	if err != nil {
 		return nil, err


Test patch:
diff --git a/yorkie/backend/db/memory/db_test.go b/yorkie/backend/db/memory/db_test.go
index d00f93f7..2d003ee4 100644
--- a/yorkie/backend/db/memory/db_test.go
+++ b/yorkie/backend/db/memory/db_test.go
@@ -26,6 +26,7 @@ import (
 	"github.com/stretchr/testify/assert"
 
 	"github.com/yorkie-team/yorkie/pkg/document"
+	"github.com/yorkie-team/yorkie/pkg/document/change"
 	"github.com/yorkie-team/yorkie/pkg/document/proxy"
 	"github.com/yorkie-team/yorkie/pkg/document/time"
 	"github.com/yorkie-team/yorkie/yorkie/backend/db"
@@ -162,17 +163,23 @@ func TestDB(t *testing.T) {
 
 		doc := document.New("tests", t.Name())
 		doc.SetActor(actorID)
+
 		assert.NoError(t, doc.Update(func(root *proxy.ObjectProxy) error {
 			root.SetNewArray("array")
 			return nil
 		}))
 
-		err = memdb.CreateSnapshotInfo(ctx, docInfo.ID, doc.InternalDocument())
+		assert.NoError(t, memdb.CreateSnapshotInfo(ctx, docInfo.ID, doc.InternalDocument()))
+		snapshot, err := memdb.FindLastSnapshotInfo(ctx, docInfo.ID)
 		assert.NoError(t, err)
+		assert.Equal(t, uint64(0), snapshot.ServerSeq)
 
-		snapshot, err := memdb.FindLastSnapshotInfo(ctx, docInfo.ID)
+		pack := change.NewPack(doc.Key(), doc.Checkpoint().NextServerSeq(1), nil, nil)
+		assert.NoError(t, doc.ApplyChangePack(pack))
+		assert.NoError(t, memdb.CreateSnapshotInfo(ctx, docInfo.ID, doc.InternalDocument()))
+		snapshot, err = memdb.FindLastSnapshotInfo(ctx, docInfo.ID)
 		assert.NoError(t, err)
-		assert.NotNil(t, snapshot)
+		assert.Equal(t, uint64(1), snapshot.ServerSeq)
 	})
 
 	t.Run("housekeeping test", func(t *testing.T) {