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

[Placement Group] Add support for soft anti affinity #148

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 39 additions & 5 deletions pkg/cluster/compute/affinityrule/affinityrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package affinityrule

import (
"strings"
"time"

"github.com/microsoft/wmi/pkg/base/host"
Expand All @@ -19,6 +20,15 @@ type AffinityRule struct {
*fc.MSCluster_AffinityRule
}

type FailoverClusterAffinityRuleType int

const (
SameFaultDomain FailoverClusterAffinityRuleType = 1
SameNode FailoverClusterAffinityRuleType = 2
DifferentFaultDomain FailoverClusterAffinityRuleType = 3
DifferentNode FailoverClusterAffinityRuleType = 4
)

// NewAffinityRule
func NewAffinityRule(instance *wmi.WmiInstance) (*AffinityRule, error) {
wmiafRule, err := fc.NewMSCluster_AffinityRuleEx1(instance)
Expand All @@ -29,7 +39,7 @@ func NewAffinityRule(instance *wmi.WmiInstance) (*AffinityRule, error) {
}

// CreateAffinityRule
func CreateAffinityRule(whost *host.WmiHost, name string, ruleType int) (affinityRule *AffinityRule, err error) {
func CreateAffinityRule(whost *host.WmiHost, name string, ruleType int, strict bool) (affinityRule *AffinityRule, err error) {
query := "SELECT * FROM meta_class WHERE __CLASS = 'MSCluster_AffinityRule'"
classes, err := instance.GetWmiClasssesFromHostRawQuery(whost, string(constant.FailoverCluster), query)
if err != nil {
Expand All @@ -51,16 +61,30 @@ func CreateAffinityRule(whost *host.WmiHost, name string, ruleType int) (affinit
// added layer of protection, GetClusterAffinityRule can return not found immediately after creating the affinity rule
// if this happens, we will retry the get operation a few times before returning an error
maxAttempts := 5
for i := 0; i < maxAttempts; i++ {
affinityRule, err := GetAffinityRule(whost, name)
for i := 1; i <= maxAttempts; i++ {
affinityRule, err = GetAffinityRule(whost, name)
if err != nil {
if errors.IsNotFound(err) {
if errors.IsNotFound(err) && i < maxAttempts {
time.Sleep(2 * time.Second)
continue
}
return
}
break
}
defer func() {
if err != nil {
affinityRule.RemoveAffinityRule()
affinityRule.Close()
}
}()

isAntiAffinityRule := (ruleType == int(DifferentFaultDomain) || ruleType == int(DifferentNode))
if !strict && isAntiAffinityRule && isSoftAntiAffinitySupported(affinityRule) {
_, err = affinityRule.InvokeMethod("SetAffinityRule", int(ruleType), 1 /* Enabled */, 1 /* SoftAntiAffinity */)
if err != nil {
return nil, err
}
return affinityRule, nil
}

return
Expand Down Expand Up @@ -97,3 +121,13 @@ func GetAffinityRule(whost *host.WmiHost, affinityRuleName string) (caffinityRul
caffinityRule = &AffinityRule{wmiafRule}
return
}

func isSoftAntiAffinitySupported(affinityRule *AffinityRule) bool {
propeties := affinityRule.GetClass().GetPropertiesNames()
for _, property := range propeties {
if strings.EqualFold(property, "SoftAntiAffinity") {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
"path/filepath"
"strings"

"reflect"

"github.com/microsoft/wmi/pkg/base/host"
"github.com/microsoft/wmi/pkg/base/instance"
"github.com/microsoft/wmi/pkg/base/query"
fcconstant "github.com/microsoft/wmi/pkg/cluster/constant"
"github.com/microsoft/wmi/pkg/constant"
"github.com/microsoft/wmi/pkg/errors"
"reflect"

"github.com/microsoft/wmi/pkg/cluster/compute/resource"
wmi "github.com/microsoft/wmi/pkg/wmiinstance"
Expand Down Expand Up @@ -96,7 +97,9 @@ func GetClusterSharedVolumebyName(whost *host.WmiHost, name string) (cvolume *Cl
return
}
matchingPath := strings.ToLower(filepath.Clean(instanceName))
if strings.Contains(inPath, matchingPath) {
// Append "\\" to eliminate false positive due to partial match
// e.g. "C:\ClusterStorage\Volume10\image" and "C:\ClusterStorage\Volume1"
if strings.HasPrefix(inPath+"\\", matchingPath+"\\") {
tmpInstance, err1 := instance.Clone()
if err1 != nil {
err = err1
Expand Down