-
Notifications
You must be signed in to change notification settings - Fork 3
/
response_handler.go
407 lines (338 loc) · 9.99 KB
/
response_handler.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2019-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package n1fty
import (
"encoding/gob"
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/buger/jsonparser"
"github.com/couchbase/cbft"
pb "github.com/couchbase/cbft/protobuf"
"github.com/couchbase/n1fty/util"
"github.com/couchbase/query/datastore"
"github.com/couchbase/query/logging"
"github.com/couchbase/query/tenant"
"github.com/couchbase/query/value"
)
type responseHandler struct {
i *FTSIndex
requestID string
backfillFile *os.File
sr *cbft.SearchRequest
}
func newResponseHandler(i *FTSIndex, requestID string,
sr *cbft.SearchRequest) *responseHandler {
return &responseHandler{
i: i,
requestID: requestID,
sr: sr,
}
}
func (r *responseHandler) handleResponse(conn *datastore.IndexConnection,
waitGroup *sync.WaitGroup,
backfillSync *int64,
stream pb.SearchService_SearchClient) {
sender := conn.Sender()
backfillLimit := getBackfillSpaceLimit()
firstResponseByte, starttm, ftsDur := false, time.Now(), time.Now()
var enc *gob.Encoder
var dec *gob.Decoder
var readfd *os.File
logPrefix := fmt.Sprintf("n1fty[%s/%s-%v]", r.i.Name(), r.i.KeyspaceId(), time.Now().UnixNano())
var tmpfile *os.File
var backfillFin, backfillEntries int64
var hits []byte
var numHits uint64
backfill := func() {
var entries []byte
name := tmpfile.Name()
defer func() {
if readfd != nil {
readfd.Close()
}
waitGroup.Done()
atomic.AddInt64(&backfillFin, 1)
logging.Infof("n1fty: response_handler: %v %q finished backfill for %v ",
logPrefix, r.requestID, name)
// TODO: revisit this for better pattern?
recover() // need this because entryChannel() would have closed
}()
logging.Infof("n1fty: response_handler: %v %q started backfill for %v",
logPrefix, r.requestID, name)
for {
if pending := atomic.LoadInt64(&backfillEntries); pending > 0 {
atomic.AddInt64(&backfillEntries, -1)
} else if done := atomic.LoadInt64(backfillSync); done == doneRequest {
return
} else {
// wait a bit
time.Sleep(1 * time.Millisecond)
continue
}
cummsizeInMB := float64(atomic.LoadInt64(
&r.i.indexer.stats.CurBackFillSize)) / 1048576
if cummsizeInMB > float64(backfillLimit) {
fmsg := "%q backfill size: %v exceeded limit: %v"
err := fmt.Errorf(fmsg, r.requestID, cummsizeInMB, backfillLimit)
conn.Error(util.N1QLError(err, ""))
return
}
if err := dec.Decode(&entries); err != nil {
fmsg := "%v %q decoding from backfill file: %v: err: %v"
err = fmt.Errorf(fmsg, logPrefix, r.requestID, name, err)
conn.Error(util.N1QLError(err, ""))
return
}
atomic.AddInt64(&r.i.indexer.stats.TotalThrottledFtsDuration,
int64(time.Since(ftsDur)))
connOk := r.sendEntries(entries, conn)
if !connOk {
return
}
// reset the time taken by fts
ftsDur = time.Now()
}
}
for {
results, err := stream.Recv()
if err == io.EOF {
// return as it read all data
return
}
if err != nil {
conn.Error(util.N1QLError(err, "response_handler: stream.Recv, err "))
return
}
// account the time to first byte response from fts
if firstResponseByte == false {
atomic.AddInt64(&r.i.indexer.stats.TotalTTFBDuration,
int64(time.Since(starttm)))
firstResponseByte = true
}
switch res := results.Contents.(type) {
case *pb.StreamSearchResults_Hits:
hits = res.Hits.Bytes
numHits = res.Hits.Total
case *pb.StreamSearchResults_SearchResult:
if res.SearchResult == nil {
break
}
searchStatus, _, _, err := jsonparser.Get(res.SearchResult, "status")
if err != nil || len(searchStatus) == 0 {
conn.Error(util.N1QLError(err, "error in retrieving status"))
return
}
errorsBytes, _, _, _ := jsonparser.Get(searchStatus, "errors")
if len(errorsBytes) > 0 {
var errs []error
jsonparser.ObjectEach(errorsBytes,
func(partition []byte, er []byte, datatype jsonparser.ValueType,
offset int) error {
errs = append(errs,
fmt.Errorf("partition: %s, err: %s, ", string(partition), string(er)))
return nil
})
if len(errs) > 0 {
conn.Error(util.N1QLError(fmt.Errorf("search err summary: %v", errs),
"response_handler: err"))
// return here, as partial results are NOT supported
return
}
}
hits, _, _, err = jsonparser.Get(res.SearchResult, "hits")
if err != nil {
conn.Error(util.N1QLError(err, "error in retrieving hits"))
return
}
if IsServerlessMode() {
diskBytesRead, _, _, err := jsonparser.Get(res.SearchResult, "cost")
if err != nil {
conn.Error(util.N1QLError(err, "error in retrieving read units"+
" for this query"))
return
}
bytesRead, err := strconv.ParseUint(string(diskBytesRead), 10, 64)
if err != nil {
conn.Error(util.N1QLError(err, "error in parsing read units"+
" for this query"))
return
}
rus, err := getReadUnits(r.i.indexDef.SourceName, bytesRead)
if err != nil {
conn.Error(util.N1QLError(err, "error in read units conversion"))
return
}
conn.RecordFtsRU(tenant.Unit(rus))
}
numHits = 0
}
ln := sender.Length()
cp := sender.Capacity()
if backfillLimit > 0 && tmpfile == nil &&
(uint64(cp-ln) < numHits) {
logging.Infof("n1fty: response_handler: buffer overflow [cap %d len %d],"+
" initiating backfill", cp, ln)
enc, dec, tmpfile, err = initBackFill(logPrefix, r.requestID, r)
if err != nil {
conn.Error(util.N1QLError(err, "initBackFill failed, err:"))
return
}
waitGroup.Add(1)
go backfill()
}
// slow reader found and hence start dumping the results to the backfill file
if tmpfile != nil {
// whether temp-file is exhausted the limit.
cummsizeInMB := float64(atomic.LoadInt64(
&r.i.indexer.stats.CurBackFillSize)) / 1048576
if cummsizeInMB > float64(backfillLimit) {
fmsg := "%q backfill exceeded limit %v, %v"
err := fmt.Errorf(fmsg, r.requestID, backfillLimit, cummsizeInMB)
conn.Error(util.N1QLError(err, ""))
return
}
if atomic.LoadInt64(&backfillFin) > 0 {
return
}
err := writeToBackfill(hits, enc)
if err != nil {
conn.Error(util.N1QLError(err, "writeToBackfill err:"))
return
}
atomic.AddInt64(&backfillEntries, 1)
} else if hits != nil {
atomic.AddInt64(&r.i.indexer.stats.TotalThrottledFtsDuration,
int64(time.Since(ftsDur)))
connOk := r.sendEntries(hits, conn)
if !connOk {
return
}
// reset the time taken by fts
ftsDur = time.Now()
}
}
}
func (r *responseHandler) cleanupBackfill() {
if r.backfillFile != nil {
r.backfillFile.Close()
fname := r.backfillFile.Name()
if err := os.Remove(fname); err != nil {
fmsg := "%v remove backfill file %v unexpected failure: %v\n"
logging.Errorf("n1fty: response_handler: cleanupBackfills, err: %v",
fmt.Errorf(fmsg, fname, err))
}
atomic.AddInt64(&r.i.indexer.stats.TotalBackFills, 1)
}
}
func (r *responseHandler) sendEntries(hits []byte, conn *datastore.IndexConnection) bool {
if len(hits) == 0 {
return true // so next set of hits can be processed
}
var start time.Time
sender := conn.Sender()
var sendEntriesFailed bool
_, err := jsonparser.ArrayEach(hits,
func(hit []byte, dataType jsonparser.ValueType, offset int, err error) {
if sendEntriesFailed {
// skip sending rest of hits
return
}
blockedtm, blocked := int64(0), false
cp, ln := sender.Capacity(), sender.Length()
if ln == cp {
start, blocked = time.Now(), true
}
var hitMap map[string]interface{}
err = json.Unmarshal(hit, &hitMap)
if err != nil {
sendEntriesFailed = true
return
}
delete(hitMap, "index")
delete(hitMap, "sort")
if r.sr.Score == "none" {
delete(hitMap, "score")
}
id := hitMap["id"].(string)
if !sender.SendEntry(&datastore.IndexEntry{
PrimaryKey: id,
MetaData: value.NewValue(hitMap),
}) {
sendEntriesFailed = true
return
}
if blocked {
blockedtm += int64(time.Since(start))
atomic.AddInt64(&r.i.indexer.stats.TotalThrottledN1QLDuration, blockedtm)
}
})
if err != nil || sendEntriesFailed {
return false
}
return true
}
// TODO: need to cleanup any orphaned backfill subdirs from last time
// if there was a process crash and restart?
func initBackFill(logPrefix, requestID string, rh *responseHandler) (*gob.Encoder,
*gob.Decoder, *os.File, error) {
prefix := backfillPrefix + strconv.Itoa(os.Getpid())
tmpfile, err := os.CreateTemp(getBackfillSpaceDir(), prefix)
if err != nil {
fmsg := "%v %s creating backfill file, err: %v\n"
return nil, nil, nil, fmt.Errorf(fmsg, logPrefix, requestID, err)
}
name := ""
if tmpfile != nil {
name = tmpfile.Name()
rh.backfillFile = tmpfile
}
// encoder
enc := gob.NewEncoder(tmpfile)
readfd, err := os.OpenFile(name, os.O_RDONLY, 0666)
if err != nil {
fmsg := "%v %v reading backfill file %v, err: %v\n"
return nil, nil, tmpfile, fmt.Errorf(fmsg, logPrefix, requestID, name, err)
}
// decoder
return enc, gob.NewDecoder(readfd), tmpfile, nil
}
// -----------------------------------------------------------------------------
func getBackfillSpaceDir() string {
conf := clientConfig.GetConfig()
if conf == nil {
return getDefaultTmpDir()
}
if v, ok := conf[backfillSpaceDir]; ok {
return v.(string)
}
return getDefaultTmpDir()
}
func getBackfillSpaceLimit() int64 {
conf := clientConfig.GetConfig()
if conf == nil {
return defaultBackfillLimit
}
if v, ok := conf[backfillSpaceLimit]; ok {
return v.(int64)
}
return defaultBackfillLimit
}
func writeToBackfill(hits []byte, enc *gob.Encoder) error {
if hits != nil {
if err := enc.Encode(hits); err != nil {
return err
}
}
return nil
}