Skip to content

Commit f1941b3

Browse files
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]>
1 parent 9ab8768 commit f1941b3

File tree

9 files changed

+175
-118
lines changed

9 files changed

+175
-118
lines changed

dgraph/cmd/alpha/run.go

+22-13
Original file line numberDiff line numberDiff line change
@@ -753,43 +753,52 @@ func run() {
753753
}
754754
}()
755755

756-
// Setup external communication.
757-
aclCloser := y.NewCloser(1)
756+
updaters := y.NewCloser(4)
758757
go func() {
759758
worker.StartRaftNodes(worker.State.WALstore, bindall)
760759
// initialization of the admin account can only be done after raft nodes are running
761760
// and health check passes
762-
edgraph.ResetAcl()
763-
edgraph.RefreshAcls(aclCloser)
764-
edgraph.ResetCors()
761+
edgraph.ResetAcl(updaters)
762+
edgraph.RefreshAcls(updaters)
763+
edgraph.ResetCors(updaters)
765764
// Update the accepted cors origins.
766-
for {
767-
origins, err := edgraph.GetCorsOrigins(context.TODO())
765+
for updaters.Ctx().Err() == nil {
766+
origins, err := edgraph.GetCorsOrigins(updaters.Ctx())
768767
if err != nil {
769768
glog.Errorf("Error while retriving cors origins: %s", err.Error())
770769
continue
771770
}
772771
x.UpdateCorsOrigins(origins)
773-
break
772+
return
774773
}
775774
}()
776775
// Listen for any new cors origin update.
777-
corsCloser := y.NewCloser(1)
778-
go listenForCorsUpdate(corsCloser)
776+
go listenForCorsUpdate(updaters)
779777

780778
// Graphql subscribes to alpha to get schema updates. We need to close that before we
781779
// close alpha. This closer is for closing and waiting that subscription.
782780
adminCloser := y.NewCloser(1)
783781

784782
setupServer(adminCloser)
785783
glog.Infoln("GRPC and HTTP stopped.")
786-
aclCloser.SignalAndWait()
787-
corsCloser.SignalAndWait()
784+
785+
// This might not close until group is given the signal to close. So, only signal here,
786+
// wait for it after group is closed.
787+
updaters.Signal()
788+
788789
worker.BlockingStop()
790+
glog.Infoln("worker stopped.")
791+
789792
adminCloser.SignalAndWait()
790-
glog.Info("Disposing server state.")
793+
glog.Infoln("adminCloser closed.")
794+
791795
worker.State.Dispose()
792796
x.RemoveCidFile()
797+
glog.Info("worker.State disposed.")
798+
799+
updaters.Wait()
800+
glog.Infoln("updaters closed.")
801+
793802
glog.Infoln("Server shutdown. Bye!")
794803
}
795804

dgraph/cmd/zero/run.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -336,5 +336,10 @@ func run() {
336336

337337
glog.Infoln("Running Dgraph Zero...")
338338
st.zero.closer.Wait()
339-
glog.Infoln("All done.")
339+
glog.Infoln("Closer closed.")
340+
341+
err = kv.Close()
342+
glog.Infof("Badger closed with err: %v\n", err)
343+
344+
glog.Infoln("All done. Goodbye!")
340345
}

edgraph/access.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (s *Server) Login(ctx context.Context,
4242
}
4343

