Skip to content

Commit 2703cb9

Browse files
committed
Merge branch 'upgrade-sarama-v1.38.1' into upgrade-sarama-v1.38.1
2 parents 810629b + 66e6610 commit 2703cb9

File tree

12 files changed

+698
-705
lines changed

12 files changed

+698
-705
lines changed

cmd/query/app/mocks/Watcher.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

cmd/query/app/static_handler.go

Lines changed: 18 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"strings"
2828
"sync/atomic"
2929

30-
"github.com/fsnotify/fsnotify"
3130
"github.com/gorilla/mux"
3231
"go.uber.org/zap"
3332

@@ -61,10 +60,10 @@ func RegisterStaticHandler(r *mux.Router, logger *zap.Logger, qOpts *QueryOption
6160

6261
// StaticAssetsHandler handles static assets
6362
type StaticAssetsHandler struct {
64-
options StaticAssetsHandlerOptions
65-
indexHTML atomic.Value // stores []byte
66-
assetsFS http.FileSystem
67-
newWatcher func() (fswatcher.Watcher, error)
63+
options StaticAssetsHandlerOptions
64+
indexHTML atomic.Value // stores []byte
65+
assetsFS http.FileSystem
66+
watcher *fswatcher.FSWatcher
6867
}
6968

7069
// StaticAssetsHandlerOptions defines options for NewStaticAssetsHandler
@@ -73,7 +72,6 @@ type StaticAssetsHandlerOptions struct {
7372
UIConfigPath string
7473
LogAccess bool
7574
Logger *zap.Logger
76-
NewWatcher func() (fswatcher.Watcher, error)
7775
}
7876

7977
type loadedConfig struct {
@@ -92,23 +90,23 @@ func NewStaticAssetsHandler(staticAssetsRoot string, options StaticAssetsHandler
9290
options.Logger = zap.NewNop()
9391
}
9492

95-
if options.NewWatcher == nil {
96-
options.NewWatcher = fswatcher.NewWatcher
97-
}
98-
9993
indexHTML, err := loadAndEnrichIndexHTML(assetsFS.Open, options)
10094
if err != nil {
10195
return nil, err
10296
}
10397

10498
h := &StaticAssetsHandler{
105-
options: options,
106-
assetsFS: assetsFS,
107-
newWatcher: options.NewWatcher,
99+
options: options,
100+
assetsFS: assetsFS,
108101
}
109102

103+
watcher, err := fswatcher.New([]string{options.UIConfigPath}, h.reloadUIConfig, h.options.Logger)
104+
if err != nil {
105+
return nil, err
106+
}
107+
h.watcher = watcher
108+
110109
h.indexHTML.Store(indexHTML)
111-
h.watch()
112110

113111
return h, nil
114112
}
@@ -142,67 +140,14 @@ func loadAndEnrichIndexHTML(open func(string) (http.File, error), options Static
142140
return indexBytes, nil
143141
}
144142

145-
func (sH *StaticAssetsHandler) configListener(watcher fswatcher.Watcher) {
146-
for {
147-
select {
148-
case event := <-watcher.Events():
149-
// ignore if the event filename is not the UI configuration
150-
if filepath.Base(event.Name) != filepath.Base(sH.options.UIConfigPath) {
151-
continue
152-
}
153-
// ignore if the event is a chmod event (permission or owner changes)
154-
if event.Op&fsnotify.Chmod == fsnotify.Chmod {
155-
continue
156-
}
157-
if event.Op&fsnotify.Remove == fsnotify.Remove {
158-
sH.options.Logger.Warn("the UI config file has been removed, using the last known version")
159-
continue
160-
}
161-
// this will catch events for all files inside the same directory, which is OK if we don't have many changes
162-
sH.options.Logger.Info("reloading UI config", zap.String("filename", sH.options.UIConfigPath))
163-
content, err := loadAndEnrichIndexHTML(sH.assetsFS.Open, sH.options)
164-
if err != nil {
165-
sH.options.Logger.Error("error while reloading the UI config", zap.Error(err))
166-
}
167-
sH.indexHTML.Store(content)
168-
sH.options.Logger.Info("reloaded UI config", zap.String("filename", sH.options.UIConfigPath))
169-
case err, ok := <-watcher.Errors():
170-
if !ok {
171-
return
172-
}
173-
sH.options.Logger.Error("event", zap.Error(err))
174-
}
175-
}
176-
}
177-
178-
func (sH *StaticAssetsHandler) watch() {
179-
if sH.options.UIConfigPath == "" {
180-
sH.options.Logger.Info("UI config path not provided, config file will not be watched")
181-
return
182-
}
183-
184-
watcher, err := sH.newWatcher()
143+
func (sH *StaticAssetsHandler) reloadUIConfig() {
144+
sH.options.Logger.Info("reloading UI config", zap.String("filename", sH.options.UIConfigPath))
145+
content, err := loadAndEnrichIndexHTML(sH.assetsFS.Open, sH.options)
185146
if err != nil {
186-
sH.options.Logger.Error("failed to create a new watcher for the UI config", zap.Error(err))
187-
return
188-
}
189-
190-
go func() {
191-
sH.configListener(watcher)
192-
}()
193-
194-
if err := watcher.Add(sH.options.UIConfigPath); err != nil {
195-
sH.options.Logger.Error("error adding watcher to file", zap.String("file", sH.options.UIConfigPath), zap.Error(err))
196-
} else {
197-
sH.options.Logger.Info("watching", zap.String("file", sH.options.UIConfigPath))
198-
}
199-
200-
dir := filepath.Dir(sH.options.UIConfigPath)
201-
if err := watcher.Add(dir); err != nil {
202-
sH.options.Logger.Error("error adding watcher to dir", zap.String("dir", dir), zap.Error(err))
203-
} else {
204-
sH.options.Logger.Info("watching", zap.String("dir", dir))
147+
sH.options.Logger.Error("error while reloading the UI config", zap.Error(err))
205148
}
149+
sH.indexHTML.Store(content)
150+
sH.options.Logger.Info("reloaded UI config", zap.String("filename", sH.options.UIConfigPath))
206151
}
207152

208153
func loadIndexHTML(open func(string) (http.File, error)) ([]byte, error) {

cmd/query/app/static_handler_test.go

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,13 @@ import (
2626
"testing"
2727
"time"
2828

29-
"github.com/fsnotify/fsnotify"
3029
"github.com/gorilla/mux"
3130
"github.com/stretchr/testify/assert"
32-
"github.com/stretchr/testify/mock"
3331
"github.com/stretchr/testify/require"
3432
"go.uber.org/zap"
3533
"go.uber.org/zap/zapcore"
3634
"go.uber.org/zap/zaptest/observer"
3735

38-
"github.com/jaegertracing/jaeger/cmd/query/app/mocks"
39-
"github.com/jaegertracing/jaeger/pkg/fswatcher"
4036
"github.com/jaegertracing/jaeger/pkg/testutils"
4137
)
4238

@@ -151,100 +147,6 @@ func TestNewStaticAssetsHandlerErrors(t *testing.T) {
151147
}
152148
}
153149

154-
func TestWatcherError(t *testing.T) {
155-
const totalWatcherAddCalls = 2
156-
157-
for _, tc := range []struct {
158-
name string
159-
errorOnNthAdd int
160-
newWatcherErr error
161-
watcherAddErr error
162-
wantWatcherAddCalls int
163-
}{
164-
{
165-
name: "NewWatcher error",
166-
newWatcherErr: fmt.Errorf("new watcher error"),
167-
},
168-
{
169-
name: "Watcher.Add first call error",
170-
errorOnNthAdd: 0,
171-
watcherAddErr: fmt.Errorf("add first error"),
172-
wantWatcherAddCalls: 2,
173-
},
174-
{
175-
name: "Watcher.Add second call error",
176-
errorOnNthAdd: 1,
177-
watcherAddErr: fmt.Errorf("add second error"),
178-
wantWatcherAddCalls: 2,
179-
},
180-
} {
181-
t.Run(tc.name, func(t *testing.T) {
182-
// Prepare
183-
zcore, logObserver := observer.New(zapcore.InfoLevel)
184-
logger := zap.New(zcore)
185-
defer func() {
186-
if r := recover(); r != nil {
187-
// Select loop exits without logging error, only containing previous error log.
188-
assert.Equal(t, logObserver.FilterMessage("event").Len(), 1)
189-
assert.Equal(t, "send on closed channel", fmt.Sprint(r))
190-
}
191-
}()
192-
193-
watcher := &mocks.Watcher{}
194-
for i := 0; i < totalWatcherAddCalls; i++ {
195-
var err error
196-
if i == tc.errorOnNthAdd {
197-
err = tc.watcherAddErr
198-
}
199-
watcher.On("Add", mock.Anything).Return(err).Once()
200-
}
201-
watcher.On("Events").Return(make(chan fsnotify.Event))
202-
errChan := make(chan error)
203-
watcher.On("Errors").Return(errChan)
204-
205-
// Test
206-
_, err := NewStaticAssetsHandler("fixture", StaticAssetsHandlerOptions{
207-
UIConfigPath: "fixture/ui-config-hotreload.json",
208-
NewWatcher: func() (fswatcher.Watcher, error) {
209-
return watcher, tc.newWatcherErr
210-
},
211-
Logger: logger,
212-
})
213-
214-
// Validate
215-
216-
// Error logged but not returned
217-
assert.NoError(t, err)
218-
if tc.newWatcherErr != nil {
219-
assert.Equal(t, logObserver.FilterField(zap.Error(tc.newWatcherErr)).Len(), 1)
220-
} else {
221-
assert.Zero(t, logObserver.FilterField(zap.Error(tc.newWatcherErr)).Len())
222-
}
223-
224-
if tc.watcherAddErr != nil {
225-
assert.Equal(t, logObserver.FilterField(zap.Error(tc.watcherAddErr)).Len(), 1)
226-
} else {
227-
assert.Zero(t, logObserver.FilterField(zap.Error(tc.watcherAddErr)).Len())
228-
}
229-
230-
watcher.AssertNumberOfCalls(t, "Add", tc.wantWatcherAddCalls)
231-
232-
// Validate Events and Errors channels
233-
if tc.newWatcherErr == nil {
234-
errChan <- fmt.Errorf("first error")
235-
236-
waitUntil(t, func() bool {
237-
return logObserver.FilterMessage("event").Len() > 0
238-
}, 100, 10*time.Millisecond, "timed out waiting for error")
239-
assert.Equal(t, logObserver.FilterMessage("event").Len(), 1)
240-
241-
close(errChan)
242-
errChan <- fmt.Errorf("second error on closed chan")
243-
}
244-
})
245-
}
246-
}
247-
248150
func TestHotReloadUIConfig(t *testing.T) {
249151
dir, err := os.MkdirTemp("", "ui-config-hotreload-*")
250152
require.NoError(t, err)

0 commit comments

Comments
 (0)