-
Notifications
You must be signed in to change notification settings - Fork 58
/
proxy.go
415 lines (378 loc) · 13.6 KB
/
proxy.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
*
* 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 hazelcast
import (
"context"
"fmt"
"strings"
"time"
"github.com/hazelcast/hazelcast-go-client/aggregate"
pubcluster "github.com/hazelcast/hazelcast-go-client/cluster"
"github.com/hazelcast/hazelcast-go-client/internal/cb"
"github.com/hazelcast/hazelcast-go-client/internal/check"
"github.com/hazelcast/hazelcast-go-client/internal/client"
"github.com/hazelcast/hazelcast-go-client/internal/cluster"
ihzerrors "github.com/hazelcast/hazelcast-go-client/internal/hzerrors"
"github.com/hazelcast/hazelcast-go-client/internal/invocation"
"github.com/hazelcast/hazelcast-go-client/internal/logger"
"github.com/hazelcast/hazelcast-go-client/internal/proto"
"github.com/hazelcast/hazelcast-go-client/internal/proto/codec"
iproxy "github.com/hazelcast/hazelcast-go-client/internal/proxy"
iserialization "github.com/hazelcast/hazelcast-go-client/internal/serialization"
"github.com/hazelcast/hazelcast-go-client/predicate"
"github.com/hazelcast/hazelcast-go-client/types"
)
const (
ServiceNameMap = "hz:impl:mapService"
ServiceNameReplicatedMap = "hz:impl:replicatedMapService"
ServiceNameMultiMap = "hz:impl:multiMapService"
ServiceNameQueue = "hz:impl:queueService"
ServiceNameTopic = "hz:impl:topicService"
ServiceNameList = "hz:impl:listService"
ServiceNameRingBuffer = "hz:impl:ringbufferService"
ServiceNameSet = "hz:impl:setService"
ServiceNamePNCounter = "hz:impl:PNCounterService"
ServiceNameFlakeIDGenerator = "hz:impl:flakeIdGeneratorService"
)
const (
ttlUnset = -1
ttlUnlimited = 0
)
const (
maxIndexAttributes = 255
defaultLockID = 0
leaseUnset = -1
)
type creationBundle struct {
InvocationService *invocation.Service
SerializationService *iserialization.Service
PartitionService *cluster.PartitionService
ClusterService *cluster.Service
InvocationFactory *cluster.ConnectionInvocationFactory
ListenerBinder *cluster.ConnectionListenerBinder
Config *Config
Logger logger.LogAdaptor
NCMDestroyFn func(service, object string)
Invoker *client.Invoker
}
func (b creationBundle) Check() {
if b.InvocationService == nil {
panic("InvocationService is nil")
}
if b.SerializationService == nil {
panic("SerializationService is nil")
}
if b.PartitionService == nil {
panic("PartitionService is nil")
}
if b.ClusterService == nil {
panic("ClusterService is nil")
}
if b.InvocationFactory == nil {
panic("ConnectionInvocationFactory is nil")
}
if b.ListenerBinder == nil {
panic("ListenerBinder is nil")
}
if b.Config == nil {
panic("Config is nil")
}
if b.Logger.Logger == nil {
panic("LogAdaptor is nil")
}
if b.NCMDestroyFn == nil {
panic("NearCacheManager is nil")
}
if b.Invoker == nil {
panic("Invoker is nil")
}
}
type proxy struct {
logger logger.LogAdaptor
serializationService *iserialization.Service
partitionService *cluster.PartitionService
listenerBinder *cluster.ConnectionListenerBinder
config *Config
clusterService *cluster.Service
refIDGen *iproxy.ReferenceIDGenerator
removeFromCacheFn func(ctx context.Context) bool
invoker *client.Invoker
serviceName string
name string
smart bool
}
func newProxy(ctx context.Context, bundle creationBundle, svc string, obj string, idg *iproxy.ReferenceIDGenerator, removeFromCacheFn func(ctx context.Context) bool, remote bool) (*proxy, error) {
bundle.Check()
p := &proxy{
serviceName: svc,
name: obj,
serializationService: bundle.SerializationService,
partitionService: bundle.PartitionService,
clusterService: bundle.ClusterService,
listenerBinder: bundle.ListenerBinder,
config: bundle.Config,
logger: bundle.Logger,
invoker: bundle.Invoker,
removeFromCacheFn: removeFromCacheFn,
refIDGen: idg,
smart: !bundle.Config.Cluster.Unisocket,
}
if !remote {
return p, nil
}
if err := p.create(ctx); err != nil {
return nil, err
}
return p, nil
}
func (p *proxy) create(ctx context.Context) error {
request := codec.EncodeClientCreateProxyRequest(p.name, p.serviceName)
if _, err := p.invokeOnRandomTarget(ctx, request, nil); err != nil {
return fmt.Errorf("error creating proxy: %w", err)
}
return nil
}
// Destroy removes this object cluster-wide.
// Clears and releases all resources for this object.
func (p *proxy) Destroy(ctx context.Context) error {
// wipe from proxy manager cache
if !p.removeFromCacheFn(ctx) {
// no need to destroy on cluster, since the proxy is stale and was already destroyed
return nil
}
// destroy on cluster
request := codec.EncodeClientDestroyProxyRequest(p.name, p.serviceName)
if _, err := p.invokeOnRandomTarget(ctx, request, nil); err != nil {
return fmt.Errorf("error destroying proxy: %w", err)
}
return nil
}
func (p *proxy) removeFromCache(ctx context.Context) bool {
return p.removeFromCacheFn(ctx)
}
func (p *proxy) validateAndSerialize(arg1 interface{}) (iserialization.Data, error) {
if check.Nil(arg1) {
return nil, ihzerrors.NewIllegalArgumentError("nil arg is not allowed", nil)
}
return p.serializationService.ToData(arg1)
}
func (p *proxy) validateAndSerialize2(arg1 interface{}, arg2 interface{}) (arg1Data iserialization.Data,
arg2Data iserialization.Data, err error) {
if check.Nil(arg1) || check.Nil(arg2) {
return nil, nil, ihzerrors.NewIllegalArgumentError("nil arg is not allowed", nil)
}
arg1Data, err = p.serializationService.ToData(arg1)
if err != nil {
return
}
arg2Data, err = p.serializationService.ToData(arg2)
return
}
func (p *proxy) validateAndSerialize3(arg1 interface{}, arg2 interface{}, arg3 interface{}) (arg1Data iserialization.Data,
arg2Data iserialization.Data, arg3Data iserialization.Data, err error) {
if check.Nil(arg1) || check.Nil(arg2) || check.Nil(arg3) {
return nil, nil, nil, ihzerrors.NewIllegalArgumentError("nil arg is not allowed", nil)
}
arg1Data, err = p.serializationService.ToData(arg1)
if err != nil {
return
}
arg2Data, err = p.serializationService.ToData(arg2)
if err != nil {
return
}
arg3Data, err = p.serializationService.ToData(arg3)
return
}
func (p *proxy) validateAndSerializeAggregate(agg aggregate.Aggregator) (arg1Data iserialization.Data, err error) {
if check.Nil(agg) {
return nil, ihzerrors.NewIllegalArgumentError("aggregate should not be nil", nil)
}
arg1Data, err = p.serializationService.ToData(agg)
return
}
func (p *proxy) validateAndSerializePredicate(pred predicate.Predicate) (arg1Data iserialization.Data, err error) {
if check.Nil(pred) {
return nil, ihzerrors.NewIllegalArgumentError("predicate should not be nil", nil)
}
arg1Data, err = p.serializationService.ToData(pred)
return
}
func (p *proxy) validateAndSerializeValues(values []interface{}) ([]iserialization.Data, error) {
valuesData := make([]iserialization.Data, len(values))
for i, value := range values {
if data, err := p.validateAndSerialize(value); err != nil {
return nil, err
} else {
valuesData[i] = data
}
}
return valuesData, nil
}
func (p *proxy) invokeOnKey(ctx context.Context, request *proto.ClientMessage, keyData iserialization.Data) (*proto.ClientMessage, error) {
partitionID, err := p.partitionService.GetPartitionID(keyData)
if err != nil {
return nil, err
}
return p.invoker.InvokeOnPartition(ctx, request, partitionID)
}
func (p *proxy) invokeOnRandomTarget(ctx context.Context, request *proto.ClientMessage, handler proto.ClientMessageHandler) (*proto.ClientMessage, error) {
return p.invoker.InvokeOnRandomTarget(ctx, request, handler)
}
func (p *proxy) invokeOnPartition(ctx context.Context, request *proto.ClientMessage, partitionID int32) (*proto.ClientMessage, error) {
return p.invoker.InvokeOnPartition(ctx, request, partitionID)
}
func (p *proxy) invokeOnPartitionAsync(ctx context.Context, request *proto.ClientMessage, partitionID int32, now time.Time) (invocation.Invocation, error) {
return p.invoker.InvokeOnPartitionAsync(ctx, request, partitionID, now)
}
func (p *proxy) convertToObject(data iserialization.Data) (interface{}, error) {
return p.serializationService.ToObject(data)
}
func (p *proxy) convertToObjects(values []iserialization.Data) ([]interface{}, error) {
decodedValues := make([]interface{}, len(values))
for i, value := range values {
if decodedValue, err := p.convertToObject(value); err != nil {
return nil, err
} else {
decodedValues[i] = decodedValue
}
}
return decodedValues, nil
}
func (p *proxy) convertToData(object interface{}) (iserialization.Data, error) {
return p.serializationService.ToData(object)
}
func (p *proxy) partitionToPairs(keyValuePairs []types.Entry) (map[int32][]proto.Pair, error) {
ps := p.partitionService
partitionToPairs := map[int32][]proto.Pair{}
for _, pair := range keyValuePairs {
if keyData, valueData, err := p.validateAndSerialize2(pair.Key, pair.Value); err != nil {
return nil, err
} else {
if partitionKey, err := ps.GetPartitionID(keyData); err != nil {
return nil, err
} else {
arr := partitionToPairs[partitionKey]
partitionToPairs[partitionKey] = append(arr, proto.NewPair(keyData, valueData))
}
}
}
return partitionToPairs, nil
}
func (p *proxy) convertPairsToEntries(pairs []proto.Pair) ([]types.Entry, error) {
kvPairs := make([]types.Entry, len(pairs))
for i, pair := range pairs {
key, err := p.convertToObject(pair.Key.(iserialization.Data))
if err != nil {
return nil, err
}
value, err := p.convertToObject(pair.Value.(iserialization.Data))
if err != nil {
return nil, err
}
kvPairs[i] = types.Entry{Key: key, Value: value}
}
return kvPairs, nil
}
func (p *proxy) convertPairsToValues(pairs []proto.Pair) ([]interface{}, error) {
values := make([]interface{}, len(pairs))
for i, pair := range pairs {
value, err := p.convertToObject(pair.Value.(iserialization.Data))
if err != nil {
return nil, err
}
values[i] = value
}
return values, nil
}
func (p *proxy) putAll(keyValuePairs []types.Entry, f func(partitionID int32, entries []proto.Pair) cb.Future) error {
if partitionToPairs, err := p.partitionToPairs(keyValuePairs); err != nil {
return err
} else {
// create futures
futures := make([]cb.Future, 0, len(partitionToPairs))
for partitionID, entries := range partitionToPairs {
futures = append(futures, f(partitionID, entries))
}
for _, future := range futures {
if _, err := future.Result(); err != nil {
return err
}
}
return nil
}
}
func (p *proxy) stringToPartitionID(key string) (int32, error) {
idx := strings.Index(key, "@")
if keyData, err := p.convertToData(key[idx+1:]); err != nil {
return 0, err
} else if partitionID, err := p.partitionService.GetPartitionID(keyData); err != nil {
return 0, err
} else {
return partitionID, nil
}
}
func (p *proxy) decodeEntryNotified(binKey, binValue, binOldValue, binMergingValue iserialization.Data) (key, value, oldValue, mergingValue interface{}, err error) {
if key, err = p.convertToObject(binKey); err != nil {
err = fmt.Errorf("invalid key: %w", err)
} else if value, err = p.convertToObject(binValue); err != nil {
err = fmt.Errorf("invalid value: %w", err)
} else if oldValue, err = p.convertToObject(binOldValue); err != nil {
err = fmt.Errorf("invalid oldValue: %w", err)
} else if mergingValue, err = p.convertToObject(binMergingValue); err != nil {
err = fmt.Errorf("invalid mergingValue: %w", err)
}
return
}
func (p *proxy) makeEntryNotifiedListenerHandler(handler EntryNotifiedHandler) entryNotifiedHandler {
return func(binKey, binValue, binOldValue, binMergingValue iserialization.Data, binEventType int32, binUUID types.UUID, affectedEntries int32) {
event, err := p.prepareEntryNotifiedEvent(binKey, binValue, binOldValue, binMergingValue, binEventType, binUUID, affectedEntries)
if err != nil {
p.logger.Errorf("error while preparing entry notified event: %w", err)
return
}
handler(event)
}
}
func (p *proxy) prepareEntryNotifiedEvent(binKey, binValue, binOldValue, binMergingValue iserialization.Data, binEventType int32, binUUID types.UUID, affectedEntries int32) (*EntryNotified, error) {
key, value, oldValue, mergingValue, err := p.decodeEntryNotified(binKey, binValue, binOldValue, binMergingValue)
if err != nil {
return nil, err
}
// prevent panic if member not found
var member pubcluster.MemberInfo
if m := p.clusterService.GetMemberByUUID(binUUID); m != nil {
member = *m
}
eventType := EntryEventType(binEventType)
event := newEntryNotifiedEvent(p.name, member, key, value, oldValue, mergingValue, int(affectedEntries), eventType)
return event, nil
}
func (p *proxy) Name() string {
return p.name
}
type entryNotifiedHandler func(
binKey, binValue, binOldValue, binMergingValue iserialization.Data,
binEventType int32,
binUUID types.UUID,
affectedEntries int32)
func flagsSetOrClear(flags *int32, flag int32, enable bool) {
if enable {
*flags |= flag
} else {
*flags &^= flag
}
}