Skip to content

eskipfile: optimize Watch.LoadUpdates #3448

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 18 additions & 7 deletions eskipfile/watch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eskipfile

import (
"bytes"
"os"
"reflect"
"sync"
Expand All @@ -17,12 +18,13 @@ type watchResponse struct {
// WatchClient implements a route configuration client with file watching. Use the Watch function to initialize
// instances of it.
type WatchClient struct {
fileName string
routes map[string]*eskip.Route
getAll chan (chan<- watchResponse)
getUpdates chan (chan<- watchResponse)
quit chan struct{}
once sync.Once
fileName string
lastContent []byte
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should better store the sha256 value and compare that one to detect a change, because it is really fast and doesn't require to duplicate the memory of routes as byte slice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its fast but not computing it is even faster :)
Do we care about memory consumed by eskip given that we parse and keep routes for lookup anyways?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can move forward with this, given that the concern with large eskipfiles is more about network not memory, and we can update later if we need.

routes map[string]*eskip.Route
getAll chan (chan<- watchResponse)
getUpdates chan (chan<- watchResponse)
quit chan struct{}
once sync.Once
}

// Watch creates a route configuration client with file watching. Watch doesn't follow file system nodes, it
Expand Down Expand Up @@ -97,35 +99,44 @@ func cloneRoutes(r []*eskip.Route) []*eskip.Route {
func (c *WatchClient) loadAll() watchResponse {
content, err := os.ReadFile(c.fileName)
if err != nil {
c.lastContent = nil
return watchResponse{err: err}
}

r, err := eskip.Parse(string(content))
if err != nil {
c.lastContent = nil
return watchResponse{err: err}
}

c.storeRoutes(r)
c.lastContent = content
return watchResponse{routes: cloneRoutes(r)}
}

func (c *WatchClient) loadUpdates() watchResponse {
content, err := os.ReadFile(c.fileName)
if err != nil {
c.lastContent = nil
if os.IsNotExist(err) {
deletedIDs := c.deleteAllListIDs()
return watchResponse{deletedIDs: deletedIDs}
}

return watchResponse{err: err}
}

if bytes.Equal(content, c.lastContent) {
return watchResponse{}
}

r, err := eskip.Parse(string(content))
if err != nil {
c.lastContent = nil
return watchResponse{err: err}
}

upsert, del := c.diffStoreRoutes(r)
c.lastContent = content
return watchResponse{routes: cloneRoutes(upsert), deletedIDs: del}
}

Expand Down
33 changes: 33 additions & 0 deletions eskipfile/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package eskipfile

import (
"fmt"
"net/http"
"net/url"
"os"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/logging/loggingtest"
"github.com/zalando/skipper/routing"
Expand Down Expand Up @@ -236,3 +238,34 @@ func TestWatchUpdate(t *testing.T) {
updateFile(t)
test.waitAndSucceedUpdated()
}

func BenchmarkWatchLoadUpdate(b *testing.B) {
f, err := os.CreateTemp(b.TempDir(), "routes*.eskip")
require.NoError(b, err)
b.Cleanup(func() { require.NoError(b, os.Remove(f.Name())) })

const nRoutes = 10_000
for i := range nRoutes {
_, err := f.WriteString(fmt.Sprintf(`r%d: Path("/%d") -> "https://foo%d.example.org";`, i, i, i))
require.NoError(b, err)
}
require.NoError(b, f.Close())

w := Watch(f.Name())
b.Cleanup(w.Close)

r, err := w.LoadAll()
require.Len(b, r, nRoutes)
require.NoError(b, err)

r, deletedIds, err := w.LoadUpdate()
require.Nil(b, r)
require.Empty(b, deletedIds)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()
for range b.N {
w.LoadUpdate()
}
}