Skip to content

Commit

Permalink
chore: remove panic and trigger full update on incremental update fai…
Browse files Browse the repository at this point in the history
…lure (#4109)
  • Loading branch information
Sidddddarth authored Nov 9, 2023
1 parent 836b0f0 commit 92e0918
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
24 changes: 17 additions & 7 deletions backend-config/namespace_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
)

var (
jsonfast = jsoniter.ConfigCompatibleWithStandardLibrary
updatedAfterTimeFormat = "2006-01-02T15:04:05.000Z"
jsonfast = jsoniter.ConfigCompatibleWithStandardLibrary
updatedAfterTimeFormat = "2006-01-02T15:04:05.000Z"
ErrIncrementalUpdateFailed = errors.New("incremental update failed")
)

type namespaceConfig struct {
Expand Down Expand Up @@ -81,7 +82,14 @@ func (nc *namespaceConfig) SetUp() (err error) {

// Get returns sources from the workspace
func (nc *namespaceConfig) Get(ctx context.Context) (map[string]ConfigT, error) {
return nc.getFromAPI(ctx)
config, err := nc.getFromAPI(ctx)
if errors.Is(err, ErrIncrementalUpdateFailed) {
// reset state here
// this triggers a full update
nc.lastUpdatedAt = time.Time{}
return nc.getFromAPI(ctx)
}
return config, err
}

// getFromApi gets the workspace config from api
Expand Down Expand Up @@ -139,10 +147,12 @@ func (nc *namespaceConfig) getFromAPI(ctx context.Context) (map[string]ConfigT,
if workspace == nil { // this workspace was not updated, populate it with the previous config
previousConfig, ok := nc.workspacesConfig[workspaceID]
if !ok {
panic(fmt.Errorf(
"workspace %q was not updated but was not present in previous config: %+v",
workspaceID, req,
))
nc.logger.Errorw(
"workspace was not updated but was not present in previous config",
"workspaceID", workspaceID,
"req", req,
)
return configOnError, ErrIncrementalUpdateFailed
}
workspace = &previousConfig
} else {
Expand Down
23 changes: 22 additions & 1 deletion backend-config/namespace_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func Test_Namespace_IncrementalUpdates(t *testing.T) {
receivedUpdatedAfter []time.Time
)

responseBody, err := os.ReadFile("./testdata/sample_namespace.json")
responseBodyFromFile, err := os.ReadFile("./testdata/sample_namespace.json")
require.NoError(t, err)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -164,6 +164,7 @@ func Test_Namespace_IncrementalUpdates(t *testing.T) {
var (
err error
updatedAfterTime time.Time
responseBody []byte
)
for k, v := range r.URL.Query() {
if k != "updatedAfter" {
Expand All @@ -178,6 +179,7 @@ func Test_Namespace_IncrementalUpdates(t *testing.T) {

switch requestNumber {
case 0: // 1st request, return file content as is
responseBody = responseBodyFromFile
case 1: // 2nd request, return new workspace, no updates for the other 2
responseBody = []byte(fmt.Sprintf(`{
"dummy":{"updatedAt":%q,"libraries":[{"versionId":"foo"},{"versionId":"bar"}]},
Expand All @@ -195,6 +197,10 @@ func Test_Namespace_IncrementalUpdates(t *testing.T) {
"2CCgbmvBSa8Mv81YaIgtR36M7aW":null,
"2CChLejq5aIWi3qsKVm1PjHkyTj":null
}`)
case 5: // new workspace, but it's update time is before the last request, so no updates
responseBody = []byte(`{"someWorkspaceID": null}`)
default:
responseBody = responseBodyFromFile
}

_, _ = w.Write(responseBody)
Expand Down Expand Up @@ -267,6 +273,21 @@ func Test_Namespace_IncrementalUpdates(t *testing.T) {
require.Contains(t, c, "2CChLejq5aIWi3qsKVm1PjHkyTj")
require.Len(t, receivedUpdatedAfter, 4)
require.Equal(t, receivedUpdatedAfter[3], expectedUpdatedAt.Add(2*time.Minute), updatedAfterTimeFormat)

// send the request again, should receive the new workspace
// should trigger a full update
c, err = client.Get(ctx)
require.NoError(t, err)
require.Len(t, c, 2)
require.Contains(t, c, "2CCgbmvBSa8Mv81YaIgtR36M7aW")
require.Contains(t, c, "2CChLejq5aIWi3qsKVm1PjHkyTj")
require.Len(t, receivedUpdatedAfter, 5)
require.NotContains(t, c, "someNewWorkspaceID")
expectedUpdatedAt, err = time.Parse(updatedAfterTimeFormat, "2022-07-20T10:00:00.000Z")
require.NoError(t, err)
require.Equal(t, receivedUpdatedAfter[0], expectedUpdatedAt, updatedAfterTimeFormat)
require.Equal(t, receivedUpdatedAfter[0], client.lastUpdatedAt, updatedAfterTimeFormat)
require.Equal(t, 7, requestNumber)
}

type backendConfigServer struct {
Expand Down

0 comments on commit 92e0918

Please sign in to comment.