Skip to content

Commit

Permalink
Fix rootfs blocks order in diffs (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
dobrac committed Feb 15, 2025
1 parent 772f9f6 commit 5b58a62
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions packages/orchestrator/internal/sandbox/block/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"os"
"sort"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -88,20 +89,18 @@ func (m *Cache) Export(out io.Writer) (*bitset.BitSet, error) {

tracked := bitset.New(uint(header.TotalBlocks(m.size, m.blockSize)))

m.dirty.Range(func(key, value any) bool {
block := header.BlockIdx(key.(int64), m.blockSize)
for _, key := range m.dirtySortedKeys() {
block := header.BlockIdx(key, m.blockSize)

tracked.Set(uint(block))

_, err := out.Write((*m.mmap)[key.(int64) : key.(int64)+m.blockSize])
_, err := out.Write((*m.mmap)[key : key+m.blockSize])
if err != nil {
fmt.Printf("error writing to out: %v\n", err)

return false
return nil, err
}

return true
})
}

return tracked, nil
}
Expand Down Expand Up @@ -209,3 +208,18 @@ func (m *Cache) WriteAtWithoutLock(b []byte, off int64) (int, error) {

return n, nil
}

// dirtySortedKeys returns a sorted list of dirty keys.
// Key represents a block offset.
func (m *Cache) dirtySortedKeys() []int64 {
var keys []int64
m.dirty.Range(func(key, _ any) bool {
keys = append(keys, key.(int64))
return true
})
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})

return keys
}

0 comments on commit 5b58a62

Please sign in to comment.