Skip to content

Commit

Permalink
Fix partial cache invalidation for data changes
Browse files Browse the repository at this point in the history
The partial cache was not be invalidated when data changed. As a result,
callers would receive stale results when data updated.

Fixes open-policy-agent#589

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
  • Loading branch information
tsandall committed Jun 4, 2018
1 parent 17e7eed commit e186bcc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 0 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down
29 changes: 29 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, `{
Expand Down

0 comments on commit e186bcc

Please sign in to comment.