@@ -972,15 +972,32 @@ func getServerEndpointCounter(method string) telemetry.Counter {
972
972
return telemetry .GetCounter (fmt .Sprintf ("%s.%s" , counterPrefix , method ))
973
973
}
974
974
975
- // Start starts the server on the specified port, starts gossip and initializes
976
- // the node using the engines from the server's context. This is complex since
977
- // it sets up the listeners and the associated port muxing, but especially since
978
- // it has to solve the "bootstrapping problem": nodes need to connect to Gossip
979
- // fairly early, but what drives Gossip connectivity are the first range
980
- // replicas in the kv store. This in turn suggests opening the Gossip server
981
- // early. However, naively doing so also serves most other services prematurely,
982
- // which exposes a large surface of potentially underinitialized services. This
983
- // is avoided with some additional complexity that can be summarized as follows:
975
+ // Start calls PreStart() and AcceptClient() in sequence.
976
+ // This is suitable for use e.g. in tests.
977
+ func (s * Server ) Start (ctx context.Context ) error {
978
+ if err := s .PreStart (ctx ); err != nil {
979
+ return err
980
+ }
981
+ return s .AcceptClients (ctx )
982
+ }
983
+
984
+ // PreStart starts the server on the specified port, starts gossip and
985
+ // initializes the node using the engines from the server's context.
986
+ //
987
+ // It does not activate the pgwire listener over the network / unix
988
+ // socket, which is done by the AcceptClients() method. The separation
989
+ // between the two exists so that SQL initialization can take place
990
+ // before the first client is accepted.
991
+ //
992
+ // PreStart is complex since it sets up the listeners and the associated
993
+ // port muxing, but especially since it has to solve the
994
+ // "bootstrapping problem": nodes need to connect to Gossip fairly
995
+ // early, but what drives Gossip connectivity are the first range
996
+ // replicas in the kv store. This in turn suggests opening the Gossip
997
+ // server early. However, naively doing so also serves most other
998
+ // services prematurely, which exposes a large surface of potentially
999
+ // underinitialized services. This is avoided with some additional
1000
+ // complexity that can be summarized as follows:
984
1001
//
985
1002
// - before blocking trying to connect to the Gossip network, we already open
986
1003
// the admin UI (so that its diagnostics are available)
@@ -990,7 +1007,7 @@ func getServerEndpointCounter(method string) telemetry.Counter {
990
1007
//
991
1008
// The passed context can be used to trace the server startup. The context
992
1009
// should represent the general startup operation.
993
- func (s * Server ) Start (ctx context.Context ) error {
1010
+ func (s * Server ) PreStart (ctx context.Context ) error {
994
1011
ctx = s .AnnotateCtx (ctx )
995
1012
996
1013
// Start the time sanity checker.
@@ -1691,7 +1708,7 @@ func (s *Server) Start(ctx context.Context) error {
1691
1708
// executes a SQL query, this must be done after the SQL layer is ready.
1692
1709
s .node .recordJoinEvent ()
1693
1710
1694
- if err := s .sqlServer .start (
1711
+ if err := s .sqlServer .preStart (
1695
1712
workersCtx ,
1696
1713
s .stopper ,
1697
1714
s .cfg .TestingKnobs ,
@@ -1712,6 +1729,24 @@ func (s *Server) Start(ctx context.Context) error {
1712
1729
// been run so it is safe to upgrade to the binary's current version.
1713
1730
s .startAttemptUpgrade (ctx )
1714
1731
1732
+ log .Event (ctx , "server initialized" )
1733
+ return nil
1734
+ }
1735
+
1736
+ // AcceptClients starts listening for incoming SQL clients over the network.
1737
+ func (s * Server ) AcceptClients (ctx context.Context ) error {
1738
+ workersCtx := s .AnnotateCtx (context .Background ())
1739
+
1740
+ if err := s .sqlServer .startServeSQL (
1741
+ workersCtx ,
1742
+ s .stopper ,
1743
+ s .sqlServer .connManager ,
1744
+ s .sqlServer .pgL ,
1745
+ s .cfg .SocketFile ,
1746
+ ); err != nil {
1747
+ return err
1748
+ }
1749
+
1715
1750
log .Event (ctx , "server ready" )
1716
1751
return nil
1717
1752
}
0 commit comments