Skip to content

Commit

Permalink
errorutil: moved kv-specific errors out of errorutil
Browse files Browse the repository at this point in the history
Errors for missing store and node descriptors have been moved from the
errorutil package to the kvpb package, because the errorutil package is
a low-level package to aid in working with errors in general. It should
not contain facilities for creating specific errors as this muddles the
package and can lead to import cycles.

Release note: None
  • Loading branch information
mgartner committed Sep 14, 2023
1 parent 82ea947 commit 5460e1e
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 59 deletions.
1 change: 0 additions & 1 deletion pkg/ccl/kvccl/kvfollowerreadsccl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ go_test(
"//pkg/testutils/sqlutils",
"//pkg/testutils/testcluster",
"//pkg/util",
"//pkg/util/errorutil",
"//pkg/util/hlc",
"//pkg/util/leaktest",
"//pkg/util/log",
Expand Down
5 changes: 2 additions & 3 deletions pkg/ccl/kvccl/kvfollowerreadsccl/followerreads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/testutils/testcluster"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -497,15 +496,15 @@ func (s mockNodeStore) GetNodeDescriptor(id roachpb.NodeID) (*roachpb.NodeDescri
return desc, nil
}
}
return nil, errorutil.NewNodeNotFoundError(id)
return nil, kvpb.NewNodeDescNotFoundError(id)
}

func (s mockNodeStore) GetNodeDescriptorCount() int {
return len(s)
}

func (s mockNodeStore) GetStoreDescriptor(id roachpb.StoreID) (*roachpb.StoreDescriptor, error) {
return nil, errorutil.NewStoreNotFoundError(id)
return nil, kvpb.NewStoreDescNotFoundError(id)
}

// TestOracle tests the Oracle exposed by this package.
Expand Down
2 changes: 1 addition & 1 deletion pkg/gossip/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ func (g *Gossip) getNodeDescriptor(
return nodeDescriptor, nil
}

return nil, errorutil.NewNodeNotFoundError(nodeID)
return nil, kvpb.NewNodeDescNotFoundError(nodeID)
}

// getNodeIDAddress looks up the address of the node by ID. The method accepts a
Expand Down
1 change: 0 additions & 1 deletion pkg/kv/kvclient/kvcoord/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ go_test(
"//pkg/util/caller",
"//pkg/util/ctxgroup",
"//pkg/util/encoding",
"//pkg/util/errorutil",
"//pkg/util/grpcutil",
"//pkg/util/hlc",
"//pkg/util/leaktest",
Expand Down
6 changes: 3 additions & 3 deletions pkg/kv/kvclient/kvcoord/replica_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (
"testing"
"time"

"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/shuffle"
Expand All @@ -39,7 +39,7 @@ func (ns *mockNodeStore) GetNodeDescriptor(nodeID roachpb.NodeID) (*roachpb.Node
return &nd, nil
}
}
return nil, errorutil.NewNodeNotFoundError(nodeID)
return nil, kvpb.NewNodeDescNotFoundError(nodeID)
}

