/
indexer.go
128 lines (112 loc) · 4.06 KB
/
indexer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregate
import (
"github.com/apache/servicecomb-service-center/pkg/util"
"github.com/apache/servicecomb-service-center/server/plugin/discovery"
"github.com/apache/servicecomb-service-center/server/plugin/registry"
"context"
)
// AdaptorsIndexer implements discovery.Indexer.
// AdaptorsIndexer is an aggregator of multi Indexers, and it aggregates all the
// Indexers' data as it's result.
type AdaptorsIndexer struct {
Adaptors []discovery.Adaptor
}
// Search implements discovery.Indexer.Search.
// AdaptorsIndexer ignores the errors during search to ensure availability, so
// it always searches successfully, no matter how many Adaptors are abnormal.
// But at the cost of that, AdaptorsIndexer doesn't guarantee the correctness
// of the search results.
func (i *AdaptorsIndexer) Search(ctx context.Context, opts ...registry.PluginOpOption) (*discovery.Response, error) {
var (
response discovery.Response
exists = make(map[string]struct{})
)
for _, a := range i.Adaptors {
resp, err := a.Search(ctx, opts...)
if err != nil {
continue
}
for _, kv := range resp.Kvs {
key := util.BytesToStringWithNoCopy(kv.Key)
if _, ok := exists[key]; !ok {
exists[key] = struct{}{}
response.Kvs = append(response.Kvs, kv)
}
}
response.Count += resp.Count
}
return &response, nil
}
// Creditable implements discovery.Indexer.Creditable.
// AdaptorsIndexer's search result's are not creditable as it ignores the
// errors. In other words, AdaptorsIndexer makes the best efforts to search
// data, but it does not ensure the correctness.
func (i *AdaptorsIndexer) Creditable() bool {
return false
}
func NewAdaptorsIndexer(as []discovery.Adaptor) *AdaptorsIndexer {
return &AdaptorsIndexer{Adaptors: as}
}
// AggregatorIndexer implements discovery.Indexer.
// AggregatorIndexer consists of multi Indexers and it decides which Indexer to
// use based on it's mechanism.
type AggregatorIndexer struct {
// CacheIndexer searches data from all the adaptors's cache.
*discovery.CacheIndexer
// AdaptorsIndexer searches data from all the adaptors.
AdaptorsIndexer discovery.Indexer
// LocalIndexer data from registry indexer.
LocalIndexer discovery.Indexer
}
// Search implements discovery.Indexer.Search.
func (i *AggregatorIndexer) Search(ctx context.Context, opts ...registry.PluginOpOption) (resp *discovery.Response, err error) {
op := registry.OpGet(opts...)
indexer := i.LocalIndexer
if op.Global {
// request with global param then do not use local indexer
indexer = i.AdaptorsIndexer
}
if op.NoCache() || !op.Global {
return indexer.Search(ctx, opts...)
}
resp, err = i.CacheIndexer.Search(ctx, opts...)
if err != nil {
return
}
if resp.Count > 0 || op.CacheOnly() {
return resp, nil
}
return indexer.Search(ctx, opts...)
}
// Creditable implements discovery.Indexer.Creditable.
func (i *AggregatorIndexer) Creditable() bool {
return i.AdaptorsIndexer.Creditable() &&
i.LocalIndexer.Creditable() &&
i.CacheIndexer.Creditable()
}
func NewAggregatorIndexer(as *Aggregator) *AggregatorIndexer {
indexer := NewAdaptorsIndexer(as.Adaptors)
ai := &AggregatorIndexer{
CacheIndexer: discovery.NewCacheIndexer(as.Cache()),
AdaptorsIndexer: indexer,
LocalIndexer: indexer,
}
if registryIndex >= 0 {
ai.LocalIndexer = as.Adaptors[registryIndex]
}
return ai
}