forked from kakao/varlog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): introduce append batch
There were already two kinds of batches in storage: - The write batch saves only data keyed by the LLSN. - The commit batch saves only the commit context and GLSN, and the commit context must exist. This patch introduces a new batch type - append batch. The append b catch saves both log entries and a commit context. However, it is not necessary to keep the commit context. During synchronization, the append batch must store log entries from a destination replica. Updates kakao#125
- Loading branch information
Showing
3 changed files
with
84 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package storage | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/cockroachdb/pebble" | ||
|
||
"github.com/kakao/varlog/pkg/types" | ||
) | ||
|
||
var appendBatchPool = sync.Pool{ | ||
New: func() interface{} { | ||
return &AppendBatch{ | ||
dk: make([]byte, dataKeyLength), | ||
ck: make([]byte, commitKeyLength), | ||
cc: make([]byte, commitContextLength), | ||
} | ||
}, | ||
} | ||
|
||
// AppendBatch is a batch to put one or more log entries. | ||
type AppendBatch struct { | ||
batch *pebble.Batch | ||
writeOpts *pebble.WriteOptions | ||
dk []byte | ||
ck []byte | ||
cc []byte | ||
} | ||
|
||
func newAppendBatch(batch *pebble.Batch, writeOpts *pebble.WriteOptions) *AppendBatch { | ||
ab := appendBatchPool.Get().(*AppendBatch) | ||
ab.batch = batch | ||
ab.writeOpts = writeOpts | ||
return ab | ||
} | ||
|
||
func (ab *AppendBatch) release() { | ||
ab.batch = nil | ||
ab.writeOpts = nil | ||
appendBatchPool.Put(ab) | ||
} | ||
|
||
// SetLogEntry inserts a log entry. | ||
func (ab *AppendBatch) SetLogEntry(llsn types.LLSN, glsn types.GLSN, data []byte) error { | ||
dk := encodeDataKeyInternal(llsn, ab.dk) | ||
ck := encodeCommitKeyInternal(glsn, ab.ck) | ||
if err := ab.batch.Set(dk, data, nil); err != nil { | ||
return err | ||
} | ||
if err := ab.batch.Set(ck, dk, nil); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// SetCommitContext inserts a commit context. | ||
func (ab *AppendBatch) SetCommitContext(cc CommitContext) error { | ||
return ab.batch.Set(commitContextKey, encodeCommitContext(cc, ab.cc), nil) | ||
} | ||
|
||
// Apply saves a batch of appended log entries to the storage. | ||
func (ab *AppendBatch) Apply() error { | ||
return ab.batch.Commit(ab.writeOpts) | ||
} | ||
|
||
// Close releases an AppendBatch. | ||
func (ab *AppendBatch) Close() error { | ||
err := ab.batch.Close() | ||
ab.release() | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters