forked from couchbase/cbgt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pindex_impl_blackhole.go
244 lines (209 loc) · 6.63 KB
/
pindex_impl_blackhole.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright (c) 2014 Couchbase, Inc.
// Licensed 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 cbgt
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
)
func init() {
RegisterPIndexImplType("blackhole", &PIndexImplType{
New: NewBlackHolePIndexImpl,
Open: OpenBlackHolePIndexImpl,
OpenUsing: OpenBlackHolePIndexImplUsing,
Count: nil, // Content of blackhole isn't countable.
Query: nil, // Content of blackhole isn't queryable.
AnalyzeIndexDefUpdates: restartOnIndexDefChanges,
Description: "advanced/blackhole" +
" - a blackhole index ignores all data and is not queryable;" +
" used for testing",
})
}
func NewBlackHolePIndexImpl(indexType, indexParams,
path string, restart func()) (PIndexImpl, Dest, error) {
err := os.MkdirAll(path, 0700)
if err != nil {
return nil, nil, err
}
err = ioutil.WriteFile(path+string(os.PathSeparator)+"black.hole",
EMPTY_BYTES, 0600)
if err != nil {
return nil, nil, err
}
dest := &BlackHole{path: path}
return dest, dest, nil
}
func OpenBlackHolePIndexImpl(indexType, path string, restart func()) (
PIndexImpl, Dest, error) {
return OpenBlackHolePIndexImplUsing(indexType, path, "", restart)
}
func OpenBlackHolePIndexImplUsing(indexType, path, indexParams string, restart func()) (
PIndexImpl, Dest, error) {
buf, err := ioutil.ReadFile(path + string(os.PathSeparator) + "black.hole")
if err != nil {
return nil, nil, err
}
if len(buf) > 0 {
return nil, nil, fmt.Errorf("blackhole: expected empty black.hole")
}
dest := &BlackHole{path: path}
return dest, dest, nil
}
// ---------------------------------------------------------
// Implements both Dest and PIndexImpl interfaces.
type BlackHole struct {
path string
}
func (t *BlackHole) Close() error {
return nil
}
func (t *BlackHole) DataUpdate(partition string,
key []byte, seq uint64, val []byte,
cas uint64,
extrasType DestExtrasType, extras []byte) error {
return nil
}
func (t *BlackHole) DataDelete(partition string,
key []byte, seq uint64,
cas uint64,
extrasType DestExtrasType, extras []byte) error {
return nil
}
func (t *BlackHole) SnapshotStart(partition string,
snapStart, snapEnd uint64) error {
return nil
}
func (t *BlackHole) OpaqueGet(partition string) (
value []byte, lastSeq uint64, err error) {
return nil, 0, nil
}
func (t *BlackHole) OpaqueSet(partition string, value []byte) error {
return nil
}
func (t *BlackHole) Rollback(partition string, rollbackSeq uint64) error {
return nil
}
func (t *BlackHole) ConsistencyWait(partition, partitionUUID string,
consistencyLevel string,
consistencySeq uint64,
cancelCh <-chan bool) error {
return nil
}
func (t *BlackHole) Count(pindex *PIndex,
cancelCh <-chan bool) (uint64, error) {
return 0, nil
}
func (t *BlackHole) Query(pindex *PIndex, req []byte, w io.Writer,
cancelCh <-chan bool) error {
return nil
}
func (t *BlackHole) Stats(w io.Writer) error {
_, err := w.Write(JsonNULL)
return err
}
func reloadableIndexDefParamChange(mgr *Manager, paramPrev, paramCur string) bool {
if paramPrev == paramCur {
mgr.log.Printf("reloadableSourceParamsChange returned true")
return true
}
if len(paramPrev) == 0 {
// make it a json unmarshal-able string
paramPrev = "{}"
}
var prevMap map[string]interface{}
err := json.Unmarshal([]byte(paramPrev), &prevMap)
if err != nil {
mgr.log.Printf("pindex_bleve: reloadableSourceParamsChange"+
" json parse paramPrev: %s, err: %v",
paramPrev, err)
return false
}
if len(paramCur) == 0 {
// make it a json unmarshal-able string
paramCur = "{}"
}
var curMap map[string]interface{}
err = json.Unmarshal([]byte(paramCur), &curMap)
if err != nil {
mgr.log.Printf("pindex_bleve: reloadableSourceParamsChange"+
" json parse paramCur: %s, err: %v",
paramCur, err)
return false
}
// any parsing err doesn't matter here.
po, _ := ParseFeedAllotmentOption(paramPrev)
co, _ := ParseFeedAllotmentOption(paramCur)
if po != co {
prevMap["feedAllotment"] = ""
curMap["feedAllotment"] = ""
}
return reflect.DeepEqual(prevMap, curMap)
}
func reloadableSourceParamsChange(mgr *Manager, paramPrev, paramCur string) bool {
if paramPrev == paramCur {
return true
}
var prevMap map[string]interface{}
err := json.Unmarshal([]byte(paramPrev), &prevMap)
if err != nil {
mgr.log.Printf("pindex_impl_blackhole: reloadableSourceParamsChange"+
" json parse paramPrev: %s, err: %v",
paramPrev, err)
return false
}
var curMap map[string]interface{}
err = json.Unmarshal([]byte(paramCur), &curMap)
if err != nil {
mgr.log.Printf("pindex_impl_blackhole: reloadableSourceParamsChange"+
" json parse paramCur: %s, err: %v",
paramCur, err)
return false
}
// any parsing err doesn't matter here.
po, _ := ParseFeedAllotmentOption(paramPrev)
co, _ := ParseFeedAllotmentOption(paramCur)
if po != co {
prevMap["feedAllotment"] = ""
curMap["feedAllotment"] = ""
}
return reflect.DeepEqual(prevMap, curMap)
}
// restartOnIndexDefChanges checks whether the changes in the indexDefns are
// quickly adoptable over a reboot of the pindex implementations.
// eg: kvstore configs updates like compaction percentage.
func restartOnIndexDefChanges(mgr *Manager,
configRequest *ConfigAnalyzeRequest) ResultCode {
if configRequest == nil || configRequest.IndexDefnCur == nil ||
configRequest.IndexDefnPrev == nil {
return ""
}
if configRequest.IndexDefnPrev.Name != configRequest.IndexDefnCur.Name ||
configRequest.IndexDefnPrev.SourceName !=
configRequest.IndexDefnCur.SourceName ||
configRequest.IndexDefnPrev.SourceType !=
configRequest.IndexDefnCur.SourceType ||
configRequest.IndexDefnPrev.SourceUUID !=
configRequest.IndexDefnCur.SourceUUID ||
!reloadableSourceParamsChange(mgr, configRequest.IndexDefnPrev.SourceParams,
configRequest.IndexDefnCur.SourceParams) ||
configRequest.IndexDefnPrev.Type !=
configRequest.IndexDefnCur.Type ||
!reflect.DeepEqual(configRequest.SourcePartitionsCur,
configRequest.SourcePartitionsPrev) ||
!reloadableIndexDefParamChange(mgr, configRequest.IndexDefnPrev.Params,
configRequest.IndexDefnCur.Params) {
return ""
}
return PINDEXES_RESTART
}