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 1 commit
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
2 changes: 2 additions & 0 deletions error/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ var (
ErrUnknown = errors.New("unknow")
// ErrResultUndetermined is the error when execution result is unknown.
ErrResultUndetermined = errors.New("execution result undetermined")
// ErrStoreIsExiting is the error when store is exiting.
ErrStoreIsExiting = errors.New("store is exiting")
)

// MismatchClusterID represents the message that the cluster ID of the PD client does not match the PD.
Expand Down
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
14 changes: 14 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 @@ -775,6 +777,9 @@ 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() {
return tikverr.ErrStoreIsExiting
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
}
c.store.WaitGroup().Add(1)
go func() {
defer c.store.WaitGroup().Done()
Expand Down Expand Up @@ -1100,6 +1105,12 @@ const (
)

func (c *twoPhaseCommitter) cleanup(ctx context.Context) {
if c.store.IsClose() {
logutil.Logger(ctx).Error("twoPhaseCommitter fail to cleanup because the store exited",
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1401,6 +1412,9 @@ 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() {
return tikverr.ErrStoreIsExiting
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
}
c.store.WaitGroup().Add(1)
go func() {
defer c.store.WaitGroup().Done()
Expand Down