/
faasinfra.go
154 lines (131 loc) · 4.83 KB
/
faasinfra.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
// Copyright 2022 ByteDance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
package faasinfra
import (
"context"
"reflect"
"strconv"
"sync"
"github.com/tidwall/gjson"
cConstants "github.com/byted-apaas/server-common-go/constants"
cExceptions "github.com/byted-apaas/server-common-go/exceptions"
cHttp "github.com/byted-apaas/server-common-go/http"
cUtils "github.com/byted-apaas/server-common-go/utils"
"github.com/byted-apaas/server-sdk-go/common/structs"
"github.com/byted-apaas/server-sdk-go/common/utils"
"github.com/byted-apaas/server-sdk-go/request"
"github.com/byted-apaas/server-sdk-go/request/common"
"github.com/byted-apaas/server-sdk-go/service/tasks"
)
type requestFaaSInfra struct{}
var (
reqFaaSInfra request.IRequestFaaSInfra
reqFaaSInfraOnce sync.Once
)
func GetInstance() request.IRequestFaaSInfra {
if reqFaaSInfra == nil {
reqFaaSInfraOnce.Do(func() {
reqFaaSInfra = &requestFaaSInfra{}
})
}
return reqFaaSInfra
}
func (r *requestFaaSInfra) InvokeFunction(ctx context.Context, appCtx *structs.AppCtx, apiName string, params map[string]interface{}, result interface{}) error {
body, err := common.BuildInvokeParamsStr(ctx, apiName, params)
if err != nil {
return err
}
namespace, err := utils.GetNamespace(ctx, appCtx)
if err != nil {
return err
}
tenantName, err := utils.GetTenantName(ctx, appCtx)
if err != nil {
return err
}
headers := map[string][]string{
cConstants.HttpHeaderKeyTenant: {tenantName},
cConstants.HttpHeaderKeyUser: {strconv.FormatInt(cUtils.GetUserIDFromCtx(ctx), 10)},
}
data, err := cUtils.ErrorWrapper(getFaaSInfraClient().PostJson(utils.SetAppConfToCtx(ctx, appCtx), GetPathInvokeFunction(namespace), headers, body, cHttp.AppTokenMiddleware))
if err != nil {
return err
}
resultRaw := gjson.GetBytes(data, "data").Raw
err = cUtils.JsonUnmarshalBytes([]byte(resultRaw), result)
if err != nil {
return cExceptions.InternalError("InvokeFunction failed, err: %v", err)
}
return nil
}
func (r *requestFaaSInfra) InvokeFunctionAsync(ctx context.Context, appCtx *structs.AppCtx, apiName string, params map[string]interface{}) (int64, error) {
body, err := common.BuildInvokeParamsStr(ctx, apiName, params)
if err != nil {
return 0, err
}
namespace, err := utils.GetNamespace(ctx, appCtx)
if err != nil {
return 0, err
}
tenantName, err := utils.GetTenantName(ctx, appCtx)
if err != nil {
return 0, err
}
headers := map[string][]string{
cConstants.HttpHeaderKeyTenant: {tenantName},
cConstants.HttpHeaderKeyUser: {strconv.FormatInt(cUtils.GetUserIDFromCtx(ctx), 10)},
}
data, err := cUtils.ErrorWrapper(getFaaSInfraClient().PostJson(utils.SetAppConfToCtx(ctx, appCtx), GetPathInvokeFunctionAsync(namespace), headers, body, cHttp.AppTokenMiddleware))
if err != nil {
return 0, err
}
return gjson.GetBytes(data, "task_id").Int(), nil
}
func (r *requestFaaSInfra) InvokeFunctionDistributed(ctx context.Context, appCtx *structs.AppCtx, dataset interface{}, handlerFunc string, progressCallbackFunc string, completedCallbackFunc string, options *tasks.Options) (int64, error) {
v := reflect.ValueOf(dataset)
if v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() != reflect.Slice {
return 0, cExceptions.InvalidParamError("The type of dataset should be slice, but %s", v.Kind())
}
namespace, err := utils.GetNamespace(ctx, appCtx)
if err != nil {
return 0, err
}
tenantName, err := utils.GetTenantName(ctx, appCtx)
if err != nil {
return 0, err
}
headers := map[string][]string{
cConstants.HttpHeaderKeyTenant: {tenantName},
cConstants.HttpHeaderKeyUser: {strconv.FormatInt(cUtils.GetUserIDFromCtx(ctx), 10)},
}
lookMask := cUtils.GetLoopMaskFromCtx(ctx)
if (handlerFunc != "" && cUtils.StrInStrs(lookMask, handlerFunc)) ||
(progressCallbackFunc != "" && cUtils.StrInStrs(lookMask, progressCallbackFunc)) ||
(completedCallbackFunc != "" && cUtils.StrInStrs(lookMask, completedCallbackFunc)) {
return 0, cExceptions.InvalidParamError("Distributed task execution forms a loop.")
}
body := map[string]interface{}{
"domainName": tenantName,
"namespace": namespace,
"userId": cUtils.GetUserIDFromCtx(ctx),
"dataset": []interface{}{dataset},
"handlerFunc": handlerFunc,
"progressCallbackFunc": progressCallbackFunc,
"completedCallbackFunc": completedCallbackFunc,
"options": options,
"x-kunlun-loop-masks": lookMask,
}
data, err := cUtils.ErrorWrapper(getFaaSInfraClient().PostJson(utils.SetAppConfToCtx(ctx, appCtx), GetPathInvokeFunctionDistributed(namespace), headers, body, cHttp.AppTokenMiddleware))
if err != nil {
return 0, err
}
var taskID int64
err = cUtils.JsonUnmarshalBytes(data, &taskID)
if err != nil {
return 0, cExceptions.InternalError("Unmarshal result failed, err: %v", err)
}
return taskID, nil
}