2019-01-02 10:23:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-19 05:46:39 +01:00
|
|
|
"crypto/tls"
|
2019-01-02 10:23:25 +00:00
|
|
|
"net"
|
2019-09-07 01:02:38 +01:00
|
|
|
"sync"
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-07-31 13:09:45 +01:00
|
|
|
"go.uber.org/zap"
|
2019-02-04 14:50:55 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2019-01-02 10:23:25 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
2019-09-07 01:02:38 +01:00
|
|
|
"storj.io/drpc/drpcserver"
|
2019-09-07 01:00:42 +01:00
|
|
|
"storj.io/storj/pkg/listenmux"
|
2019-01-02 10:23:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Service represents a specific gRPC method collection to be registered
|
2019-10-04 21:48:41 +01:00
|
|
|
// on a shared gRPC server. Metainfo, OverlayCache, PieceStore,
|
2019-03-25 22:25:09 +00:00
|
|
|
// etc. are all examples of services.
|
2019-01-02 10:23:25 +00:00
|
|
|
type Service interface {
|
|
|
|
Run(ctx context.Context, server *Server) error
|
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
type public struct {
|
|
|
|
listener net.Listener
|
2019-09-07 01:02:38 +01:00
|
|
|
drpc *drpcserver.Server
|
2019-03-07 18:19:37 +00:00
|
|
|
grpc *grpc.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
type private struct {
|
|
|
|
listener net.Listener
|
2019-09-07 01:02:38 +01:00
|
|
|
drpc *drpcserver.Server
|
2019-03-07 18:19:37 +00:00
|
|
|
grpc *grpc.Server
|
|
|
|
}
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
// Server represents a bundle of services defined by a specific ID.
|
|
|
|
// Examples of servers are the satellite, the storagenode, and the uplink.
|
|
|
|
type Server struct {
|
2019-09-19 05:46:39 +01:00
|
|
|
log *zap.Logger
|
|
|
|
public public
|
|
|
|
private private
|
|
|
|
next []Service
|
|
|
|
tlsOptions *tlsopts.Options
|
2019-09-07 01:02:38 +01:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
wg sync.WaitGroup
|
|
|
|
once sync.Once
|
|
|
|
done chan struct{}
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 15:04:53 +00:00
|
|
|
// New creates a Server out of an Identity, a net.Listener,
|
2019-01-02 10:23:25 +00:00
|
|
|
// a UnaryServerInterceptor, and a set of services.
|
2019-09-19 05:46:39 +01:00
|
|
|
func New(log *zap.Logger, tlsOptions *tlsopts.Options, publicAddr, privateAddr string, interceptor grpc.UnaryServerInterceptor, services ...Service) (*Server, error) {
|
2019-07-31 13:09:45 +01:00
|
|
|
server := &Server{
|
2019-09-19 05:46:39 +01:00
|
|
|
log: log,
|
|
|
|
next: services,
|
|
|
|
tlsOptions: tlsOptions,
|
|
|
|
done: make(chan struct{}),
|
2019-07-31 13:09:45 +01:00
|
|
|
}
|
|
|
|
|
2019-11-13 15:43:28 +00:00
|
|
|
serverOptions := drpcserver.Options{
|
|
|
|
Manager: rpc.NewDefaultManagerOptions(),
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:09:45 +01:00
|
|
|
unaryInterceptor := server.logOnErrorUnaryInterceptor
|
2019-01-02 10:23:25 +00:00
|
|
|
if interceptor != nil {
|
2019-06-04 13:55:24 +01:00
|
|
|
unaryInterceptor = CombineInterceptors(unaryInterceptor, interceptor)
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
publicListener, err := net.Listen("tcp", publicAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-31 13:09:45 +01:00
|
|
|
server.public = public{
|
2020-01-03 20:21:34 +00:00
|
|
|
listener: wrapListener(publicListener),
|
2019-11-13 15:43:28 +00:00
|
|
|
drpc: drpcserver.NewWithOptions(serverOptions),
|
2019-01-02 10:23:25 +00:00
|
|
|
grpc: grpc.NewServer(
|
2019-07-31 13:09:45 +01:00
|
|
|
grpc.StreamInterceptor(server.logOnErrorStreamInterceptor),
|
2019-01-02 10:23:25 +00:00
|
|
|
grpc.UnaryInterceptor(unaryInterceptor),
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions.ServerOption(),
|
2019-01-02 10:23:25 +00:00
|
|
|
),
|
2019-03-07 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
privateListener, err := net.Listen("tcp", privateAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Combine(err, publicListener.Close())
|
|
|
|
}
|
2019-07-31 13:09:45 +01:00
|
|
|
server.private = private{
|
2020-01-03 20:21:34 +00:00
|
|
|
listener: wrapListener(privateListener),
|
2019-11-13 15:43:28 +00:00
|
|
|
drpc: drpcserver.NewWithOptions(serverOptions),
|
2019-03-07 18:19:37 +00:00
|
|
|
grpc: grpc.NewServer(),
|
|
|
|
}
|
|
|
|
|
2019-07-31 13:09:45 +01:00
|
|
|
return server, nil
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Identity returns the server's identity
|
2019-09-19 05:46:39 +01:00
|
|
|
func (p *Server) Identity() *identity.FullIdentity { return p.tlsOptions.Ident }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
// Addr returns the server's public listener address
|
|
|
|
func (p *Server) Addr() net.Addr { return p.public.listener.Addr() }
|
|
|
|
|
|
|
|
// PrivateAddr returns the server's private listener address
|
|
|
|
func (p *Server) PrivateAddr() net.Addr { return p.private.listener.Addr() }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
|
|
|
// GRPC returns the server's gRPC handle for registration purposes
|
2019-03-07 18:19:37 +00:00
|
|
|
func (p *Server) GRPC() *grpc.Server { return p.public.grpc }
|
|
|
|
|
2019-09-07 01:02:38 +01:00
|
|
|
// DRPC returns the server's dRPC handle for registration purposes
|
|
|
|
func (p *Server) DRPC() *drpcserver.Server { return p.public.drpc }
|
|
|
|
|
2019-03-07 18:19:37 +00:00
|
|
|
// PrivateGRPC returns the server's gRPC handle for registration purposes
|
|
|
|
func (p *Server) PrivateGRPC() *grpc.Server { return p.private.grpc }
|
2019-01-02 10:23:25 +00:00
|
|
|
|
2019-09-07 01:02:38 +01:00
|
|
|
// PrivateDRPC returns the server's dRPC handle for registration purposes
|
|
|
|
func (p *Server) PrivateDRPC() *drpcserver.Server { return p.private.drpc }
|
|
|
|
|
2019-01-02 10:23:25 +00:00
|
|
|
// Close shuts down the server
|
|
|
|
func (p *Server) Close() error {
|
2019-09-07 01:02:38 +01:00
|
|
|
p.mu.Lock()
|
|
|
|
defer p.mu.Unlock()
|
|
|
|
|
|
|
|
// Close done and wait for any Runs to exit.
|
|
|
|
p.once.Do(func() { close(p.done) })
|
|
|
|
p.wg.Wait()
|
|
|
|
|
|
|
|
// Ensure the listeners are closed in case Run was never called.
|
|
|
|
// We ignore these errors because there's not really anything to do
|
|
|
|
// even if they happen, and they'll just be errors due to duplicate
|
|
|
|
// closes anyway.
|
|
|
|
_ = p.public.listener.Close()
|
|
|
|
_ = p.private.listener.Close()
|
2019-01-02 10:23:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run will run the server and all of its services
|
|
|
|
func (p *Server) Run(ctx context.Context) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
// are there any unstarted services? start those first. the
|
|
|
|
// services should know to call Run again once they're ready.
|
|
|
|
if len(p.next) > 0 {
|
|
|
|
next := p.next[0]
|
|
|
|
p.next = p.next[1:]
|
|
|
|
return next.Run(ctx, p)
|
|
|
|
}
|
|
|
|
|
2019-09-07 01:02:38 +01:00
|
|
|
// Make sure the server isn't already closed. If it is, register
|
|
|
|
// ourselves in the wait group so that Close can wait on it.
|
|
|
|
p.mu.Lock()
|
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
p.mu.Unlock()
|
|
|
|
return errs.New("server closed")
|
|
|
|
default:
|
|
|
|
p.wg.Add(1)
|
|
|
|
defer p.wg.Done()
|
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
// We want to launch the muxes in a different group so that they are
|
|
|
|
// only closed after we're sure that p.Close is called. The reason why
|
|
|
|
// is so that we don't get "listener closed" errors because the
|
|
|
|
// Run call exits and closes the listeners before the servers have had
|
|
|
|
// a chance to be notified that they're done running.
|
|
|
|
const drpcHeader = "DRPC!!!1"
|
|
|
|
|
|
|
|
publicMux := listenmux.New(p.public.listener, len(drpcHeader))
|
2019-09-19 05:46:39 +01:00
|
|
|
publicDRPCListener := tls.NewListener(publicMux.Route(drpcHeader), p.tlsOptions.ServerTLSConfig())
|
2019-09-07 01:02:38 +01:00
|
|
|
|
|
|
|
privateMux := listenmux.New(p.private.listener, len(drpcHeader))
|
|
|
|
privateDRPCListener := privateMux.Route(drpcHeader)
|
|
|
|
|
|
|
|
// We need a new context chain because we require this context to be
|
|
|
|
// canceled only after all of the upcoming grpc/drpc servers have
|
|
|
|
// fully exited. The reason why is because Run closes listener for
|
|
|
|
// the mux when it exits, and we can only do that after all of the
|
|
|
|
// Servers are no longer accepting.
|
|
|
|
muxCtx, muxCancel := context.WithCancel(context.Background())
|
|
|
|
defer muxCancel()
|
|
|
|
|
|
|
|
var muxGroup errgroup.Group
|
|
|
|
muxGroup.Go(func() error {
|
|
|
|
return publicMux.Run(muxCtx)
|
|
|
|
})
|
|
|
|
muxGroup.Go(func() error {
|
|
|
|
return privateMux.Run(muxCtx)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Now we launch all the stuff that uses the listeners.
|
2019-02-04 14:50:55 +00:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2019-09-07 01:00:42 +01:00
|
|
|
defer cancel()
|
|
|
|
|
2019-02-04 14:50:55 +00:00
|
|
|
var group errgroup.Group
|
2019-09-07 01:00:42 +01:00
|
|
|
group.Go(func() error {
|
2019-09-07 01:02:38 +01:00
|
|
|
select {
|
|
|
|
case <-p.done:
|
|
|
|
cancel()
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
|
|
|
|
p.public.grpc.GracefulStop()
|
|
|
|
p.private.grpc.GracefulStop()
|
|
|
|
|
|
|
|
return nil
|
2019-09-07 01:00:42 +01:00
|
|
|
})
|
2019-09-07 01:02:38 +01:00
|
|
|
|
2019-09-07 01:00:42 +01:00
|
|
|
group.Go(func() error {
|
2019-09-07 01:02:38 +01:00
|
|
|
defer cancel()
|
|
|
|
return p.public.grpc.Serve(publicMux.Default())
|
2019-09-07 01:00:42 +01:00
|
|
|
})
|
2019-02-04 14:50:55 +00:00
|
|
|
group.Go(func() error {
|
2019-09-07 01:02:38 +01:00
|
|
|
defer cancel()
|
|
|
|
return p.public.drpc.Serve(ctx, publicDRPCListener)
|
2019-02-04 14:50:55 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
2019-02-06 13:19:14 +00:00
|
|
|
defer cancel()
|
2019-09-07 01:02:38 +01:00
|
|
|
return p.private.grpc.Serve(privateMux.Default())
|
2019-03-07 18:19:37 +00:00
|
|
|
})
|
|
|
|
group.Go(func() error {
|
|
|
|
defer cancel()
|
2019-09-07 01:02:38 +01:00
|
|
|
return p.private.drpc.Serve(ctx, privateDRPCListener)
|
2019-02-04 14:50:55 +00:00
|
|
|
})
|
|
|
|
|
2019-09-07 01:02:38 +01:00
|
|
|
// Now we wait for all the stuff using the listeners to exit.
|
|
|
|
err = group.Wait()
|
|
|
|
|
|
|
|
// Now we close down our listeners.
|
|
|
|
muxCancel()
|
|
|
|
return errs.Combine(err, muxGroup.Wait())
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|