-
Notifications
You must be signed in to change notification settings - Fork 199
/
connection_heap.go
280 lines (237 loc) · 6.12 KB
/
connection_heap.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
// 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 (
"runtime"
"sync"
)
// singleConnectionHeap is a non-blocking LIFO heap.
// If the heap is empty, nil is returned.
// if the heap is full, offer will return false
type singleConnectionHeap struct {
head, tail uint32
data []*Connection
size uint32
full bool
mutex sync.Mutex
}
// newSingleConnectionHeap creates a new heap with initial size.
func newSingleConnectionHeap(size int) *singleConnectionHeap {
if size <= 0 {
panic("Heap size cannot be less than 1")
}
return &singleConnectionHeap{
full: false,
data: make([]*Connection, uint32(size)),
size: uint32(size),
}
}
func (h *singleConnectionHeap) cleanup() {
h.mutex.Lock()
defer h.mutex.Unlock()
for i := range h.data {
if h.data[i] != nil {
h.data[i].Close()
}
h.data[i] = nil
}
// make sure offer and poll both fail
h.data = nil
h.full = true
h.head = 0
h.tail = 0
}
// Offer adds an item to the heap unless the heap is full.
// In case the heap is full, the item will not be added to the heap
// and false will be returned
func (h *singleConnectionHeap) Offer(conn *Connection) bool {
h.mutex.Lock()
// make sure heap is not full or cleaned up
if h.full || len(h.data) == 0 {
h.mutex.Unlock()
return false
}
h.head = (h.head + 1) % h.size
h.full = (h.head == h.tail)
h.data[h.head] = conn
h.mutex.Unlock()
return true
}
// Poll removes and returns an item from the heap.
// If the heap is empty, nil will be returned.
func (h *singleConnectionHeap) Poll() (res *Connection) {
h.mutex.Lock()
// the heap has been cleaned up
if len(h.data) == 0 {
h.mutex.Unlock()
return nil
}
// if heap is not empty
if (h.tail != h.head) || h.full {
res = h.data[h.head]
h.data[h.head] = nil
h.full = false
if h.head == 0 {
h.head = h.size - 1
} else {
h.head--
}
}
h.mutex.Unlock()
return res
}
// DropIdleTail closes idle connection in tail.
// It will return true if tail connection was idle and dropped
func (h *singleConnectionHeap) DropIdleTail() bool {
h.mutex.Lock()
defer h.mutex.Unlock()
// the heap has been cleaned up
if h.data == nil {
return false
}
// if heap is not empty
if h.full || (h.tail != h.head) {
conn := h.data[(h.tail+1)%h.size]
if conn.IsConnected() && !conn.isIdle() {
return false
}
h.tail = (h.tail + 1) % h.size
h.data[h.tail] = nil
h.full = false
if conn.node != nil {
conn.node.stats.ConnectionsIdleDropped.IncrementAndGet()
}
conn.Close()
return true
}
return false
}
// Len returns the number of connections in the heap
func (h *singleConnectionHeap) Len() int {
cnt := 0
h.mutex.Lock()
if !h.full {
if h.head >= h.tail {
cnt = int(h.head) - int(h.tail)
} else {
cnt = int(h.size) - (int(h.tail) - int(h.head))
}
} else {
cnt = int(h.size)
}
h.mutex.Unlock()
return cnt
}
// connectionHeap is a non-blocking FIFO heap.
// If the heap is empty, nil is returned.
// if the heap is full, offer will return false
type connectionHeap struct {
maxSize int
minSize int
heaps []singleConnectionHeap
}
// Close cleans up all the data and removes all the references from
// active objects to ensure GC cleans up everything.
func (h *connectionHeap) cleanup() {
for i := range h.heaps {
h.heaps[i].cleanup()
}
}
func newConnectionHeap(minSize, maxSize int) *connectionHeap {
if minSize > maxSize {
panic("minSize is bigger than maxSize for connection heap")
}
heapCount := runtime.NumCPU()
if heapCount > maxSize {
heapCount = maxSize
}
// will be >= 1
perHeapSize := maxSize / heapCount
heaps := make([]singleConnectionHeap, heapCount)
for i := range heaps {
heaps[i] = *newSingleConnectionHeap(perHeapSize)
}
// add a heap for the remainder
remainder := maxSize - heapCount*perHeapSize
if remainder > 0 {
heaps = append(heaps, *newSingleConnectionHeap(remainder))
}
return &connectionHeap{
maxSize: maxSize,
minSize: minSize,
heaps: heaps,
}
}
// Offer adds an item to the heap unless the heap is full.
// In case the heap is full, the item will not be added to the heap
// and false will be returned
func (h *connectionHeap) Offer(conn *Connection, hint byte) bool {
idx := int(hint) % len(h.heaps)
end := idx + len(h.heaps)
for i := idx; i < end; i++ {
if h.heaps[i%len(h.heaps)].Offer(conn) {
// success
return true
}
}
return false
}
// Poll removes and returns an item from the heap.
// If the heap is empty, nil will be returned.
func (h *connectionHeap) Poll(hint byte) (res *Connection) {
idx := int(hint)
end := idx + len(h.heaps)
for i := idx; i < end; i++ {
if conn := h.heaps[i%len(h.heaps)].Poll(); conn != nil {
return conn
}
}
return nil
}
// DropIdle closes all idle connections.
// It will only drop connections if there are
// at least ClientPolicy.MinConnectionPerNode available
func (h *connectionHeap) DropIdle() {
// decide how many conns are allowed to drop
// in minSize is 0, up to all connection can
// be closed if idle
excessCount := h.LenAll() - h.minSize
if excessCount <= 0 {
return
}
for i := 0; i < len(h.heaps); i++ {
for h.heaps[i].DropIdleTail() {
excessCount--
if excessCount == 0 {
return
}
}
}
}
// Cap returns the total capacity of the connectionHeap
func (h *connectionHeap) Cap() int {
return h.maxSize
}
// Len returns the number of connections in a specific sub-heap.
func (h *connectionHeap) Len(hint byte) (cnt int) {
return h.heaps[hint].Len()
}
// LenAll returns the number of connections in all sub-heaps.
func (h *connectionHeap) LenAll() int {
cnt := 0
for i := range h.heaps {
cnt += h.heaps[i].Len()
}
return cnt
}