-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to persist and load buffer from disk. Co-authored-by: Utku Ozdemir <utku.ozdemir@siderolabs.com> Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
- Loading branch information
1 parent
3c48c53
commit a874ed6
Showing
7 changed files
with
468 additions
and
2 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
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
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,203 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package circular | ||
|
||
import ( | ||
"cmp" | ||
"context" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"slices" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func (buf *Buffer) load() error { | ||
if buf.opt.PersistenceOptions.ChunkPath == "" { | ||
// persistence is disabled | ||
return nil | ||
} | ||
|
||
chunkPaths, err := filepath.Glob(buf.opt.PersistenceOptions.ChunkPath + ".*") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
type parsedChunkPath struct { | ||
path string | ||
id int64 | ||
} | ||
|
||
parsedChunkPaths := make([]parsedChunkPath, 0, len(chunkPaths)) | ||
|
||
for _, chunkPath := range chunkPaths { | ||
idx := strings.LastIndexByte(chunkPath, '.') | ||
if idx == -1 { | ||
continue | ||
} | ||
|
||
idStr := chunkPath[idx+1:] | ||
|
||
id, err := strconv.ParseInt(idStr, 10, 64) | ||
if err != nil || id < 0 { | ||
continue | ||
} | ||
|
||
parsedChunkPaths = append(parsedChunkPaths, parsedChunkPath{ | ||
id: id, | ||
path: chunkPath, | ||
}) | ||
} | ||
|
||
// sort chunks by ID, from smallest to biggest | ||
slices.SortFunc(parsedChunkPaths, func(a, b parsedChunkPath) int { | ||
return cmp.Compare(a.id, b.id) | ||
}) | ||
|
||
idx := 0 | ||
|
||
if len(parsedChunkPaths) > 1 && parsedChunkPaths[0].id == 0 { | ||
idx = 1 | ||
} | ||
|
||
if len(parsedChunkPaths)-idx > buf.opt.NumCompressedChunks { | ||
for j := idx; j < len(parsedChunkPaths)-buf.opt.NumCompressedChunks; j++ { | ||
if err := os.Remove(parsedChunkPaths[j].path); err != nil { | ||
// failed to remove the chunk | ||
continue | ||
} | ||
} | ||
|
||
parsedChunkPaths = slices.Delete(parsedChunkPaths, idx, len(parsedChunkPaths)-buf.opt.NumCompressedChunks) | ||
} | ||
|
||
chunks := make([]chunk, 0, len(parsedChunkPaths)) | ||
|
||
for _, chunkPath := range parsedChunkPaths { | ||
data, err := os.ReadFile(chunkPath.path) | ||
if err != nil { | ||
// failed to read the chunk | ||
continue | ||
} | ||
|
||
if chunkPath.id == 0 { | ||
buf.data, err = buf.opt.Compressor.Decompress(data, buf.data[:0]) | ||
if err != nil { | ||
// failed to decompress the data | ||
buf.data = buf.data[:cap(buf.data)] | ||
|
||
continue | ||
} | ||
|
||
buf.off = int64(len(buf.data)) | ||
buf.data = buf.data[:cap(buf.data)] | ||
} else { | ||
decompressedSize, err := buf.opt.Compressor.DecompressedSize(data) | ||
if err != nil { | ||
// failed to get the decompressed size | ||
continue | ||
} | ||
|
||
chunks = append(chunks, | ||
chunk{ | ||
compressed: data, | ||
id: chunkPath.id, | ||
size: decompressedSize, | ||
}) | ||
} | ||
} | ||
|
||
// re-calculate all offsets | ||
var sizeCompressed int64 | ||
|
||
for i := range chunks { | ||
sizeCompressed += chunks[i].size | ||
} | ||
|
||
// if chunk sizes are [10, 30, 20], the offsets will be [-60, -50, -20]. | ||
// the current buffer starts at 0 and goes to b.off (size of the buffer). | ||
for i := range chunks { | ||
chunks[i].startOffset = -sizeCompressed | ||
sizeCompressed -= chunks[i].size | ||
} | ||
|
||
buf.chunks = chunks | ||
|
||
return nil | ||
} | ||
|
||
type persistenceCommand struct { | ||
chunkID int64 | ||
drop bool | ||
|
||
data []byte | ||
} | ||
|
||
func (buf *Buffer) chunkPath(chunkID int64) string { | ||
return buf.opt.PersistenceOptions.ChunkPath + "." + strconv.FormatInt(chunkID, 10) | ||
} | ||
|
||
func (buf *Buffer) runPersistence(ctx context.Context, ch <-chan persistenceCommand) { | ||
var ( | ||
timerC <-chan time.Time | ||
timer *time.Timer | ||
) | ||
|
||
defer func() { | ||
if timer == nil { | ||
return | ||
} | ||
|
||
if !timer.Stop() { | ||
<-timer.C | ||
} | ||
}() | ||
|
||
setTimer := func() { | ||
interval := time.Duration(((rand.Float64()*2-1)*buf.opt.PersistenceOptions.FlushJitter + 1.0) * float64(buf.opt.PersistenceOptions.FlushInterval)) | ||
|
||
if timer == nil { | ||
timer = time.NewTimer(interval) | ||
timerC = timer.C | ||
} else { | ||
timer.Reset(interval) | ||
} | ||
} | ||
|
||
if buf.opt.PersistenceOptions.FlushInterval > 0 { | ||
setTimer() | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case command := <-ch: | ||
if command.drop { | ||
os.Remove(buf.chunkPath(command.chunkID)) | ||
} else { | ||
os.WriteFile(buf.chunkPath(command.chunkID), command.data, 0o644) | ||
} | ||
case <-timerC: | ||
// persist current chunk if changed | ||
|
||
// if Changed | ||
buf.mu.Lock() | ||
data := slices.Clone(buf.data[:buf.off%int64(cap(buf.data))]) | ||
buf.mu.Unlock() | ||
|
||
compressed, err := buf.opt.Compressor.Compress(data, nil) | ||
if err != nil { | ||
// WTF??? | ||
} | ||
|
||
os.WriteFile(buf.chunkPath(0), compressed, 0o644) | ||
|
||
setTimer() | ||
} | ||
} | ||
} |
Oops, something went wrong.