From 0e3f781003f3b5111bd8f0f228a4f94ec3529b29 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Thu, 15 Oct 2020 22:21:49 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=81=BF=E5=85=8D=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=AE=89=E5=85=A8=E7=BB=84=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E8=A7=84=E5=88=99=E6=B7=B7=E4=B9=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/models/syncworkers.go | 17 +- pkg/compute/regiondrivers/managedvirtual.go | 170 +++++++++++--------- 2 files changed, 106 insertions(+), 81 deletions(-) diff --git a/pkg/compute/models/syncworkers.go b/pkg/compute/models/syncworkers.go index 793432670b..32c91bb61d 100644 --- a/pkg/compute/models/syncworkers.go +++ b/pkg/compute/models/syncworkers.go @@ -26,9 +26,10 @@ import ( ) var ( - syncAccountWorker *appsrv.SWorkerManager - syncWorkers []*appsrv.SWorkerManager - syncWorkerRing *hashring.HashRing + syncSecgroupWorker *appsrv.SWorkerManager + syncAccountWorker *appsrv.SWorkerManager + syncWorkers []*appsrv.SWorkerManager + syncWorkerRing *hashring.HashRing ) func InitSyncWorkers(count int) { @@ -50,6 +51,12 @@ func InitSyncWorkers(count int) { 2048, true, ) + syncSecgroupWorker = appsrv.NewWorkerManager( + "syncSecgroupProbeWorkerManager", + 1, + 2048, + true, + ) } func RunSyncCloudproviderRegionTask(key string, syncFunc func()) { @@ -62,3 +69,7 @@ func RunSyncCloudproviderRegionTask(key string, syncFunc func()) { func RunSyncCloudAccountTask(probeFunc func()) { syncAccountWorker.Run(probeFunc, nil, nil) } + +func RunSyncSecgroupTask(syncFunc func()) { + syncAccountWorker.Run(syncFunc, nil, nil) +} diff --git a/pkg/compute/regiondrivers/managedvirtual.go b/pkg/compute/regiondrivers/managedvirtual.go index edb3a1ef73..5f335096f2 100644 --- a/pkg/compute/regiondrivers/managedvirtual.go +++ b/pkg/compute/regiondrivers/managedvirtual.go @@ -1480,95 +1480,109 @@ func (self *SManagedVirtualizationRegionDriver) RequestSyncSecurityGroup(ctx con return "", errors.Wrap(err, "SSecurityGroupCache.Register") } - iRegion, err := vpc.GetIRegion() - if err != nil { - return "", errors.Wrap(err, "vpc.GetIRegion") - } + waitChan := make(chan error) - var iSecgroup cloudprovider.ICloudSecurityGroup = nil - if len(cache.ExternalId) > 0 { - iSecgroup, err = iRegion.GetISecurityGroupById(cache.ExternalId) - if err != nil { - if errors.Cause(err) != cloudprovider.ErrNotFound { - return "", errors.Wrap(err, "iRegion.GetSecurityGroupById") - } - cache.ExternalId = "" - } - } - - if len(cache.ExternalId) == 0 { - if strings.ToLower(secgroup.Name) == "default" { //避免有些云不支持default关键字 - secgroup.Name = "DefaultGroup" - } - // 避免有的云不支持重名安全组 - randomString := func(prefix string, length int) string { - return fmt.Sprintf("%s-%s", prefix, rand.String(length)) - } - opts := &cloudprovider.SecurityGroupFilterOptions{ - Name: randomString(secgroup.Name, 1), - VpcId: vpcId, - ProjectId: remoteProjectId, - } - for i := 2; i < 30; i++ { - _, err := iRegion.GetISecurityGroupByName(opts) + models.RunSyncSecgroupTask(func() { + err := func() error { + iRegion, err := vpc.GetIRegion() if err != nil { - if errors.Cause(err) == cloudprovider.ErrNotFound { - break - } - if errors.Cause(err) != cloudprovider.ErrDuplicateId { - return "", err + return errors.Wrap(err, "vpc.GetIRegion") + } + + var iSecgroup cloudprovider.ICloudSecurityGroup = nil + if len(cache.ExternalId) > 0 { + iSecgroup, err = iRegion.GetISecurityGroupById(cache.ExternalId) + if err != nil { + if errors.Cause(err) != cloudprovider.ErrNotFound { + return errors.Wrap(err, "iRegion.GetSecurityGroupById") + } + cache.ExternalId = "" } } - opts.Name = randomString(secgroup.Name, i) - } - conf := &cloudprovider.SecurityGroupCreateInput{ - Name: opts.Name, - Desc: secgroup.Description, - VpcId: vpcId, - ProjectId: remoteProjectId, - Rules: secgroup.GetSecRules(""), - } - iSecgroup, err = iRegion.CreateISecurityGroup(conf) - if err != nil { - return "", errors.Wrap(err, "iRegion.CreateISecurityGroup") - } - } - _, err = db.Update(cache, func() error { - cache.ExternalId = iSecgroup.GetGlobalId() - cache.Name = iSecgroup.GetName() - cache.Status = api.SECGROUP_CACHE_STATUS_READY - return nil + if len(cache.ExternalId) == 0 { + if strings.ToLower(secgroup.Name) == "default" { //避免有些云不支持default关键字 + secgroup.Name = "DefaultGroup" + } + // 避免有的云不支持重名安全组 + randomString := func(prefix string, length int) string { + return fmt.Sprintf("%s-%s", prefix, rand.String(length)) + } + opts := &cloudprovider.SecurityGroupFilterOptions{ + Name: randomString(secgroup.Name, 1), + VpcId: vpcId, + ProjectId: remoteProjectId, + } + for i := 2; i < 30; i++ { + _, err := iRegion.GetISecurityGroupByName(opts) + if err != nil { + if errors.Cause(err) == cloudprovider.ErrNotFound { + break + } + if errors.Cause(err) != cloudprovider.ErrDuplicateId { + return errors.Wrapf(err, "GetISecurityGroupByName") + } + } + opts.Name = randomString(secgroup.Name, i) + } + conf := &cloudprovider.SecurityGroupCreateInput{ + Name: opts.Name, + Desc: secgroup.Description, + VpcId: vpcId, + ProjectId: remoteProjectId, + Rules: secgroup.GetSecRules(""), + } + iSecgroup, err = iRegion.CreateISecurityGroup(conf) + if err != nil { + return errors.Wrapf(err, "iRegion.CreateISecurityGroup") + } + } + + _, err = db.Update(cache, func() error { + cache.ExternalId = iSecgroup.GetGlobalId() + cache.Name = iSecgroup.GetName() + cache.Status = api.SECGROUP_CACHE_STATUS_READY + return nil + }) + + if err != nil { + return errors.Wrapf(err, "db.Update") + } + + rules, err := iSecgroup.GetRules() + if err != nil { + return errors.Wrapf(err, "iSecgroup.GetRules") + } + + maxPriority := region.GetDriver().GetSecurityGroupRuleMaxPriority() + minPriority := region.GetDriver().GetSecurityGroupRuleMinPriority() + + defaultInRule := region.GetDriver().GetDefaultSecurityGroupInRule() + defaultOutRule := region.GetDriver().GetDefaultSecurityGroupOutRule() + order := region.GetDriver().GetSecurityGroupRuleOrder() + onlyAllowRules := region.GetDriver().IsOnlySupportAllowRules() + + localRules := secrules.SecurityRuleSet(secgroup.GetSecRules("")) + + common, inAdds, outAdds, inDels, outDels := cloudprovider.CompareRules(minPriority, maxPriority, order, localRules, rules, defaultInRule, defaultOutRule, onlyAllowRules, false) + + if len(inAdds) == 0 && len(inDels) == 0 && len(outAdds) == 0 && len(outDels) == 0 { + return nil + } + return iSecgroup.SyncRules(common, inAdds, outAdds, inDels, outDels) + }() + + waitChan <- err }) + err = <-waitChan if err != nil { - return "", errors.Wrap(err, "db.Update") + return "", err } - rules, err := iSecgroup.GetRules() + cache, err = models.SecurityGroupCacheManager.Register(ctx, userCred, secgroup.Id, vpcId, region.Id, vpc.ManagerId, remoteProjectId) if err != nil { - return "", errors.Wrap(err, "iSecgroup.GetRules") - } - - maxPriority := region.GetDriver().GetSecurityGroupRuleMaxPriority() - minPriority := region.GetDriver().GetSecurityGroupRuleMinPriority() - - defaultInRule := region.GetDriver().GetDefaultSecurityGroupInRule() - defaultOutRule := region.GetDriver().GetDefaultSecurityGroupOutRule() - order := region.GetDriver().GetSecurityGroupRuleOrder() - onlyAllowRules := region.GetDriver().IsOnlySupportAllowRules() - - localRules := secrules.SecurityRuleSet(secgroup.GetSecRules("")) - - common, inAdds, outAdds, inDels, outDels := cloudprovider.CompareRules(minPriority, maxPriority, order, localRules, rules, defaultInRule, defaultOutRule, onlyAllowRules, false) - - if len(inAdds) == 0 && len(inDels) == 0 && len(outAdds) == 0 && len(outDels) == 0 { - return cache.ExternalId, nil - } - - err = iSecgroup.SyncRules(common, inAdds, outAdds, inDels, outDels) - if err != nil { - return "", errors.Wrap(err, "iSecgroup.SyncRules") + return "", errors.Wrap(err, "SSecurityGroupCache.Register") } return cache.ExternalId, nil