Skip to content

Commit

Permalink
Fix locking logic and related tests
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Nayak <rohit@planetscale.com>
  • Loading branch information
rohit-nayak-ps committed Apr 28, 2024
1 parent 5f24ca0 commit 522b014
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 16 deletions.
35 changes: 32 additions & 3 deletions go/vt/topo/keyspace_routing_rules_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package topo
import (
"context"
"fmt"
"path"

"vitess.io/vitess/go/vt/log"
)

// KeyspaceRoutingRulesLock is a wrapper over TopoLock, to serialize updates to the keyspace routing rules.
Expand All @@ -29,15 +32,41 @@ type KeyspaceRoutingRulesLock struct {
sourceKeyspace string
}

func checkAndCreateLocksFile(ctx context.Context, ts *Server) error {
topoPath := path.Join(KeyspaceRoutingRulesPath, "lock")
_, _, err := ts.GetGlobalCell().Get(ctx, topoPath)
if IsErrType(err, NoNode) {
log.Infof("Creating keyspace routing rules file %s", topoPath)
_, err = ts.globalCell.Create(ctx, topoPath, []byte("lock file for keyspace routing rules"))
if err != nil {
log.Errorf("Failed to create keyspace routing rules lock file: %v", err)
} else {
_, _, err := ts.GetGlobalCell().Get(ctx, topoPath)
if err != nil {
log.Errorf("Failed to read keyspace routing rules lock file: %v", err)
}
}
}
return err
}

func NewKeyspaceRoutingRulesLock(ctx context.Context, ts *Server, sourceKeyspace string) (*KeyspaceRoutingRulesLock, error) {
if sourceKeyspace == "" {
return nil, fmt.Errorf("sourceKeyspace is not specified")
}

// TODO: check if this can be done better: catch is that we never explicitly create keyspaces routing rules file,
// unless required.
if err := checkAndCreateLocksFile(ctx, ts); err != nil {
log.Errorf("Failed to create keyspace routing rules lock file: %v", err)
return nil, err
}

return &KeyspaceRoutingRulesLock{
TopoLock: &TopoLock{
Root: "", // global
Key: KeyspaceRoutingRulesFile,
Action: "KeyspaceRoutingRulesLock",
Root: KeyspaceRoutingRulesPath,
Key: "",
Action: "Lock",
Name: fmt.Sprintf("KeyspaceRoutingRules for %s", sourceKeyspace),
ts: ts,
},
Expand Down
4 changes: 1 addition & 3 deletions go/vt/topo/keyspace_routing_rules_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ func TestKeyspaceRoutingRulesLock(t *testing.T) {
defer cancel()
ts := memorytopo.NewServer(ctx, "zone1")
defer ts.Close()
conn := ts.GetGlobalConn()
conn := ts.GetGlobalCell()
require.NotNil(t, conn)
_, err := conn.Create(ctx, topo.KeyspaceRoutingRulesFile, []byte(""))
require.NoError(t, err)

currentTopoLockTimeout := topo.LockTimeout
topo.LockTimeout = testLockTimeout
Expand Down
18 changes: 10 additions & 8 deletions go/vt/topo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ const (

// Path for all object types.
const (
CellsPath = "cells"
CellsAliasesPath = "cells_aliases"
KeyspacesPath = "keyspaces"
ShardsPath = "shards"
TabletsPath = "tablets"
MetadataPath = "metadata"
ExternalClusterVitess = "vitess"
CellsPath = "cells"
CellsAliasesPath = "cells_aliases"
KeyspacesPath = "keyspaces"
ShardsPath = "shards"
TabletsPath = "tablets"
MetadataPath = "metadata"
ExternalClusterVitess = "vitess"
KeyspaceRoutingRulesPath = "keyspaces_routing_rules"
KeyspaceRoutingRulesLockDir = "lock"
)

// Factory is a factory interface to create Conn objects.
Expand Down Expand Up @@ -352,7 +354,7 @@ func (ts *Server) Close() {
ts.cellConns = make(map[string]cellConn)
}

func (ts *Server) GetGlobalConn() Conn {
func (ts *Server) GetGlobalCell() Conn {
return ts.globalCell
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/topo/topo_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestTopoLockTimeout(t *testing.T) {
defer cancel()
ts := memorytopo.NewServer(ctx, "zone1")
defer ts.Close()
conn := ts.GetGlobalConn()
conn := ts.GetGlobalCell()
require.NotNil(t, conn)
_, err := conn.Create(ctx, "root/key1", []byte("value"))
if err != nil {
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestTopoLockBasic(t *testing.T) {
defer cancel()
ts := memorytopo.NewServer(ctx, "zone1")
defer ts.Close()
conn := ts.GetGlobalConn()
conn := ts.GetGlobalCell()
require.NotNil(t, conn)
_, err := conn.Create(ctx, "root/key1", []byte("value"))
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions go/vt/vtctl/workflow/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package workflow

import (
"context"
"testing"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/topo/memorytopo"
)

func TestUpdateKeyspaceRoutingRule(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ts := memorytopo.NewServer(ctx, "zone1")
defer ts.Close()
routes := make(map[string]string)
routes["from"] = "to"
err := updateKeyspaceRoutingRule(ctx, ts, "ks", routes)
require.NoError(t, err)
}

0 comments on commit 522b014

Please sign in to comment.