-
Notifications
You must be signed in to change notification settings - Fork 726
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
*: add GetOperator service #1477
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
76e442b
*: update kvproto
nolouch 9fed5d0
implement proto
nolouch a6ca8e3
*: adjust kvproto
nolouch 9f63287
Merge remote-tracking branch 'upstream/master' into for-lightning
nolouch 9016472
cleanup
nolouch a18d918
add test
nolouch 13330b9
address comments
nolouch 5acefbb
Merge branch 'master' into for-lightning
nolouch 03b6d0f
Merge branch 'master' into for-lightning
nolouch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"github.com/pingcap/kvproto/pkg/metapb" | ||
"github.com/pingcap/kvproto/pkg/pdpb" | ||
log "github.com/pingcap/log" | ||
"github.com/pingcap/pd/server/cache" | ||
"github.com/pingcap/pd/server/core" | ||
"go.uber.org/zap" | ||
) | ||
|
@@ -41,6 +42,7 @@ type OperatorController struct { | |
hbStreams HeartbeatStreams | ||
histories *list.List | ||
counts map[OperatorKind]uint64 | ||
opRecords *OperatorRecords | ||
} | ||
|
||
// NewOperatorController creates a OperatorController. | ||
|
@@ -51,6 +53,7 @@ func NewOperatorController(cluster Cluster, hbStreams HeartbeatStreams) *Operato | |
hbStreams: hbStreams, | ||
histories: list.New(), | ||
counts: make(map[OperatorKind]uint64), | ||
opRecords: NewOperatorRecords(), | ||
} | ||
} | ||
|
||
|
@@ -69,10 +72,12 @@ func (oc *OperatorController) Dispatch(region *core.RegionInfo) { | |
operatorCounter.WithLabelValues(op.Desc(), "finish").Inc() | ||
operatorDuration.WithLabelValues(op.Desc()).Observe(op.ElapsedTime().Seconds()) | ||
oc.pushHistory(op) | ||
oc.opRecords.Put(op, pdpb.OperatorStatus_SUCCESS) | ||
oc.RemoveOperator(op) | ||
} else if timeout { | ||
log.Info("operator timeout", zap.Uint64("region-id", region.GetID()), zap.Reflect("operator", op)) | ||
oc.RemoveOperator(op) | ||
oc.opRecords.Put(op, pdpb.OperatorStatus_TIMEOUT) | ||
} | ||
} | ||
} | ||
|
@@ -85,6 +90,7 @@ func (oc *OperatorController) AddOperator(ops ...*Operator) bool { | |
for _, op := range ops { | ||
if !oc.checkAddOperator(op) { | ||
operatorCounter.WithLabelValues(op.Desc(), "canceled").Inc() | ||
oc.opRecords.Put(op, pdpb.OperatorStatus_CANCEL) | ||
return false | ||
} | ||
} | ||
|
@@ -131,6 +137,7 @@ func (oc *OperatorController) addOperatorLocked(op *Operator) bool { | |
if old, ok := oc.operators[regionID]; ok { | ||
log.Info("replace old operator", zap.Uint64("region-id", regionID), zap.Reflect("operator", old)) | ||
operatorCounter.WithLabelValues(old.Desc(), "replaced").Inc() | ||
oc.opRecords.Put(old, pdpb.OperatorStatus_REPLACE) | ||
oc.removeOperatorLocked(old) | ||
} | ||
|
||
|
@@ -154,6 +161,19 @@ func (oc *OperatorController) RemoveOperator(op *Operator) { | |
oc.removeOperatorLocked(op) | ||
} | ||
|
||
// GetOperatorStatus gets the operator and its status with the specify id. | ||
func (oc *OperatorController) GetOperatorStatus(id uint64) *OperatorWithStatus { | ||
oc.Lock() | ||
defer oc.Unlock() | ||
if op, ok := oc.operators[id]; ok { | ||
return &OperatorWithStatus{ | ||
Op: op, | ||
Status: pdpb.OperatorStatus_RUNNING, | ||
} | ||
} | ||
return oc.opRecords.Get(id) | ||
} | ||
|
||
func (oc *OperatorController) removeOperatorLocked(op *Operator) { | ||
regionID := op.RegionID() | ||
delete(oc.operators, regionID) | ||
|
@@ -401,9 +421,46 @@ func (s StoreInfluence) ResourceSize(kind core.ResourceKind) int64 { | |
} | ||
} | ||
|
||
// SetOperator is only used for test | ||
// SetOperator is only used for test. | ||
func (oc *OperatorController) SetOperator(op *Operator) { | ||
oc.Lock() | ||
defer oc.Unlock() | ||
oc.operators[op.RegionID()] = op | ||
} | ||
|
||
// OperatorWithStatus records the operator and its status. | ||
type OperatorWithStatus struct { | ||
Op *Operator | ||
Status pdpb.OperatorStatus | ||
} | ||
|
||
// OperatorRecords remains the operator and its status for a while. | ||
type OperatorRecords struct { | ||
ttl *cache.TTL | ||
} | ||
|
||
// NewOperatorRecords returns a OperatorRecords. | ||
func NewOperatorRecords() *OperatorRecords { | ||
return &OperatorRecords{ | ||
ttl: cache.NewTTL(time.Minute, 10*time.Minute), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this parameter configurable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think no need, just keep for a while. |
||
} | ||
} | ||
|
||
// Get gets the operator and its status. | ||
func (o *OperatorRecords) Get(id uint64) *OperatorWithStatus { | ||
v, exist := o.ttl.Get(id) | ||
if !exist { | ||
return nil | ||
} | ||
return v.(*OperatorWithStatus) | ||
} | ||
|
||
// Put puts the operator and its status. | ||
func (o *OperatorRecords) Put(op *Operator, status pdpb.OperatorStatus) { | ||
id := op.regionID | ||
record := &OperatorWithStatus{ | ||
Op: op, | ||
Status: status, | ||
} | ||
o.ttl.Put(id, record) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be better to keep the first letter consistent with other panic messages.