github.com/polarismesh/polaris@v1.17.8/service/batch/batch.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 batch
    19  
    20  import (
    21  	"context"
    22  	"time"
    23  
    24  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    25  
    26  	"github.com/polarismesh/polaris/cache"
    27  	"github.com/polarismesh/polaris/store"
    28  )
    29  
    30  // Controller 批量控制器
    31  type Controller struct {
    32  	register         *InstanceCtrl
    33  	deregister       *InstanceCtrl
    34  	heartbeat        *InstanceCtrl
    35  	clientRegister   *ClientCtrl
    36  	clientDeregister *ClientCtrl
    37  }
    38  
    39  // NewBatchCtrlWithConfig 根据配置文件创建一个批量控制器
    40  func NewBatchCtrlWithConfig(storage store.Store, cacheMgn *cache.CacheManager, config *Config) (*Controller, error) {
    41  	if config == nil {
    42  		return nil, nil
    43  	}
    44  
    45  	var err error
    46  	var register *InstanceCtrl
    47  	register, err = NewBatchRegisterCtrl(storage, cacheMgn, config.Register)
    48  	if err != nil {
    49  		log.Errorf("[Batch] new batch register instance ctrl err: %s", err.Error())
    50  		return nil, err
    51  	}
    52  
    53  	var deregister *InstanceCtrl
    54  	deregister, err = NewBatchDeregisterCtrl(storage, cacheMgn, config.Deregister)
    55  	if err != nil {
    56  		log.Errorf("[Batch] new batch deregister instance ctrl err: %s", err.Error())
    57  		return nil, err
    58  	}
    59  
    60  	var heartbeat *InstanceCtrl
    61  	heartbeat, err = NewBatchHeartbeatCtrl(storage, cacheMgn, config.Heartbeat)
    62  	if err != nil {
    63  		log.Errorf("[Batch] new batch heartbeat instance ctrl err: %s", err.Error())
    64  		return nil, err
    65  	}
    66  
    67  	var clientRegister *ClientCtrl
    68  	clientRegister, err = NewBatchRegisterClientCtrl(storage, config.ClientRegister)
    69  	if err != nil {
    70  		log.Errorf("[Batch] new batch client register ctrl err: %s", err.Error())
    71  		return nil, err
    72  	}
    73  
    74  	var clientDeregister *ClientCtrl
    75  	clientDeregister, err = NewBatchDeregisterClientCtrl(storage, config.ClientDeregister)
    76  	if err != nil {
    77  		log.Errorf("[Batch] new batch client deregister ctrl err: %s", err.Error())
    78  		return nil, err
    79  	}
    80  
    81  	bc := &Controller{
    82  		register:         register,
    83  		deregister:       deregister,
    84  		heartbeat:        heartbeat,
    85  		clientRegister:   clientRegister,
    86  		clientDeregister: clientDeregister,
    87  	}
    88  	return bc, nil
    89  }
    90  
    91  // Start 开启批量控制器
    92  // 启动多个协程,接受外部create/delete请求
    93  func (bc *Controller) Start(ctx context.Context) {
    94  	if bc.CreateInstanceOpen() {
    95  		bc.register.Start(ctx)
    96  	}
    97  	if bc.DeleteInstanceOpen() {
    98  		bc.deregister.Start(ctx)
    99  	}
   100  	if bc.HeartbeatOpen() {
   101  		bc.heartbeat.Start(ctx)
   102  	}
   103  	if bc.ClientRegisterOpen() {
   104  		bc.clientRegister.Start(ctx)
   105  	}
   106  	if bc.ClientDeregisterOpen() {
   107  		bc.clientDeregister.Start(ctx)
   108  	}
   109  }
   110  
   111  // CreateInstanceOpen 创建是否开启
   112  func (bc *Controller) CreateInstanceOpen() bool {
   113  	return bc.register != nil
   114  }
   115  
   116  // DeleteInstanceOpen 删除实例是否开启
   117  func (bc *Controller) DeleteInstanceOpen() bool {
   118  	return bc.deregister != nil
   119  }
   120  
   121  // HeartbeatOpen 心跳是否开启
   122  func (bc *Controller) HeartbeatOpen() bool {
   123  	return bc.heartbeat != nil
   124  }
   125  
   126  // ClientRegisterOpen 添加客户端是否开启
   127  func (bc *Controller) ClientRegisterOpen() bool {
   128  	return bc.clientRegister != nil
   129  }
   130  
   131  // ClientDeregisterOpen 删除客户端是否开启
   132  func (bc *Controller) ClientDeregisterOpen() bool {
   133  	return bc.clientDeregister != nil
   134  }
   135  
   136  // AsyncCreateInstance 异步创建实例,返回一个future,根据future获取创建结果
   137  func (bc *Controller) AsyncCreateInstance(svcId string, instance *apiservice.Instance, needWait bool) *InstanceFuture {
   138  	future := &InstanceFuture{
   139  		serviceId: svcId,
   140  		needWait:  needWait,
   141  		request:   instance,
   142  		begin:     time.Now(),
   143  	}
   144  
   145  	if needWait {
   146  		future.result = make(chan error, 1)
   147  	}
   148  
   149  	// 发送到注册请求队列
   150  	bc.register.queue <- future
   151  	return future
   152  }
   153  
   154  // AsyncDeleteInstance 异步合并反注册
   155  func (bc *Controller) AsyncDeleteInstance(instance *apiservice.Instance, needWait bool) *InstanceFuture {
   156  	future := &InstanceFuture{
   157  		request:  instance,
   158  		result:   make(chan error, 1),
   159  		needWait: true,
   160  	}
   161  
   162  	bc.deregister.queue <- future
   163  	return future
   164  }
   165  
   166  // AsyncHeartbeat 异步心跳
   167  func (bc *Controller) AsyncHeartbeat(instance *apiservice.Instance, healthy bool,
   168  	lastBeatTime int64) *InstanceFuture {
   169  	future := &InstanceFuture{
   170  		request:              instance,
   171  		result:               make(chan error, 1),
   172  		healthy:              healthy,
   173  		needWait:             true,
   174  		lastHeartbeatTimeSec: lastBeatTime,
   175  	}
   176  
   177  	bc.heartbeat.queue <- future
   178  	return future
   179  }
   180  
   181  // AsyncRegisterClient 异步合并反注册
   182  func (bc *Controller) AsyncRegisterClient(client *apiservice.Client) *ClientFuture {
   183  	future := &ClientFuture{
   184  		request: client,
   185  		result:  make(chan error, 1),
   186  	}
   187  
   188  	bc.clientRegister.queue <- future
   189  	return future
   190  }
   191  
   192  // AsyncDeregisterClient 异步合并反注册
   193  func (bc *Controller) AsyncDeregisterClient(client *apiservice.Client) *ClientFuture {
   194  	future := &ClientFuture{
   195  		request: client,
   196  		result:  make(chan error, 1),
   197  	}
   198  
   199  	bc.clientDeregister.queue <- future
   200  	return future
   201  }