tianxiaoliang commented on a change in pull request #953: URL: https://github.com/apache/servicecomb-service-center/pull/953#discussion_r620795779
########## File path: datasource/mongo/fast_register_timer.go ########## @@ -0,0 +1,187 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package mongo + +import ( + "context" + "errors" + "fmt" + "runtime" + "time" + + "github.com/apache/servicecomb-service-center/pkg/gopool" + "github.com/apache/servicecomb-service-center/pkg/log" + "github.com/apache/servicecomb-service-center/pkg/util" +) + +const ( + loopTime = 100 * time.Millisecond + batchLen = 10000 + fuseMinCount = 3 + fuseTime = 5 * time.Second + maxRegisterFailedTime = 500 + ctxCancelTimeOut = 60 * time.Second +) + +var fastRegisterTimer *FastRegisterTimer + +type FastRegisterTimer struct { + goroutine *gopool.Pool +} + +func NewRegisterTimer() *FastRegisterTimer { + return &FastRegisterTimer{ + goroutine: gopool.New(context.Background()), + } +} + +func (rt *FastRegisterTimer) Run() { + gopool.Go(fastRegisterTimer.loopRegister) +} + +func (rt *FastRegisterTimer) Stop() { + rt.goroutine.Close(true) +} + +func (rt *FastRegisterTimer) loopRegister(ctx context.Context) { + defer rt.Stop() + + blockCh := make(chan struct{}, runtime.NumCPU()-1) + failedCount := make(chan int, 1) + failedCount <- 0 + ticker := time.NewTicker(loopTime) + + for { + select { + case <-ctx.Done(): + // server shutdown + return + case <-ticker.C: + length := len(GetFastRegisterInstanceService().InstEventCh) + if length == 0 { + continue + } + + // fuse is triggered if failed registry counts more than fuseMinCount + count := <-failedCount + if count > fuseMinCount { + time.Sleep(fuseTime) + } else { + failedCount <- count + } + + events := rt.generateEvents(length) + blockCh <- struct{}{} + go rt.BatchRegisterInstance(events, blockCh, failedCount) + + case event, ok := <-GetFastRegisterInstanceService().FailedInstCh: + // if instance batch register failed, register it single + if !ok { + log.Error("failed instance channel is error", errors.New("channel closed")) + continue + } + + if event.failedTime > maxRegisterFailedTime { + log.Error(fmt.Sprintf("instance register retry time is more than max register time:%d, "+ + "the instance params maybe wrong, drop it", maxRegisterFailedTime), + errors.New("retry register instance failed")) + continue + } + + blockCh <- struct{}{} + + go rt.RegisterInstance(event, blockCh) + } + } +} + +func (rt *FastRegisterTimer) generateEvents(length int) []*InstanceRegisterEvent { + // if channel len >= batch len, use batch len, otherwise use channel len + if length >= batchLen { + length = batchLen + } + + events := make([]*InstanceRegisterEvent, 0) + + for i := 0; i < length; i++ { + event, ok := <-GetFastRegisterInstanceService().InstEventCh + + refreshCanceledCtx(event) + + if !ok { + log.Error("instance event channel is error", errors.New("channel closed")) + continue + } + events = append(events, event) + } + return events +} + +func (rt *FastRegisterTimer) BatchRegisterInstance(events []*InstanceRegisterEvent, blockCh chan struct{}, failedCount chan int) { + defer endBlock(blockCh) + + ctx, cancel := context.WithTimeout(context.Background(), ctxCancelTimeOut) + defer cancel() + + _, err := RegisterInstanceBatch(ctx, events) Review comment: RegisterInstanceBatch 和BatchRegisterInstance名字令人不易理解,容易混淆,一眼看不出职责 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
