@@ -13,6 +13,7 @@ import (
13
13
"math"
14
14
"math/rand"
15
15
"strings"
16
+ "sync"
16
17
"sync/atomic"
17
18
"time"
18
19
"unicode/utf8"
@@ -1273,18 +1274,6 @@ func (s *Server) newConnExecutor(
1273
1274
1274
1275
ex .extraTxnState .hasAdminRoleCache = HasAdminRoleCache {}
1275
1276
1276
- if lm := ex .server .cfg .LeaseManager ; executorType == executorTypeExec && lm != nil {
1277
- if desc , err := lm .Acquire (ctx , ex .server .cfg .Clock .Now (), keys .SystemDatabaseID ); err != nil {
1278
- log .Infof (ctx , "unable to lease system database to determine if PCR reader is in use: %s" , err )
1279
- } else {
1280
- defer desc .Release (ctx )
1281
- // The system database ReplicatedPCRVersion is set during reader tenant bootstrap,
1282
- // which guarantees that all user tenant sql connections to the reader tenant will
1283
- // correctly set this
1284
- ex .isPCRReaderCatalog = desc .Underlying ().(catalog.DatabaseDescriptor ).GetReplicatedPCRVersion () != 0
1285
- }
1286
- }
1287
-
1288
1277
if postSetupFn != nil {
1289
1278
postSetupFn (ex )
1290
1279
}
@@ -1888,6 +1877,10 @@ type connExecutor struct {
1888
1877
// PCR reader catalog, which is done by checking for the ReplicatedPCRVersion
1889
1878
// field on the system database (which is set during tenant bootstrap).
1890
1879
isPCRReaderCatalog bool
1880
+
1881
+ // isPCRReaderCatalogInit tracks if the system database has been queried
1882
+ // once for this connect to determine if this tenant is a reader catalog.
1883
+ isPCRReaderCatalogInit sync.Once
1891
1884
}
1892
1885
1893
1886
// ctxHolder contains a connection's context and, while session tracing is
@@ -3852,10 +3845,41 @@ func (ex *connExecutor) initEvalCtx(ctx context.Context, evalCtx *extendedEvalCo
3852
3845
evalCtx .copyFromExecCfg (ex .server .cfg )
3853
3846
}
3854
3847
3848
+ // maybeInitPCRReaderCatalog leases the system database to determine if
3849
+ // we are connecting to a PCR reader catalog, if this has not been attempted
3850
+ // before.
3851
+ func (ex * connExecutor ) maybeInitPCRReaderCatalog (ctx context.Context ) {
3852
+ ex .isPCRReaderCatalogInit .Do (func () {
3853
+ // Wait up to 30 seconds attempting to acquire the lease on the system
3854
+ // database. Normally we should already have a lease on this object,
3855
+ // unless there is some availability issue.
3856
+ const initPCRReaderCatalogTimeout = 30 * time .Second
3857
+ err := timeutil .RunWithTimeout (ctx , "detect-pcr-reader-catalog" , initPCRReaderCatalogTimeout ,
3858
+ func (ctx context.Context ) error {
3859
+ if lm := ex .server .cfg .LeaseManager ; ex .executorType == executorTypeExec && lm != nil {
3860
+ desc , err := lm .Acquire (ctx , ex .server .cfg .Clock .Now (), keys .SystemDatabaseID )
3861
+ if err != nil {
3862
+ return err
3863
+ }
3864
+ defer desc .Release (ctx )
3865
+ // The system database ReplicatedPCRVersion is set during reader tenant bootstrap,
3866
+ // which guarantees that all user tenant sql connections to the reader tenant will
3867
+ // correctly set this
3868
+ ex .isPCRReaderCatalog = desc .Underlying ().(catalog.DatabaseDescriptor ).GetReplicatedPCRVersion () != 0
3869
+ }
3870
+ return nil
3871
+ })
3872
+ if err != nil {
3873
+ log .Infof (ctx , "unable to lease system database to determine if PCR reader is in use: %s" , err )
3874
+ }
3875
+ })
3876
+ }
3877
+
3855
3878
// GetPCRReaderTimestamp if the system database is setup as PCR
3856
3879
// catalog reader, then this function will return an non-zero timestamp
3857
3880
// to use for all read operations.
3858
3881
func (ex * connExecutor ) GetPCRReaderTimestamp () hlc.Timestamp {
3882
+ ex .maybeInitPCRReaderCatalog (ex .Ctx ())
3859
3883
if ex .isPCRReaderCatalog && ! ex .sessionData ().BypassPCRReaderCatalogAOST {
3860
3884
return ex .server .cfg .LeaseManager .GetSafeReplicationTS ()
3861
3885
}
0 commit comments