Skip to content

Commit

Permalink
release/v1.2 - Fix(Alpha): MASA: Make Alpha Shutdown Again (#6313) (#…
Browse files Browse the repository at this point in the history
…6406)

* Fix(Alpha): MASA: Make Alpha Shutdown Again (#6313)

This PR removes the usage of context.Background() in groups.go and ensures that all the various goroutines exit as intended.

Changes:
* Shutdown SubscribeForUpdates correctly.
* Fix up all the closing conditions.
* Consolidate updaters into one closer
* Update Badger to master
* fix(build): Update ResetAcl args for OSS build.
* chore: Remove TODO comment.

Co-authored-by: Daniel Mai <[email protected]>
(cherry picked from commit f1941b3)
  • Loading branch information
Ibrahim Jarif authored Sep 4, 2020
1 parent db1fc38 commit 5e8cf02
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 88 deletions.
21 changes: 15 additions & 6 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,23 +707,32 @@ func run() {
}
}()

// Setup external communication.
aclCloser := z.NewCloser(1)
updaters := z.NewCloser(2)
go func() {
worker.StartRaftNodes(worker.State.WALstore, bindall)
atomic.AddUint32(&initDone, 1)

// initialization of the admin account can only be done after raft nodes are running
// and health check passes
edgraph.ResetAcl()
edgraph.RefreshAcls(aclCloser)
edgraph.ResetAcl(updaters)
edgraph.RefreshAcls(updaters)
}()

setupServer()
glog.Infoln("GRPC and HTTP stopped.")
aclCloser.SignalAndWait()

// This might not close until group is given the signal to close. So, only signal here,
// wait for it after group is closed.
updaters.Signal()

worker.BlockingStop()
glog.Info("Disposing server state.")
glog.Infoln("worker stopped.")

worker.State.Dispose()
glog.Info("worker.State disposed.")

updaters.Wait()
glog.Infoln("updaters closed.")

glog.Infoln("Server shutdown. Bye!")
}
7 changes: 6 additions & 1 deletion dgraph/cmd/zero/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,5 +347,10 @@ func run() {

glog.Infoln("Running Dgraph Zero...")
st.zero.closer.Wait()
glog.Infoln("All done.")
glog.Infoln("Closer closed.")

err = kv.Close()
glog.Infof("Badger closed with err: %v\n", err)

glog.Infoln("All done. Goodbye!")
}
2 changes: 1 addition & 1 deletion edgraph/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Server) Login(ctx context.Context,
}

