github.com/polarismesh/polaris@v1.17.8/common/model/naming.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 model
    19  
    20  import (
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/golang/protobuf/ptypes/wrappers"
    25  	apifault "github.com/polarismesh/specification/source/go/api/v1/fault_tolerance"
    26  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    27  )
    28  
    29  // Namespace 命名空间结构体
    30  type Namespace struct {
    31  	Name       string
    32  	Comment    string
    33  	Token      string
    34  	Owner      string
    35  	Valid      bool
    36  	CreateTime time.Time
    37  	ModifyTime time.Time
    38  }
    39  
    40  type ServicePort struct {
    41  	Port     uint32
    42  	Protocol string
    43  }
    44  
    45  // Service 服务数据
    46  type Service struct {
    47  	ID           string
    48  	Name         string
    49  	Namespace    string
    50  	Business     string
    51  	Ports        string
    52  	Meta         map[string]string
    53  	Comment      string
    54  	Department   string
    55  	CmdbMod1     string
    56  	CmdbMod2     string
    57  	CmdbMod3     string
    58  	Token        string
    59  	Owner        string
    60  	Revision     string
    61  	Reference    string
    62  	ReferFilter  string
    63  	PlatformID   string
    64  	Valid        bool
    65  	CreateTime   time.Time
    66  	ModifyTime   time.Time
    67  	Mtime        int64
    68  	Ctime        int64
    69  	ServicePorts []*ServicePort
    70  }
    71  
    72  // EnhancedService 服务增强数据
    73  type EnhancedService struct {
    74  	*Service
    75  	TotalInstanceCount   uint32
    76  	HealthyInstanceCount uint32
    77  }
    78  
    79  // ServiceKey 服务名
    80  type ServiceKey struct {
    81  	Namespace string
    82  	Name      string
    83  }
    84  
    85  func (s *ServiceKey) Equal(o *ServiceKey) bool {
    86  	if s == nil {
    87  		return false
    88  	}
    89  	if o == nil {
    90  		return false
    91  	}
    92  	return s.Name == o.Name && s.Namespace == o.Namespace
    93  }
    94  
    95  func (s *ServiceKey) IsExact() bool {
    96  	return s.Namespace != "" && s.Namespace != MatchAll && s.Name != "" && s.Name != MatchAll
    97  }
    98  
    99  // IsAlias 便捷函数封装
   100  func (s *Service) IsAlias() bool {
   101  	return s.Reference != ""
   102  }
   103  
   104  // ServiceAlias 服务别名结构体
   105  type ServiceAlias struct {
   106  	ID             string
   107  	Alias          string
   108  	AliasNamespace string
   109  	ServiceID      string
   110  	Service        string
   111  	Namespace      string
   112  	Owner          string
   113  	Comment        string
   114  	CreateTime     time.Time
   115  	ModifyTime     time.Time
   116  }
   117  
   118  // WeightType 服务下实例的权重类型
   119  type WeightType uint32
   120  
   121  const (
   122  	// WEIGHTDYNAMIC 动态权重
   123  	WEIGHTDYNAMIC WeightType = iota
   124  
   125  	// WEIGHTSTATIC 静态权重
   126  	WEIGHTSTATIC
   127  )
   128  
   129  // WeightString weight string map
   130  var WeightString = map[WeightType]string{
   131  	WEIGHTDYNAMIC: "dynamic",
   132  	WEIGHTSTATIC:  "static",
   133  }
   134  
   135  // WeightEnum weight enum map
   136  var WeightEnum = map[string]WeightType{
   137  	"dynamic": WEIGHTDYNAMIC,
   138  	"static":  WEIGHTSTATIC,
   139  }
   140  
   141  // LocationStore 地域信息,对应数据库字段
   142  type LocationStore struct {
   143  	IP         string
   144  	Region     string
   145  	Zone       string
   146  	Campus     string
   147  	RegionID   uint32
   148  	ZoneID     uint32
   149  	CampusID   uint32
   150  	Flag       int
   151  	ModifyTime int64
   152  }
   153  
   154  // Location cmdb信息,对应内存结构体
   155  type Location struct {
   156  	Proto    *apimodel.Location
   157  	RegionID uint32
   158  	ZoneID   uint32
   159  	CampusID uint32
   160  	Valid    bool
   161  }
   162  
   163  // LocationView cmdb信息,对应内存结构体
   164  type LocationView struct {
   165  	IP       string
   166  	Region   string
   167  	Zone     string
   168  	Campus   string
   169  	RegionID uint32
   170  	ZoneID   uint32
   171  	CampusID uint32
   172  }
   173  
   174  // Store2Location 转成内存数据结构
   175  func Store2Location(s *LocationStore) *Location {
   176  	return &Location{
   177  		Proto: &apimodel.Location{
   178  			Region: &wrappers.StringValue{Value: s.Region},
   179  			Zone:   &wrappers.StringValue{Value: s.Zone},
   180  			Campus: &wrappers.StringValue{Value: s.Campus},
   181  		},
   182  		RegionID: s.RegionID,
   183  		ZoneID:   s.ZoneID,
   184  		CampusID: s.CampusID,
   185  		Valid:    flag2valid(s.Flag),
   186  	}
   187  }
   188  
   189  // CircuitBreaker 熔断规则
   190  type CircuitBreaker struct {
   191  	ID         string
   192  	Version    string
   193  	Name       string
   194  	Namespace  string
   195  	Business   string
   196  	Department string
   197  	Comment    string
   198  	Inbounds   string
   199  	Outbounds  string
   200  	Token      string
   201  	Owner      string
   202  	Revision   string
   203  	Valid      bool
   204  	CreateTime time.Time
   205  	ModifyTime time.Time
   206  }
   207  
   208  // ServiceWithCircuitBreaker 与服务关系绑定的熔断规则
   209  type ServiceWithCircuitBreaker struct {
   210  	ServiceID      string
   211  	CircuitBreaker *CircuitBreaker
   212  	Valid          bool
   213  	CreateTime     time.Time
   214  	ModifyTime     time.Time
   215  }
   216  
   217  // ServiceWithCircuitBreakerRules 与服务关系绑定的熔断规则
   218  type ServiceWithCircuitBreakerRules struct {
   219  	mutex               sync.RWMutex
   220  	Service             ServiceKey
   221  	circuitBreakerRules map[string]*CircuitBreakerRule
   222  	Revision            string
   223  }
   224  
   225  func NewServiceWithCircuitBreakerRules(svcKey ServiceKey) *ServiceWithCircuitBreakerRules {
   226  	return &ServiceWithCircuitBreakerRules{
   227  		Service:             svcKey,
   228  		circuitBreakerRules: make(map[string]*CircuitBreakerRule),
   229  	}
   230  }
   231  
   232  func (s *ServiceWithCircuitBreakerRules) AddCircuitBreakerRule(rule *CircuitBreakerRule) {
   233  	s.mutex.Lock()
   234  	defer s.mutex.Unlock()
   235  	s.circuitBreakerRules[rule.ID] = rule
   236  }
   237  
   238  func (s *ServiceWithCircuitBreakerRules) DelCircuitBreakerRule(id string) {
   239  	s.mutex.Lock()
   240  	defer s.mutex.Unlock()
   241  	delete(s.circuitBreakerRules, id)
   242  }
   243  
   244  func (s *ServiceWithCircuitBreakerRules) IterateCircuitBreakerRules(callback func(*CircuitBreakerRule)) {
   245  	s.mutex.RLock()
   246  	defer s.mutex.RUnlock()
   247  	for _, rule := range s.circuitBreakerRules {
   248  		callback(rule)
   249  	}
   250  }
   251  
   252  func (s *ServiceWithCircuitBreakerRules) CountCircuitBreakerRules() int {
   253  	s.mutex.RLock()
   254  	defer s.mutex.RUnlock()
   255  	return len(s.circuitBreakerRules)
   256  }
   257  
   258  func (s *ServiceWithCircuitBreakerRules) Clear() {
   259  	s.mutex.Lock()
   260  	defer s.mutex.Unlock()
   261  	s.circuitBreakerRules = make(map[string]*CircuitBreakerRule)
   262  	s.Revision = ""
   263  }
   264  
   265  // ServiceWithFaultDetectRules 与服务关系绑定的探测规则
   266  type ServiceWithFaultDetectRules struct {
   267  	mutex            sync.RWMutex
   268  	Service          ServiceKey
   269  	faultDetectRules map[string]*FaultDetectRule
   270  	Revision         string
   271  }
   272  
   273  func NewServiceWithFaultDetectRules(svcKey ServiceKey) *ServiceWithFaultDetectRules {
   274  	return &ServiceWithFaultDetectRules{
   275  		Service:          svcKey,
   276  		faultDetectRules: make(map[string]*FaultDetectRule),
   277  	}
   278  }
   279  
   280  func (s *ServiceWithFaultDetectRules) AddFaultDetectRule(rule *FaultDetectRule) {
   281  	s.mutex.Lock()
   282  	defer s.mutex.Unlock()
   283  	s.faultDetectRules[rule.ID] = rule
   284  }
   285  
   286  func (s *ServiceWithFaultDetectRules) DelFaultDetectRule(id string) {
   287  	s.mutex.Lock()
   288  	defer s.mutex.Unlock()
   289  	delete(s.faultDetectRules, id)
   290  }
   291  
   292  func (s *ServiceWithFaultDetectRules) IterateFaultDetectRules(callback func(*FaultDetectRule)) {
   293  	s.mutex.RLock()
   294  	defer s.mutex.RUnlock()
   295  	for _, rule := range s.faultDetectRules {
   296  		callback(rule)
   297  	}
   298  }
   299  
   300  func (s *ServiceWithFaultDetectRules) CountFaultDetectRules() int {
   301  	s.mutex.RLock()
   302  	defer s.mutex.RUnlock()
   303  	return len(s.faultDetectRules)
   304  }
   305  
   306  func (s *ServiceWithFaultDetectRules) Clear() {
   307  	s.mutex.Lock()
   308  	defer s.mutex.Unlock()
   309  	s.faultDetectRules = make(map[string]*FaultDetectRule)
   310  	s.Revision = ""
   311  }
   312  
   313  // CircuitBreakerRelation 熔断规则绑定关系
   314  type CircuitBreakerRelation struct {
   315  	ServiceID   string
   316  	RuleID      string
   317  	RuleVersion string
   318  	Valid       bool
   319  	CreateTime  time.Time
   320  	ModifyTime  time.Time
   321  }
   322  
   323  // CircuitBreakerDetail 返回给控制台的熔断规则及服务数据
   324  type CircuitBreakerDetail struct {
   325  	Total               uint32
   326  	CircuitBreakerInfos []*CircuitBreakerInfo
   327  }
   328  
   329  // CircuitBreakerInfo 熔断规则及绑定服务
   330  type CircuitBreakerInfo struct {
   331  	CircuitBreaker *CircuitBreaker
   332  	Services       []*Service
   333  }
   334  
   335  // Int2bool 整数转换为bool值
   336  func Int2bool(entry int) bool {
   337  	return entry != 0
   338  }
   339  
   340  // StatusBoolToInt 状态bool转int
   341  func StatusBoolToInt(value bool) int {
   342  	if value {
   343  		return 1
   344  	}
   345  	return 0
   346  }
   347  
   348  // store的flag转换为valid
   349  // flag==1为无效,其他情况为有效
   350  func flag2valid(flag int) bool {
   351  	return flag != 1
   352  }
   353  
   354  // InstanceCount Service instance statistics
   355  type InstanceCount struct {
   356  	// IsolateInstanceCount 隔离状态的实例
   357  	IsolateInstanceCount uint32
   358  	// HealthyInstanceCount 健康实例数
   359  	HealthyInstanceCount uint32
   360  	// TotalInstanceCount 总实例数
   361  	TotalInstanceCount uint32
   362  }
   363  
   364  // NamespaceServiceCount Namespace service data
   365  type NamespaceServiceCount struct {
   366  	// ServiceCount 服务数量
   367  	ServiceCount uint32
   368  	// InstanceCnt 实例健康数/实例总数
   369  	InstanceCnt *InstanceCount
   370  }
   371  
   372  // CircuitBreakerRule 熔断规则
   373  type CircuitBreakerRule struct {
   374  	Proto        *apifault.CircuitBreakerRule
   375  	ID           string
   376  	Name         string
   377  	Namespace    string
   378  	Description  string
   379  	Level        int
   380  	SrcService   string
   381  	SrcNamespace string
   382  	DstService   string
   383  	DstNamespace string
   384  	DstMethod    string
   385  	Rule         string
   386  	Revision     string
   387  	Enable       bool
   388  	Valid        bool
   389  	CreateTime   time.Time
   390  	ModifyTime   time.Time
   391  	EnableTime   time.Time
   392  }
   393  
   394  func (c *CircuitBreakerRule) IsServiceChange(other *CircuitBreakerRule) bool {
   395  	srcSvcEqual := c.SrcService == other.SrcService && c.SrcNamespace == other.SrcNamespace
   396  	dstSvcEqual := c.DstService == other.DstService && c.DstNamespace == other.DstNamespace
   397  	return !srcSvcEqual || !dstSvcEqual
   398  }
   399  
   400  // FaultDetectRule 故障探测规则
   401  type FaultDetectRule struct {
   402  	Proto        *apifault.FaultDetectRule
   403  	ID           string
   404  	Name         string
   405  	Namespace    string
   406  	Description  string
   407  	DstService   string
   408  	DstNamespace string
   409  	DstMethod    string
   410  	Rule         string
   411  	Revision     string
   412  	Valid        bool
   413  	CreateTime   time.Time
   414  	ModifyTime   time.Time
   415  }
   416  
   417  func (c *FaultDetectRule) IsServiceChange(other *FaultDetectRule) bool {
   418  	dstSvcEqual := c.DstService == other.DstService && c.DstNamespace == other.DstNamespace
   419  	return !dstSvcEqual
   420  }