Skip to content

Commit

Permalink
refactor(storage): do not write a commit context for each commit
Browse files Browse the repository at this point in the history
Previously, the log stream wrote a commit context for each commit. However, to fix kakao#125, we no
longer need to persist in a sequence of commit contexts. So this patch removes lines that keep all
commit context.

Updates kakao#125
  • Loading branch information
ijsong committed Nov 7, 2022
1 parent b25b869 commit 84e2eef
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 95 deletions.
41 changes: 0 additions & 41 deletions internal/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ func (s *Storage) NewCommitBatch(cc CommitContext) (*CommitBatch, error) {
_ = cb.Close()
return nil, err
}
if err := cb.batch.Set(encodeCommitContextKeyInternal(cc, cb.cck), nil, nil); err != nil {
_ = cb.Close()
return nil, err
}
return cb, nil
}

Expand Down Expand Up @@ -184,43 +180,6 @@ func (s *Storage) ReadCommitContext() (cc CommitContext, err error) {
return decodeCommitContext(buf), nil
}

// CommitContextOf looks up a commit context that contains the log entry for the argument glsn.
func (s *Storage) CommitContextOf(glsn types.GLSN) (cc CommitContext, err error) {
cck := make([]byte, commitContextKeyLength)
cck = encodeCommitContextKeyInternal(CommitContext{HighWatermark: glsn}, cck)
it := s.db.NewIter(&pebble.IterOptions{
LowerBound: cck,
UpperBound: []byte{commitContextKeySentinelPrefix},
})
defer func() {
_ = it.Close()
}()
if it.First() {
cc = decodeCommitContextKey(it.Key())
if cc.CommittedGLSNBegin <= glsn && glsn < cc.CommittedGLSNEnd {
return cc, nil
}
}
return cc, ErrNoCommitContext
}

// NextCommitContextOf returns the next commit context after the argument cc if exists.
func (s *Storage) NextCommitContextOf(cc CommitContext) (next CommitContext, err error) {
cck := make([]byte, commitContextKeyLength)
cck = encodeCommitContextKeyInternal(cc, cck)
it := s.db.NewIter(&pebble.IterOptions{
LowerBound: cck,
UpperBound: []byte{commitContextKeySentinelPrefix},
})
defer func() {
_ = it.Close()
}()
if !it.First() || !decodeCommitContextKey(it.Key()).Equal(cc) || !it.Next() {
return next, ErrNoCommitContext
}
return decodeCommitContextKey(it.Key()), nil
}

// Trim deletes log entries whose GLSNs are less than or equal to the argument
// glsn. Internally, it removes records for both data and commits but does not
// remove the commit context.
Expand Down
54 changes: 0 additions & 54 deletions internal/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,60 +723,6 @@ func TestStorageReadRecoveryPoints_InconsistentWriteCommit(t *testing.T) {
})
}

func TestStorage_CommitContextOf(t *testing.T) {
testStorage(t, func(t testing.TB, stg *Storage) {
cc1 := CommitContext{
Version: 1,
HighWatermark: 10,
CommittedGLSNBegin: 8,
CommittedGLSNEnd: 11,
CommittedLLSNBegin: 1,
}
cc2 := CommitContext{
Version: 2,
HighWatermark: 22,
CommittedGLSNBegin: 21,
CommittedGLSNEnd: 23,
CommittedLLSNBegin: 4,
}
cb, err := stg.NewCommitBatch(cc1)
assert.NoError(t, err)
assert.NoError(t, cb.Apply())
assert.NoError(t, cb.Close())

cb, err = stg.NewCommitBatch(cc2)
assert.NoError(t, err)
assert.NoError(t, cb.Apply())
assert.NoError(t, cb.Close())

for glsn := types.GLSN(0); glsn < cc1.CommittedGLSNBegin; glsn++ {
_, err := stg.CommitContextOf(glsn)
assert.ErrorIs(t, err, ErrNoCommitContext)
}
for glsn := cc1.CommittedGLSNBegin; glsn < cc1.CommittedGLSNEnd; glsn++ {
cc, err := stg.CommitContextOf(glsn)
assert.NoError(t, err)
assert.Equal(t, cc1, cc)

cc, err = stg.NextCommitContextOf(cc)
assert.NoError(t, err)
assert.Equal(t, cc2, cc)
}
for glsn := cc1.CommittedGLSNEnd; glsn < cc2.CommittedGLSNBegin; glsn++ {
_, err := stg.CommitContextOf(glsn)
assert.ErrorIs(t, err, ErrNoCommitContext)
}
for glsn := cc2.CommittedGLSNBegin; glsn < cc2.CommittedGLSNEnd; glsn++ {
cc, err := stg.CommitContextOf(glsn)
assert.NoError(t, err)
assert.Equal(t, cc2, cc)

_, err = stg.NextCommitContextOf(cc)
assert.ErrorIs(t, err, ErrNoCommitContext)
}
})
}

func TestStorage_TrimWhenNoLogEntry(t *testing.T) {
tcs := []struct {
name string
Expand Down

0 comments on commit 84e2eef

Please sign in to comment.