|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package datalayer |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "reflect" |
| 23 | + "sync" |
| 24 | +) |
| 25 | + |
| 26 | +// DataSource is an interface required from all data layer data collection |
| 27 | +// sources. |
| 28 | +type DataSource interface { |
| 29 | + // Name returns the name of this datasource. |
| 30 | + Name() string |
| 31 | + |
| 32 | + // AddExtractor adds an extractor to the data source. |
| 33 | + // The extractor will be called whenever the Collector might |
| 34 | + // have some new raw information regarding an endpoint. |
| 35 | + // The Extractor's expected input type should be validated against |
| 36 | + // the data source's output type upon registration. |
| 37 | + AddExtractor(extractor Extractor) error |
| 38 | + |
| 39 | + // Collect is triggered by the data layer framework to fetch potentially new |
| 40 | + // data for an endpoint. It passes retrieved data to registered Extractors. |
| 41 | + Collect(ep Endpoint) |
| 42 | +} |
| 43 | + |
| 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). |
| 50 | +type Extractor interface { |
| 51 | + // Name returns the name of the extractor. |
| 52 | + 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. |
| 56 | + ExpectedInputType() reflect.Type |
| 57 | + |
| 58 | + // Extract transforms the data source output into a concrete attribute that |
| 59 | + // is stored on the given endpoint. |
| 60 | + Extract(data any, ep Endpoint) |
| 61 | +} |
| 62 | + |
| 63 | +var ( |
| 64 | + // defaultDataSources is the system default data source registry. |
| 65 | + defaultDataSources = DataSourceRegistry{} |
| 66 | +) |
| 67 | + |
| 68 | +// DataSourceRegistry stores named data sources and makes them |
| 69 | +// accessible to other subsystems in the inference gateway. |
| 70 | +type DataSourceRegistry struct { |
| 71 | + sources sync.Map |
| 72 | +} |
| 73 | + |
| 74 | +// Register adds a source to the registry. |
| 75 | +func (dsr *DataSourceRegistry) Register(src DataSource) error { |
| 76 | + if src == nil { |
| 77 | + return errors.New("unable to register a nil data source") |
| 78 | + } |
| 79 | + |
| 80 | + if _, found := dsr.sources.Load(src.Name()); found { |
| 81 | + return fmt.Errorf("unable to register duplicate data source: %s", src.Name()) |
| 82 | + } |
| 83 | + dsr.sources.Store(src.Name(), src) |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// GetNamedSource returns the named data source, if found. |
| 88 | +func (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 { |
| 94 | + if ds, ok := val.(DataSource); ok { |
| 95 | + return ds, true |
| 96 | + } // ignore type assertion failures and fall through |
| 97 | + } |
| 98 | + return nil, false |
| 99 | +} |
| 100 | + |
| 101 | +// GetSources returns all sources registered. |
| 102 | +func (dsr *DataSourceRegistry) GetSources() []DataSource { |
| 103 | + sources := []DataSource{} |
| 104 | + dsr.sources.Range(func(_, val any) bool { |
| 105 | + if ds, ok := val.(DataSource); ok { |
| 106 | + sources = append(sources, ds) |
| 107 | + } |
| 108 | + return true // continue iteration |
| 109 | + }) |
| 110 | + return sources |
| 111 | +} |
| 112 | + |
| 113 | +// RegisterSource adds the data source to the default registry. |
| 114 | +func RegisterSource(src DataSource) error { |
| 115 | + return defaultDataSources.Register(src) |
| 116 | +} |
| 117 | + |
| 118 | +// GetNamedSource returns the named source from the default registry, |
| 119 | +// if found. |
| 120 | +func GetNamedSource(name string) (DataSource, bool) { |
| 121 | + return defaultDataSources.GetNamedSource(name) |
| 122 | +} |
| 123 | + |
| 124 | +// GetSources returns all sources in the default registry. |
| 125 | +func GetSources() []DataSource { |
| 126 | + return defaultDataSources.GetSources() |
| 127 | +} |
| 128 | + |
| 129 | +// 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 |
| 139 | + } |
| 140 | + |
| 141 | + // check if collector output implements extractor input interface |
| 142 | + if collectorOutputType.Implements(extractorInputType) { |
| 143 | + return nil |
| 144 | + } |
| 145 | + |
| 146 | + return fmt.Errorf("extractor input type %v cannot handle collector output type %v", |
| 147 | + extractorInputType, collectorOutputType) |
| 148 | +} |
0 commit comments