Skip to content

Commit

Permalink
Use stats to validate new manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenh committed Dec 28, 2023
1 parent c07d1c8 commit 2d870a9
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 66 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ go 1.20

require (
github.com/Masterminds/semver v1.5.0
github.com/aserto-dev/azm v0.0.18-0.20231227204629-4f6f0bf970ed
github.com/aserto-dev/aserto-grpc v0.2.2
github.com/aserto-dev/azm v0.0.18-0.20231228153554-e4a565fe3cd6
github.com/aserto-dev/errors v0.0.6
github.com/aserto-dev/go-directory v0.30.7
github.com/bufbuild/protovalidate-go v0.4.3
Expand Down
12 changes: 10 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3Q
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/aserto-dev/azm v0.0.18-0.20231227204629-4f6f0bf970ed h1:4YQgSN6FgxpFKE4QyJzqoysmSBUhC3cZIOaiSerU1g8=
github.com/aserto-dev/azm v0.0.18-0.20231227204629-4f6f0bf970ed/go.mod h1:t/+CxbaWuS8qzwrVZZefHSNtQf8aK44imB+/rC4uOyw=
github.com/aserto-dev/aserto-grpc v0.2.2 h1:i6rWdvydyqs7eBqsZHYdPPCC+xVwQve8KrBgAdkjYr8=
github.com/aserto-dev/aserto-grpc v0.2.2/go.mod h1:uDMB5njLdoCa0Hr+TOg6A4gH85uNqTvufkM5sKtEjcE=
github.com/aserto-dev/azm v0.0.18-0.20231228153554-e4a565fe3cd6 h1:6dc+NVHOjPJf99QC93fmOsTTLy4p2rfTl8Uh/owSBys=
github.com/aserto-dev/azm v0.0.18-0.20231228153554-e4a565fe3cd6/go.mod h1:t/+CxbaWuS8qzwrVZZefHSNtQf8aK44imB+/rC4uOyw=
github.com/aserto-dev/errors v0.0.6 h1:iH5fkJwBGFPbcdS4B8mwvNdwODlhDEXXPduZtjLh6vo=
github.com/aserto-dev/errors v0.0.6/go.mod h1:kenI1gamsemaR2wS+M2un0kXIJ9exTrmeRT/fCFwlWc=
github.com/aserto-dev/go-directory v0.30.7 h1:YNxwH/TovKxhr3/HILs04f0bYvOgcWIXoqEskb8iBAI=
github.com/aserto-dev/go-directory v0.30.7/go.mod h1:qd/+uHrKvskPSN48FLGeZ/FoiORxjRmikCKRIp3pnYY=
github.com/aserto-dev/header v0.0.5 h1:nEvYNgBoIwomxuvo14JuQ/ZqGGHU67NKuZyNdW4n4FU=
github.com/aserto-dev/logger v0.0.3 h1:lBB5LMdOsHCJKfEej2xY7s4OzCWUWCBhkhUU6RJ4LbM=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/bufbuild/protovalidate-go v0.4.3 h1:1Xsm3qhkwioxLDEtxWgtn0Ch71xBP/sBauT/FZnn76A=
github.com/bufbuild/protovalidate-go v0.4.3/go.mod h1:RcgJ+onKVv4OkAVtzkRUxkocb8stcUAMK0EoqR4fuZE=
Expand Down Expand Up @@ -72,6 +76,7 @@ github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI=
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 h1:6UKoz5ujsI55KNpsJH3UwCq3T8kKbZwNZBNPuTTje8U=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1/go.mod h1:YvJ2f6MplWDhfxiUC3KpyTy76kYUZA4W3pTv/wdKQ9Y=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
Expand Down Expand Up @@ -108,6 +113,7 @@ github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/nsf/jsondiff v0.0.0-20230430225905-43f6cf3098c1 h1:dOYG7LS/WK00RWZc8XGgcUTlTxpp3mKhdR2Q9z9HbXM=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/gomega v1.28.0 h1:i2rg/p9n/UqIDAMFUJ6qIUUMcsqOuUHgbpbu235Vr1c=
Expand All @@ -127,6 +133,7 @@ github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXn
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -242,6 +249,7 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
9 changes: 9 additions & 0 deletions pkg/directory/v3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ func (s *Model) SetManifest(stream dsm3.Model_SetManifestServer) error {
}

if err := s.store.DB().Update(func(tx *bolt.Tx) error {
stats, err := ds.CalculateStats(stream.Context(), tx)
if err != nil {
return derr.ErrUnknown.Msgf("failed to calculate stats: %s", err.Error())
}

if err := s.store.MC().CanUpdate(m, stats); err != nil {
return err
}

if err := ds.Manifest(md).Set(stream.Context(), tx, data); err != nil {
return derr.ErrUnknown.Msgf("failed to set manifest: %s", err.Error())
}
Expand Down
36 changes: 21 additions & 15 deletions pkg/ds/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (s *Stats) CountRelations(ctx context.Context, tx *bolt.Tx) error {
func (s *Stats) incObject(obj *dsc3.Object) {
ot, ok := s.ObjectTypes[model.ObjectName(obj.Type)]
if !ok {
ot.Relations = stats.Relations{}
ot = &stats.ObjectType{
Relations: stats.Relations{},
}
s.ObjectTypes[model.ObjectName(obj.Type)] = ot
}

Expand All @@ -84,36 +86,40 @@ func (s *Stats) incRelation(rel *dsc3.Relation) {
}

// object_types
ot, ok := s.ObjectTypes[objType]
if !ok {
ot := s.ObjectTypes[objType]
if ot == nil {
ot = &stats.ObjectType{
Relations: stats.Relations{},
}
s.ObjectTypes[objType] = ot
}
atomic.AddInt32(&ot.Count, 1)

if ot.Relations == nil {
ot.Relations = stats.Relations{}
}

// relations
re, ok := ot.Relations[relation]
if !ok {
re.SubjectTypes = stats.SubjectTypes{}
re := ot.Relations[relation]
if re == nil {
re = &stats.Relation{
SubjectTypes: stats.SubjectTypes{},
}
ot.Relations[relation] = re
}
atomic.AddInt32(&re.Count, 1)

// subject_types
st, ok := re.SubjectTypes[subType]
if !ok {
st.SubjectRelations = stats.SubjectRelations{}
st := re.SubjectTypes[subType]
if st == nil {
st = &stats.SubjectType{
SubjectRelations: stats.SubjectRelations{},
}
re.SubjectTypes[subType] = st
}
atomic.AddInt32(&st.Count, 1)

// subject_relations
if subRel != "" {
sr, ok := st.SubjectRelations[subRel]
if !ok {
sr := st.SubjectRelations[subRel]
if sr == nil {
sr = &stats.SubjectRelation{}
st.SubjectRelations[subRel] = sr
}
atomic.AddInt32(&sr.Count, 1)
Expand Down
7 changes: 6 additions & 1 deletion pkg/server/test-edge-ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
dsr3 "github.com/aserto-dev/go-directory/aserto/directory/reader/v3"
dsw3 "github.com/aserto-dev/go-directory/aserto/directory/writer/v3"

"github.com/aserto-dev/aserto-grpc/grpcutil/middlewares/gerr"
eds "github.com/aserto-dev/go-edge-ds"
"github.com/aserto-dev/go-edge-ds/pkg/directory"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -55,7 +56,11 @@ func NewTestEdgeServer(ctx context.Context, logger *zerolog.Logger, cfg *directo
logger.Error().Err(err).Msg("failed to start edge directory server")
}

s := grpc.NewServer()
errMiddleware := gerr.NewErrorMiddleware()
s := grpc.NewServer(
grpc.UnaryInterceptor(errMiddleware.Unary()),
grpc.StreamInterceptor(errMiddleware.Stream()),
)
dsr2.RegisterReaderServer(s, edgeDirServer.Reader2())
dsw2.RegisterWriterServer(s, edgeDirServer.Writer2())
dse2.RegisterExporterServer(s, edgeDirServer.Exporter2())
Expand Down
25 changes: 25 additions & 0 deletions tests/diff_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"objects": [
{
"type": "user",
"id": "michael",
"display_name": "Michael"
},
{
"type": "user",
"id": "jim",
"display_name": "Jim"
}
],

"relations": [
{
"object_type": "user",
"object_id": "jim",
"relation": "manager",
"subject_type": "user",
"subject_id": "michael"
}

]
}
Loading

0 comments on commit 2d870a9

Please sign in to comment.