Skip to content

Commit

Permalink
fix can not connect to remote backend when down cn
Browse files Browse the repository at this point in the history
  • Loading branch information
iamlinjunhong committed May 22, 2024
1 parent 5f5d068 commit f2b8f0e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
56 changes: 56 additions & 0 deletions pkg/lockservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,62 @@ func TestReLockSuccWithLockTableBindChanged(t *testing.T) {
)
}

func TestIssue3288(t *testing.T) {
runLockServiceTestsWithLevel(
t,
zapcore.DebugLevel,
[]string{"s1", "s2"},
time.Second*1,
func(alloc *lockTableAllocator, s []*service) {
l1 := s[0]
l2 := s[1]

ctx, cancel := context.WithTimeout(
context.Background(),
time.Second*10)
defer cancel()
option := pb.LockOptions{
Granularity: pb.Granularity_Row,
Mode: pb.LockMode_Exclusive,
Policy: pb.WaitPolicy_Wait,
}

_, err := l1.Lock(
ctx,
0,
[][]byte{{1}},
[]byte("txn1"),
option)
require.NoError(t, err)
require.NoError(t, l1.Unlock(ctx, []byte("txn1"), timestamp.Timestamp{}))

l1.Close()

// should lock failed
_, err = l2.Lock(
ctx,
0,
[][]byte{{1}},
[]byte("txn2"),
option)
require.True(t, moerr.IsMoErrCode(err, moerr.ErrBackendCannotConnect))

time.Sleep(time.Second * 3)

// should lock succ
_, err = l2.Lock(
ctx,
0,
[][]byte{{1}},
[]byte("txn2"),
option)
require.NoError(t, err)

},
nil,
)
}

func TestIssue16121(t *testing.T) {
runLockServiceTestsWithLevel(
t,
Expand Down
14 changes: 11 additions & 3 deletions pkg/sql/colexec/lockop/lock_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,15 +589,23 @@ func doLock(
return true, result.TableDefChanged, snapshotTS, nil
}

const defaultWaitTimeOnRetryLock = time.Second

func canRetryLock(txn client.TxnOperator, err error) bool {
if moerr.IsMoErrCode(err, moerr.ErrRetryForCNRollingRestart) {
time.Sleep(defaultWaitTimeOnRetryLock)
return true
}
if !moerr.IsMoErrCode(err, moerr.ErrLockTableBindChanged) ||
!moerr.IsMoErrCode(err, moerr.ErrLockTableNotFound) {
if txn.LockTableCount() > 0 {
return false
}
if txn.LockTableCount() == 0 {
if moerr.IsMoErrCode(err, moerr.ErrLockTableBindChanged) ||
moerr.IsMoErrCode(err, moerr.ErrLockTableNotFound) {
return true
}
if moerr.IsMoErrCode(err, moerr.ErrBackendClosed) ||
moerr.IsMoErrCode(err, moerr.ErrBackendCannotConnect) {
time.Sleep(defaultWaitTimeOnRetryLock)
return true
}
return false
Expand Down

0 comments on commit f2b8f0e

Please sign in to comment.