github.com/polarismesh/polaris@v1.17.8/plugin/history.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 plugin
    19  
    20  import (
    21  	"os"
    22  	"sync"
    23  
    24  	"github.com/polarismesh/polaris/common/model"
    25  )
    26  
    27  var (
    28  	// historyOnce Plugin initialization atomic variable
    29  	historyOnce      sync.Once
    30  	compositeHistory *CompositeHistory
    31  )
    32  
    33  // History 历史记录插件
    34  type History interface {
    35  	Plugin
    36  	Record(entry *model.RecordEntry)
    37  }
    38  
    39  // GetHistory Get the historical record plugin
    40  func GetHistory() History {
    41  	if compositeHistory != nil {
    42  		return compositeHistory
    43  	}
    44  
    45  	historyOnce.Do(func() {
    46  		var (
    47  			entries []ConfigEntry
    48  		)
    49  
    50  		if len(config.History.Entries) != 0 {
    51  			entries = append(entries, config.History.Entries...)
    52  		} else {
    53  			entries = append(entries, ConfigEntry{
    54  				Name:   config.History.Name,
    55  				Option: config.History.Option,
    56  			})
    57  		}
    58  
    59  		compositeHistory = &CompositeHistory{
    60  			chain:   make([]History, 0, len(entries)),
    61  			options: entries,
    62  		}
    63  
    64  		if err := compositeHistory.Initialize(nil); err != nil {
    65  			log.Errorf("History plugin init err: %s", err.Error())
    66  			os.Exit(-1)
    67  		}
    68  	})
    69  
    70  	return compositeHistory
    71  }
    72  
    73  type CompositeHistory struct {
    74  	chain   []History
    75  	options []ConfigEntry
    76  }
    77  
    78  func (c *CompositeHistory) Name() string {
    79  	return "CompositeHistory"
    80  }
    81  
    82  func (c *CompositeHistory) Initialize(config *ConfigEntry) error {
    83  	for i := range c.options {
    84  		entry := c.options[i]
    85  		item, exist := pluginSet[entry.Name]
    86  		if !exist {
    87  			log.Errorf("plugin History not found target: %s", entry.Name)
    88  			continue
    89  		}
    90  
    91  		history, ok := item.(History)
    92  		if !ok {
    93  			log.Errorf("plugin target: %s not History", entry.Name)
    94  			continue
    95  		}
    96  
    97  		if err := history.Initialize(&entry); err != nil {
    98  			return err
    99  		}
   100  		c.chain = append(c.chain, history)
   101  	}
   102  	return nil
   103  }
   104  
   105  func (c *CompositeHistory) Destroy() error {
   106  	for i := range c.chain {
   107  		if err := c.chain[i].Destroy(); err != nil {
   108  			return err
   109  		}
   110  	}
   111  	return nil
   112  }
   113  
   114  func (c *CompositeHistory) Record(entry *model.RecordEntry) {
   115  	for i := range c.chain {
   116  		c.chain[i].Record(entry)
   117  	}
   118  }