-
Notifications
You must be signed in to change notification settings - Fork 199
/
client_appengine_exclusions.go
154 lines (128 loc) · 4.4 KB
/
client_appengine_exclusions.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
//go:build !app_engine
// Copyright 2014-2022 Aerospike, 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 aerospike
import (
"context"
"sync"
"golang.org/x/sync/semaphore"
lualib "github.com/aerospike/aerospike-client-go/v7/internal/lua"
"github.com/aerospike/aerospike-client-go/v7/logger"
lua "github.com/yuin/gopher-lua"
)
//--------------------------------------------------------
// Query Aggregate functions (Supported by Aerospike 3+ servers only)
//--------------------------------------------------------
// SetLuaPath sets the Lua interpreter path to files
// This path is used to load UDFs for QueryAggregate command
func SetLuaPath(lpath string) {
lualib.SetPath(lpath)
}
// QueryAggregate executes a Map/Reduce query and returns the results.
// The query executor puts records on the channel from separate goroutines.
// The caller can concurrently pop records off the channel through the
// Recordset.Records channel.
//
// This method is only supported by Aerospike 3+ servers.
// If the policy is nil, the default relevant policy will be used.
func (clnt *Client) QueryAggregate(policy *QueryPolicy, statement *Statement, packageName, functionName string, functionArgs ...Value) (*Recordset, Error) {
statement.SetAggregateFunction(packageName, functionName, functionArgs, true)
policy = clnt.getUsableQueryPolicy(policy)
nodes := clnt.cluster.GetNodes()
if len(nodes) == 0 {
return nil, ErrClusterIsEmpty.err()
}
// results channel must be async for performance
recSet := newRecordset(policy.RecordQueueSize, len(nodes))
// get a lua instance
luaInstance := lualib.LuaPool.Get().(*lua.LState)
if luaInstance == nil {
return nil, ErrLuaPoolEmpty.err()
}
// Input Channel
inputChan := make(chan interface{}, 4096) // 4096 = number of partitions
istream := lualib.NewStream(luaInstance, inputChan)
// Output Channe;
outputChan := make(chan interface{})
ostream := lualib.NewStream(luaInstance, outputChan)
// results channel must be async for performance
var wg sync.WaitGroup
wg.Add(len(nodes))
// results channel must be async for performance
maxConcurrentNodes := policy.MaxConcurrentNodes
if maxConcurrentNodes <= 0 {
maxConcurrentNodes = len(nodes)
}
sem := semaphore.NewWeighted(int64(maxConcurrentNodes))
ctx := context.Background()
for _, node := range nodes {
// copy policies to avoid race conditions
newPolicy := *policy
command := newQueryAggregateCommand(node, &newPolicy, statement, recSet)
command.luaInstance = luaInstance
command.inputChan = inputChan
if err := sem.Acquire(ctx, 1); err != nil {
logger.Logger.Error("Constraint Semaphore failed for QueryAggregate: %s", err.Error())
}
go func() {
defer sem.Release(1)
defer wg.Done()
command.Execute()
}()
}
go func() {
wg.Wait()
close(inputChan)
}()
go func() {
// we cannot signal end and close the recordset
// while processing is still going on
// We will do it only here, after all processing is over
defer func() {
for i := 0; i < len(nodes); i++ {
recSet.signalEnd()
}
}()
for val := range outputChan {
recSet.records <- &Result{Record: &Record{Bins: BinMap{"SUCCESS": val}}, Err: nil}
}
}()
go func() {
defer close(outputChan)
defer luaInstance.Close()
err := luaInstance.DoFile(lualib.Path() + packageName + ".lua")
if err != nil {
recSet.sendError(newCommonError(err))
return
}
fn := luaInstance.GetGlobal(functionName)
luaArgs := []lua.LValue{fn, lualib.NewValue(luaInstance, 2), istream, ostream}
for _, a := range functionArgs {
luaArgs = append(luaArgs, lualib.NewValue(luaInstance, unwrapValue(a)))
}
if err := luaInstance.CallByParam(lua.P{
Fn: luaInstance.GetGlobal("apply_stream"),
NRet: 1,
Protect: true,
},
luaArgs...,
); err != nil {
recSet.sendError(newCommonError(err))
return
}
luaInstance.Get(-1) // returned value
luaInstance.Pop(1) // remove received value
}()
return recSet, nil
}