1
0
Fork 0

More Sink tweaks

master
Philip O'Toole 1 year ago
parent 6cbbaca035
commit f3d7a5b8d4

@ -20,6 +20,7 @@ type Sink struct {
nextGenDir string nextGenDir string
meta *Meta meta *Meta
nWritten int64
dataFD *os.File dataFD *os.File
logger *log.Logger logger *log.Logger
@ -51,7 +52,9 @@ func (s *Sink) Open() error {
// Write writes snapshot data to the sink. The snapshot is not in place // Write writes snapshot data to the sink. The snapshot is not in place
// until Close is called. // until Close is called.
func (s *Sink) Write(p []byte) (n int, err error) { func (s *Sink) Write(p []byte) (n int, err error) {
return s.dataFD.Write(p) n, err = s.dataFD.Write(p)
s.nWritten += int64(n)
return
} }
// ID returns the ID of the snapshot being written. // ID returns the ID of the snapshot being written.
@ -76,6 +79,10 @@ func (s *Sink) Close() error {
} }
func (s *Sink) processSnapshotData() error { func (s *Sink) processSnapshotData() error {
if s.nWritten == 0 {
return nil
}
if _, err := s.dataFD.Seek(0, 0); err != nil { if _, err := s.dataFD.Seek(0, 0); err != nil {
return err return err
} }

@ -6,7 +6,22 @@ import (
"testing" "testing"
) )
func Test_NewSinkOpenOK(t *testing.T) { func Test_NewSinkOpenCloseOK(t *testing.T) {
tmpDir := t.TempDir()
workDir := filepath.Join(tmpDir, "work")
mustCreateDir(workDir)
currGenDir := filepath.Join(tmpDir, "curr")
nextGenDir := filepath.Join(tmpDir, "next")
s := NewSink(workDir, currGenDir, nextGenDir, &Meta{})
if err := s.Open(); err != nil {
t.Fatal(err)
}
if err := s.Close(); err != nil {
t.Fatal(err)
}
}
func Test_SinkFullSnapshot(t *testing.T) {
tmpDir := t.TempDir() tmpDir := t.TempDir()
workDir := filepath.Join(tmpDir, "work") workDir := filepath.Join(tmpDir, "work")
mustCreateDir(workDir) mustCreateDir(workDir)

@ -75,13 +75,26 @@ func (s *Store) Create(version raft.SnapshotVersion, index, term uint64, configu
if err != nil { if err != nil {
return nil, err return nil, err
} }
snapshotName := snapshotName(term, index) nextGenDir, err := s.GetNextGenerationDir()
snapshotPath := filepath.Join(currGenDir, snapshotName+tmpSuffix) if err != nil {
if err := os.MkdirAll(snapshotPath, 0755); err != nil {
return nil, err return nil, err
} }
return nil, nil meta := &Meta{
SnapshotMeta: raft.SnapshotMeta{
Index: index,
Term: term,
Configuration: configuration,
ConfigurationIndex: configurationIndex,
Version: version,
},
}
sink := NewSink(s.rootDir, currGenDir, nextGenDir, meta)
if err := sink.Open(); err != nil {
return nil, fmt.Errorf("failed to open Sink: %v", err)
}
return sink, nil
} }
// List returns a list of all the snapshots in the Store. // List returns a list of all the snapshots in the Store.

Loading…
Cancel
Save