From 6882bb5cd7ebbfc46d3b0af67dd96cafb77b32a3 Mon Sep 17 00:00:00 2001 From: Ricky Stewart Date: Fri, 22 Jan 2021 17:49:20 -0600 Subject: [PATCH 1/3] build,bazel: inject nodejs support into our bazel setup Use the standard Google-supported Bazel tools to do so. This isn't plugged into anything yet, but you can verify that it works with `bazel query @npm//...`. Release note: None --- WORKSPACE | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/WORKSPACE b/WORKSPACE index 5eefbafa6ed6..1cd33b8a7808 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -1,6 +1,9 @@ # Define the top level namespace. This lets everything be addressable using # `@cockroach//...`. -workspace(name = "cockroach") +workspace( + name = "cockroach", + managed_directories = {"@npm": ["node_modules"]}, +) # Load the things that let us load other things. load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") @@ -16,6 +19,13 @@ http_archive( ], ) +# Like the above, but for nodeJS. +http_archive( + name = "build_bazel_rules_nodejs", + sha256 = "6142e9586162b179fdd570a55e50d1332e7d9c030efd853453438d607569721d", + urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.0.0/rules_nodejs-3.0.0.tar.gz"], +) + # Load gazelle. This lets us auto-generate BUILD.bazel files throughout the # repo. # @@ -82,6 +92,15 @@ go_rules_dependencies() go_register_toolchains(go_version = "1.15.6") +# Configure nodeJS. +load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install") + +yarn_install( + name = "npm", + package_json = "//pkg/ui:package.json", + yarn_lock = "//pkg/ui:yarn.lock", +) + # NB: @bazel_skylib comes from go_rules_dependencies(). load("@bazel_skylib//lib:versions.bzl", "versions") From 3f0c54213315dedaf926d3701f985b4227809453 Mon Sep 17 00:00:00 2001 From: Raphael 'kena' Poss Date: Wed, 20 Jan 2021 14:29:41 +0100 Subject: [PATCH 2/3] server: wait for SQL readiness in the `/health?ready=1` probe Release note (api change): the health API now checks that the SQL server is ready to accept clients when a readiness check is requested. --- pkg/server/admin.go | 8 ++++++-- pkg/server/admin_test.go | 16 ++++++++++++++-- pkg/server/drain.go | 1 + pkg/server/server.go | 3 +++ pkg/server/server_sql.go | 5 +++++ 5 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pkg/server/admin.go b/pkg/server/admin.go index 887f9f0035cc..b6e28d60285f 100644 --- a/pkg/server/admin.go +++ b/pkg/server/admin.go @@ -1358,13 +1358,13 @@ func (s *adminServer) Health( return resp, nil } - if err := s.checkReadinessForHealthCheck(); err != nil { + if err := s.checkReadinessForHealthCheck(ctx); err != nil { return nil, err } return resp, nil } -func (s *adminServer) checkReadinessForHealthCheck() error { +func (s *adminServer) checkReadinessForHealthCheck(ctx context.Context) error { serveMode := s.server.grpc.mode.get() switch serveMode { case modeInitializing: @@ -1397,6 +1397,10 @@ func (s *adminServer) checkReadinessForHealthCheck() error { return status.Errorf(codes.Unavailable, "node is shutting down") } + if !s.server.sqlServer.acceptingClients.Get() { + return status.Errorf(codes.Unavailable, "node is not accepting SQL clients") + } + return nil } diff --git a/pkg/server/admin_test.go b/pkg/server/admin_test.go index d9acddbcf8d4..6b0844faf74d 100644 --- a/pkg/server/admin_test.go +++ b/pkg/server/admin_test.go @@ -1355,6 +1355,7 @@ func TestHealthAPI(t *testing.T) { s, _, _ := serverutils.StartServer(t, base.TestServerArgs{}) defer s.Stopper().Stop(ctx) + ts := s.(*TestServer) // We need to retry because the node ID isn't set until after // bootstrapping. @@ -1363,15 +1364,26 @@ func TestHealthAPI(t *testing.T) { return getAdminJSONProto(s, "health", &resp) }) + // Make the SQL listener appear unavailable. Verify that health fails after that. + ts.sqlServer.acceptingClients.Set(false) + var resp serverpb.HealthResponse + err := getAdminJSONProto(s, "health?ready=1", &resp) + if err == nil { + t.Error("server appears ready even though SQL listener is not") + } + ts.sqlServer.acceptingClients.Set(true) + err = getAdminJSONProto(s, "health?ready=1", &resp) + if err != nil { + t.Errorf("server not ready after SQL listener is ready again: %v", err) + } + // Expire this node's liveness record by pausing heartbeats and advancing the // server's clock. - ts := s.(*TestServer) defer ts.nodeLiveness.PauseAllHeartbeatsForTest()() self, ok := ts.nodeLiveness.Self() assert.True(t, ok) s.Clock().Update(self.Expiration.ToTimestamp().Add(1, 0).UnsafeToClockTimestamp()) - var resp serverpb.HealthResponse testutils.SucceedsSoon(t, func() error { err := getAdminJSONProto(s, "health?ready=1", &resp) if err == nil { diff --git a/pkg/server/drain.go b/pkg/server/drain.go index a21e9c3202a6..4b4db5f3d26d 100644 --- a/pkg/server/drain.go +++ b/pkg/server/drain.go @@ -188,6 +188,7 @@ func (s *Server) drainClients(ctx context.Context, reporter func(int, redact.Saf // Mark the server as draining in a way that probes to // /health?ready=1 will notice. s.grpc.setMode(modeDraining) + s.sqlServer.acceptingClients.Set(false) // Wait for drainUnreadyWait. This will fail load balancer checks and // delay draining so that client traffic can move off this node. time.Sleep(drainWait.Get(&s.st.SV)) diff --git a/pkg/server/server.go b/pkg/server/server.go index db1cb44c21ff..316d3d0943ee 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -2073,6 +2073,9 @@ func (s *SQLServer) startServeSQL( return err } } + + s.acceptingClients.Set(true) + return nil } diff --git a/pkg/server/server_sql.go b/pkg/server/server_sql.go index 439d0e86e000..9c522ad0b0c7 100644 --- a/pkg/server/server_sql.go +++ b/pkg/server/server_sql.go @@ -70,6 +70,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/mon" "github.com/cockroachdb/cockroach/pkg/util/netutil" "github.com/cockroachdb/cockroach/pkg/util/stop" + "github.com/cockroachdb/cockroach/pkg/util/syncutil" "github.com/cockroachdb/cockroach/pkg/util/timeutil" "github.com/cockroachdb/errors" "github.com/marusama/semaphore" @@ -111,6 +112,10 @@ type SQLServer struct { // connManager is the connection manager to use to set up additional // SQL listeners in AcceptClients(). connManager netutil.Server + + // set to true when the server has started accepting client conns. + // Used by health checks. + acceptingClients syncutil.AtomicBool } // sqlServerOptionalKVArgs are the arguments supplied to newSQLServer which are From ef93819a995bf361ad33a5ede7c6df946ee4ea99 Mon Sep 17 00:00:00 2001 From: Alanmas Date: Thu, 14 Jan 2021 17:53:51 -0600 Subject: [PATCH 3/3] bazel: generate `//go:generate stringer` files within sandbox. First part of #57787 work. As `gazelle` is not taking care by itself in any changes related to `//go:generate stringer` we are creating a `genrule` to handle it. From all the `//go:generate stringer` files, there are some that are having troubles during bazel build: ``` -pkg/util/encoding/encoding.go -pkg/util/encoding/BUILD.bazel "stringer: can't handle non-integer constant type Type" -pkg/workload/schemachange/schemachange.go -pkg/workload/schemachange/BUILD.bazel "stringer: can't happen: constant is not an integer TxStatusInFailure" ``` I already added some TODO task over there to keep them in track Release note: None --- pkg/STRINGER.bzl | 18 +++++++++ pkg/base/BUILD.bazel | 8 ++++ pkg/ccl/sqlproxyccl/BUILD.bazel | 8 ++++ pkg/cli/BUILD.bazel | 8 ++++ pkg/clusterversion/BUILD.bazel | 8 ++++ pkg/kv/kvclient/kvcoord/BUILD.bazel | 8 ++++ pkg/kv/kvserver/BUILD.bazel | 8 ++++ pkg/roachpb/BUILD.bazel | 17 +++++++++ pkg/sql/BUILD.bazel | 29 +++++++++++++++ pkg/sql/catalog/catalogkv/BUILD.bazel | 8 ++++ pkg/sql/catalog/descpb/BUILD.bazel | 15 ++++++++ pkg/sql/colfetcher/BUILD.bazel | 8 ++++ pkg/sql/execinfra/BUILD.bazel | 15 ++++++++ pkg/sql/opt/optgen/lang/BUILD.bazel | 15 ++++++++ pkg/sql/pgwire/pgwirebase/BUILD.bazel | 43 ++++++++++++++++++++++ pkg/sql/privilege/BUILD.bazel | 8 ++++ pkg/sql/roleoption/BUILD.bazel | 8 ++++ pkg/sql/schemachange/BUILD.bazel | 18 +++++++++ pkg/sql/sem/tree/BUILD.bazel | 15 ++++++++ pkg/util/encoding/BUILD.bazel | 24 ++++++++++++ pkg/util/encoding/encodingtype/BUILD.bazel | 7 ++++ pkg/util/timeutil/pgdate/BUILD.bazel | 15 ++++++++ pkg/workload/schemachange/BUILD.bazel | 30 +++++++++++++++ 23 files changed, 341 insertions(+) create mode 100644 pkg/STRINGER.bzl diff --git a/pkg/STRINGER.bzl b/pkg/STRINGER.bzl new file mode 100644 index 000000000000..b0a9a7a43624 --- /dev/null +++ b/pkg/STRINGER.bzl @@ -0,0 +1,18 @@ +# Common function to create //go:generate stringer files within bazel sandbox + +def stringer(file, typ, name): + native.genrule( + name = name, + srcs = [ + file, + ], + outs = [typ.lower() + "_string.go"], + cmd = """ + env PATH=`dirname $(location @go_sdk//:bin/go)` HOME=$(GENDIR) \ + $(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ -type={} $< + """.format(typ), + tools = [ + "@go_sdk//:bin/go", + "@org_golang_x_tools//cmd/stringer", + ], + ) diff --git a/pkg/base/BUILD.bazel b/pkg/base/BUILD.bazel index c0370613c693..e67ed6964710 100644 --- a/pkg/base/BUILD.bazel +++ b/pkg/base/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "base", @@ -14,6 +15,7 @@ go_library( "testclusterreplicationmode_string.go", "testing_knobs.go", "zone.go", + ":gen-clusterreplication-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/base", visibility = ["//visibility:public"], @@ -64,3 +66,9 @@ go_test( "@com_github_stretchr_testify//require", ], ) + +stringer( + name = "gen-clusterreplication-stringer", + file = "test_server_args.go", + typ = "TestClusterReplicationMode", +) diff --git a/pkg/ccl/sqlproxyccl/BUILD.bazel b/pkg/ccl/sqlproxyccl/BUILD.bazel index b3a4bbf23959..17ac989d8ebe 100644 --- a/pkg/ccl/sqlproxyccl/BUILD.bazel +++ b/pkg/ccl/sqlproxyccl/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "sqlproxyccl", @@ -12,6 +13,7 @@ go_library( "metrics.go", "proxy.go", "server.go", + ":gen-errorcode-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/ccl/sqlproxyccl", visibility = ["//visibility:public"], @@ -58,3 +60,9 @@ go_test( "@com_github_stretchr_testify//require", ], ) + +stringer( + name = "gen-errorcode-stringer", + file = "error.go", + typ = "ErrorCode", +) diff --git a/pkg/cli/BUILD.bazel b/pkg/cli/BUILD.bazel index ffc956c29704..8d8333c1e256 100644 --- a/pkg/cli/BUILD.bazel +++ b/pkg/cli/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "cli", @@ -51,6 +52,7 @@ go_library( "tsdump.go", "userfile.go", "zip.go", + ":gen-keytype-stringer", # keep ], # keep cdeps = [ @@ -336,3 +338,9 @@ go_test( "@com_github_stretchr_testify//require", ], ) + +stringer( + name = "gen-keytype-stringer", + file = "flags_util.go", + typ = "keyType", +) diff --git a/pkg/clusterversion/BUILD.bazel b/pkg/clusterversion/BUILD.bazel index 63abb2d40c8a..510ebff47f1d 100644 --- a/pkg/clusterversion/BUILD.bazel +++ b/pkg/clusterversion/BUILD.bazel @@ -1,6 +1,7 @@ load("@rules_proto//proto:defs.bzl", "proto_library") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "clusterversion", @@ -11,6 +12,7 @@ go_library( "keyed_versions.go", "setting.go", "testutils.go", + ":gen-key-stringer", # keep ], embed = [":clusterversion_go_proto"], importpath = "github.com/cockroachdb/cockroach/pkg/clusterversion", @@ -61,3 +63,9 @@ go_proto_library( "@com_github_gogo_protobuf//gogoproto", ], ) + +stringer( + name = "gen-key-stringer", + file = "cockroach_versions.go", + typ = "Key", +) diff --git a/pkg/kv/kvclient/kvcoord/BUILD.bazel b/pkg/kv/kvclient/kvcoord/BUILD.bazel index 93b19d7104d2..d0e3085c5c87 100644 --- a/pkg/kv/kvclient/kvcoord/BUILD.bazel +++ b/pkg/kv/kvclient/kvcoord/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "kvcoord", @@ -28,6 +29,7 @@ go_library( "txn_lock_gatekeeper.go", "txn_metrics.go", "txnstate_string.go", + ":gen-txnstate-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord", visibility = ["//visibility:public"], @@ -149,3 +151,9 @@ go_test( "@org_golang_x_sync//errgroup", ], ) + +stringer( + name = "gen-txnstate-stringer", + file = "txn_coord_sender.go", + typ = "txnState", +) diff --git a/pkg/kv/kvserver/BUILD.bazel b/pkg/kv/kvserver/BUILD.bazel index 566a29f5a1b5..9c6ddc5eb752 100644 --- a/pkg/kv/kvserver/BUILD.bazel +++ b/pkg/kv/kvserver/BUILD.bazel @@ -1,6 +1,7 @@ load("@rules_proto//proto:defs.bzl", "proto_library") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "kvserver", @@ -91,6 +92,7 @@ go_library( "testing_knobs.go", "track_raft_protos.go", "ts_maintenance_queue.go", + ":gen-refreshraftreason-stringer", # keep ], embed = [":kvserver_go_proto"], importpath = "github.com/cockroachdb/cockroach/pkg/kv/kvserver", @@ -427,3 +429,9 @@ go_proto_library( "@io_etcd_go_etcd_raft_v3//raftpb", ], ) + +stringer( + name = "gen-refreshraftreason-stringer", + file = "replica_raft.go", + typ = "refreshRaftReason", +) diff --git a/pkg/roachpb/BUILD.bazel b/pkg/roachpb/BUILD.bazel index 873b7aa85eaa..ae7198ac2453 100644 --- a/pkg/roachpb/BUILD.bazel +++ b/pkg/roachpb/BUILD.bazel @@ -1,6 +1,7 @@ load("@rules_proto//proto:defs.bzl", "proto_library") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "roachpb", @@ -21,6 +22,8 @@ go_library( "span_group.go", "tenant.go", "version.go", + ":gen-errordetailtype-stringer", # keep + ":gen-method-stringer", # keep ], embed = [":roachpb_go_proto"], importpath = "github.com/cockroachdb/cockroach/pkg/roachpb", @@ -146,3 +149,17 @@ go_proto_library( "@com_github_gogo_protobuf//gogoproto", ], ) + +# Using common function for stringer to create method_string.go +stringer( + name = "gen-method-stringer", + file = "method.go", + typ = "Method", +) + +# Using common function for stringer to create errordetailtype_string.go +stringer( + name = "gen-errordetailtype-stringer", + file = "errors.go", + typ = "ErrorDetailType", +) diff --git a/pkg/sql/BUILD.bazel b/pkg/sql/BUILD.bazel index 8daa8d602419..e52ceaa5fec4 100644 --- a/pkg/sql/BUILD.bazel +++ b/pkg/sql/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "sql", @@ -218,6 +219,10 @@ go_library( "zero.go", "zigzag_join.go", "zone_config.go", + ":gen-advancecode-stringer", # keep + ":gen-nodestatus-stringer", # keep + ":gen-txnevent-stringer", # keep + ":gen-txntype-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql", visibility = ["//visibility:public"], @@ -588,3 +593,27 @@ go_test( "@org_golang_x_sync//errgroup", ], ) + +stringer( + name = "gen-txnevent-stringer", + file = "txn_state.go", + typ = "txnEvent", +) + +stringer( + name = "gen-txntype-stringer", + file = "txn_state.go", + typ = "txnType", +) + +stringer( + name = "gen-advancecode-stringer", + file = "txn_state.go", + typ = "advanceCode", +) + +stringer( + name = "gen-nodestatus-stringer", + file = "distsql_physical_planner.go", + typ = "NodeStatus", +) diff --git a/pkg/sql/catalog/catalogkv/BUILD.bazel b/pkg/sql/catalog/catalogkv/BUILD.bazel index 4fd1d99fdb93..8d5fe9fc061a 100644 --- a/pkg/sql/catalog/catalogkv/BUILD.bazel +++ b/pkg/sql/catalog/catalogkv/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "catalogkv", @@ -7,6 +8,7 @@ go_library( "descriptorkind_string.go", "namespace.go", "test_utils.go", + ":gen-descriptorkind-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkv", visibility = ["//visibility:public"], @@ -49,3 +51,9 @@ go_test( "@com_github_stretchr_testify//require", ], ) + +stringer( + name = "gen-descriptorkind-stringer", + file = "catalogkv.go", + typ = "DescriptorKind", +) diff --git a/pkg/sql/catalog/descpb/BUILD.bazel b/pkg/sql/catalog/descpb/BUILD.bazel index e4890696062a..f9d701d0f1d1 100644 --- a/pkg/sql/catalog/descpb/BUILD.bazel +++ b/pkg/sql/catalog/descpb/BUILD.bazel @@ -1,6 +1,7 @@ load("@rules_proto//proto:defs.bzl", "proto_library") load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "descpb", @@ -16,6 +17,8 @@ go_library( "privilegedescversion_string.go", "region_name.go", "structured.go", + ":gen-formatversion-stringer", # keep + ":gen-privilegedescversion-stringer", # keep ], embed = [":descpb_go_proto"], importpath = "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb", @@ -83,3 +86,15 @@ go_proto_library( "@com_github_gogo_protobuf//gogoproto", ], ) + +stringer( + name = "gen-privilegedescversion-stringer", + file = "privilege.go", + typ = "PrivilegeDescVersion", +) + +stringer( + name = "gen-formatversion-stringer", + file = "structured.go", + typ = "FormatVersion", +) diff --git a/pkg/sql/colfetcher/BUILD.bazel b/pkg/sql/colfetcher/BUILD.bazel index 11c9d68ab301..0f49f06dba29 100644 --- a/pkg/sql/colfetcher/BUILD.bazel +++ b/pkg/sql/colfetcher/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "colfetcher", @@ -6,6 +7,7 @@ go_library( "cfetcher.go", "colbatch_scan.go", "fetcherstate_string.go", + ":gen-fetcherstate-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/colfetcher", visibility = ["//visibility:public"], @@ -38,3 +40,9 @@ go_library( "@com_github_cockroachdb_errors//:errors", ], ) + +stringer( + name = "gen-fetcherstate-stringer", + file = "cfetcher.go", + typ = "fetcherState", +) diff --git a/pkg/sql/execinfra/BUILD.bazel b/pkg/sql/execinfra/BUILD.bazel index 69d49df33f68..bb9eca26ad46 100644 --- a/pkg/sql/execinfra/BUILD.bazel +++ b/pkg/sql/execinfra/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "execinfra", @@ -18,6 +19,8 @@ go_library( "server_config.go", "testutils.go", "version.go", + ":gen-consumerstatus-stringer", # keep + ":gen-procstate-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/execinfra", visibility = ["//visibility:public"], @@ -88,3 +91,15 @@ go_test( "//pkg/util/randutil", ], ) + +stringer( + name = "gen-procstate-stringer", + file = "processorsbase.go", + typ = "procState", +) + +stringer( + name = "gen-consumerstatus-stringer", + file = "base.go", + typ = "ConsumerStatus", +) diff --git a/pkg/sql/opt/optgen/lang/BUILD.bazel b/pkg/sql/opt/optgen/lang/BUILD.bazel index a43a20f01544..e219d909517e 100644 --- a/pkg/sql/opt/optgen/lang/BUILD.bazel +++ b/pkg/sql/opt/optgen/lang/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "lang", @@ -13,6 +14,8 @@ go_library( "token_string.go", ":gen-expr", # keep ":gen-operator", # keep + ":gen-operator-stringer", # keep + ":gen-token-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/opt/optgen/lang", visibility = ["//visibility:public"], @@ -78,3 +81,15 @@ genrule( """, tools = ["//pkg/sql/opt/optgen/cmd/langgen"], ) + +stringer( + name = "gen-token-stringer", + file = "scanner.go", + typ = "Token", +) + +stringer( + name = "gen-operator-stringer", + file = "operator.og.go", + typ = "Operator", +) diff --git a/pkg/sql/pgwire/pgwirebase/BUILD.bazel b/pkg/sql/pgwire/pgwirebase/BUILD.bazel index 975d1f09e09a..0cf1530d318b 100644 --- a/pkg/sql/pgwire/pgwirebase/BUILD.bazel +++ b/pkg/sql/pgwire/pgwirebase/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "pgwirebase", @@ -14,6 +15,12 @@ go_library( "servererrfieldtype_string.go", "servermessagetype_string.go", "too_big_error.go", + ":gen-clientmessagetype-stringer", # keep + ":gen-formatcode-stringer", # keep + ":gen-pgnumericsign-stringer", # keep + ":gen-preparetype-stringer", # keep + ":gen-servererrfieldtype-stringer", # keep + ":gen-servermessagetype-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgwirebase", visibility = ["//visibility:public"], @@ -39,3 +46,39 @@ go_library( "@com_github_lib_pq//oid", ], ) + +stringer( + name = "gen-pgnumericsign-stringer", + file = "encoding.go", + typ = "PGNumericSign", +) + +stringer( + name = "gen-formatcode-stringer", + file = "encoding.go", + typ = "FormatCode", +) + +stringer( + name = "gen-clientmessagetype-stringer", + file = "msg.go", + typ = "ClientMessageType", +) + +stringer( + name = "gen-servermessagetype-stringer", + file = "msg.go", + typ = "ServerMessageType", +) + +stringer( + name = "gen-servererrfieldtype-stringer", + file = "msg.go", + typ = "ServerErrFieldType", +) + +stringer( + name = "gen-preparetype-stringer", + file = "msg.go", + typ = "PrepareType", +) diff --git a/pkg/sql/privilege/BUILD.bazel b/pkg/sql/privilege/BUILD.bazel index 08082baae826..0706d6323ac1 100644 --- a/pkg/sql/privilege/BUILD.bazel +++ b/pkg/sql/privilege/BUILD.bazel @@ -1,10 +1,12 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "privilege", srcs = [ "kind_string.go", "privilege.go", + ":gen-kind-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/privilege", visibility = ["//visibility:public"], @@ -27,3 +29,9 @@ go_test( "//pkg/util/log", ], ) + +stringer( + name = "gen-kind-stringer", + file = "privilege.go", + typ = "Kind", +) diff --git a/pkg/sql/roleoption/BUILD.bazel b/pkg/sql/roleoption/BUILD.bazel index a39bc57be358..ca7fb92c2e4a 100644 --- a/pkg/sql/roleoption/BUILD.bazel +++ b/pkg/sql/roleoption/BUILD.bazel @@ -1,10 +1,12 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "roleoption", srcs = [ "option_string.go", "role_option.go", + ":gen-option-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/roleoption", visibility = ["//visibility:public"], @@ -15,3 +17,9 @@ go_library( "@com_github_cockroachdb_errors//:errors", ], ) + +stringer( + name = "gen-option-stringer", + file = "role_option.go", + typ = "Option", +) diff --git a/pkg/sql/schemachange/BUILD.bazel b/pkg/sql/schemachange/BUILD.bazel index cfb876de9bbc..b52f30ba17ec 100644 --- a/pkg/sql/schemachange/BUILD.bazel +++ b/pkg/sql/schemachange/BUILD.bazel @@ -5,6 +5,7 @@ go_library( srcs = [ "alter_column_type.go", "columnconversionkind_string.go", + ":gen-columnconversionkind-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/schemachange", visibility = ["//visibility:public"], @@ -42,3 +43,20 @@ go_test( "//pkg/util/uuid", ], ) + +# Keep this genrule and not ussing common string function due to extra flags eg: trimprefix +genrule( + name = "gen-columnconversionkind-stringer", + srcs = [ + "alter_column_type.go", + ], + outs = ["columnconversionkind_string.go"], + cmd = """ + env PATH=`dirname $(location @go_sdk//:bin/go)` HOME=$(GENDIR) \ + $(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ -type=ColumnConversionKind -trimprefix ColumnConversion $< + """, + tools = [ + "@go_sdk//:bin/go", + "@org_golang_x_tools//cmd/stringer", + ], +) diff --git a/pkg/sql/sem/tree/BUILD.bazel b/pkg/sql/sem/tree/BUILD.bazel index 0084c2cdca8f..bd23b2825ad8 100644 --- a/pkg/sql/sem/tree/BUILD.bazel +++ b/pkg/sql/sem/tree/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "tree", @@ -96,6 +97,8 @@ go_library( "window_funcs_util.go", "with.go", "zone.go", + ":gen-createtypevariety-stringer", # keep + ":gen-statementtype-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/sql/sem/tree", visibility = ["//visibility:public"], @@ -227,3 +230,15 @@ go_test( "@org_golang_x_sync//errgroup", ], ) + +stringer( + name = "gen-createtypevariety-stringer", + file = "create.go", + typ = "CreateTypeVariety", +) + +stringer( + name = "gen-statementtype-stringer", + file = "stmt.go", + typ = "StatementType", +) diff --git a/pkg/util/encoding/BUILD.bazel b/pkg/util/encoding/BUILD.bazel index 8be6010e2e5f..02b5638570fe 100644 --- a/pkg/util/encoding/BUILD.bazel +++ b/pkg/util/encoding/BUILD.bazel @@ -58,3 +58,27 @@ go_test( "@com_github_stretchr_testify//require", ], ) + +# TODO (alanmas): Solve stringer issue "stringer: can't handle non-integer constant type Type" +# Seems that we need to include pkg/util/encoding/encodingtype somehow. +# We already tried to copy the source files over so bazel source now are enconding and encodingtype +# but it is still failing due to the same error. + +# genrule( +# name = "gen-type-stringer", +# srcs = [ +# "//pkg/util/encoding/encodingtype:encoding_type.go", +# "encoding.go", +# ], +# outs = ["type_string.go"], +# cmd = """ +# cp $(location encoding.go) `dirname $(location //pkg/util/encoding/encodingtype:encoding_type.go)`/encoding.go +# env PATH=`dirname $(location @go_sdk//:bin/go)` HOME=$(GENDIR) \ +# $(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ \ +# -type=Type `dirname $(location //pkg/util/encoding/encodingtype:encoding_type.go)`/encoding.go $(location //pkg/util/encoding/encodingtype:encoding_type.go) +# """, +# tools = [ +# "@go_sdk//:bin/go", +# "@org_golang_x_tools//cmd/stringer", +# ], +# ) diff --git a/pkg/util/encoding/encodingtype/BUILD.bazel b/pkg/util/encoding/encodingtype/BUILD.bazel index 18afcaefb2f2..1567799615ad 100644 --- a/pkg/util/encoding/encodingtype/BUILD.bazel +++ b/pkg/util/encoding/encodingtype/BUILD.bazel @@ -6,3 +6,10 @@ go_library( importpath = "github.com/cockroachdb/cockroach/pkg/util/encoding/encodingtype", visibility = ["//visibility:public"], ) + +exports_files( + [ + "encoding_type.go", + ], + visibility = ["//visibility:public"], +) diff --git a/pkg/util/timeutil/pgdate/BUILD.bazel b/pkg/util/timeutil/pgdate/BUILD.bazel index 1c4828639aaa..57bfda8aff57 100644 --- a/pkg/util/timeutil/pgdate/BUILD.bazel +++ b/pkg/util/timeutil/pgdate/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "pgdate", @@ -12,6 +13,8 @@ go_library( "pgdate.go", "setters.go", "zone_cache.go", + ":gen-field-stringer", # keep + ":gen-parsemode-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate", visibility = ["//visibility:public"], @@ -40,3 +43,15 @@ go_test( "@com_github_lib_pq//:pq", ], ) + +stringer( + name = "gen-field-stringer", + file = "fields.go", + typ = "field", +) + +stringer( + name = "gen-parsemode-stringer", + file = "parsing.go", + typ = "ParseMode", +) diff --git a/pkg/workload/schemachange/BUILD.bazel b/pkg/workload/schemachange/BUILD.bazel index e0d3c6101d5d..bee8c4a62f1c 100644 --- a/pkg/workload/schemachange/BUILD.bazel +++ b/pkg/workload/schemachange/BUILD.bazel @@ -1,4 +1,5 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("//pkg:STRINGER.bzl", "stringer") go_library( name = "schemachange", @@ -11,6 +12,7 @@ go_library( "schemachange.go", "txstatus_string.go", "type_resolver.go", + ":gen-optype-stringer", # keep ], importpath = "github.com/cockroachdb/cockroach/pkg/workload/schemachange", visibility = ["//visibility:public"], @@ -34,3 +36,31 @@ go_library( "@com_github_spf13_pflag//:pflag", ], ) + +stringer( + name = "gen-optype-stringer", + file = "operation_generator.go", + typ = "opType", +) + +# TODO (alanmas): Solve stringer issue "stringer: can't happen: constant is not an integer TxStatusInFailure" +# Seems that we need to include github.com/jackc/pgx somehow. +# We already tried to copy the source files over so bazel source now are enconding and encodingtype +# but it is still failing due to the same error. + +# genrule( +# name = "gen-txstatus-stringer", +# srcs = [ +# "schemachange.go", +# ], +# outs = ["txstatus_string.go"], +# cmd = """ +# env PATH=`dirname $(location @go_sdk//:bin/go)` HOME=$(GENDIR) \ +# $(location @org_golang_x_tools//cmd/stringer:stringer) -output=$@ -type TxStatus $(location @com_github_jackc_pgx//:pgx) $(location schemachange.go) +# """, +# tools = [ +# "@go_sdk//:bin/go", +# "@org_golang_x_tools//cmd/stringer", +# "@com_github_jackc_pgx//:pgx", +# ], +# )