Skip to content

Commit

Permalink
add test and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
reshke committed Apr 4, 2024
1 parent 018b288 commit 2902b08
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 2 additions & 3 deletions coordinator/provider/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"context"
"crypto/tls"
"fmt"
"net"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion qdb/etcdqdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net"
"path"
"sort"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down
48 changes: 48 additions & 0 deletions test/feature/features/init_etcd.feature
Original file line number Diff line number Diff line change
@@ -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"
}]
"""

0 comments on commit 2902b08

Please sign in to comment.