-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_elasticsearch.go
171 lines (158 loc) · 5.14 KB
/
multi_elasticsearch.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package kasper
import (
"sort"
"fmt"
"golang.org/x/net/context"
"gopkg.in/olivere/elastic.v5"
)
// MultiElasticsearch is an implementation of MultiStore that uses Elasticsearch.
// Each instance provides key-value access to a subset of the Elasticsearch documents
// defined by a tenancy instance (see ElasticsearchTenancy).
// This implementation supports Elasticsearch 5.x
type MultiElasticsearch struct {
config *Config
client *elastic.Client
context context.Context
stores map[string]Store
tenancy ElasticsearchTenancy
logger Logger
labelValues []string
pushSummary Summary
fetchSummary Summary
pushBytesSummary Summary
fetchBytesSummary Summary
}
// ElasticsearchTenancy defines how tenanted keys are mapped to an index and type.
// Here is a simple example:
// type CustomerTenancy struct{}
// func (CustomerTenancy) TenantIndexAndType(tenant string) (indexName, typeName string) {
// indexName = fmt.Sprintf("sales-service~%s", tenant)
// typeName = "customer"
// return
// }
type ElasticsearchTenancy interface {
TenantIndexAndType(tenant string) (indexName, typeName string)
}
// NewMultiElasticsearch creates MultiElasticsearch instances.
// All documents read and written will correspond to the URL:
// https://{cluster}:9092/{indexName}/{typeName}/{key}
// where indexName and typeName depend on the tenant and the tenancy instance.
func NewMultiElasticsearch(config *Config, client *elastic.Client, tenancy ElasticsearchTenancy) *MultiElasticsearch {
indexName, typeName := tenancy.TenantIndexAndType("tenant")
metrics := config.MetricsProvider
labelNames := []string{"topicProcessor", "indexAndType"}
labelValues := []string{config.TopicProcessorName, fmt.Sprintf("%s/%s", indexName, typeName)}
s := &MultiElasticsearch{
config,
client,
context.Background(),
make(map[string]Store),
tenancy,
config.Logger,
labelValues,
metrics.NewSummary("MultiElasticsearch_Push", "Summary of Push() calls", labelNames...),
metrics.NewSummary("MultiElasticsearch_Fetch", "Summary of Fetch() calls", labelNames...),
metrics.NewSummary("MultiElasticsearch_Push_Bytes", "Summary of Push() bytes written", labelNames...),
metrics.NewSummary("MultiElasticsearch_Fetch_Bytes", "Summary of Fetch() bytes read", labelNames...),
}
return s
}
// Tenant returns an Elasticsearch Store for the given tenant.
// Created instances are cached on future invocations.
func (s *MultiElasticsearch) Tenant(tenant string) Store {
kv, found := s.stores[tenant]
if !found {
indexName, typeName := s.indexAndType(tenant)
kv = NewElasticsearch(s.config, s.client, indexName, typeName)
s.stores[tenant] = kv
}
return kv
}
// AllTenants returns the list of tenants known to this instance.
func (s *MultiElasticsearch) AllTenants() []string {
tenants := make([]string, len(s.stores))
i := 0
for tenant := range s.stores {
tenants[i] = tenant
i++
}
sort.Strings(tenants)
return tenants
}
// Fetch performs a single MultiGet operation on the Elasticsearch cluster across multiple tenants (i.e. indexes).
func (s *MultiElasticsearch) Fetch(keys []TenantKey) (*MultiMap, error) {
s.fetchSummary.Observe(float64(len(keys)), s.labelValues...)
res := NewMultiMap(len(keys) / 10)
if len(keys) == 0 {
return res, nil
}
s.logger.Debugf("Multitenant MultiElasticsearch GetAll: %#v", keys)
multiGet := s.client.MultiGet()
for _, key := range keys {
indexName, typeName := s.indexAndType(key.Tenant)
item := elastic.NewMultiGetItem().
Index(indexName).
Type(typeName).
Id(key.Key)
multiGet.Add(item)
}
response, err := multiGet.Do(s.context)
if err != nil {
return nil, err
}
bytesRead := 0
for _, doc := range response.Docs {
if !doc.Found {
s.logger.Debug(doc.Index, doc.Id, " not found")
continue
}
s.logger.Debug("unmarshalling ", doc.Source)
err := res.Tenant(doc.Index).Put(doc.Id, *doc.Source)
if err != nil {
return nil, err
}
bytesRead += len(*doc.Source)
}
s.fetchBytesSummary.Observe(float64(bytesRead), s.labelValues...)
return res, nil
}
// Push performs a single Bulk index request with all documents provided.
// It returns an error if any operation fails.
func (s *MultiElasticsearch) Push(m *MultiMap) error {
for _, tenant := range m.AllTenants() {
s.Tenant(tenant) // force creation of index & mappings if they don't exist
}
bulk := s.client.Bulk()
i := 0
bytesWritten := 0
for _, tenant := range m.AllTenants() {
indexName, typeName := s.indexAndType(tenant)
for key, value := range m.Tenant(tenant).(*Map).GetMap() {
bulk.Add(elastic.NewBulkIndexRequest().
Index(indexName).
Type(typeName).
Id(key).
Doc(string(value)),
)
i++
bytesWritten += len(value)
}
}
if i == 0 {
return nil
}
s.logger.Debugf("Multitenant MultiElasticsearch PutAll of %d keys", i)
s.pushSummary.Observe(float64(i), s.labelValues...)
s.pushBytesSummary.Observe(float64(bytesWritten), s.labelValues...)
response, err := bulk.Do(s.context)
if err != nil {
return err
}
if response.Errors {
return createBulkError(response)
}
return nil
}
func (s *MultiElasticsearch) indexAndType(tenant string) (indexName, indexType string) {
return s.tenancy.TenantIndexAndType(tenant)
}