Skip to content

Commit

Permalink
Improve lock action string (#13355)
Browse files Browse the repository at this point in the history
* feat: move storing the lock action outside

Signed-off-by: Manan Gupta <[email protected]>

* feat: improve shard lock action

Signed-off-by: Manan Gupta <[email protected]>

---------

Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 authored Jun 22, 2023
1 parent c3f346a commit f0d7289
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go/vt/vtctl/reparentutil/emergency_reparenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ func NewEmergencyReparenter(ts *topo.Server, tmc tmclient.TabletManagerClient, l
// keyspace and shard.
func (erp *EmergencyReparenter) ReparentShard(ctx context.Context, keyspace string, shard string, opts EmergencyReparentOptions) (*events.Reparent, error) {
var err error
opts.lockAction = erp.getLockAction(opts.NewPrimaryAlias)
// First step is to lock the shard for the given operation, if not already locked
if err = topo.CheckShardLocked(ctx, keyspace, shard); err != nil {
var unlock func(*error)
opts.lockAction = erp.getLockAction(opts.NewPrimaryAlias)
ctx, unlock, err = erp.ts.LockShard(ctx, keyspace, shard, opts.lockAction)
if err != nil {
return nil, err
Expand Down
13 changes: 9 additions & 4 deletions go/vt/vtorc/logic/tablet_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package logic
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -252,14 +253,18 @@ func refreshTablets(tablets map[string]*topo.TabletInfo, query string, args []an
}
}

func getLockAction(analysedInstance string, code inst.AnalysisCode) string {
return fmt.Sprintf("VTOrc Recovery for %v on %v", code, analysedInstance)
}

// LockShard locks the keyspace-shard preventing others from performing conflicting actions.
func LockShard(ctx context.Context, tabletAlias string) (context.Context, func(*error), error) {
func LockShard(ctx context.Context, tabletAlias string, lockAction string) (context.Context, func(*error), error) {
if tabletAlias == "" {
return nil, nil, errors.New("Can't lock shard: instance is unspecified")
return nil, nil, errors.New("can't lock shard: instance is unspecified")
}
val := atomic.LoadInt32(&hasReceivedSIGTERM)
if val > 0 {
return nil, nil, errors.New("Can't lock shard: SIGTERM received")
return nil, nil, errors.New("can't lock shard: SIGTERM received")
}

tablet, err := inst.ReadTablet(tabletAlias)
Expand All @@ -268,7 +273,7 @@ func LockShard(ctx context.Context, tabletAlias string) (context.Context, func(*
}

atomic.AddInt32(&shardsLockCounter, 1)
ctx, unlock, err := ts.TryLockShard(ctx, tablet.Keyspace, tablet.Shard, "Orc Recovery")
ctx, unlock, err := ts.TryLockShard(ctx, tablet.Keyspace, tablet.Shard, lockAction)
if err != nil {
atomic.AddInt32(&shardsLockCounter, -1)
return nil, nil, err
Expand Down
24 changes: 24 additions & 0 deletions go/vt/vtorc/logic/tablet_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package logic

import (
"context"
"fmt"
"sync/atomic"
"testing"

Expand Down Expand Up @@ -307,3 +308,26 @@ func verifyTabletCount(t *testing.T, countWanted int) {
require.NoError(t, err)
require.Equal(t, countWanted, totalTablets)
}

func TestGetLockAction(t *testing.T) {
tests := []struct {
analysedInstance string
code inst.AnalysisCode
want string
}{
{
analysedInstance: "zone1-100",
code: inst.DeadPrimary,
want: "VTOrc Recovery for DeadPrimary on zone1-100",
}, {
analysedInstance: "zone1-200",
code: inst.ReplicationStopped,
want: "VTOrc Recovery for ReplicationStopped on zone1-200",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%v-%v", tt.analysedInstance, tt.code), func(t *testing.T) {
require.Equal(t, tt.want, getLockAction(tt.analysedInstance, tt.code))
})
}
}
2 changes: 1 addition & 1 deletion go/vt/vtorc/logic/topology_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ func executeCheckAndRecoverFunction(analysisEntry inst.ReplicationAnalysis) (err
}

// We lock the shard here and then refresh the tablets information
ctx, unlock, err := LockShard(context.Background(), analysisEntry.AnalyzedInstanceAlias)
ctx, unlock, err := LockShard(context.Background(), analysisEntry.AnalyzedInstanceAlias, getLockAction(analysisEntry.AnalyzedInstanceAlias, analysisEntry.Analysis))
if err != nil {
return err
}
Expand Down

0 comments on commit f0d7289

Please sign in to comment.