databricks-cli/libs/sync/diff_test.go

187 lines
3.9 KiB
Go
Raw Permalink Normal View History

Add directory tracking to sync (#425) ## Changes This change replaces usage of the `repofiles` package with the `filer` package to consolidate WSFS code paths. The `repofiles` package implemented the following behavior. If a file at `foo/bar.txt` was created and removed, the directory `foo` was kept around because we do not perform directory tracking. If subsequently, a file at `foo` was created, it resulted in an `fs.ErrExist` because it is impossible to overwrite a directory. It would then perform a recursive delete of the path if this happened and retry the file write. To make this use case work without resorting to a recursive delete on conflict, we need to implement directory tracking as part of sync. The approach in this commit is as follows: 1. Maintain set of directories needed for current set of files. Compare to previous set of files. This results in mkdir of added directories and rmdir of removed directories. 2. Creation of new directories should happen prior to writing files. Otherwise, many file writes may race to create the same parent directories, resulting in additional API calls. Removal of existing directories should happen after removing files. 3. Making new directories can be deduped across common prefixes where only the longest prefix is created recursively. 4. Removing existing directories must happen sequentially, starting with the longest prefix. 5. Removal of directories is a best effort. It fails only if the directory is not empty, and if this happens we know something placed a file or directory manually, outside of sync. ## Tests * Existing integration tests pass (modified where it used to assert directories weren't cleaned up) * New integration test to confirm the inability to remove a directory doesn't fail the sync run
2023-06-12 11:44:00 +00:00
package sync
import (
"testing"
"time"
Add directory tracking to sync (#425) ## Changes This change replaces usage of the `repofiles` package with the `filer` package to consolidate WSFS code paths. The `repofiles` package implemented the following behavior. If a file at `foo/bar.txt` was created and removed, the directory `foo` was kept around because we do not perform directory tracking. If subsequently, a file at `foo` was created, it resulted in an `fs.ErrExist` because it is impossible to overwrite a directory. It would then perform a recursive delete of the path if this happened and retry the file write. To make this use case work without resorting to a recursive delete on conflict, we need to implement directory tracking as part of sync. The approach in this commit is as follows: 1. Maintain set of directories needed for current set of files. Compare to previous set of files. This results in mkdir of added directories and rmdir of removed directories. 2. Creation of new directories should happen prior to writing files. Otherwise, many file writes may race to create the same parent directories, resulting in additional API calls. Removal of existing directories should happen after removing files. 3. Making new directories can be deduped across common prefixes where only the longest prefix is created recursively. 4. Removing existing directories must happen sequentially, starting with the longest prefix. 5. Removal of directories is a best effort. It fails only if the directory is not empty, and if this happens we know something placed a file or directory manually, outside of sync. ## Tests * Existing integration tests pass (modified where it used to assert directories weren't cleaned up) * New integration test to confirm the inability to remove a directory doesn't fail the sync run
2023-06-12 11:44:00 +00:00
"github.com/stretchr/testify/assert"
)
func TestDiffGroupedMkdir(t *testing.T) {
d := diff{
mkdir: []string{
"foo",
"foo/bar",
"foo/bar/baz1",
"foo/bar/baz2",
"foo1",
"a/b",
"a/b/c/d/e/f",
},
}
// Expect only leaf directories to be included.
out := d.groupedMkdir()
assert.Len(t, out, 1)
assert.ElementsMatch(t, []string{
"foo/bar/baz1",
"foo/bar/baz2",
"foo1",
"a/b/c/d/e/f",
}, out[0])
}
func TestDiffGroupedRmdir(t *testing.T) {
d := diff{
rmdir: []string{
"a/b/c/d/e/f",
"a/b/c/d/e",
"a/b/c/d",
"a/b/c",
"a/b/e/f/g/h",
"a/b/e/f/g",
"a/b/e/f",
"a/b/e",
"a/b",
},
}
out := d.groupedRmdir()
assert.Len(t, out, 5)
assert.ElementsMatch(t, []string{"a/b/c/d/e/f", "a/b/e/f/g/h"}, out[0])
assert.ElementsMatch(t, []string{"a/b/c/d/e", "a/b/e/f/g"}, out[1])
assert.ElementsMatch(t, []string{"a/b/c/d", "a/b/e/f"}, out[2])
assert.ElementsMatch(t, []string{"a/b/c", "a/b/e"}, out[3])
assert.ElementsMatch(t, []string{"a/b"}, out[4])
}
func TestDiffGroupedRmdirWithLeafsOnly(t *testing.T) {
d := diff{
rmdir: []string{
"foo/bar/baz1",
"foo/bar1",
"foo/bar/baz2",
"foo/bar2",
"foo1",
"foo2",
},
}
// Expect all directories to be included.
out := d.groupedRmdir()
assert.Len(t, out, 1)
assert.ElementsMatch(t, d.rmdir, out[0])
}
func TestDiffComputationForRemovedFiles(t *testing.T) {
before := &SnapshotState{
LocalToRemoteNames: map[string]string{
"foo/a/b/c.py": "foo/a/b/c",
},
RemoteToLocalNames: map[string]string{
"foo/a/b/c": "foo/a/b/c.py",
},
}
after := &SnapshotState{}
expected := diff{
delete: []string{"foo/a/b/c"},
rmdir: []string{"foo", "foo/a", "foo/a/b"},
mkdir: []string{},
put: []string{},
}
assert.Equal(t, expected, computeDiff(after, before))
}
func TestDiffComputationWhenRemoteNameIsChanged(t *testing.T) {
tick := time.Now()
before := &SnapshotState{
LocalToRemoteNames: map[string]string{
"foo/a/b/c.py": "foo/a/b/c",
},
RemoteToLocalNames: map[string]string{
"foo/a/b/c": "foo/a/b/c.py",
},
LastModifiedTimes: map[string]time.Time{
"foo/a/b/c.py": tick,
},
}
tick = tick.Add(time.Second)
after := &SnapshotState{
LocalToRemoteNames: map[string]string{
"foo/a/b/c.py": "foo/a/b/c.py",
},
RemoteToLocalNames: map[string]string{
"foo/a/b/c.py": "foo/a/b/c.py",
},
LastModifiedTimes: map[string]time.Time{
"foo/a/b/c.py": tick,
},
}
expected := diff{
delete: []string{"foo/a/b/c"},
rmdir: []string{},
mkdir: []string{},
put: []string{"foo/a/b/c.py"},
}
assert.Equal(t, expected, computeDiff(after, before))
}
func TestDiffComputationForNewFiles(t *testing.T) {
after := &SnapshotState{
LocalToRemoteNames: map[string]string{
"foo/a/b/c.py": "foo/a/b/c",
},
RemoteToLocalNames: map[string]string{
"foo/a/b/c": "foo/a/b/c.py",
},
LastModifiedTimes: map[string]time.Time{
"foo/a/b/c.py": time.Now(),
},
}
expected := diff{
delete: []string{},
rmdir: []string{},
mkdir: []string{"foo", "foo/a", "foo/a/b"},
put: []string{"foo/a/b/c.py"},
}
assert.Equal(t, expected, computeDiff(after, &SnapshotState{}))
}
func TestDiffComputationForUpdatedFiles(t *testing.T) {
tick := time.Now()
before := &SnapshotState{
LocalToRemoteNames: map[string]string{
"foo/a/b/c": "foo/a/b/c",
},
RemoteToLocalNames: map[string]string{
"foo/a/b/c": "foo/a/b/c",
},
LastModifiedTimes: map[string]time.Time{
"foo/a/b/c": tick,
},
}
tick = tick.Add(time.Second)
after := &SnapshotState{
LocalToRemoteNames: map[string]string{
"foo/a/b/c": "foo/a/b/c",
},
RemoteToLocalNames: map[string]string{
"foo/a/b/c": "foo/a/b/c",
},
LastModifiedTimes: map[string]time.Time{
"foo/a/b/c": tick,
},
}
expected := diff{
delete: []string{},
rmdir: []string{},
mkdir: []string{},
put: []string{"foo/a/b/c"},
}
assert.Equal(t, expected, computeDiff(after, before))
}