-
Notifications
You must be signed in to change notification settings - Fork 0
/
pooler.go
284 lines (259 loc) · 7.7 KB
/
pooler.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
package main
import (
"fmt"
"log/slog"
"slices"
"time"
"github.com/livinlefevreloca/pgspanner/utils"
)
const (
CONNECTION_SWEEP_INTERVAL = 5 * time.Second
)
type Pooler struct {
connections []*ServerConnection
clusterConfig *ClusterConfig
databaseConfig *DatabaseConfig
}
func newPooler(
databaseConfig DatabaseConfig,
clusterConfig ClusterConfig,
) *Pooler {
conections := make([]*ServerConnection, 0, databaseConfig.PoolSettings.MaxOpenConns)
return &Pooler{
connections: conections,
databaseConfig: &databaseConfig,
clusterConfig: &clusterConfig,
}
}
func (p *Pooler) getPoolSettings() *PoolConfig {
return &p.databaseConfig.PoolSettings
}
func (p *Pooler) GetAddr() string {
return p.clusterConfig.GetAddr()
}
func (p *Pooler) removeConnection(connection *ServerConnection) {
if len(p.connections) == 0 {
return
}
var index int
for i, conn := range p.connections {
if conn.GetBackendPid() == connection.GetBackendPid() {
index = i
break
}
}
slices.Delete(p.connections, index, index)
}
func (p *Pooler) getConnection(frontendPid int) (*ServerConnection, error) {
connectionCount := len(p.connections)
poolSettings := p.getPoolSettings()
var connection *ServerConnection
var err error
for {
if connectionCount == 0 {
connection, err = CreateServerConnection(p.databaseConfig, p.clusterConfig)
if err != nil {
slog.Error(
"Error creating connection",
"Pooler", p.GetAddr(),
"Error", err,
)
return nil, err
}
break
}
var ptr **ServerConnection
p.connections, ptr = utils.Pop(p.connections)
connection = *ptr
if connection.IsPoisoned() {
connection.Close()
connectionCount = len(p.connections)
} else if connection.GetAge() > int64(poolSettings.MaxConnLifetime) {
slog.Info(
"Closing connection. Connection has exceeded max lifetime",
"Pooler", p.GetAddr(),
"BackendPid", connection.GetBackendPid(),
)
connection.Close()
connectionCount = len(p.connections)
} else {
break
}
}
return connection, nil
}
func (p *Pooler) returnConnection(connection ServerConnection, frontendPid int) {
poolSettings := p.getPoolSettings()
if len(p.connections) < poolSettings.MaxOpenConns {
slog.Info(
"Returning connection",
"Pooler", p.GetAddr(),
"BackendPid", connection.GetBackendPid(),
)
p.connections = append(p.connections, &connection)
} else if connection.GetAge() > int64(poolSettings.MaxConnLifetime) {
slog.Info(
"Closing connection. Connection has exceeded max lifetime",
"Pooler", p.GetAddr(),
"BackendPid", connection.GetBackendPid(),
)
connection.Close()
} else {
slog.Info(
"Closing connection. Pool is full",
"Pooler", p.GetAddr(),
"BackendPid", connection.GetBackendPid(),
)
connection.Close()
}
}
func (p *Pooler) CloseConnection(connection *ServerConnection, frontendPid int) {
connection.Close()
}
type PoolerManager struct {
poolers map[string]map[string]*Pooler
ConnectionServer *ConnectionRequester
connectionTable map[int][]ServerProcessIdentity
}
func NewPoolerManager(config *SpannerConfig, server *ConnectionRequester) *PoolerManager {
poolers := make(map[string]map[string]*Pooler)
for _, database := range config.Databases {
for _, cluster := range database.Clusters {
if poolers[database.Name] == nil {
poolers[database.Name] = make(map[string]*Pooler)
}
poolers[database.Name][cluster.GetAddr()] = newPooler(database, cluster)
}
}
return &PoolerManager{
poolers: poolers,
ConnectionServer: server,
}
}
func (pm *PoolerManager) SendConnection(request ConnectionRequest) {
pooler := pm.poolers[request.database][request.clusterAddr]
var response ConnectionResponse
slog.Info(
"Received connection request for cluster",
"cluster", request.clusterAddr,
"database", request.database,
)
connection, err := pooler.getConnection(request.FrontendPid)
if err != nil {
response = ConnectionResponse{
Event: ACTION_GET_CONNECTION,
Result: RESULT_ERROR,
Detail: err,
Conn: nil,
}
request.responder <- response
return
}
response = ConnectionResponse{
Event: ACTION_GET_CONNECTION,
Conn: connection,
}
if pm.connectionTable == nil {
pm.connectionTable = make(map[int][]ServerProcessIdentity)
}
if pm.connectionTable[request.FrontendPid] == nil {
pm.connectionTable[request.FrontendPid] = make([]ServerProcessIdentity, 0)
}
pm.connectionTable[request.FrontendPid] = append(pm.connectionTable[request.FrontendPid], connection.GetServerIdentity())
request.responder <- response
}
func (pm *PoolerManager) CloseConnection(request ConnectionRequest) {
pooler := pm.poolers[request.database][request.clusterAddr]
if request.Connection == nil {
slog.Error(
"Received nil connection in CloseConnection",
"cluster", request.clusterAddr,
"database", request.database,
)
return
}
if pm.connectionTable == nil {
pm.connectionTable = make(map[int][]ServerProcessIdentity)
}
if pm.connectionTable[request.FrontendPid] == nil {
pm.connectionTable[request.FrontendPid] = make([]ServerProcessIdentity, 0)
}
pm.connectionTable[request.FrontendPid] = utils.DeleteFromUnsorted[ServerProcessIdentity](
pm.connectionTable[request.FrontendPid],
request.Connection.GetServerIdentity(),
)
pooler.CloseConnection(request.Connection, request.FrontendPid)
}
func (pm *PoolerManager) ReturnConnection(request ConnectionRequest) {
pooler := pm.poolers[request.database][request.clusterAddr]
if request.Connection == nil {
slog.Error(
"Received nil connection in ReturnConnection",
"cluster", request.clusterAddr,
"database", request.database,
)
return
}
if pm.connectionTable == nil {
pm.connectionTable = make(map[int][]ServerProcessIdentity)
}
if pm.connectionTable[request.FrontendPid] == nil {
pm.connectionTable[request.FrontendPid] = make([]ServerProcessIdentity, 0)
}
pm.connectionTable[request.FrontendPid] = utils.DeleteFromUnsorted[ServerProcessIdentity](
pm.connectionTable[request.FrontendPid],
request.Connection.GetServerIdentity(),
)
pooler.returnConnection(*request.Connection, request.FrontendPid)
}
type ConnectionMappingNotFound struct {
FrontendPid int
}
func (e ConnectionMappingNotFound) Error() string {
return fmt.Sprintf("No connections found for FrontendPid: %d", e.FrontendPid)
}
func (pm *PoolerManager) SendConnectionMapping(request ConnectionRequest) {
if pm.connectionTable == nil {
pm.connectionTable = make(map[int][]ServerProcessIdentity)
}
var serverIdentities []ServerProcessIdentity
var result string
serverIdentities, ok := pm.connectionTable[request.FrontendPid]
if !ok {
serverIdentities = make([]ServerProcessIdentity, 0)
result = RESULT_ERROR
} else {
result = RESULT_SUCCESS
}
response := ConnectionResponse{
Event: ACTION_GET_CONNECTION_MAPPING,
Result: result,
Detail: ConnectionMappingNotFound{FrontendPid: request.FrontendPid},
ConnMapping: serverIdentities,
}
request.responder <- response
}
func RunPoolManager(config *SpannerConfig, keepAlive *KeepAlive, connectionReqester *ConnectionRequester) {
// Start the pool manager
poolManager := NewPoolerManager(config, connectionReqester)
timeout := time.After(CONNECTION_SWEEP_INTERVAL)
for {
select {
case request := <-connectionReqester.ReceiveConnectionRequest():
slog.Info("Received connection request", "action", request.Event)
switch request.Event {
case ACTION_GET_CONNECTION:
poolManager.SendConnection(*request)
case ACTION_RETURN_CONNECTION:
poolManager.ReturnConnection(*request)
case ACTION_CLOSE_CONNECTION:
poolManager.CloseConnection(*request)
case ACTION_GET_CONNECTION_MAPPING:
poolManager.SendConnectionMapping(*request)
}
case <-timeout:
keepAlive.Notify()
timeout = time.After(CONNECTION_SWEEP_INTERVAL)
}
}
}