forked from hazelcast/hazelcast-go-client
/
partition_service.go
161 lines (143 loc) · 4.7 KB
/
partition_service.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
/*
* Copyright (c) 2008-2021, 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 cluster
import (
"fmt"
"reflect"
"sync"
"sync/atomic"
"github.com/JorgenPo/hazelcast-go-client/hzerrors"
"github.com/JorgenPo/hazelcast-go-client/internal/event"
ilogger "github.com/JorgenPo/hazelcast-go-client/internal/logger"
"github.com/JorgenPo/hazelcast-go-client/internal/proto"
iserialization "github.com/JorgenPo/hazelcast-go-client/internal/serialization"
"github.com/JorgenPo/hazelcast-go-client/internal/util/murmur"
"github.com/JorgenPo/hazelcast-go-client/types"
)
type PartitionServiceCreationBundle struct {
EventDispatcher *event.DispatchService
Logger ilogger.Logger
}
func (b PartitionServiceCreationBundle) Check() {
if b.EventDispatcher == nil {
panic("EventDispatcher is nil")
}
if b.Logger == nil {
panic("Logger is nil")
}
}
type PartitionService struct {
logger ilogger.Logger
eventDispatcher *event.DispatchService
partitionTable partitionTable
partitionCount int32
}
func NewPartitionService(bundle PartitionServiceCreationBundle) *PartitionService {
bundle.Check()
return &PartitionService{
eventDispatcher: bundle.EventDispatcher,
partitionTable: defaultPartitionTable(),
logger: bundle.Logger,
}
}
func (s *PartitionService) GetPartitionOwner(partitionId int32) (types.UUID, bool) {
return s.partitionTable.GetOwnerUUID(partitionId)
}
func (s *PartitionService) PartitionCount() int32 {
return atomic.LoadInt32(&s.partitionCount)
}
func (s *PartitionService) GetPartitionID(keyData *iserialization.Data) (int32, error) {
if count := s.PartitionCount(); count == 0 {
// Partition count can not be zero for the sync mode.
// On the sync mode, we are waiting for the first connection to be established.
// We are initializing the partition count with the value coming from the server with authentication.
// This exception is used only for async mode client.
return 0, hzerrors.ErrClientOffline
} else {
return murmur.HashToIndex(keyData.PartitionHash(), count), nil
}
}
func (s *PartitionService) Update(connID int64, partitions []proto.Pair, version int32) {
if s.partitionTable.Update(partitions, version, connID) {
s.logger.Debug(func() string { return "partitions updated" })
}
}
func (s *PartitionService) Reset() {
s.partitionTable.Reset()
}
func (s *PartitionService) checkAndSetPartitionCount(newPartitionCount int32) error {
if atomic.CompareAndSwapInt32(&s.partitionCount, 0, newPartitionCount) {
return nil
}
if atomic.LoadInt32(&s.partitionCount) != newPartitionCount {
return fmt.Errorf("client cannot work with this cluster because it has different partition count, expected %d, got %d: %w",
atomic.LoadInt32(&s.partitionCount), newPartitionCount, hzerrors.ErrClientNotAllowedInCluster)
}
return nil
}
type partitionTable struct {
partitions map[int32]types.UUID
mu *sync.RWMutex
connectionID int64
partitionStateVersion int32
}
func (p *partitionTable) Update(pairs []proto.Pair, version int32, connectionID int64) bool {
p.mu.Lock()
defer p.mu.Unlock()
cantApply := len(pairs) == 0 || p.connectionID == connectionID && version <= p.partitionStateVersion
if cantApply {
return false
}
newPartitions := map[int32]types.UUID{}
for _, pair := range pairs {
uuids := pair.Key().([]types.UUID)
ids := pair.Value().([]int32)
for _, uuid := range uuids {
for _, id := range ids {
newPartitions[id] = uuid
}
}
}
if reflect.DeepEqual(p.partitions, newPartitions) {
return false
}
p.partitions = newPartitions
p.partitionStateVersion = version
p.connectionID = connectionID
return true
}
func (p *partitionTable) GetOwnerUUID(partitionID int32) (types.UUID, bool) {
p.mu.RLock()
defer p.mu.RUnlock()
if uuid, ok := p.partitions[partitionID]; ok {
return uuid, true
}
return types.UUID{}, false
}
func (p *partitionTable) Reset() {
p.mu.Lock()
p.partitionStateVersion = -1
p.partitions = map[int32]types.UUID{}
p.connectionID = 0
p.mu.Unlock()
}
func defaultPartitionTable() partitionTable {
return partitionTable{
partitionStateVersion: -1,
partitions: map[int32]types.UUID{},
mu: &sync.RWMutex{},
}
}