2022-02-09 19:42:28 +03:00
|
|
|
/*
|
|
|
|
_____ _____ _____ ____ ______ _____ ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| --- | | | | |-----| |---- | | |-----| |----- ------
|
|
|
|
| | | | | | | | | | | | |
|
|
|
|
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
|
|
|
|
2023-01-07 05:41:07 +03:00
|
|
|
Copyright © 2020-2023 Microsoft Corporation. All rights reserved.
|
2022-02-09 19:42:28 +03:00
|
|
|
Author : <blobfusedev@microsoft.com>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE
|
|
|
|
*/
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-27 09:49:36 +03:00
|
|
|
|
|
|
|
"github.com/Azure/azure-storage-fuse/v2/common/log"
|
2022-02-09 19:42:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Pipeline: Base pipeline structure holding list of components deployed along with the head of pipeline
|
|
|
|
type Pipeline struct {
|
|
|
|
components []Component
|
|
|
|
Header Component
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:00:13 +03:00
|
|
|
// NewComponent : Function that all components have to register to allow their instantiation
|
2022-02-09 19:42:28 +03:00
|
|
|
type NewComponent func() Component
|
|
|
|
|
|
|
|
// Map holding all possible components along with their respective constructors
|
|
|
|
var registeredComponents map[string]NewComponent
|
|
|
|
|
|
|
|
// NewPipeline : Using a list of strings holding name of components, create and configure the component objects
|
2022-07-27 08:05:32 +03:00
|
|
|
func NewPipeline(components []string, isParent bool) (*Pipeline, error) {
|
2022-02-09 19:42:28 +03:00
|
|
|
comps := make([]Component, 0)
|
|
|
|
lastPriority := EComponentPriority.Producer()
|
|
|
|
for _, name := range components {
|
|
|
|
// Search component exists in our registered map or not
|
|
|
|
compInit, ok := registeredComponents[name]
|
|
|
|
if ok {
|
|
|
|
// Call the constructor method registered by the component
|
|
|
|
comp := compInit()
|
|
|
|
|
|
|
|
// request component to parse and validate config of its interest
|
2022-07-27 08:05:32 +03:00
|
|
|
err := comp.Configure(isParent)
|
2022-02-09 19:42:28 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Err("Pipeline: error creating pipeline component %s [%s]", comp.Name(), err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(comp.Priority() <= lastPriority) {
|
|
|
|
log.Err("Pipeline::NewPipeline : Invalid Component order [priority of %s higher than above components]", comp.Name())
|
2022-03-10 06:48:05 +03:00
|
|
|
return nil, fmt.Errorf("config error in Pipeline [component %s is out of order]", name)
|
2022-02-09 19:42:28 +03:00
|
|
|
} else {
|
|
|
|
lastPriority = comp.Priority()
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the configured object in list of components
|
|
|
|
comps = append(comps, comp)
|
|
|
|
} else {
|
|
|
|
log.Err("Pipeline: error [component %s not registered]", name)
|
2022-03-10 06:48:05 +03:00
|
|
|
return nil, fmt.Errorf("config error in Pipeline [component %s not registered]", name)
|
2022-02-09 19:42:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create pipeline structure holding list of all component objects requested by config file
|
|
|
|
return &Pipeline{
|
|
|
|
components: comps,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create : Use the initialized objects to form a pipeline by registering next component to each component
|
|
|
|
func (p *Pipeline) Create() {
|
|
|
|
p.Header = p.components[0]
|
|
|
|
curComp := p.Header
|
|
|
|
|
|
|
|
for i := 1; i < len(p.components); i++ {
|
|
|
|
nextComp := p.components[i]
|
|
|
|
curComp.SetNextComponent(nextComp)
|
|
|
|
curComp = nextComp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start : Start the pipeline by calling 'Start' method of each component in reverse order of chaining
|
|
|
|
func (p *Pipeline) Start(ctx context.Context) (err error) {
|
|
|
|
p.Create()
|
|
|
|
|
|
|
|
for i := len(p.components) - 1; i >= 0; i-- {
|
|
|
|
if err = p.components[i].Start(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop : Stop the pipeline by calling 'Stop' method of each component
|
|
|
|
func (p *Pipeline) Stop() (err error) {
|
|
|
|
for i := 0; i < len(p.components); i++ {
|
|
|
|
if err = p.components[i].Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddComponent : Each component calls this method in their init to register the constructor
|
|
|
|
func AddComponent(name string, init NewComponent) {
|
|
|
|
registeredComponents[name] = init
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registeredComponents = make(map[string]NewComponent)
|
|
|
|
}
|