github.com/polarismesh/polaris@v1.17.8/store/boltdb/l5.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 boltdb
    19  
    20  import (
    21  	"fmt"
    22  	"time"
    23  
    24  	bolt "go.etcd.io/bbolt"
    25  
    26  	"github.com/polarismesh/polaris/common/model"
    27  )
    28  
    29  type l5Store struct {
    30  	handler BoltHandler
    31  }
    32  
    33  // GetL5Extend 获取扩展数据
    34  func (l *l5Store) GetL5Extend(serviceID string) (map[string]interface{}, error) {
    35  	return nil, nil
    36  }
    37  
    38  // SetL5Extend 设置meta里保存的扩展数据,并返回剩余的meta
    39  func (l *l5Store) SetL5Extend(serviceID string, meta map[string]interface{}) (map[string]interface{}, error) {
    40  	return nil, nil
    41  }
    42  
    43  // InitL5Data 初始化L5数据
    44  func (l *l5Store) InitL5Data() error {
    45  	return l.handler.Execute(true, func(tx *bolt.Tx) error {
    46  		var err error
    47  		var tblBucket *bolt.Bucket
    48  		tblBucket, err = tx.CreateBucketIfNotExists([]byte(tblNameL5))
    49  		if err != nil {
    50  			return err
    51  		}
    52  		rowBucket := tblBucket.Bucket([]byte(rowSidKey))
    53  		if rowBucket != nil {
    54  			// 数据已存在,不做处理
    55  			return nil
    56  		}
    57  		rowBucket, err = tblBucket.CreateBucket([]byte(rowSidKey))
    58  		if err != nil {
    59  			return err
    60  		}
    61  		return updateL5SidTable(rowBucket, 3000001, 1, 0)
    62  	})
    63  }
    64  
    65  const (
    66  	tblNameL5      = "l5"
    67  	rowSidKey      = "sidSequence"
    68  	colModuleId    = "module_id"
    69  	colInterfaceId = "interface_id"
    70  	colRangeNum    = "range_num"
    71  )
    72  
    73  func updateL5SidTable(rowBucket *bolt.Bucket, mid uint64, iid uint64, rnum uint64) error {
    74  	var err error
    75  	if err = rowBucket.Put([]byte(colModuleId), encodeUintBuffer(mid, typeUint32)); err != nil {
    76  		return err
    77  	}
    78  	if err = rowBucket.Put([]byte(colInterfaceId), encodeUintBuffer(iid, typeUint32)); err != nil {
    79  		return err
    80  	}
    81  	if err = rowBucket.Put([]byte(colRangeNum), encodeUintBuffer(rnum, typeUint32)); err != nil {
    82  		return err
    83  	}
    84  	return nil
    85  }
    86  
    87  // GenNextL5Sid 获取module
    88  func (l *l5Store) GenNextL5Sid(layoutID uint32) (string, error) {
    89  	var pmid *uint64
    90  	var piid *uint64
    91  	var prnum *uint64
    92  	err := l.handler.Execute(true, func(tx *bolt.Tx) error {
    93  		tblBucket := tx.Bucket([]byte(tblNameL5))
    94  		if tblBucket == nil {
    95  			return fmt.Errorf("[BlobStore] table bucket %s not exists", tblNameL5)
    96  		}
    97  		rowBucket := tblBucket.Bucket([]byte(rowSidKey))
    98  		if rowBucket == nil {
    99  			return fmt.Errorf("[BlobStore] row bucket %s not exists", rowSidKey)
   100  		}
   101  		midBytes := rowBucket.Get([]byte(colModuleId))
   102  		mid, err := decodeUintBuffer(colModuleId, midBytes, typeUint32)
   103  		if err != nil {
   104  			return err
   105  		}
   106  		iidBytes := rowBucket.Get([]byte(colInterfaceId))
   107  		iid, err := decodeUintBuffer(colInterfaceId, iidBytes, typeUint32)
   108  		if err != nil {
   109  			return err
   110  		}
   111  		rnumBytes := rowBucket.Get([]byte(colRangeNum))
   112  		rnum, err := decodeUintBuffer(colRangeNum, rnumBytes, typeUint32)
   113  		if err != nil {
   114  			return err
   115  		}
   116  		rnum++
   117  		if rnum >= 65536 {
   118  			rnum = 0
   119  			iid++
   120  		}
   121  		if iid >= 4096 {
   122  			iid = 1
   123  			mid++
   124  		}
   125  		err = updateL5SidTable(rowBucket, mid, iid, rnum)
   126  		if err != nil {
   127  			return err
   128  		}
   129  		pmid = &mid
   130  		piid = &iid
   131  		prnum = &rnum
   132  		return nil
   133  	})
   134  	if err != nil {
   135  		return "", err
   136  	}
   137  	modID := uint32(*pmid)<<6 + layoutID
   138  	cmdID := uint32(*piid)<<16 + uint32(*prnum)
   139  	return fmt.Sprintf("%d:%d", modID, cmdID), nil
   140  }
   141  
   142  // GetMoreL5Extend 获取增量数据
   143  func (l *l5Store) GetMoreL5Extend(mtime time.Time) (map[string]map[string]interface{}, error) {
   144  	return nil, nil
   145  }
   146  
   147  // GetMoreL5Routes 获取Route增量数据
   148  func (l *l5Store) GetMoreL5Routes(flow uint32) ([]*model.Route, error) {
   149  	return nil, nil
   150  }
   151  
   152  // GetMoreL5Policies 获取Policy增量数据
   153  func (l *l5Store) GetMoreL5Policies(flow uint32) ([]*model.Policy, error) {
   154  	return nil, nil
   155  }
   156  
   157  // GetMoreL5Sections 获取Section增量数据
   158  func (l *l5Store) GetMoreL5Sections(flow uint32) ([]*model.Section, error) {
   159  	return nil, nil
   160  }
   161  
   162  // GetMoreL5IPConfigs 获取IP Config增量数据
   163  func (l *l5Store) GetMoreL5IPConfigs(flow uint32) ([]*model.IPConfig, error) {
   164  	return nil, nil
   165  }