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 data race and panic when exiting #396

Merged
merged 4 commits into from
Dec 23, 2021
Merged
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
1 change: 1 addition & 0 deletions internal/unionstore/memdb_norace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !race
// +build !race

package unionstore
Expand Down
8 changes: 8 additions & 0 deletions tikv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import (
"github.com/tikv/client-go/v2/util"
pd "github.com/tikv/pd/client"
"go.etcd.io/etcd/clientv3"
atomicutil "go.uber.org/atomic"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
Expand Down Expand Up @@ -126,6 +127,7 @@ type KVStore struct {
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
close atomicutil.Bool
}

// UpdateSPCache updates cached safepoint.
Expand Down Expand Up @@ -295,6 +297,7 @@ func (s *KVStore) GetSnapshot(ts uint64) *txnsnapshot.KVSnapshot {

// Close store
func (s *KVStore) Close() error {
s.close.Store(true)
s.cancel()
s.wg.Wait()

Expand Down Expand Up @@ -456,6 +459,11 @@ func (s *KVStore) Ctx() context.Context {
return s.ctx
}

// IsClose checks whether the store is closed.
func (s *KVStore) IsClose() bool {
return s.close.Load()
}

// WaitGroup returns wg
func (s *KVStore) WaitGroup() *sync.WaitGroup {
return &s.wg
Expand Down
20 changes: 20 additions & 0 deletions txnkv/transaction/2pc.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ type kvstore interface {
// TxnLatches returns txnLatches.
TxnLatches() *latch.LatchesScheduler
GetClusterID() uint64
// IsClose checks whether the store is closed.
IsClose() bool
}

// twoPhaseCommitter executes a two-phase commit protocol.
Expand Down Expand Up @@ -781,6 +783,12 @@ func (c *twoPhaseCommitter) doActionOnGroupMutations(bo *retry.Backoffer, action
// Already spawned a goroutine for async commit transaction.
if actionIsCommit && !actionCommit.retry && !c.isAsyncCommit() {
secondaryBo := retry.NewBackofferWithVars(c.store.Ctx(), CommitSecondaryMaxBackoff, c.txn.vars)
if c.store.IsClose() {
logutil.Logger(bo.GetCtx()).Warn("the store is closed",
zap.Uint64("startTS", c.startTS), zap.Uint64("commitTS", c.commitTS),
zap.Uint64("sessionID", c.sessionID))
return nil
}
c.store.WaitGroup().Add(1)
go func() {
defer c.store.WaitGroup().Done()
Expand Down Expand Up @@ -1106,6 +1114,12 @@ const (
)

func (c *twoPhaseCommitter) cleanup(ctx context.Context) {
if c.store.IsClose() {
logutil.Logger(ctx).Warn("twoPhaseCommitter fail to cleanup because the store exited",
zap.Uint64("txnStartTS", c.startTS), zap.Bool("isPessimistic", c.isPessimistic),
zap.Bool("isOnePC", c.isOnePC()))
return
}
c.cleanWg.Add(1)
c.store.WaitGroup().Add(1)
go func() {
Expand Down Expand Up @@ -1407,6 +1421,12 @@ func (c *twoPhaseCommitter) execute(ctx context.Context) (err error) {
logutil.Logger(ctx).Debug("2PC will use async commit protocol to commit this txn",
zap.Uint64("startTS", c.startTS), zap.Uint64("commitTS", c.commitTS),
zap.Uint64("sessionID", c.sessionID))
if c.store.IsClose() {
logutil.Logger(ctx).Warn("2PC will use async commit protocol to commit this txn but the store is closed",
zap.Uint64("startTS", c.startTS), zap.Uint64("commitTS", c.commitTS),
zap.Uint64("sessionID", c.sessionID))
return nil
}
c.store.WaitGroup().Add(1)
go func() {
defer c.store.WaitGroup().Done()
Expand Down