github.com/polarismesh/polaris@v1.17.8/admin/job/delete_unhealthy_instance.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package job 19 20 import ( 21 "time" 22 23 "github.com/mitchellh/mapstructure" 24 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 25 26 api "github.com/polarismesh/polaris/common/api/v1" 27 "github.com/polarismesh/polaris/common/utils" 28 "github.com/polarismesh/polaris/service" 29 "github.com/polarismesh/polaris/store" 30 ) 31 32 type DeleteUnHealthyInstanceJobConfig struct { 33 InstanceDeleteTimeout time.Duration `mapstructure:"instanceDeleteTimeout"` 34 } 35 36 type deleteUnHealthyInstanceJob struct { 37 cfg *DeleteUnHealthyInstanceJobConfig 38 namingServer service.DiscoverServer 39 storage store.Store 40 } 41 42 func (job *deleteUnHealthyInstanceJob) init(raw map[string]interface{}) error { 43 cfg := &DeleteUnHealthyInstanceJobConfig{ 44 InstanceDeleteTimeout: 60 * time.Minute, 45 } 46 decodeConfig := &mapstructure.DecoderConfig{ 47 DecodeHook: mapstructure.StringToTimeDurationHookFunc(), 48 Result: cfg, 49 } 50 decoder, err := mapstructure.NewDecoder(decodeConfig) 51 if err != nil { 52 log.Errorf("[Maintain][Job][DeleteUnHealthyInstance] new config decoder err: %v", err) 53 return err 54 } 55 err = decoder.Decode(raw) 56 if err != nil { 57 log.Errorf("[Maintain][Job][DeleteUnHealthyInstance] parse config err: %v", err) 58 return err 59 } 60 job.cfg = cfg 61 return nil 62 } 63 64 func (job *deleteUnHealthyInstanceJob) interval() time.Duration { 65 return job.cfg.InstanceDeleteTimeout 66 } 67 68 func (job *deleteUnHealthyInstanceJob) execute() { 69 batchSize := uint32(100) 70 var count int = 0 71 for { 72 instanceIds, err := job.storage.GetUnHealthyInstances(job.cfg.InstanceDeleteTimeout, batchSize) 73 if err != nil { 74 log.Errorf("[Maintain][Job][DeleteUnHealthyInstance] get unhealthy instances, err: %v", err) 75 break 76 } 77 if len(instanceIds) == 0 { 78 break 79 } 80 81 var req []*apiservice.Instance 82 for _, id := range instanceIds { 83 req = append(req, &apiservice.Instance{Id: utils.NewStringValue(id)}) 84 } 85 86 ctx, err := buildContext(job.storage) 87 if err != nil { 88 log.Errorf("[Maintain][Job][DeleteUnHealthyInstance] build conetxt, err: %v", err) 89 return 90 } 91 resp := job.namingServer.DeleteInstances(ctx, req) 92 if api.CalcCode(resp) == 200 { 93 log.Infof("[Maintain][Job][DeleteUnHealthyInstance] delete instance count %d, list: %v", 94 len(instanceIds), instanceIds) 95 } else { 96 log.Errorf("[Maintain][Job][DeleteUnHealthyInstance] delete instance list: %v, err: %d %s", 97 instanceIds, resp.Code.GetValue(), resp.Info.GetValue()) 98 break 99 } 100 count += len(instanceIds) 101 } 102 103 log.Infof("[Maintain][Job][DeleteUnHealthyInstance] delete unhealthy instance count %d", count) 104 105 } 106 107 func (job *deleteUnHealthyInstanceJob) clear() { 108 }