forked from pingcap/tiflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tp: add info provider (pingcap#5844)
Signed-off-by: Neil Shen <overvenus@gmail.com>
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tp | ||
|
||
import ( | ||
"github.com/pingcap/tiflow/cdc/model" | ||
"github.com/pingcap/tiflow/cdc/scheduler/internal" | ||
) | ||
|
||
var _ internal.InfoProvider = (*coordinator)(nil) | ||
|
||
// GetTaskStatuses returns the task statuses. | ||
func (c *coordinator) GetTaskStatuses() (map[model.CaptureID]*model.TaskStatus, error) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
tasks := make(map[model.CaptureID]*model.TaskStatus, len(c.captureM.Captures)) | ||
for captureID, status := range c.captureM.Captures { | ||
taskStatus := &model.TaskStatus{ | ||
Tables: make(map[model.TableID]*model.TableReplicaInfo), | ||
} | ||
for _, s := range status.Tables { | ||
taskStatus.Tables[s.TableID] = &model.TableReplicaInfo{ | ||
StartTs: s.Checkpoint.CheckpointTs, | ||
} | ||
} | ||
tasks[captureID] = taskStatus | ||
} | ||
return tasks, nil | ||
} | ||
|
||
// GetTaskPositions returns the task positions. | ||
func (c *coordinator) GetTaskPositions() (map[model.CaptureID]*model.TaskPosition, error) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
p := &model.TaskPosition{} | ||
pos := make(map[model.CaptureID]*model.TaskPosition, len(c.captureM.Captures)) | ||
for captureID := range c.captureM.Captures { | ||
pos[captureID] = p | ||
} | ||
return pos, nil | ||
} | ||
|
||
// GetTotalTableCounts returns the number of tables associated | ||
// with each capture. | ||
func (c *coordinator) GetTotalTableCounts() map[model.CaptureID]int { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
tables := make(map[model.CaptureID]int, len(c.captureM.Captures)) | ||
for captureID, status := range c.captureM.Captures { | ||
tables[captureID] = len(status.Tables) | ||
} | ||
return tables | ||
} | ||
|
||
// GetPendingTableCounts returns the number of tables in a non-ready | ||
// status (Adding & Removing) associated with each capture. | ||
func (c *coordinator) GetPendingTableCounts() map[model.CaptureID]int { | ||
return make(map[model.CaptureID]int) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2022 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tp | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
|
||
"github.com/pingcap/tiflow/cdc/model" | ||
"github.com/pingcap/tiflow/cdc/scheduler/internal" | ||
"github.com/pingcap/tiflow/cdc/scheduler/internal/tp/schedulepb" | ||
"github.com/pingcap/tiflow/pkg/config" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestInfoProvider(t *testing.T) { | ||
t.Parallel() | ||
|
||
coord := newCoordinator("a", model.ChangeFeedID{}, 1, &config.SchedulerConfig{ | ||
HeartbeatTick: math.MaxInt, | ||
MaxTaskConcurrency: 1, | ||
}) | ||
coord.captureM.Captures = map[model.CaptureID]*CaptureStatus{ | ||
"a": {Tables: []schedulepb.TableStatus{{ | ||
TableID: 1, | ||
Checkpoint: schedulepb.Checkpoint{CheckpointTs: 1}, | ||
}, { | ||
TableID: 2, | ||
Checkpoint: schedulepb.Checkpoint{CheckpointTs: 1}, | ||
}}}, | ||
"b": {}, | ||
} | ||
|
||
// Smoke testing | ||
var ip internal.InfoProvider = coord | ||
tasks, err := ip.GetTaskStatuses() | ||
require.Nil(t, err) | ||
require.EqualValues(t, map[model.CaptureID]*model.TaskStatus{ | ||
"a": {Tables: map[model.TableID]*model.TableReplicaInfo{ | ||
1: {StartTs: 1}, | ||
2: {StartTs: 1}, | ||
}}, | ||
"b": {Tables: map[model.TableID]*model.TableReplicaInfo{}}, | ||
}, tasks) | ||
pos, err := ip.GetTaskPositions() | ||
require.Nil(t, err) | ||
require.Len(t, pos, 2) | ||
require.Len(t, ip.GetTotalTableCounts(), 2) | ||
require.Empty(t, ip.GetPendingTableCounts()) | ||
} |