Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zero server shutdown endpoint #2928

Merged
merged 7 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions dgraph/cmd/zero/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import (
"github.com/golang/glog"
)

var (
emptyNum pb.Num
emptyAssignedIds pb.AssignedIds
)
var emptyAssignedIds pb.AssignedIds

const (
leaseBandwidth = uint64(10000)
Expand Down
20 changes: 17 additions & 3 deletions dgraph/cmd/zero/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net"
"net/http"
"strconv"
"sync"
"time"

"github.com/dgraph-io/dgraph/protos/pb"
Expand Down Expand Up @@ -224,15 +223,30 @@ func (st *state) getState(w http.ResponseWriter, r *http.Request) {
}
}

func (st *state) serveHTTP(l net.Listener, wg *sync.WaitGroup) {
func (st *state) shutdown(w http.ResponseWriter, r *http.Request) {
x.AddCorsHeaders(w)
if r.Method == "OPTIONS" {
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusBadRequest)
x.SetStatus(w, x.ErrorInvalidMethod, "Invalid method")
return
}

st.zero.closer.Signal()
w.Write([]byte("Server is shutting down...\n"))
srfrog marked this conversation as resolved.
Show resolved Hide resolved
}

func (st *state) serveHTTP(l net.Listener) {
srv := &http.Server{
ReadTimeout: 10 * time.Second,
WriteTimeout: 600 * time.Second,
IdleTimeout: 2 * time.Minute,
}

go func() {
defer wg.Done()
defer st.zero.closer.Done()
err := srv.Serve(l)
glog.Errorf("Stopped taking more http(s) requests. Err: %v", err)
ctx, cancel := context.WithTimeout(context.Background(), 630*time.Second)
Expand Down
2 changes: 1 addition & 1 deletion dgraph/cmd/zero/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (s *Server) Oracle(unused *api.Payload, server pb.Zero_OracleServer) error
}
case <-ctx.Done():
return ctx.Err()
case <-s.shutDownCh:
case <-s.closer.HasBeenClosed():
return errServerShutDown
}
}
Expand Down
43 changes: 29 additions & 14 deletions dgraph/cmd/zero/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -105,7 +104,7 @@ type state struct {
zero *Server
}

func (st *state) serveGRPC(l net.Listener, wg *sync.WaitGroup, store *raftwal.DiskStorage) {
func (st *state) serveGRPC(l net.Listener, store *raftwal.DiskStorage) {
if collector := Zero.Conf.GetString("jaeger.collector"); len(collector) > 0 {
// Port details: https://www.jaegertracing.io/docs/getting-started/
// Default collectorEndpointURI := "http://localhost:14268"
Expand Down Expand Up @@ -148,7 +147,7 @@ func (st *state) serveGRPC(l net.Listener, wg *sync.WaitGroup, store *raftwal.Di
pb.RegisterRaftServer(s, st.rs)

go func() {
defer wg.Done()
defer st.zero.closer.Done()
err := s.Serve(l)
glog.Infof("gRpc server stopped : %v", err)
st.node.stop <- struct{}{}
Expand Down Expand Up @@ -228,17 +227,16 @@ func run() {
defer kv.Close()
store := raftwal.Init(kv, opts.nodeId, 0)

var wg sync.WaitGroup
wg.Add(3)
// Initialize the servers.
var st state
st.serveGRPC(grpcListener, &wg, store)
st.serveHTTP(httpListener, &wg)
st.serveGRPC(grpcListener, store)
st.serveHTTP(httpListener)

http.HandleFunc("/state", st.getState)
http.HandleFunc("/removeNode", st.removeNode)
http.HandleFunc("/moveTablet", st.moveTablet)
http.HandleFunc("/assign", st.assign)
http.HandleFunc("/shutdown", st.shutdown)
zpages.Handle(http.DefaultServeMux, "/z")

// This must be here. It does not work if placed before Grpc init.
Expand All @@ -251,18 +249,35 @@ func run() {
sdCh := make(chan os.Signal, 1)
signal.Notify(sdCh, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

// handle signals
go func() {
defer wg.Done()
<-sdCh
glog.Infof("Shutting down...")
for {
select {
case sig, ok := <-sdCh:
if !ok {
return
}
glog.Infof("--- Received %s signal", sig)
signal.Stop(sdCh)
st.zero.closer.Signal()
}
}
}()

st.zero.closer.AddRunning(1)

go func() {
defer st.zero.closer.Done()
<-st.zero.closer.HasBeenClosed()
glog.Infoln("Shutting down...")
close(sdCh)
// Close doesn't close already opened connections.
httpListener.Close()
grpcListener.Close()
close(st.zero.shutDownCh)
st.node.trySnapshot(0)
}()

glog.Infof("Running Dgraph Zero...")
wg.Wait()
glog.Infof("All done.")
glog.Infoln("Running Dgraph Zero...")
st.zero.closer.Wait()
glog.Infoln("All done.")
}
9 changes: 5 additions & 4 deletions dgraph/cmd/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
otrace "go.opencensus.io/trace"
"golang.org/x/net/context"

"github.com/dgraph-io/badger/y"
"github.com/dgraph-io/dgo/protos/api"
"github.com/dgraph-io/dgraph/conn"
"github.com/dgraph-io/dgraph/protos/pb"
Expand Down Expand Up @@ -56,8 +57,8 @@ type Server struct {
// groupMap map[uint32]*Group
nextGroup uint32
leaderChangeCh chan struct{}
shutDownCh chan struct{} // Used to tell stream to close.
connectLock sync.Mutex // Used to serialize connect requests from servers.
closer *y.Closer // Used to tell stream to close.
connectLock sync.Mutex // Used to serialize connect requests from servers.

blockCommitsOn map[string]struct{}
}
Expand All @@ -76,7 +77,7 @@ func (s *Server) Init() {
s.nextTxnTs = 1
s.nextGroup = 1
s.leaderChangeCh = make(chan struct{}, 1)
s.shutDownCh = make(chan struct{}, 1)
s.closer = y.NewCloser(2) // grpc and http
s.blockCommitsOn = make(map[string]struct{})
go s.rebalanceTablets()
}
Expand Down Expand Up @@ -614,7 +615,7 @@ func (s *Server) StreamMembership(_ *api.Payload, stream pb.Zero_StreamMembershi
}
case <-ctx.Done():
return ctx.Err()
case <-s.shutDownCh:
case <-s.closer.HasBeenClosed():
return errServerShutDown
}
}
Expand Down