From e186bccbf8baa515a04bb5b8ffb7f90c5f534018 Mon Sep 17 00:00:00 2001 From: Torin Sandall Date: Mon, 4 Jun 2018 09:46:00 -0700 Subject: [PATCH] Fix partial cache invalidation for data changes The partial cache was not be invalidated when data changed. As a result, callers would receive stale results when data updated. Fixes #589 Signed-off-by: Torin Sandall --- server/server.go | 3 --- server/server_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/server/server.go b/server/server.go index e37293589a..5954d382f0 100644 --- a/server/server.go +++ b/server/server.go @@ -484,9 +484,6 @@ func (s *Server) reload(ctx context.Context, txn storage.Transaction, event stor } } - if !event.PolicyChanged() { - return - } s.partials = map[string]rego.PartialResult{} } diff --git a/server/server_test.go b/server/server_test.go index a9071c84eb..f7dcf8068d 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -228,6 +228,23 @@ p = true { false }` q[2] { input.y = 2 } r[1] { input.z = 3 }` + testMod7Modified := `package testmod + + default p = false + + p { q[x]; not r[x] } + + q[1] { input.x = 1 } + q[2] { input.y = 2 } + r[1] { input.z = 3 } + r[2] { input.z = 3 }` + + testMod8 := `package testmod + + p { + data.x = 1 + }` + tests := []struct { note string reqs []tr @@ -396,6 +413,18 @@ p = true { false }` tr{http.MethodPost, "/data/testmod/p", `{"input": {"x": 1, "y": 2, "z": 9999}}`, 200, `{"result": true}`}, tr{http.MethodPost, "/data/testmod/p", `{"input": {"x": 1, "z": 3}}`, 200, `{"result": false}`}, }}, + {"partial invalidate policy", []tr{ + tr{http.MethodPut, "/policies/test", testMod7, 200, ""}, + tr{http.MethodPost, "/data/testmod/p?partial", `{"input": {"x": 1, "y": 2, "z": 3}}`, 200, `{"result": true}`}, + tr{http.MethodPut, "/policies/test", testMod7Modified, 200, ""}, + tr{http.MethodPost, "/data/testmod/p?partial", `{"input": {"x": 1, "y": 2, "z": 3}}`, 200, `{"result": false}`}, + }}, + {"partial invalidate data", []tr{ + tr{http.MethodPut, "/policies/test", testMod8, 200, ""}, + tr{http.MethodPost, "/data/testmod/p?partial", "", 200, `{}`}, + tr{http.MethodPut, "/data/x", `1`, 204, ""}, + tr{http.MethodPost, "/data/testmod/p?partial", "", 200, `{"result": true}`}, + }}, {"evaluation conflict", []tr{ tr{http.MethodPut, "/policies/test", testMod4, 200, ""}, tr{http.MethodPost, "/data/testmod/p", "", 500, `{