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 ha when replcias is less than 3 #351

Merged
merged 2 commits into from
Mar 28, 2019
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
6 changes: 6 additions & 0 deletions pkg/scheduler/predicates/ha.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func (h *ha) Filter(instanceName string, pod *apiv1.Pod, nodes []apiv1.Node) ([]
min := -1
minNodeNames := make([]string, 0)
for nodeName, podNames := range nodeMap {
// replicas less than 3 cannot achieve high availability
if replicas < 3 {
minNodeNames = append(minNodeNames, nodeName)
continue
}

podsCount := len(podNames)
if podsCount+1 >= int(replicas+1)/2 {
continue
Expand Down
106 changes: 106 additions & 0 deletions pkg/scheduler/predicates/ha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,86 @@ func TestHAFilter(t *testing.T) {
g.Expect(strings.Contains(err.Error(), "kube nodes is empty")).To(BeTrue())
},
},
{
name: "one node, one replicas, return one node",
podFn: newHAPDPod,
nodesFn: fakeOneNode,
pvcGetFn: func(ns string, pvcName string) (*corev1.PersistentVolumeClaim, error) {
return &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaim", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "pd-cluster-1-pd-0"},
Status: corev1.PersistentVolumeClaimStatus{Phase: corev1.ClaimPending},
}, nil
},
podListFn: podListFn(map[string][]int32{}),
tcGetFn: tcGetOneReplicasFn,
acquireLockFn: acquireSuccess,
expectFn: func(nodes []apiv1.Node, err error) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(nodes)).To(Equal(1))
g.Expect(getSortedNodeNames(nodes)).To(Equal([]string{"kube-node-1"}))
},
},
{
name: "two nodes, one replicas, return two nodes",
podFn: newHAPDPod,
nodesFn: fakeTwoNodes,
pvcGetFn: func(ns string, pvcName string) (*corev1.PersistentVolumeClaim, error) {
return &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaim", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "pd-cluster-1-pd-0"},
Status: corev1.PersistentVolumeClaimStatus{Phase: corev1.ClaimPending},
}, nil
},
podListFn: podListFn(map[string][]int32{}),
tcGetFn: tcGetOneReplicasFn,
acquireLockFn: acquireSuccess,
expectFn: func(nodes []apiv1.Node, err error) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(nodes)).To(Equal(2))
g.Expect(getSortedNodeNames(nodes)).To(Equal([]string{"kube-node-1", "kube-node-2"}))
},
},
{
name: "one node, two replicas, return one node",
podFn: newHAPDPod,
nodesFn: fakeOneNode,
pvcGetFn: func(ns string, pvcName string) (*corev1.PersistentVolumeClaim, error) {
return &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaim", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "pd-cluster-1-pd-0"},
Status: corev1.PersistentVolumeClaimStatus{Phase: corev1.ClaimPending},
}, nil
},
podListFn: podListFn(map[string][]int32{}),
tcGetFn: tcGetTwoReplicasFn,
acquireLockFn: acquireSuccess,
expectFn: func(nodes []apiv1.Node, err error) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(nodes)).To(Equal(1))
g.Expect(getSortedNodeNames(nodes)).To(Equal([]string{"kube-node-1"}))
},
},
{
name: "two nodes, two replicas, return two nodes",
podFn: newHAPDPod,
nodesFn: fakeTwoNodes,
pvcGetFn: func(ns string, pvcName string) (*corev1.PersistentVolumeClaim, error) {
return &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{Kind: "PersistentVolumeClaim", APIVersion: "v1"},
ObjectMeta: metav1.ObjectMeta{Name: "pd-cluster-1-pd-0"},
Status: corev1.PersistentVolumeClaimStatus{Phase: corev1.ClaimPending},
}, nil
},
podListFn: podListFn(map[string][]int32{}),
tcGetFn: tcGetTwoReplicasFn,
acquireLockFn: acquireSuccess,
expectFn: func(nodes []apiv1.Node, err error) {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(len(nodes)).To(Equal(2))
g.Expect(getSortedNodeNames(nodes)).To(Equal([]string{"kube-node-1", "kube-node-2"}))
},
},
{
name: "one node, no pod scheduled, return the node",
podFn: newHAPDPod,
Expand Down Expand Up @@ -739,6 +819,32 @@ func tcGetFn(ns string, tcName string) (*v1alpha1.TidbCluster, error) {
}, nil
}

func tcGetOneReplicasFn(ns string, tcName string) (*v1alpha1.TidbCluster, error) {
return &v1alpha1.TidbCluster{
TypeMeta: metav1.TypeMeta{Kind: "TidbCluster", APIVersion: "v1alpha1"},
ObjectMeta: metav1.ObjectMeta{
Name: tcName,
Namespace: ns,
},
Spec: v1alpha1.TidbClusterSpec{
PD: v1alpha1.PDSpec{Replicas: 1},
},
}, nil
}

func tcGetTwoReplicasFn(ns string, tcName string) (*v1alpha1.TidbCluster, error) {
return &v1alpha1.TidbCluster{
TypeMeta: metav1.TypeMeta{Kind: "TidbCluster", APIVersion: "v1alpha1"},
ObjectMeta: metav1.ObjectMeta{
Name: tcName,
Namespace: ns,
},
Spec: v1alpha1.TidbClusterSpec{
PD: v1alpha1.PDSpec{Replicas: 2},
},
}, nil
}

func getSortedNodeNames(nodes []apiv1.Node) []string {
arr := make([]string, 0)
for _, node := range nodes {
Expand Down