4444
// ResetAcl is an empty method since ACL is only supported in the enterprise version.
45-
func ResetAcl() {
45+
func ResetAcl(closer *y.Closer) {
4646
// do nothing
4747
}
4848

edgraph/access_ee.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -306,15 +306,15 @@ func authorizeUser(ctx context.Context, userid string, password string) (
306306

307307
// RefreshAcls queries for the ACL triples and refreshes the ACLs accordingly.
308308
func RefreshAcls(closer *y.Closer) {
309-
defer closer.Done()
309+
defer func() {
310+
glog.Infoln("RefreshAcls closed")
311+
closer.Done()
312+
}()
310313
if len(worker.Config.HmacSecret) == 0 {
311314
// the acl feature is not turned on
312315
return
313316
}
314317

315-
ticker := time.NewTicker(worker.Config.AclRefreshInterval)
316-
defer ticker.Stop()
317-
318318
// retrieve the full data set of ACLs from the corresponding alpha server, and update the
319319
// aclCachePtr
320320
retrieveAcls := func() error {
@@ -324,9 +324,7 @@ func RefreshAcls(closer *y.Closer) {
324324
ReadOnly: true,
325325
}
326326

327-
ctx := context.Background()
328-
var err error
329-
queryResp, err := (&Server{}).doQuery(ctx, &queryRequest, NoAuthorize)
327+
queryResp, err := (&Server{}).doQuery(closer.Ctx(), &queryRequest, NoAuthorize)
330328
if err != nil {
331329
return errors.Errorf("unable to retrieve acls: %v", err)
332330
}
@@ -340,14 +338,16 @@ func RefreshAcls(closer *y.Closer) {
340338
return nil
341339
}
342340

341+
ticker := time.NewTicker(worker.Config.AclRefreshInterval)
342+
defer ticker.Stop()
343343
for {
344344
select {
345-
case <-closer.HasBeenClosed():
346-
return
347345
case <-ticker.C:
348346
if err := retrieveAcls(); err != nil {
349-
glog.Errorf("Error while retrieving acls:%v", err)
347+
glog.Errorf("Error while retrieving acls: %v", err)
350348
}
349+
case <-closer.HasBeenClosed():
350+
return
351351
}
352352
}
353353
}
@@ -368,7 +368,12 @@ const queryAcls = `
368368
`
369369

370370
// ResetAcl clears the aclCachePtr and upserts the Groot account.
371-
func ResetAcl() {
371+
func ResetAcl(closer *y.Closer) {
372+
defer func() {
373+
glog.Infof("ResetAcl closed")
374+
closer.Done()
375+
}()
376+
372377
if len(worker.Config.HmacSecret) == 0 {
373378
// The acl feature is not turned on.
374379
return
@@ -435,8 +440,8 @@ func ResetAcl() {
435440
return nil
436441
}
437442

438-
for {
439-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
443+
for closer.Ctx().Err() == nil {
444+
ctx, cancel := context.WithTimeout(closer.Ctx(), time.Minute)
440445
defer cancel()
441446
if err := upsertGuardians(ctx); err != nil {
442447
glog.Infof("Unable to upsert the guardian group. Error: %v", err)
@@ -446,8 +451,8 @@ func ResetAcl() {
446451
break
447452
}
448453

449-
for {
450-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
454+
for closer.Ctx().Err() == nil {
455+
ctx, cancel := context.WithTimeout(closer.Ctx(), time.Minute)
451456
defer cancel()
452457
if err := upsertGroot(ctx); err != nil {
453458
glog.Infof("Unable to upsert the groot account. Error: %v", err)

edgraph/server.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"google.golang.org/grpc/metadata"
4343
"google.golang.org/grpc/status"
4444

45+
"github.com/dgraph-io/badger/v2/y"
4546
"github.com/dgraph-io/dgo/v200"
4647
"github.com/dgraph-io/dgo/v200/protos/api"
4748
"github.com/dgraph-io/dgraph/chunker"
@@ -328,8 +329,8 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
328329
// reset their in-memory GraphQL schema
329330
_, err = UpdateGQLSchema(ctx, "", "")
330331
// recreate the admin account after a drop all operation
331-
ResetAcl()
332-
ResetCors()
332+
ResetAcl(nil)
333+
ResetCors(nil)
333334
return empty, err
334335
}
335336

@@ -353,8 +354,8 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
353354
// just reinsert the GraphQL schema, no need to alter dgraph schema as this was drop_data
354355
_, err = UpdateGQLSchema(ctx, graphQLSchema, "")
355356
// recreate the admin account after a drop data operation
356-
ResetAcl()
357-
ResetCors()
357+
ResetAcl(nil)
358+
ResetCors(nil)
358359
return empty, err
359360
}
360361

@@ -1541,7 +1542,12 @@ func isDropAll(op *api.Operation) bool {
15411542

15421543
// ResetCors make the dgraph to accept all the origins if no origins were given
15431544
// by the users.
1544-
func ResetCors() {
1545+
func ResetCors(closer *y.Closer) {
1546+
defer func() {
1547+
glog.Infof("ResetCors closed")
1548+
closer.Done()
1549+
}()
1550+
15451551
req := &api.Request{
15461552
Query: `query{
15471553
cors as var(func: has(dgraph.cors))
@@ -1561,8 +1567,8 @@ func ResetCors() {
15611567
CommitNow: true,
15621568
}
15631569

1564-
for {
1565-
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
1570+
for closer.Ctx().Err() == nil {
1571+
ctx, cancel := context.WithTimeout(closer.Ctx(), time.Minute)
15661572
defer cancel()
15671573
if _, err := (&Server{}).doQuery(ctx, req, CorsMutationAllowed); err != nil {
15681574
glog.Infof("Unable to upsert cors. Error: %v", err)
@@ -1623,9 +1629,8 @@ func GetCorsOrigins(ctx context.Context) ([]string, error) {
16231629
if err = json.Unmarshal(res.Json, corsRes); err != nil {
16241630
return nil, err
16251631
}
1626-
if len(corsRes.Me) > 1 {
1627-
glog.Errorf("Something went wrong in cors predicate, expected 1 predicate but got %d",
1628-
len(corsRes.Me))
1632+
if len(corsRes.Me) != 1 {
1633+
return []string{}, fmt.Errorf("GetCorsOrigins returned %d results", len(corsRes.Me))
16291634
}
16301635
return corsRes.Me[0].DgraphCors, nil
16311636
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ require (
1717
github.com/blevesearch/segment v0.0.0-20160915185041-762005e7a34f // indirect
1818
github.com/blevesearch/snowballstem v0.0.0-20180110192139-26b06a2c243d // indirect
1919
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd
20-
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200826122734-bc243f38bfe1
20+
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200828220306-806a325a0462
2121
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6
2222
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2
23-
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de
23+
github.com/dgraph-io/ristretto v0.0.4-0.20200820164438-623d8ef1614b
2424
github.com/dgrijalva/jwt-go v3.2.0+incompatible
2525
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1
2626
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
8484
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8585
github.com/dgraph-io/badger v1.6.0 h1:DshxFxZWXUcO0xX476VJC07Xsr6ZCBVRHKZ93Oh7Evo=
8686
github.com/dgraph-io/badger v1.6.0/go.mod h1:zwt7syl517jmP8s94KqSxTlM6IMsdhYy6psNgSztDR4=
87-
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200826122734-bc243f38bfe1 h1:vPPlQYByX3+Z3NOaB06i7VCb0bNOznVQ9eEnqLxbrH0=
88-
github.com/dgraph-io/badger/v2 v2.2007.2-0.20200826122734-bc243f38bfe1/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
87+
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200828220306-806a325a0462 h1:5A3xbCP8emF/lJ1btFdvMZ/oBaVpF9Ncldbop5MFcwM=
88+
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200828220306-806a325a0462/go.mod h1:EdVb0qoPSOGlrBrHSG4ArQTao3rVHe14qaVx1qqO7Vk=
8989
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6 h1:toHzMCdCUgYsjM0cW9+wafnKFXfp1HizIJUyzihN+vk=
9090
github.com/dgraph-io/dgo/v200 v200.0.0-20200805103119-a3544c464dd6/go.mod h1:rHa+h3kI4M8ASOirxyIyNeXBfHFgeskVUum2OrDMN3U=
9191
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2 h1:NSl3XXyON9bgmBJSAvr5FPrgILAovtoTs7FwdtaZZq0=
9292
github.com/dgraph-io/graphql-transport-ws v0.0.0-20200715131837-c0460019ead2/go.mod h1:7z3c/5w0sMYYZF5bHsrh8IH4fKwG5O5Y70cPH1ZLLRQ=
93-
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de h1:t0UHb5vdojIDUqktM6+xJAfScFBsVpXZmqC9dsgJmeA=
94-
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
93+
github.com/dgraph-io/ristretto v0.0.4-0.20200820164438-623d8ef1614b h1:/g8jOqvD1UzHTOwENtkqcLmMLzTcN18P3ut8aSUZ45g=
94+
github.com/dgraph-io/ristretto v0.0.4-0.20200820164438-623d8ef1614b/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
9595
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
9696
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
9797
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=

0 commit comments

Comments
 (0)