forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinot_client.go
174 lines (145 loc) · 5.67 KB
/
pinot_client.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
// The MIT License (MIT)
// Copyright (c) 2017-2020 Uber Technologies Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package pinot
import (
"encoding/json"
"fmt"
"github.com/startreedata/pinot-client-go/pinot"
"github.com/uber/cadence/common/config"
"github.com/uber/cadence/common/log"
p "github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)
type PinotClient struct {
client *pinot.Connection
logger log.Logger
tableName string
serviceName string
}
func NewPinotClient(client *pinot.Connection, logger log.Logger, pinotConfig *config.PinotVisibilityConfig) GenericClient {
return &PinotClient{
client: client,
logger: logger,
tableName: pinotConfig.Table,
serviceName: pinotConfig.ServiceName,
}
}
func (c *PinotClient) Search(request *SearchRequest) (*SearchResponse, error) {
resp, err := c.client.ExecuteSQL(c.tableName, request.Query)
if err != nil {
return nil, &types.InternalServiceError{
Message: fmt.Sprintf("Pinot Search failed, %v", err),
}
}
token, err := GetNextPageToken(request.ListRequest.NextPageToken)
if err != nil {
return nil, &types.InternalServiceError{
Message: fmt.Sprintf("Get NextPage token failed, %v", err),
}
}
return c.getInternalListWorkflowExecutionsResponse(resp, request.Filter, token, request.ListRequest.PageSize, request.MaxResultWindow)
}
func (c *PinotClient) SearchAggr(request *SearchRequest) (AggrResponse, error) {
resp, err := c.client.ExecuteSQL(c.tableName, request.Query)
if err != nil {
return nil, &types.InternalServiceError{
Message: fmt.Sprintf("Pinot SearchAggr failed, %v", err),
}
}
return resp.ResultTable.Rows, nil
}
func (c *PinotClient) CountByQuery(query string) (int64, error) {
resp, err := c.client.ExecuteSQL(c.tableName, query)
if err != nil {
return 0, &types.InternalServiceError{
Message: fmt.Sprintf("CountWorkflowExecutions ExecuteSQL failed, %v", err),
}
}
count, err := resp.ResultTable.Rows[0][0].(json.Number).Int64()
if err == nil {
return count, nil
}
return -1, &types.InternalServiceError{
Message: fmt.Sprintf("can't convert result to integer!, query = %s, query result = %v, err = %v", query, resp.ResultTable.Rows[0][0], err),
}
}
func (c *PinotClient) GetTableName() string {
return c.tableName
}
// Pinot Response Translator
// We flattened the search attributes into columns in Pinot table
// This function converts the search result back to VisibilityRecord
func (c *PinotClient) getInternalListWorkflowExecutionsResponse(
resp *pinot.BrokerResponse,
isRecordValid func(rec *p.InternalVisibilityWorkflowExecutionInfo) bool,
token *PinotVisibilityPageToken,
pageSize int,
maxResultWindow int,
) (*p.InternalListWorkflowExecutionsResponse, error) {
response := &p.InternalListWorkflowExecutionsResponse{}
if resp == nil || resp.ResultTable == nil || resp.ResultTable.GetRowCount() == 0 {
return response, nil
}
schema := resp.ResultTable.DataSchema // get the schema to map results
columnNames := schema.ColumnNames
actualHits := resp.ResultTable.Rows
numOfActualHits := resp.ResultTable.GetRowCount()
response.Executions = make([]*p.InternalVisibilityWorkflowExecutionInfo, 0)
for i := 0; i < numOfActualHits; i++ {
workflowExecutionInfo, err := ConvertSearchResultToVisibilityRecord(actualHits[i], columnNames)
if err != nil {
return nil, err
}
if isRecordValid == nil || isRecordValid(workflowExecutionInfo) {
response.Executions = append(response.Executions, workflowExecutionInfo)
}
}
if numOfActualHits == pageSize { // this means the response is not the last page
var nextPageToken []byte
var err error
// ES Search API support pagination using From and PageSize, but has limit that From+PageSize cannot exceed a threshold
// In pinot we just skip (previous pages * page limit) items and take the next (number of page limit) items
nextPageToken, err = SerializePageToken(&PinotVisibilityPageToken{From: token.From + numOfActualHits})
if err != nil {
return nil, err
}
response.NextPageToken = make([]byte, len(nextPageToken))
copy(response.NextPageToken, nextPageToken)
}
return response, nil
}
func (c *PinotClient) getInternalGetClosedWorkflowExecutionResponse(resp *pinot.BrokerResponse) (
*p.InternalGetClosedWorkflowExecutionResponse,
error,
) {
if resp == nil {
return nil, nil
}
response := &p.InternalGetClosedWorkflowExecutionResponse{}
schema := resp.ResultTable.DataSchema // get the schema to map results
columnNames := schema.ColumnNames
actualHits := resp.ResultTable.Rows
var err error
response.Execution, err = ConvertSearchResultToVisibilityRecord(actualHits[0], columnNames)
if err != nil {
return nil, err
}
return response, nil
}