-
Notifications
You must be signed in to change notification settings - Fork 41
/
session.go
297 lines (260 loc) · 8.5 KB
/
session.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
// Copyright 2023 Ant Group Co., Ltd.
//
// 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 application
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"google.golang.org/protobuf/encoding/protojson"
"github.com/secretflow/scql/pkg/broker/scheduler"
"github.com/secretflow/scql/pkg/broker/storage"
"github.com/secretflow/scql/pkg/constant"
exe "github.com/secretflow/scql/pkg/executor"
pb "github.com/secretflow/scql/pkg/proto-gen/scql"
"github.com/secretflow/scql/pkg/proto-gen/spu"
"github.com/secretflow/scql/pkg/sessionctx/stmtctx"
"github.com/secretflow/scql/pkg/sessionctx/variable"
"github.com/secretflow/scql/pkg/util/sliceutil"
)
const (
Version pb.BrokerProtocolVersion = 1
)
func GenerateJobID() (string, error) {
id, err := uuid.NewUUID()
if err != nil {
return "", err
}
return id.String(), nil
}
// remove hyphens due to project id may work as db name.
func GenerateProjectID() (string, error) {
id, err := uuid.NewUUID()
if err != nil {
return "", err
}
return strings.ReplaceAll(fmt.Sprintf("proj_%s", id.String()), "-", ""), nil
}
// session stores information that is unrelated to the query.
type Session struct {
CreatedAt time.Time
SessionVars *variable.SessionVars
Ctx context.Context
App *App
// all sessions have independent stub to avoid concurrent problems
ExecuteInfo *ExecutionInfo
Engine scheduler.EngineInstance
// current broker host for local engine to call back
CallBackHost string
Values map[fmt.Stringer]interface{}
DryRun bool
AsyncMode bool
OutputNames []string
Warning *pb.Warning
// mu protects the Result when running in async mode
mu sync.Mutex
Result *pb.QueryResponse
}
func (s *Session) GetResultSafely() *pb.QueryResponse {
s.mu.Lock()
defer s.mu.Unlock()
return s.Result
}
func (s *Session) SetResultSafely(result *pb.QueryResponse) {
s.mu.Lock()
defer s.mu.Unlock()
if s.Result == nil {
s.Result = result
} else {
logrus.Warnf("session %s already has result, ignore new one", s.ExecuteInfo.JobID)
}
}
func (s *Session) GetSelfPartyCode() string {
return s.App.Conf.PartyCode
}
// OnError is called when query job failed
func (s *Session) OnError(err error) {
logrus.Warnf("error occurred during executing query job %s: %v", s.ExecuteInfo.JobID, err)
if s.Engine != nil {
if err := s.Engine.Stop(); err != nil {
logrus.Warnf("failed to stop engine on query job %s: %v", s.ExecuteInfo.JobID, err)
}
}
// TODO: add more actions on error
}
// OnSuccess is called when query job success
func (s *Session) OnSuccess() {
if s.Engine != nil {
if err := s.Engine.Stop(); err != nil {
logrus.Warnf("failed to stop engine on query job %s: %v", s.ExecuteInfo.JobID, err)
}
}
}
type ExecutionInfo struct {
Issuer *pb.PartyId
ProjectID string
JobID string
Query string
// map from party to engine endpoint
EngineEndpoints sync.Map
EngineClient exe.EngineClient
InterStub *InterStub
// sorted parties who's data has been used in current session
DataParties []string
// sorted parties who work in current session
WorkParties []string
Checksums ChecksumStorage
CompileOpts *pb.CompileOptions
// for debug
DebugOpts *pb.DebugOptions
}
func (e *ExecutionInfo) CheckProjectConf() error {
spuConf := e.CompileOpts.SpuConf
if spuConf == nil {
return fmt.Errorf("spu runtime config is nil")
}
partyNum := len(e.WorkParties)
if partyNum == 0 {
return fmt.Errorf("work parties is empty")
}
if partyNum == 1 {
return nil
}
if spuConf.Protocol == spu.ProtocolKind_CHEETAH && partyNum != 2 {
return fmt.Errorf("protocol cheetah only support 2 parties, but got %d", partyNum)
}
if spuConf.Protocol == spu.ProtocolKind_ABY3 && partyNum != 3 {
return fmt.Errorf("protocol cheetah only support 3 parties, but got %d", partyNum)
}
return nil
}
func NewSession(ctx context.Context, info *ExecutionInfo, app *App, asyncMode bool, dryRun bool) (session *Session, err error) {
spuConf := &spu.RuntimeConfig{}
err = app.MetaMgr.ExecInMetaTransaction(func(txn *storage.MetaTransaction) error {
project, err := txn.GetProject(info.ProjectID)
if err != nil {
return fmt.Errorf("NewSession: project %v not exists", project.ID)
}
return protojson.Unmarshal([]byte(project.ProjectConf.SpuConf), spuConf)
})
if err != nil {
return nil, err
}
groupByThreshold := constant.DefaultGroupByThreshold
if app.Conf.SecurityCompromise.GroupByThreshold > 0 {
groupByThreshold = app.Conf.SecurityCompromise.GroupByThreshold
}
info.CompileOpts = &pb.CompileOptions{
SpuConf: spuConf,
SecurityCompromise: &pb.SecurityCompromiseConfig{
RevealGroupMark: app.Conf.SecurityCompromise.RevealGroupMark,
GroupByThreshold: groupByThreshold,
},
DumpExeGraph: true,
OptimizerHints: &pb.OptimizerHints{
PsiAlgorithmType: pb.PsiAlgorithmType_AUTO,
},
}
// NOTE(xiaoyuan): use ecdh psi when EnablePsiDetailLog is true
if info.DebugOpts != nil && info.DebugOpts.EnablePsiDetailLog {
info.CompileOpts.OptimizerHints.PsiAlgorithmType = pb.PsiAlgorithmType_ECDH
}
session = &Session{
CreatedAt: time.Now(),
ExecuteInfo: info,
SessionVars: variable.NewSessionVars(),
Ctx: ctx,
App: app,
CallBackHost: app.Conf.IntraHost,
AsyncMode: asyncMode,
DryRun: dryRun,
}
session.ExecuteInfo.InterStub = app.InterStub
session.GetSessionVars().StmtCtx = &stmtctx.StatementContext{}
// use project id as default db name
session.GetSessionVars().CurrentDB = info.ProjectID
if !dryRun {
eng, err := app.Scheduler.Schedule(info.JobID)
if err != nil {
return nil, fmt.Errorf("failed to schedule scqlengine for job %v: %v", info.JobID, err)
}
session.Engine = eng
// set self engine endpoint work in current session
session.SaveEndpoint(session.GetSelfPartyCode(), eng.GetEndpointForSelf())
}
return
}
// GetSessionVars implements the context.Context interface.
func (s *Session) GetSessionVars() *variable.SessionVars {
return s.SessionVars
}
// SetValue implements sessionctx.Context SetValue interface.
func (s *Session) SetValue(key fmt.Stringer, value interface{}) {
s.Values[key] = value
}
// Value implements sessionctx.Context Value interface.
func (s *Session) Value(key fmt.Stringer) interface{} {
value := s.Values[key]
return value
}
func (s *Session) GetSelfChecksum() (Checksum, error) {
selfCode := s.GetSelfPartyCode()
return s.ExecuteInfo.Checksums.GetLocal(selfCode)
}
func (s *Session) GetLocalChecksum(partyCode string) (Checksum, error) {
return s.ExecuteInfo.Checksums.GetLocal(partyCode)
}
func (s *Session) GetRemoteChecksum(partyCode string) (Checksum, error) {
return s.ExecuteInfo.Checksums.GetRemote(partyCode)
}
func (s *Session) SaveLocalChecksum(partyCode string, sum Checksum) error {
return s.ExecuteInfo.Checksums.SaveLocal(partyCode, sum)
}
func (s *Session) SaveRemoteChecksum(partyCode string, pbChecksum *pb.Checksum) error {
return s.ExecuteInfo.Checksums.SaveRemote(partyCode, pbChecksum)
}
// CheckChecksum checks data consistency with other parties via comparing checksum
func (s *Session) CheckChecksum() error {
for _, p := range sliceutil.Subtraction(s.ExecuteInfo.DataParties, []string{s.GetSelfPartyCode()}) {
compareResult, err := s.ExecuteInfo.Checksums.CompareChecksumFor(p)
if err != nil {
return err
}
if compareResult != pb.ChecksumCompareResult_EQUAL {
return fmt.Errorf("checksum not equal with party %s", p)
}
}
return nil
}
func (s *Session) GetEndpoint(partyCode string) (string, error) {
value, ok := s.ExecuteInfo.EngineEndpoints.Load(partyCode)
if !ok {
return "", fmt.Errorf("endpoint of %s not found", partyCode)
}
endpoint, ok := value.(string)
if !ok {
return "", fmt.Errorf("failed to parse endpoint from %+v of party %s", value, partyCode)
}
return endpoint, nil
}
func (s *Session) SaveEndpoint(partyCode, endpoint string) {
logrus.Infof("add endpoint %s for party %s", endpoint, partyCode)
s.ExecuteInfo.EngineEndpoints.Store(partyCode, endpoint)
}
func (s *Session) IsIssuer() bool {
return s.ExecuteInfo.Issuer.Code == s.GetSelfPartyCode()
}