-
Notifications
You must be signed in to change notification settings - Fork 45
/
searchengine.go
253 lines (213 loc) · 7.39 KB
/
searchengine.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
// Copyright 2020 dfuse Platform 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 eosws
import (
"context"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
"github.com/streamingfast/derr"
"github.com/dfuse-io/dfuse-eosio/eosws/mdl"
pbsearcheos "github.com/dfuse-io/dfuse-eosio/pb/dfuse/eosio/search/v1"
"github.com/streamingfast/dtracing"
v1 "github.com/dfuse-io/eosws-go/mdl/v1"
"github.com/streamingfast/logging"
pbsearch "github.com/streamingfast/pbgo/dfuse/search/v1"
"github.com/golang/protobuf/ptypes"
"github.com/streamingfast/dmetering"
"github.com/streamingfast/opaque"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
)
type SearchEngine struct {
trxdb DB
searchClient pbsearch.RouterClient
}
func NewSearchEngine(db DB, searchClient pbsearch.RouterClient) *SearchEngine {
return &SearchEngine{
trxdb: db,
searchClient: searchClient,
}
}
type SearchQuery struct {
Query string `json:"query"`
StartBlock uint32 `json:"start_block"`
BlockCount uint32 `json:"block_count"`
SortDescending bool `json:"sort_desc"`
Limit uint64 `json:"limit"`
Cursor string `json:"cursor"`
WithReversible bool `json:"with_reversible"`
Format string `json:"format"`
}
type searchClientResponse struct {
Cursor string `json:"cursor"`
Transactions []*searchClientTransaction `json:"transactions"`
}
type searchFormattedClientResponse struct {
Cursor string `json:"cursor"`
Transactions []json.RawMessage `json:"transactions"`
}
type searchClientTransaction struct {
Lifecycle *v1.TransactionLifecycle `json:"lifecycle"`
Actions []uint32 `json:"action_idx"`
}
func (s *SearchEngine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
errors := validateSearchTransactionsRequest(r)
if len(errors) > 0 {
WriteError(w, r, derr.RequestValidationError(ctx, errors))
return
}
zlogger := logging.Logger(ctx, zlog)
searchQuery := extractSearchQueryFromRequest(r)
zlogger.Debug("extracted search query", zap.Any("query", searchQuery))
matches, rangeCompleted, err := s.DoRequest(ctx, toSearchNativeRequest(ctx, searchQuery))
if err != nil {
WriteError(w, r, derr.Wrap(err, "server error: unable to initiate to search"))
return
}
clientResponse, err := s.fillSearchClientResponse(ctx, zlogger, matches, rangeCompleted)
if err != nil {
WriteError(w, r, derr.Wrap(err, "unable to fill search client response"))
return
}
writeResponse(ctx, w, r, false, clientResponse)
//////////////////////////////////////////////////////////////////////
// Billable event on REST API endpoint
// WARNING: Ingress / Egress bytess is taken care by the middleware
//////////////////////////////////////////////////////////////////////
dmetering.EmitWithContext(dmetering.Event{
Source: "eosws",
Kind: "REST API",
Method: "/v0/search/transactions",
RequestsCount: 1,
ResponsesCount: int64(len(matches)),
}, ctx)
//////////////////////////////////////////////////////////////////////
}
func (s *SearchEngine) DoRequest(ctx context.Context, q *pbsearch.RouterRequest) (matches []*pbsearch.SearchMatch, rangeCompleted bool, err error) {
zlogger := logging.Logger(ctx, zlog)
zlogger.Debug("running search native request", zap.Any("native_search_request", q))
stream, err := s.searchClient.StreamMatches(ctx, q)
if err != nil {
return nil, false, err
}
for {
searchMatch, err := stream.Recv()
if err != nil {
if err == io.EOF {
break
}
return nil, false, err
}
matches = append(matches, searchMatch)
}
trail := stream.Trailer()
rangeCompleted, _ = strconv.ParseBool(trailerGetOne(trail, "range-completed")) // ignoring error -> will be false
return
}
func trailerGetOne(in metadata.MD, key string) string {
vals := in.Get(key)
if len(vals) == 0 {
return ""
}
return vals[0]
}
func toSearchNativeRequest(ctx context.Context, from *SearchQuery) (out *pbsearch.RouterRequest) {
return &pbsearch.RouterRequest{
UseLegacyBoundaries: true,
// FIXME: Does this have an impact? I think yes because within this code path, I assume `-100` is possible ...!
StartBlock: uint64(from.StartBlock),
BlockCount: uint64(from.BlockCount),
Query: from.Query,
Limit: int64(from.Limit),
Cursor: from.Cursor,
WithReversible: from.WithReversible,
Descending: from.SortDescending,
}
}
func writeResponse(ctx context.Context, w http.ResponseWriter, r *http.Request, formatted bool, response interface{}) {
ctx, span := dtracing.StartSpan(ctx, "search write response", "formatted", formatted)
defer span.End()
WriteJSON(w, r, response)
}
func (s *SearchEngine) fillSearchClientResponse(
ctx context.Context,
zlogger *zap.Logger,
matches []*pbsearch.SearchMatch,
rangeCompleted bool,
) (*searchClientResponse, error) {
actions := map[string][]uint32{}
trxIDS := make([]string, len(matches))
var lastCursor string
for i, match := range matches {
var eosMatchAny ptypes.DynamicAny
err := ptypes.UnmarshalAny(match.GetChainSpecific(), &eosMatchAny)
if err != nil {
return nil, err
}
eosMatch := eosMatchAny.Message.(*pbsearcheos.Match)
actions[match.TrxIdPrefix] = eosMatch.ActionIndexes
trxIDS[i] = match.TrxIdPrefix
lastCursor = match.Cursor
}
zlogger.Debug("fetching transactions from bigtable", zap.Int("count", len(trxIDS)))
lifecycles, err := s.trxdb.GetTransactions(ctx, trxIDS)
if err != nil {
return nil, derr.Wrap(err, "unable to get transactions from database")
}
zlogger.Debug("got transaction lifecycles", zap.Int("count", len(lifecycles)))
out := &searchClientResponse{}
for _, lifecycle := range lifecycles {
truncatedTrxID := lifecycle.Id[:32] // CONSTANT number of hex chars, pourri..
lc, err := mdl.ToV1TransactionLifecycle(lifecycle)
if err != nil {
return nil, fmt.Errorf("fill search client response: %w", err)
}
out.Transactions = append(out.Transactions, &searchClientTransaction{
Lifecycle: lc,
Actions: actions[truncatedTrxID],
})
}
if !rangeCompleted && lastCursor != "" { // no cursor if search completed over the range
out.Cursor, _ = opaque.ToOpaque(lastCursor)
}
return out, nil
}
func extractSearchQueryFromRequest(r *http.Request) (q *SearchQuery) {
q = &SearchQuery{}
q.Query = r.FormValue("q")
q.StartBlock = uint32(int64Input(r.FormValue("start_block")))
if blockCount := r.FormValue("block_count"); blockCount != "" {
q.BlockCount = uint32(int64Input(blockCount))
} else {
q.BlockCount = math.MaxUint32
}
if limit := r.FormValue("limit"); limit != "" {
q.Limit = uint64(int64Input(limit))
} else {
q.Limit = 100
}
if cursor := r.FormValue("cursor"); cursor != "" {
q.Cursor, _ = opaque.FromOpaque(cursor)
}
q.SortDescending = strings.ToLower(r.FormValue("sort")) == "desc"
q.WithReversible = strings.ToLower(r.FormValue("with_reversible")) == "true"
q.Format = r.FormValue("format")
return q
}