Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[viper WatchConfig] platform-specific write to ensure callback fires exactly once #13627

Merged
merged 2 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions go/viperutil/internal/sync/sync_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sync_test

import "os"

// atomicWrite overwrites a file in such a way as to produce exactly one
// filesystem event of the type CREATE or WRITE (which are tracked by viper)
// without producing any REMOVE events.
//
// At time of writing, this produces the following on darwin:
// CHMOD => WRITE => CHMOD.
func atomicWrite(path string, data []byte) error {
stat, err := os.Stat(path)
if err != nil {
return err
}

return os.WriteFile(path, data, stat.Mode())
}
39 changes: 39 additions & 0 deletions go/viperutil/internal/sync/sync_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sync_test

import "os"

// atomicWrite overwrites a file in such a way as to produce exactly one
// filesystem event of the type CREATE or WRITE (which are tracked by viper)
// without producing any REMOVE events.
//
// At time of writing, this produces the following on x86_64 linux:
// CREATE.
func atomicWrite(path string, data []byte) error {
stat, err := os.Stat(path)
if err != nil {
return err
}

tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, stat.Mode()); err != nil {
return err
}

return os.Rename(tmp, path)
}
50 changes: 8 additions & 42 deletions go/viperutil/internal/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ package sync_test
import (
"context"
"encoding/json"
"fmt"
"math"
"math/rand"
"os"
"sync"
"testing"
"time"

"vitess.io/vitess/go/vt/log"

"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
Expand All @@ -44,36 +40,17 @@ func TestWatchConfig(t *testing.T) {
}

writeConfig := func(tmp *os.File, a, b int) error {
stat, err := os.Stat(tmp.Name())
if err != nil {
return err
}

data, err := json.Marshal(&config{A: a, B: b})
if err != nil {
return err
}

err = os.WriteFile(tmp.Name(), data, stat.Mode())
if err != nil {
return err
}

data, err = os.ReadFile(tmp.Name())
if err != nil {
return err
}

var cfg config
if err := json.Unmarshal(data, &cfg); err != nil {
return err
}

if cfg.A != a || cfg.B != b {
return fmt.Errorf("config did not persist; want %+v got %+v", config{A: a, B: b}, cfg)
}

return nil
// In order to guarantee viper's watcher detects exactly one config
// change, we perform a write specific to the platform we're executing
// on.
//
// Consequently, this test only supports linux and macos for now.
return atomicWrite(tmp.Name(), data)
}
writeRandomConfig := func(tmp *os.File) error {
a, b := rand.Intn(100), rand.Intn(100)
Expand Down Expand Up @@ -109,19 +86,8 @@ func TestWatchConfig(t *testing.T) {
require.NoError(t, writeConfig(tmp, a+1, b+1))
<-wCh // wait for the update to finish

// temporary hack to fix flakiness where we seem to miss one update.
const permittedVariance = 1
closeEnoughTo := func(want, got int) bool {
if math.Abs(float64(want-got)) <= permittedVariance {
return true
}
log.Infof("TestWatchConfig: count not close enough: want %d, got %d, permitted variance %d",
want, got, permittedVariance)
return false
}

require.True(t, closeEnoughTo(a+1, v.GetInt("a")))
require.True(t, closeEnoughTo(b+1, v.GetInt("b")))
require.Equal(t, a+1, v.GetInt("a"))
require.Equal(t, b+1, v.GetInt("b"))

rCh <- struct{}{}

Expand Down