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 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
package protocol
import (
"context"
"encoding/binary"
"fmt"
"io"
"net"
"sort"
"sync"
"time"
"github.com/Rican7/retry"
"github.com/canonical/go-dqlite/v2/logging"
"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)
// MaxConcurrentLeaderConns is the default maximum number of concurrent requests to other cluster members to probe for leadership.
const MaxConcurrentLeaderConns int64 = 10
// DialFunc is a function that can be used to establish a network connection.
type DialFunc func(context.Context, string) (net.Conn, error)
// LeaderTracker remembers the address of the cluster leader, and possibly
// holds a reusable connection to it.
type LeaderTracker struct {
mu sync.RWMutex
lastKnownLeaderAddr string
proto *Protocol
}
func (lt *LeaderTracker) GetLeaderAddr() string {
lt.mu.RLock()
defer lt.mu.RUnlock()
return lt.lastKnownLeaderAddr
}
func (lt *LeaderTracker) SetLeaderAddr(address string) {
lt.mu.Lock()
defer lt.mu.Unlock()
lt.lastKnownLeaderAddr = address
}
func (lt *LeaderTracker) UnsetLeaderAddr() {
lt.mu.Lock()
defer lt.mu.Unlock()
lt.lastKnownLeaderAddr = ""
}
func (lt *LeaderTracker) TakeSharedProtocol() (proto *Protocol) {
lt.mu.Lock()
defer lt.mu.Unlock()
if proto, lt.proto = lt.proto, nil; proto != nil {
proto.lt = lt
}
return
}
func (lt *LeaderTracker) DonateSharedProtocol(proto *Protocol) (accepted bool) {
lt.mu.Lock()
defer lt.mu.Unlock()
if accepted = lt.proto == nil; accepted {
lt.proto = proto
}
return
}
type Connector struct {
clientID uint64 // Conn ID to use when registering against the server.
store NodeStore
nodeID uint64
nodeAddress string
lt *LeaderTracker
config Config // Connection parameters.
log logging.Func // Logging function.
}
// NewConnector returns a Connector that will connect to the current cluster
// leader.
func NewLeaderConnector(store NodeStore, config Config, log logging.Func) *Connector {
if config.Dial == nil {
config.Dial = Dial
}
if config.DialTimeout == 0 {
config.DialTimeout = 5 * time.Second
}
if config.AttemptTimeout == 0 {
config.AttemptTimeout = 15 * time.Second
}
if config.BackoffFactor == 0 {
config.BackoffFactor = 100 * time.Millisecond
}
if config.BackoffCap == 0 {
config.BackoffCap = time.Second
}
if config.ConcurrentLeaderConns == 0 {
config.ConcurrentLeaderConns = MaxConcurrentLeaderConns
}
return &Connector{
store: store,
lt: &LeaderTracker{},
config: config,
log: log,
}
}
// NewDirectConnector returns a Connector that will connect to the node with
// the given ID and address.
func NewDirectConnector(id uint64, address string, config Config, log logging.Func) *Connector {
if config.Dial == nil {
config.Dial = Dial
}
if config.DialTimeout == 0 {
config.DialTimeout = 5 * time.Second
}
if config.AttemptTimeout == 0 {
config.AttemptTimeout = 15 * time.Second
}
if config.BackoffFactor == 0 {
config.BackoffFactor = 100 * time.Millisecond
}
if config.BackoffCap == 0 {
config.BackoffCap = time.Second
}
if config.ConcurrentLeaderConns == 0 {
config.ConcurrentLeaderConns = MaxConcurrentLeaderConns
}
return &Connector{
nodeID: id,
nodeAddress: address,
lt: &LeaderTracker{},
config: config,
log: log,
}
}
// Connect opens a new Protocol based on the Connector's configuration.
func (c *Connector) Connect(ctx context.Context) (*Protocol, error) {
if c.nodeID != 0 {
ctx, cancel := context.WithTimeout(ctx, c.config.AttemptTimeout)
defer cancel()
conn, err := c.config.Dial(ctx, c.nodeAddress)
if err != nil {
return nil, errors.Wrap(err, "dial")
}
version := VersionOne
protocol, err := Handshake(ctx, conn, version, c.nodeAddress)
if err == errBadProtocol {
c.log(logging.Warn, "unsupported protocol %d, attempt with legacy", version)
version = VersionLegacy
protocol, err = Handshake(ctx, conn, version, c.nodeAddress)
}
if err != nil {
conn.Close()
return nil, errors.Wrap(err, "handshake")
}
return protocol, nil
}
if c.config.PermitShared {
if sharedProto := c.lt.TakeSharedProtocol(); sharedProto != nil {
if leaderAddr, err := askLeader(ctx, sharedProto); err == nil && sharedProto.addr == leaderAddr {
c.log(logging.Debug, "reusing shared connection to %s", sharedProto.addr)
c.lt.SetLeaderAddr(leaderAddr)
return sharedProto, nil
}
c.log(logging.Debug, "discarding shared connection to %s", sharedProto.addr)
sharedProto.Bad()
sharedProto.Close()
}
}
var protocol *Protocol
err := retry.Retry(func(attempt uint) error {
log := func(l logging.Level, format string, a ...interface{}) {
format = fmt.Sprintf("attempt %d: ", attempt) + format
c.log(l, format, a...)
}
if attempt > 1 {
select {
case <-ctx.Done():
// Stop retrying
return nil
default:
}
}
var err error
protocol, err = c.connectAttemptAll(ctx, log)
return err
}, c.config.RetryStrategies()...)
if err != nil || ctx.Err() != nil {
return nil, ErrNoAvailableLeader
}
// At this point we should have a connected protocol object, since the
// retry loop didn't hit any error and the given context hasn't
// expired.
if protocol == nil {
panic("no protocol object")
}
c.lt.SetLeaderAddr(protocol.addr)
if c.config.PermitShared {
protocol.lt = c.lt
}
return protocol, nil
}
// connectAttemptAll tries to establish a new connection to the cluster leader.
//
// First, if the address of the last known leader has been recorded, try
// to connect to that server and confirm its leadership. This is a fast path
// for stable clusters that avoids opening lots of connections. If that fails,
// fall back to probing all servers in parallel, checking whether each
// is the leader itself or knows who the leader is.
func (c *Connector) connectAttemptAll(ctx context.Context, log logging.Func) (*Protocol, error) {
if addr := c.lt.GetLeaderAddr(); addr != "" {
// TODO In the event of failure, we could still use the second
// return value to guide the next stage of the search.
if proto, _, _ := c.connectAttemptOne(ctx, addr, log); proto != nil {
log(logging.Debug, "server %s: connected on fast path", addr)
return proto, nil
}
c.lt.UnsetLeaderAddr()
}
servers, err := c.store.Get(ctx)
if err != nil {
return nil, errors.Wrap(err, "get servers")
}
// Probe voters before standbys before spares. Only voters can potentially
// be the leader, and standbys are more likely to know who the leader is
// than spares since they participate more in the cluster.
sort.Slice(servers, func(i, j int) bool {
return servers[i].Role < servers[j].Role
})
// The new context will be cancelled when we successfully connect
// to the leader.
ctx, cancel := context.WithCancel(ctx)
defer cancel()
leaderCh := make(chan *Protocol)
sem := semaphore.NewWeighted(c.config.ConcurrentLeaderConns)
wg := &sync.WaitGroup{}
wg.Add(len(servers))
go func() {
wg.Wait()
close(leaderCh)
}()
for _, server := range servers {
go func(server NodeInfo) {
defer wg.Done()
if err := sem.Acquire(ctx, 1); err != nil {
log(logging.Warn, "server %s: %v", server.Address, err)
return
}
defer sem.Release(1)
protocol, leader, err := c.connectAttemptOne(ctx, server.Address, log)
if err != nil {
log(logging.Warn, "server %s: %v", server.Address, err)
return
} else if protocol != nil {
leaderCh <- protocol
return
} else if leader == "" {
log(logging.Warn, "server %s: no known leader", server.Address)
return
}
// Try the server that the original server thinks is the leader.
log(logging.Debug, "server %s: connect to reported leader %s", server.Address, leader)
protocol, _, err = c.connectAttemptOne(ctx, leader, log)
if err != nil {
log(logging.Warn, "server %s: %v", leader, err)
return
} else if protocol == nil {
log(logging.Warn, "server %s: reported leader server is not the leader", leader)
return
}
leaderCh <- protocol
}(server)
}
leader, ok := <-leaderCh
cancel()
if !ok {
return nil, ErrNoAvailableLeader
}
log(logging.Debug, "server %s: connected on fallback path", leader.addr)
for extra := range leaderCh {
extra.Close()
}
return leader, nil
}
// Perform the initial handshake using the given protocol version.
func Handshake(ctx context.Context, conn net.Conn, version uint64, addr string) (*Protocol, error) {
// Latest protocol version.
protocol := make([]byte, 8)
binary.LittleEndian.PutUint64(protocol, version)
// Honor the ctx deadline, if present.
if deadline, ok := ctx.Deadline(); ok {
conn.SetDeadline(deadline)
defer conn.SetDeadline(time.Time{})
}
// Perform the protocol handshake.
n, err := conn.Write(protocol)
if err != nil {
return nil, errors.Wrap(err, "write handshake")
}
if n != 8 {
return nil, errors.Wrap(io.ErrShortWrite, "short handshake write")
}
return &Protocol{conn: conn, version: version, addr: addr}, nil
}
// Connect to the given dqlite server and check if it's the leader.
//
// Return values:
//
// - Any failure is hit: -> nil, "", err
// - Target not leader and no leader known: -> nil, "", nil
// - Target not leader and leader known: -> nil, leader, nil
// - Target is the leader: -> server, "", nil
func (c *Connector) connectAttemptOne(
ctx context.Context,
address string,
origLog logging.Func,
) (*Protocol, string, error) {
log := func(l logging.Level, format string, a ...interface{}) {
format = fmt.Sprintf("server %s: ", address) + format
origLog(l, format, a...)
}
if ctx.Err() != nil {
return nil, "", ctx.Err()
}
ctx, cancel := context.WithTimeout(ctx, c.config.AttemptTimeout)
defer cancel()
dialCtx, cancel := context.WithTimeout(ctx, c.config.DialTimeout)
defer cancel()
// Establish the connection.
conn, err := c.config.Dial(dialCtx, address)
if err != nil {
return nil, "", errors.Wrap(err, "dial")
}
version := VersionOne
protocol, err := Handshake(ctx, conn, version, address)
if err == errBadProtocol {
log(logging.Warn, "unsupported protocol %d, attempt with legacy", version)
version = VersionLegacy
protocol, err = Handshake(ctx, conn, version, address)
}
if err != nil {
conn.Close()
return nil, "", err
}
leader, err := askLeader(ctx, protocol)
if err != nil {
protocol.Close()
return nil, "", err
}
switch leader {
case "":
// Currently this server does not know about any leader.
protocol.Close()
return nil, "", nil
case address:
// This server is the leader, register ourselves and return.
request := Message{}
request.Init(16)
response := Message{}
response.Init(512)
EncodeClient(&request, c.clientID)
if err := protocol.Call(ctx, &request, &response); err != nil {
protocol.Close()
return nil, "", err
}
_, err := DecodeWelcome(&response)
if err != nil {
protocol.Close()
return nil, "", err
}
// TODO: enable heartbeat
// protocol.heartbeatTimeout = time.Duration(heartbeatTimeout) * time.Millisecond
// go protocol.heartbeat()
return protocol, "", nil
default:
// This server claims to know who the current leader is.
protocol.Close()
return nil, leader, nil
}
}
// TODO move client logic including Leader method to Protocol,
// and get rid of this.
func askLeader(ctx context.Context, protocol *Protocol) (string, error) {
request := Message{}
request.Init(16)
response := Message{}
response.Init(512)
EncodeLeader(&request)
if err := protocol.Call(ctx, &request, &response); err != nil {
cause := errors.Cause(err)
// Best-effort detection of a pre-1.0 dqlite node: when sent
// version 1 it should close the connection immediately.
if err, ok := cause.(*net.OpError); ok && !err.Timeout() || cause == io.EOF {
return "", errBadProtocol
}
return "", err
}
_, leader, err := DecodeNodeCompat(protocol, &response)
if err != nil {
return "", err
}
return leader, nil
}
var errBadProtocol = fmt.Errorf("bad protocol")
|