Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Context returned from LockShard #15926

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 10 additions & 22 deletions go/vt/topo/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,9 @@ func (ts *Server) internalLockShard(ctx context.Context, keyspace, shard, action
// lock
l := newLock(action)
var lockDescriptor LockDescriptor
var cancel context.CancelFunc
var err error
if isBlocking {
lockDescriptor, err = l.lockShard(ctx, ts, keyspace, shard)
} else {
lockDescriptor, err = l.tryLockShard(ctx, ts, keyspace, shard)
}
ctx, cancel, lockDescriptor, err = l.internalLockShard(ctx, ts, keyspace, shard, isBlocking)
if err != nil {
return nil, nil, err
}
Expand All @@ -356,6 +353,7 @@ func (ts *Server) internalLockShard(ctx context.Context, keyspace, shard, action
return ctx, func(finalErr *error) {
i.mu.Lock()
defer i.mu.Unlock()
cancel()

if _, ok := i.info[mapKey]; !ok {
if *finalErr != nil {
Expand Down Expand Up @@ -401,23 +399,10 @@ func CheckShardLocked(ctx context.Context, keyspace, shard string) error {
return li.lockDescriptor.Check(ctx)
}

// lockShard will lock the shard in the topology server.
// UnlockShard should be called if this returns no error.
func (l *Lock) lockShard(ctx context.Context, ts *Server, keyspace, shard string) (LockDescriptor, error) {
return l.internalLockShard(ctx, ts, keyspace, shard, true)
}

// tryLockShard will lock the shard in the topology server but unlike `lockShard` it fail-fast if not able to get lock
// UnlockShard should be called if this returns no error.
func (l *Lock) tryLockShard(ctx context.Context, ts *Server, keyspace, shard string) (LockDescriptor, error) {
return l.internalLockShard(ctx, ts, keyspace, shard, false)
}

func (l *Lock) internalLockShard(ctx context.Context, ts *Server, keyspace, shard string, isBlocking bool) (LockDescriptor, error) {
func (l *Lock) internalLockShard(ctx context.Context, ts *Server, keyspace, shard string, isBlocking bool) (context.Context, context.CancelFunc, LockDescriptor, error) {
log.Infof("Locking shard %v/%v for action %v", keyspace, shard, l.Action)

ctx, cancel := context.WithTimeout(ctx, getLockTimeout())
defer cancel()

span, ctx := trace.NewSpan(ctx, "TopoServer.LockShardForAction")
span.Annotate("action", l.Action)
Expand All @@ -428,12 +413,15 @@ func (l *Lock) internalLockShard(ctx context.Context, ts *Server, keyspace, shar
shardPath := path.Join(KeyspacesPath, keyspace, ShardsPath, shard)
j, err := l.ToJSON()
if err != nil {
return nil, err
return ctx, cancel, nil, err
}
var ld LockDescriptor
if isBlocking {
return ts.globalCell.Lock(ctx, shardPath, j)
ld, err = ts.globalCell.Lock(ctx, shardPath, j)
} else {
ld, err = ts.globalCell.TryLock(ctx, shardPath, j)
}
return ts.globalCell.TryLock(ctx, shardPath, j)
return ctx, cancel, ld, err
}

// unlockShard unlocks a previously locked shard.
Expand Down
16 changes: 16 additions & 0 deletions go/vt/topo/memorytopo/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"testing"

"github.com/stretchr/testify/require"

"vitess.io/vitess/go/vt/topo"
"vitess.io/vitess/go/vt/topo/test"
)
Expand All @@ -32,3 +34,17 @@ func TestMemoryTopo(t *testing.T) {
return NewServer(ctx, test.LocalCellName)
}, []string{"checkTryLock", "checkShardWithLock"})
}

func TestLockShardContextHasDeadline(t *testing.T) {
cell := "cell-1"
ks := "ks"
shard := "-"
ts := NewServer(context.Background(), cell)
_, err := ts.GetOrCreateShard(context.Background(), ks, shard)
require.NoError(t, err)
ctx, unlock, err := ts.LockShard(context.Background(), ks, shard, "action")
require.NoError(t, err)
defer unlock(&err)
_, hasDeadline := ctx.Deadline()
require.True(t, hasDeadline)
}
Loading