// ResetAcl is an empty method since ACL is only supported in the enterprise version.
func ResetAcl() {
func ResetAcl(closer *z.Closer) {
// do nothing
}

Expand Down
35 changes: 20 additions & 15 deletions edgraph/access_ee.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ func authorizeUser(ctx context.Context, userid string, password string) (

// RefreshAcls queries for the ACL triples and refreshes the ACLs accordingly.
func RefreshAcls(closer *z.Closer) {
defer closer.Done()
defer func() {
glog.Infoln("RefreshAcls closed")
closer.Done()
}()
if len(worker.Config.HmacSecret) == 0 {
// the acl feature is not turned on
return
}

ticker := time.NewTicker(worker.Config.AclRefreshInterval)
defer ticker.Stop()

// retrieve the full data set of ACLs from the corresponding alpha server, and update the
// aclCachePtr
retrieveAcls := func() error {
Expand All @@ -315,9 +315,7 @@ func RefreshAcls(closer *z.Closer) {
ReadOnly: true,
}

ctx := context.Background()
var err error
queryResp, err := (&Server{}).doQuery(ctx, &queryRequest, NoAuthorize)
queryResp, err := (&Server{}).doQuery(closer.Ctx(), &queryRequest, NoAuthorize)
if err != nil {
return errors.Errorf("unable to retrieve acls: %v", err)
}
Expand All @@ -331,14 +329,16 @@ func RefreshAcls(closer *z.Closer) {
return nil
}

ticker := time.NewTicker(worker.Config.AclRefreshInterval)
defer ticker.Stop()
for {
select {
case <-closer.HasBeenClosed():
return
case <-ticker.C:
if err := retrieveAcls(); err != nil {
glog.Errorf("Error while retrieving acls:%v", err)
glog.Errorf("Error while retrieving acls: %v", err)
}
case <-closer.HasBeenClosed():
return
}
}
}
Expand All @@ -353,7 +353,12 @@ const queryAcls = `
`

// ResetAcl clears the aclCachePtr and upserts the Groot account.
func ResetAcl() {
func ResetAcl(closer *z.Closer) {
defer func() {
glog.Infof("ResetAcl closed")
closer.Done()
}()

if len(worker.Config.HmacSecret) == 0 {
// The acl feature is not turned on.
return
Expand Down Expand Up @@ -420,8 +425,8 @@ func ResetAcl() {
return nil
}

for {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
for closer.Ctx().Err() == nil {
ctx, cancel := context.WithTimeout(closer.Ctx(), time.Minute)
defer cancel()
if err := upsertGuardians(ctx); err != nil {
glog.Infof("Unable to upsert the guardian group. Error: %v", err)
Expand All @@ -431,8 +436,8 @@ func ResetAcl() {
break
}

for {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
for closer.Ctx().Err() == nil {
ctx, cancel := context.WithTimeout(closer.Ctx(), time.Minute)
defer cancel()
if err := upsertGroot(ctx); err != nil {
glog.Infof("Unable to upsert the groot account. Error: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
_, err := query.ApplyMutations(ctx, m)

// recreate the admin account after a drop all operation
ResetAcl()
ResetAcl(nil)
return empty, err
}

Expand All @@ -143,7 +143,7 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
_, err := query.ApplyMutations(ctx, m)

// recreate the admin account after a drop data operation
ResetAcl()
ResetAcl(nil)
return empty, err
}

Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ contrib.go.opencensus.io/exporter/prometheus v0.1.0 h1:SByaIoWwNgMdPSgl5sMqM2KDE
contrib.go.opencensus.io/exporter/prometheus v0.1.0/go.mod h1:cGFniUXGZlKRjzOyuZJ6mgB+PgBcCIa79kEKR8YCW+A=
github.com/99designs/gqlgen v0.10.1 h1:1BgB6XKGTHq7uH4G1/PYyKe2Kz7/vw3AlvMZlD3TEEY=
github.com/99designs/gqlgen v0.10.1/go.mod h1:IviubpnyI4gbBcj8IcxSSc/Q/+af5riwCmJmwF0uaPE=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.3.2 h1:2L2f5t3kKnCLxnClDD/PrDfExFFa1wjESgxHG/B1ibo=
Expand Down Expand Up @@ -63,17 +62,10 @@ github.com/d4l3k/messagediff v1.2.1/go.mod h1:Oozbb1TVXFac9FtSIxHBMnBCq2qeH/2KkE
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgraph-io/badger v1.6.1 h1:w9pSFNSdq/JPM1N12Fz/F/bzo993Is1W+Q7HjPzi7yg=
github.com/dgraph-io/badger v1.6.1/go.mod h1:FRmFw3uxvcpa8zG3Rxs0th+hCLIuaQg8HlNV5bjgnuU=
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200827131741-d5a25b83fbf4 h1:DUDFTVgqZysKplH39/ya0aI4+zGm91L9QttXgITT2YE=
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200827131741-d5a25b83fbf4/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/dgo v1.0.0 h1:DRuI66G+j0XWDOXly4v5PSk2dGkbIopAZIirRjq7lzI=
github.com/dgraph-io/dgo/v2 v2.1.1-0.20191127085444-c7a02678e8a6 h1:5leDFqGys055YO3TbghBhk/QdRPEwyLPdgsSJfiR20I=
github.com/dgraph-io/dgo/v2 v2.1.1-0.20191127085444-c7a02678e8a6/go.mod h1:LJCkLxm5fUMcU+yb8gHFjHt7ChgNuz3YnQQ6MQkmscI=
github.com/dgraph-io/dgo/v200 v200.0.0-20200825025457-a38d5eaacbf8 h1:twtbiz+2PsuJEZWP+WGYdJEtD/NW1d7T5m3EN9JSBXI=
github.com/dgraph-io/dgo/v200 v200.0.0-20200825025457-a38d5eaacbf8/go.mod h1:Co+FwJrnndSrPORO8Gdn20dR7FPTfmXr0W/su0Ve/Ig=
github.com/dgraph-io/ristretto v0.0.2/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgraph-io/ristretto v0.0.4-0.20200904131139-4dec2770af66 h1:ectpJv2tGhTudyk0JhqE/53o/ObH30u5yt/yThsAn3I=
github.com/dgraph-io/ristretto v0.0.4-0.20200904131139-4dec2770af66/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
Expand Down Expand Up @@ -159,7 +151,6 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.0.0/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
Expand Down
10 changes: 6 additions & 4 deletions graphql/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/worker"
"github.com/dgraph-io/dgraph/x"
"github.com/dgraph-io/ristretto/z"
)

const (
Expand Down Expand Up @@ -130,7 +131,7 @@ type adminServer struct {

// NewServers initializes the GraphQL servers. It sets up an empty server for the
// main /graphql endpoint and an admin server. The result is mainServer, adminServer.
func NewServers(withIntrospection bool) (web.IServeGraphQL, web.IServeGraphQL) {
func NewServers(withIntrospection bool, closer *z.Closer) (web.IServeGraphQL, web.IServeGraphQL) {

gqlSchema, err := schema.FromString("")
if err != nil {
Expand All @@ -146,7 +147,7 @@ func NewServers(withIntrospection bool) (web.IServeGraphQL, web.IServeGraphQL) {
Urw: resolve.NewUpdateRewriter,
Drw: resolve.NewDeleteRewriter(),
}
adminResolvers := newAdminResolver(mainServer, fns, withIntrospection)
adminResolvers := newAdminResolver(mainServer, fns, withIntrospection, closer)
adminServer := web.NewServer(adminResolvers)

return mainServer, adminServer
Expand All @@ -156,7 +157,8 @@ func NewServers(withIntrospection bool) (web.IServeGraphQL, web.IServeGraphQL) {
func newAdminResolver(
gqlServer web.IServeGraphQL,
fns *resolve.ResolverFns,
withIntrospection bool) *resolve.RequestResolver {
withIntrospection bool,
closer *z.Closer) *resolve.RequestResolver {

adminSchema, err := schema.FromString(graphqlAdminSchema)
if err != nil {
Expand Down Expand Up @@ -230,7 +232,7 @@ func newAdminResolver(
server.schema = newSchema
server.status = healthy
server.resetSchema(gqlSchema)
}, 1)
}, 1, closer)

go server.initServer()

Expand Down
Loading

0 comments on commit 5e8cf02

Please sign in to comment.