@@ -23,126 +23,98 @@ import (
2323 "sync"
2424)
2525
26- // DataSource is an interface required from all data layer data collection
27- // sources.
26+ // DataSource provides raw data to registered Extractors.
2827type DataSource interface {
29- // Name returns the name of this datasource.
3028 Name () string
31-
32- // AddExtractor adds an extractor to the data source .
33- // The extractor will be called whenever the Collector might
29+ // AddExtractor adds an extractor to the data source. Multiple
30+ // Extractors can be registered .
31+ // The extractor will be called whenever the DataSource might
3432 // have some new raw information regarding an endpoint.
3533 // The Extractor's expected input type should be validated against
3634 // the data source's output type upon registration.
3735 AddExtractor (extractor Extractor ) error
38-
3936 // Collect is triggered by the data layer framework to fetch potentially new
40- // data for an endpoint. It passes retrieved data to registered Extractors.
37+ // data for an endpoint. Collect calls registered Extractors to convert the
38+ // raw data into structured attributes.
4139 Collect (ep Endpoint )
4240}
4341
44- // Extractor is used to convert raw data into relevant data layer information
45- // for an endpoint. They are called by data sources whenever new data might be
46- // available. Multiple Extractors can be registered with a source. Extractors
47- // are expected to save their output with an endpoint so it becomes accessible
48- // to consumers in other subsystem of the inference gateway (e.g., when making
49- // scheduling decisions).
42+ // Extractor transforms raw data into structured attributes.
5043type Extractor interface {
51- // Name returns the name of the extractor.
5244 Name () string
53-
54- // ExpectedType defines the type expected by the extractor. It must match
55- // the output type of the data source where the extractor is registered.
45+ // ExpectedType defines the type expected by the extractor.
5646 ExpectedInputType () reflect.Type
57-
58- // Extract transforms the data source output into a concrete attribute that
59- // is stored on the given endpoint.
47+ // Extract transforms the raw data source output into a concrete structured
48+ // attribute, stored on the given endpoint.
6049 Extract (data any , ep Endpoint )
6150}
6251
63- var (
64- // defaultDataSources is the system default data source registry.
65- defaultDataSources = DataSourceRegistry {}
66- )
52+ var defaultDataSources = DataSourceRegistry {}
6753
68- // DataSourceRegistry stores named data sources and makes them
69- // accessible to other subsystems in the inference gateway.
54+ // DataSourceRegistry stores named data sources.
7055type DataSourceRegistry struct {
7156 sources sync.Map
7257}
7358
74- // Register adds a source to the registry.
59+ // Register adds a new DataSource to the registry.
7560func (dsr * DataSourceRegistry ) Register (src DataSource ) error {
7661 if src == nil {
7762 return errors .New ("unable to register a nil data source" )
7863 }
79-
80- if _ , found := dsr .sources .Load (src .Name ()); found {
64+ if _ , loaded := dsr .sources .LoadOrStore (src .Name (), src ); loaded {
8165 return fmt .Errorf ("unable to register duplicate data source: %s" , src .Name ())
8266 }
83- dsr .sources .Store (src .Name (), src )
8467 return nil
8568}
8669
87- // GetNamedSource returns the named data source, if found .
70+ // GetNamedSource fetches a source by name .
8871func (dsr * DataSourceRegistry ) GetNamedSource (name string ) (DataSource , bool ) {
89- if name == "" {
90- return nil , false
91- }
92-
93- if val , found := dsr .sources .Load (name ); found {
72+ if val , ok := dsr .sources .Load (name ); ok {
9473 if ds , ok := val .(DataSource ); ok {
9574 return ds , true
96- } // ignore type assertion failures and fall through
75+ }
9776 }
9877 return nil , false
9978}
10079
101- // GetSources returns all sources registered.
80+ // GetSources returns all registered sources .
10281func (dsr * DataSourceRegistry ) GetSources () []DataSource {
103- sources := []DataSource {}
82+ var result []DataSource
10483 dsr .sources .Range (func (_ , val any ) bool {
10584 if ds , ok := val .(DataSource ); ok {
106- sources = append (sources , ds )
85+ result = append (result , ds )
10786 }
108- return true // continue iteration
87+ return true
10988 })
110- return sources
89+ return result
11190}
11291
113- // RegisterSource adds the data source to the default registry.
92+ // --- default registry accessors ---
93+
11494func RegisterSource (src DataSource ) error {
11595 return defaultDataSources .Register (src )
11696}
11797
118- // GetNamedSource returns the named source from the default registry,
119- // if found.
12098func GetNamedSource (name string ) (DataSource , bool ) {
12199 return defaultDataSources .GetNamedSource (name )
122100}
123101
124- // GetSources returns all sources in the default registry.
125102func GetSources () []DataSource {
126103 return defaultDataSources .GetSources ()
127104}
128105
129106// ValidateExtractorType checks if an extractor can handle
130- // the collector's output.
131- func ValidateExtractorType (collectorOutputType , extractorInputType reflect.Type ) error {
132- if collectorOutputType == extractorInputType {
133- return nil
134- }
135-
136- // extractor accepts anything (i.e., interface{})
137- if extractorInputType .Kind () == reflect .Interface && extractorInputType .NumMethod () == 0 {
138- return nil
107+ // the DataSource's output. It should be called by a DataSource
108+ // when an extractor is added.
109+ func ValidateExtractorType (collectorOutput , extractorInput reflect.Type ) error {
110+ if collectorOutput == nil || extractorInput == nil {
111+ return errors .New ("extractor input type or data source output type can't be nil" )
139112 }
140-
141- // check if collector output implements extractor input interface
142- if collectorOutputType . Implements (extractorInputType ) {
113+ if collectorOutput == extractorInput ||
114+ ( extractorInput . Kind () == reflect . Interface && extractorInput . NumMethod () == 0 ) ||
115+ ( extractorInput . Kind () == reflect . Interface && collectorOutput . Implements (extractorInput ) ) {
143116 return nil
144117 }
145-
146- return fmt .Errorf ("extractor input type %v cannot handle collector output type %v" ,
147- extractorInputType , collectorOutputType )
118+ return fmt .Errorf ("extractor input type %v cannot handle data source output type %v" ,
119+ extractorInput , collectorOutput )
148120}
0 commit comments