Skip to content

Commit baba7ac

Browse files
committed
Keep file checksum in memory
To avoid re-writing the same file at each resync. This shouldn't make a huge performance difference, except on very slow storage devices, but keeping the mtime/ctime timestamps unchanged may be helpful. Note that we don't compare file checksums to files collected from an existing repository during the first clone & resync (we'll dump every cluster object once in any case).
1 parent 8276571 commit baba7ac

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

pkg/recorder/recorder.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package recorder
44

55
import (
66
"fmt"
7+
"hash/crc64"
78
"os"
89
"path/filepath"
910
"strings"
@@ -16,12 +17,16 @@ import (
1617
"github.com/bpineau/katafygio/pkg/event"
1718
)
1819

19-
var appFs = afero.NewOsFs()
20+
var (
21+
appFs = afero.NewOsFs()
22+
crc64Table = crc64.MakeTable(crc64.ECMA)
23+
)
2024

2125
// activeFiles will contain a list of active (present in cluster) objets; we'll
2226
// use that to periodically find and garbage collect stale objets in the git repos
23-
// (ie. if some objects were delete from cluster while katafygio was not running).
24-
type activeFiles map[string]bool
27+
// (ie. if some objects were delete from cluster while katafygio was not running),
28+
// and to skip already existing and unchanged files.
29+
type activeFiles map[string]uint64
2530

2631
// Listener receive events from controllers and save them to disk as yaml files
2732
type Listener struct {
@@ -133,17 +138,22 @@ func (w *Listener) save(file string, data []byte) error {
133138
return nil
134139
}
135140

141+
csum := crc64.Checksum(data, crc64Table)
142+
143+
w.activesLock.RLock()
144+
prevsum, ok := w.actives[w.relativePath(file)]
145+
w.activesLock.RUnlock()
146+
if ok && prevsum == csum {
147+
return nil
148+
}
149+
136150
dir := filepath.Clean(filepath.Dir(file))
137151

138152
err := appFs.MkdirAll(dir, 0700)
139153
if err != nil {
140154
return fmt.Errorf("can't create local directory %s: %v", dir, err)
141155
}
142156

143-
w.activesLock.Lock()
144-
w.actives[w.relativePath(file)] = true
145-
w.activesLock.Unlock()
146-
147157
tmpf, err := afero.TempFile(appFs, "", "katafygio")
148158
if err != nil {
149159
return fmt.Errorf("failed to create a temporary file: %v", err)
@@ -162,6 +172,10 @@ func (w *Listener) save(file string, data []byte) error {
162172
return fmt.Errorf("failed to rename %s to %s: %v", tmpf.Name(), file, err)
163173
}
164174

175+
w.activesLock.Lock()
176+
w.actives[w.relativePath(file)] = csum
177+
w.activesLock.Unlock()
178+
165179
return nil
166180
}
167181

pkg/recorder/recorder_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func TestRecorder(t *testing.T) {
3636
evt.Send(newNotif(event.Upsert, "foo1"))
3737
evt.Send(newNotif(event.Upsert, "foo2"))
3838
evt.Send(newNotif(event.Delete, "foo1"))
39+
evt.Send(newNotif(event.Upsert, "foo2"))
3940

4041
rec.Stop() // to flush ongoing fs operations
4142

0 commit comments

Comments
 (0)