From 2902b08418159824205f32744894ea643f3d8dc2 Mon Sep 17 00:00:00 2001 From: reshke Date: Thu, 4 Apr 2024 17:51:48 +0000 Subject: [PATCH] add test and fix --- coordinator/provider/coordinator.go | 5 ++- qdb/etcdqdb.go | 3 +- test/feature/features/init_etcd.feature | 48 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test/feature/features/init_etcd.feature diff --git a/coordinator/provider/coordinator.go b/coordinator/provider/coordinator.go index 729cc744f..ae0429c40 100644 --- a/coordinator/provider/coordinator.go +++ b/coordinator/provider/coordinator.go @@ -3,7 +3,6 @@ package provider import ( "context" "crypto/tls" - "fmt" "net" "time" @@ -291,7 +290,7 @@ func (qc *qdbCoordinator) lockCoordinator(ctx context.Context, initialRouter boo } router := &topology.Router{ ID: uuid.NewString(), - Address: fmt.Sprintf("%s:%s", config.RouterConfig().Host, config.RouterConfig().GrpcApiPort), + Address: net.JoinHostPort(config.RouterConfig().Host, config.RouterConfig().GrpcApiPort), State: qdb.OPENED, } if err := qc.RegisterRouter(ctx, router); err != nil { @@ -300,7 +299,7 @@ func (qc *qdbCoordinator) lockCoordinator(ctx context.Context, initialRouter boo if err := qc.SyncRouterMetadata(ctx, router); err != nil { spqrlog.Zero.Error().Err(err).Msg("sync router metadata when locking coordinator") } - if err := qc.UpdateCoordinator(ctx, fmt.Sprintf("%s:%s", config.CoordinatorConfig().Host, config.CoordinatorConfig().GrpcApiPort)); err != nil { + if err := qc.UpdateCoordinator(ctx, net.JoinHostPort(config.CoordinatorConfig().Host, config.CoordinatorConfig().GrpcApiPort)); err != nil { return false } return true diff --git a/qdb/etcdqdb.go b/qdb/etcdqdb.go index cd45ebb57..4fa39dde9 100644 --- a/qdb/etcdqdb.go +++ b/qdb/etcdqdb.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "net" "path" "sort" "sync" @@ -512,7 +513,7 @@ func (q *EtcdQDB) TryCoordinatorLock(ctx context.Context) error { return err } - op := clientv3.OpPut(coordLockKey, config.CoordinatorConfig().Host, clientv3.WithLease(clientv3.LeaseID(leaseGrantResp.ID))) + op := clientv3.OpPut(coordLockKey, net.JoinHostPort(config.CoordinatorConfig().Host, config.CoordinatorConfig().GrpcApiPort), clientv3.WithLease(clientv3.LeaseID(leaseGrantResp.ID))) tx := q.cli.Txn(ctx).If(clientv3util.KeyMissing(coordLockKey)).Then(op) stat, err := tx.Commit() if err != nil { diff --git a/test/feature/features/init_etcd.feature b/test/feature/features/init_etcd.feature new file mode 100644 index 000000000..500164ac9 --- /dev/null +++ b/test/feature/features/init_etcd.feature @@ -0,0 +1,48 @@ +Feature: Initialize router metadata from Etcd + Background: + # + # Run routers with coordinators + # Stop all coordinators + # + Given cluster environment is + """ + ROUTER_CONFIG=/spqr/test/feature/conf/router_with_coordinator.yaml + COORDINATOR_CONFIG=/spqr/test/feature/conf/router_coordinator.yaml + COORDINATOR_CONFIG_2=/spqr/test/feature/conf/router_coordinator_2.yaml + """ + Given cluster is up and running + And host "coordinator2" is stopped + When I run SQL on host "router-admin" + """ + UNREGISTER ROUTER ALL; + REGISTER ROUTER r1 ADDRESS regress_router::7000; + REGISTER ROUTER r2 ADDRESS regress_router_2::7000; + """ + Then command return code should be "0" + And host "router" is stopped + And host "router2" is stopped + + Scenario: Router initialize its metadata from Etcd when no coodinator alive + When I run SQL on host "router-admin" + """ + CREATE DISTRIBUTION ds1 COLUMN TYPES integer; + CREATE KEY RANGE krid1 FROM 19 ROUTE TO sh1 FOR DISTRIBUTION ds1; + """ + Then command return code should be "0" + + When host "coordinator" is stopped + And host "router" is started + + When I run SQL on host "router-admin" + """ + SHOW key_ranges + """ + Then SQL result should match json_exactly + """ + [{ + "Key range ID":"krid1", + "Distribution ID":"ds1", + "Lower bound":"19", + "Shard ID":"sh1" + }] + """