Skip to content

Commit

Permalink
refactor: refactor routing rules locking
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <manan@planetscale.com>
  • Loading branch information
GuptaManan100 committed May 23, 2024
1 parent 7c6d5e5 commit e978473
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 18 deletions.
5 changes: 2 additions & 3 deletions go/vt/topo/routing_rules_lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package topo

import (
"context"
"fmt"
)

Expand All @@ -26,12 +25,12 @@ type RoutingRulesLock struct {
*TopoLock
}

func NewRoutingRulesLock(ctx context.Context, ts *Server, name string) (*RoutingRulesLock, error) {
func NewRoutingRulesLock(ts *Server, name string) *RoutingRulesLock {
return &RoutingRulesLock{
TopoLock: &TopoLock{
Path: RoutingRulesPath,
Name: fmt.Sprintf("RoutingRules::%s", name),
ts: ts,
},
}, nil
}
}
3 changes: 1 addition & 2 deletions go/vt/topo/routing_rules_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func TestKeyspaceRoutingRulesLock(t *testing.T) {
err := ts.CreateKeyspaceRoutingRules(ctx, &vschemapb.KeyspaceRoutingRules{})
require.NoError(t, err)

lock, err := topo.NewRoutingRulesLock(ctx, ts, "ks1")
require.NoError(t, err)
lock := topo.NewRoutingRulesLock(ts, "ks1")
_, unlock, err := lock.Lock(ctx)
require.NoError(t, err)

Expand Down
6 changes: 2 additions & 4 deletions go/vt/topo/topo_lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func TestTopoLockTimeout(t *testing.T) {

err := ts.CreateKeyspaceRoutingRules(ctx, &vschemapb.KeyspaceRoutingRules{})
require.NoError(t, err)
lock, err := topo.NewRoutingRulesLock(ctx, ts, "ks1")
require.NoError(t, err)
lock := topo.NewRoutingRulesLock(ts, "ks1")

currentTopoLockTimeout := topo.LockTimeout
topo.LockTimeout = testLockTimeout
Expand Down Expand Up @@ -70,8 +69,7 @@ func TestTopoLockBasic(t *testing.T) {

err := ts.CreateKeyspaceRoutingRules(ctx, &vschemapb.KeyspaceRoutingRules{})
require.NoError(t, err)
lock, err := topo.NewRoutingRulesLock(ctx, ts, "ks1")
require.NoError(t, err)
lock := topo.NewRoutingRulesLock(ts, "ks1")

origCtx := ctx
ctx, unlock, err := lock.Lock(origCtx)
Expand Down
7 changes: 1 addition & 6 deletions go/vt/topotools/routing_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,7 @@ func saveKeyspaceRoutingRulesLocked(ctx context.Context, ts *topo.Server, rules
// then modify the keyspace routing rules in-place.
func UpdateKeyspaceRoutingRules(ctx context.Context, ts *topo.Server, reason string,
update func(ctx context.Context, rules *map[string]string) error) (err error) {
var lock *topo.RoutingRulesLock
lock, err = topo.NewRoutingRulesLock(ctx, ts, reason)
if err != nil {
return err
}
lockCtx, unlock, lockErr := lock.Lock(ctx)
lockCtx, unlock, lockErr := topo.NewRoutingRulesLock(ts, reason).Lock(ctx)
if lockErr != nil {
// If the key does not yet exist then let's create it.
if !topo.IsErrType(lockErr, topo.NoNode) {
Expand Down
4 changes: 1 addition & 3 deletions go/vt/topotools/routing_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ func TestSaveKeyspaceRoutingRulesLocked(t *testing.T) {
})

// declare and acquire lock
lock, err := topo.NewRoutingRulesLock(ctx, ts, "test")
require.NoError(t, err)
lockCtx, unlock, err := lock.Lock(ctx)
lockCtx, unlock, err := topo.NewRoutingRulesLock(ts, "test").Lock(ctx)
require.NoError(t, err)
defer unlock(&err)

Expand Down

0 comments on commit e978473

Please sign in to comment.