Skip to content

Commit

Permalink
worker, utils: show same master status
Browse files Browse the repository at this point in the history
  • Loading branch information
lance6716 committed Jul 23, 2020
1 parent 775309a commit e3226d6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions dm/worker/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package worker

import (
"context"
"github.com/pingcap/dm/pkg/utils"
"io"
"net"
"sync"
Expand Down Expand Up @@ -250,12 +251,14 @@ func (s *Server) QueryTaskOperation(ctx context.Context, req *pb.QueryTaskOperat
func (s *Server) QueryStatus(ctx context.Context, req *pb.QueryStatusRequest) (*pb.QueryStatusResponse, error) {
log.L().Info("", zap.String("request", "QueryStatus"), zap.Stringer("payload", req))

utils.EnableMasterStatusCache()
resp := &pb.QueryStatusResponse{
Result: true,
SubTaskStatus: s.worker.QueryStatus(req.Name),
RelayStatus: s.worker.relayHolder.Status(),
SourceID: s.worker.cfg.SourceID,
}
utils.DisableMasterStatusCache()

if len(resp.SubTaskStatus) == 0 {
resp.Msg = "no sub task started"
Expand Down
35 changes: 35 additions & 0 deletions pkg/utils/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strconv"
"strings"
"sync"

"github.com/pingcap/dm/pkg/gtid"
"github.com/pingcap/dm/pkg/log"
Expand Down Expand Up @@ -135,8 +136,36 @@ func GetSlaveServerID(ctx context.Context, db *sql.DB) (map[uint32]struct{}, err
return serverIDs, nil
}

var (
useMasterStatusCache bool
masterMu sync.Mutex
masterStatusCached bool
masterPosCache gmysql.Position
masterGTIDCache gtid.Set
)

func EnableMasterStatusCache() {
masterMu.Lock()
defer masterMu.Unlock()
useMasterStatusCache = true
masterStatusCached = false
}

func DisableMasterStatusCache() {
masterMu.Lock()
defer masterMu.Unlock()
useMasterStatusCache = false
}

// GetMasterStatus gets status from master
func GetMasterStatus(db *sql.DB, flavor string) (gmysql.Position, gtid.Set, error) {
masterMu.Lock()
defer masterMu.Unlock()

if useMasterStatusCache && masterStatusCached {
return masterPosCache, masterGTIDCache, nil
}

var (
binlogPos gmysql.Position
gs gtid.Set
Expand Down Expand Up @@ -199,6 +228,12 @@ func GetMasterStatus(db *sql.DB, flavor string) (gmysql.Position, gtid.Set, erro
}
}

if useMasterStatusCache {
masterStatusCached = true
masterPosCache = binlogPos
masterGTIDCache = gs
}

return binlogPos, gs, nil
}

Expand Down

0 comments on commit e3226d6

Please sign in to comment.