// GetNodeDescriptorCount is part of the NodeDescStore interface.
Expand All @@ -51,7 +51,7 @@ func (ns *mockNodeStore) GetNodeDescriptorCount() int {
func (ns *mockNodeStore) GetStoreDescriptor(
storeID roachpb.StoreID,
) (*roachpb.StoreDescriptor, error) {
return nil, errorutil.NewStoreNotFoundError(storeID)
return nil, kvpb.NewStoreDescNotFoundError(storeID)
}

func TestNewReplicaSlice(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion pkg/kv/kvclient/kvtenant/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ go_library(
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/ts/tspb",
"//pkg/util/errorutil",
"//pkg/util/grpcutil",
"//pkg/util/hlc",
"//pkg/util/log",
Expand Down
5 changes: 2 additions & 3 deletions pkg/kv/kvclient/kvtenant/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/ts/tspb"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/grpcutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -517,7 +516,7 @@ func (c *connector) GetNodeDescriptor(nodeID roachpb.NodeID) (*roachpb.NodeDescr
defer c.mu.RUnlock()
desc, ok := c.mu.nodeDescs[nodeID]
if !ok {
return nil, errorutil.NewNodeNotFoundError(nodeID)
return nil, kvpb.NewNodeDescNotFoundError(nodeID)
}
return desc, nil
}
Expand All @@ -535,7 +534,7 @@ func (c *connector) GetStoreDescriptor(storeID roachpb.StoreID) (*roachpb.StoreD
defer c.mu.RUnlock()
desc, ok := c.mu.storeDescs[storeID]
if !ok {
return nil, errorutil.NewStoreNotFoundError(storeID)
return nil, kvpb.NewStoreDescNotFoundError(storeID)
}
return desc, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/kv/kvpb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"//pkg/util/admission/admissionpb",
"//pkg/util/buildutil",
"//pkg/util/caller",
"//pkg/util/errorutil",
"//pkg/util/hlc",
"//pkg/util/humanizeutil",
"//pkg/util/log",
Expand Down
41 changes: 41 additions & 0 deletions pkg/kv/kvpb/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/enginepb"
"github.com/cockroachdb/cockroach/pkg/util/caller"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -1627,10 +1628,50 @@ func (e *MissingRecordError) SafeFormatError(p errors.Printer) (next error) {
return nil
}

// DescNotFoundError is reported when a descriptor is missing.
type DescNotFoundError struct {
storeID roachpb.StoreID
nodeID roachpb.NodeID
isStore bool
}

// NewStoreDescNotFoundError initializes a new DescNotFoundError for a missing
// store descriptor.
func NewStoreDescNotFoundError(storeID roachpb.StoreID) *DescNotFoundError {
return &DescNotFoundError{
storeID: storeID,
isStore: true,
}
}

// NewNodeDescNotFoundError initializes a new DescNotFoundError for a missing
// node descriptor.
func NewNodeDescNotFoundError(nodeID roachpb.NodeID) *DescNotFoundError {
return &DescNotFoundError{
nodeID: nodeID,
isStore: false,
}
}

func (e *DescNotFoundError) Error() string {
return redact.Sprint(e).StripMarkers()
}

func (e *DescNotFoundError) SafeFormatError(p errors.Printer) (next error) {
if e.isStore {
p.Printf("store descriptor with store ID %d was not found", e.storeID)
} else {
p.Printf("node descriptor with node ID %d was not found", e.nodeID)
}
return nil
}

func init() {
errors.RegisterLeafDecoder(errors.GetTypeKey((*MissingRecordError)(nil)), func(_ context.Context, _ string, _ []string, _ proto.Message) error {
return &MissingRecordError{}
})
errorutilpath := reflect.TypeOf(errorutil.TempSentinel{}).PkgPath()
errors.RegisterTypeMigration(errorutilpath, "*errorutil.descriptorNotFound", &DescNotFoundError{})
}

var _ errors.SafeFormatter = &MissingRecordError{}
Expand Down
1 change: 0 additions & 1 deletion pkg/kv/kvserver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,6 @@ go_test(
"//pkg/util/circuit",
"//pkg/util/ctxgroup",
"//pkg/util/encoding",
"//pkg/util/errorutil",
"//pkg/util/future",
"//pkg/util/grunning",
"//pkg/util/hlc",
Expand Down
3 changes: 1 addition & 2 deletions pkg/kv/kvserver/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import (
"github.com/cockroachdb/cockroach/pkg/testutils/gossiputil"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
Expand Down Expand Up @@ -155,7 +154,7 @@ func (m mockNodeStore) GetNodeDescriptorCount() int {
}

func (m mockNodeStore) GetStoreDescriptor(id roachpb.StoreID) (*roachpb.StoreDescriptor, error) {
return nil, errorutil.NewStoreNotFoundError(id)
return nil, kvpb.NewStoreDescNotFoundError(id)
}

type dummyFirstRangeProvider struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4530,7 +4530,7 @@ CREATE TABLE crdb_internal.ranges_no_leases (
replicaLocalityDatum := tree.DNull
nodeDesc, err := p.ExecCfg().NodeDescs.GetNodeDescriptor(replica.NodeID)
if err != nil {
if !errorutil.IsDescriptorNotFoundError(err) {
if !errors.Is(err, &kvpb.DescNotFoundError{}) {
return nil, err
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/crdb_internal_ranges_deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sort"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
Expand All @@ -24,8 +25,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/stop"
"github.com/cockroachdb/errors"
)

// crdbInternalRangesViewDEPRECATED is the pre-v23.1
Expand Down Expand Up @@ -200,7 +201,7 @@ CREATE TABLE crdb_internal.ranges_no_leases (
replicaLocalityDatum := tree.DNull
nodeDesc, err := p.ExecCfg().NodeDescs.GetNodeDescriptor(replica.NodeID)
if err != nil {
if !errorutil.IsDescriptorNotFoundError(err) {
if !errors.Is(err, &kvpb.DescNotFoundError{}) {
return nil, err
}
} else {
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/errorutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ go_library(
name = "errorutil",
srcs = [
"catch.go",
"descriptor.go",
"error.go",
"sentinel.go",
"tenant.go",
"tenant_deprecated_wrapper.go",
],
importpath = "github.com/cockroachdb/cockroach/pkg/util/errorutil",
visibility = ["//visibility:public"],
deps = [
"//pkg/roachpb",
"//pkg/settings",
"//pkg/util/errorutil/unimplemented",
"//pkg/util/log/logcrash",
Expand Down
38 changes: 0 additions & 38 deletions pkg/util/errorutil/descriptor.go

This file was deleted.

15 changes: 15 additions & 0 deletions pkg/util/errorutil/sentinel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2023 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package errorutil

// TempSentinel is a sentinel type that allows other packages to retrieve the
// path to this package with reflect and PkgPath.
type TempSentinel struct{}

0 comments on commit 5460e1e

Please sign in to comment.