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

Clarify data migration function and unit tests #2845

Merged
merged 1 commit into from
Apr 9, 2020
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
2 changes: 1 addition & 1 deletion pkg/controller/elasticsearch/driver/downscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func calculatePerformableDownscale(
}
// iterate on all leaving nodes (ordered by highest ordinal first)
for _, node := range downscale.leavingNodeNames() {
migrating, err := migration.IsMigratingData(ctx.parentCtx, ctx.shardLister, node)
migrating, err := migration.NodeHasShard(ctx.parentCtx, ctx.shardLister, node)
if err != nil {
return performableDownscale, err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/controller/elasticsearch/migration/migrate_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ const (
AllocationExcludeAnnotationName = "elasticsearch.k8s.elastic.co/allocation-exclude"
)

// IsMigratingData looks only at the presence of shards on a given node
// and checks if there is at least one other copy of the shard in the cluster
// that is started and not relocating.
func IsMigratingData(ctx context.Context, shardLister esclient.ShardLister, podName string) (bool, error) {
// NodeHasShard returns true if the given ES Pod is holding at least one shard (primary or replica).
func NodeHasShard(ctx context.Context, shardLister esclient.ShardLister, podName string) (bool, error) {
shards, err := shardLister.GetShards(ctx)
if err != nil {
return false, err
Expand Down
56 changes: 11 additions & 45 deletions pkg/controller/elasticsearch/migration/migrate_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestIsMigratingData(t *testing.T) {
func TestNodeHasShard(t *testing.T) {
type args struct {
shardLister client.ShardLister
podName string
Expand All @@ -42,72 +42,38 @@ func TestIsMigratingData(t *testing.T) {
wantErr: true,
},
{
name: "Node needs to be completely evacuated",
name: "Node has one shard",
args: args{
podName: "A",
shardLister: NewFakeShardLister([]client.Shard{
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "A"},
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "B"},
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "C"},
{Index: "index-1", Shard: "0", NodeName: "A"},
{Index: "index-1", Shard: "0", NodeName: "B"},
{Index: "index-1", Shard: "0", NodeName: "C"},
}),
},
want: true,
},
{
name: "Nothing to migrate",
name: "No shard on the node",
args: args{
podName: "A",
shardLister: NewFakeShardLister([]client.Shard{
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "B"},
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "C"},
{Index: "index-1", Shard: "0", NodeName: "B"},
{Index: "index-1", Shard: "0", NodeName: "C"},
}),
},
want: false,
},
{
name: "Only copy needs migration",
args: args{
podName: "A",
shardLister: NewFakeShardLister([]client.Shard{
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "A"},
{Index: "index-1", Shard: "1", State: client.STARTED, NodeName: "B"},
{Index: "index-1", Shard: "2", State: client.STARTED, NodeName: "C"},
}),
},
want: true,
},
{
name: "Relocation is migration",
args: args{
podName: "A",
shardLister: NewFakeShardLister([]client.Shard{
{Index: "index-1", Shard: "0", State: client.RELOCATING, NodeName: "A"},
{Index: "index-1", Shard: "0", State: client.INITIALIZING, NodeName: "B"},
}),
},
want: true,
},
{
name: "Copy is initializing",
args: args{
podName: "A",
shardLister: NewFakeShardLister([]client.Shard{
{Index: "index-1", Shard: "0", State: client.STARTED, NodeName: "A"},
{Index: "index-1", Shard: "0", State: client.INITIALIZING, NodeName: "B"},
}),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IsMigratingData(context.Background(), tt.args.shardLister, tt.args.podName)
got, err := NodeHasShard(context.Background(), tt.args.shardLister, tt.args.podName)
if (err != nil) != tt.wantErr {
t.Errorf("IsMigratingData() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("NodeHasShard() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("IsMigratingData() = %v, want %v", got, tt.want)
t.Errorf("NodeHasShard() = %v, want %v", got, tt.want)
}
})
}
Expand Down