github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sqlbase/node_liveness.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sqlbase 12 13 import ( 14 "github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb" 15 "github.com/cockroachdb/cockroach/pkg/roachpb" 16 "github.com/cockroachdb/cockroach/pkg/util/errorutil" 17 ) 18 19 // OptionalNodeLivenessI is the interface used in OptionalNodeLiveness. 20 type OptionalNodeLivenessI interface { 21 Self() (kvserverpb.Liveness, error) 22 GetLivenesses() []kvserverpb.Liveness 23 IsLive(roachpb.NodeID) (bool, error) 24 } 25 26 // OptionalNodeLiveness optionally gives access to liveness information about 27 // the KV nodes. It is typically not available to anyone but the system tenant. 28 type OptionalNodeLiveness struct { 29 w errorutil.TenantSQLDeprecatedWrapper 30 } 31 32 // MakeOptionalNodeLiveness initializes an OptionalNodeLiveness wrapping a 33 // (possibly nil) *NodeLiveness. 34 // 35 // Use of node liveness from within the SQL layer is **deprecated**. Please do 36 // not introduce new uses of it. 37 // 38 // See TenantSQLDeprecatedWrapper for details. 39 func MakeOptionalNodeLiveness(nl OptionalNodeLivenessI) OptionalNodeLiveness { 40 return OptionalNodeLiveness{ 41 w: errorutil.MakeTenantSQLDeprecatedWrapper(nl, nl != nil), 42 } 43 } 44 45 // OptionalErr returns the NodeLiveness instance if available. Otherwise, it 46 // returns an error referring to the optionally passed in issues. 47 // 48 // Use of NodeLiveness from within the SQL layer is **deprecated**. Please do 49 // not introduce new uses of it. 50 func (nl *OptionalNodeLiveness) OptionalErr(issueNos ...int) (OptionalNodeLivenessI, error) { 51 v, err := nl.w.OptionalErr(issueNos...) 52 if err != nil { 53 return nil, err 54 } 55 return v.(OptionalNodeLivenessI), nil 56 } 57 58 var _ = (*OptionalNodeLiveness)(nil).OptionalErr // silence unused lint 59 60 // Optional returns the NodeLiveness instance and true if available. 61 // Otherwise, returns nil and false. Prefer OptionalErr where possible. 62 // 63 // Use of NodeLiveness from within the SQL layer is **deprecated**. Please do 64 // not introduce new uses of it. 65 func (nl *OptionalNodeLiveness) Optional(issueNos ...int) (OptionalNodeLivenessI, bool) { 66 v, ok := nl.w.Optional() 67 if !ok { 68 return nil, false 69 } 70 return v.(OptionalNodeLivenessI), true 71 }