File size: 2,914 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package swo

import (
	"encoding/base64"
	"fmt"
	"strings"

	"github.com/google/uuid"
)

// ConnInfo contains information about a connection to the DB for SWO.
//
// This is stored as the `application_name` for a connection in Postgres in
// the format of "GoAlert <version> SWO:<type>:<id>" where id is a base64
// encoded UUID that should match what ends up in a `switchover_log` hello message.
type ConnInfo struct {
	Version string
	Type    ConnType
	ID      uuid.UUID
}

// ConnType indicates a type of SWO connection.
type ConnType byte

const (
	// ConnTypeMainMgr is the connection pool to the main/old DB used to coordinate the switchover.
	ConnTypeMainMgr ConnType = iota + 'A'

	// ConnTypeMainApp is the connection pool used by the GoAlert application to the main/old DB.
	//
	// Connections here are protected with a shared advisory lock.
	ConnTypeMainApp

	// ConnTypeNextMgr is the connection pool to the next/new DB used for applying changes during the switchover.
	ConnTypeNextMgr

	// ConnTypeNextApp is the connection pool used by the GoAlert application to the next/new DB, after the switchover is completed.
	ConnTypeNextApp
)

// IsNext returns true if the connection is for the next DB.
func (t ConnType) IsNext() bool {
	return t == ConnTypeNextMgr || t == ConnTypeNextApp
}

// IsValid returns true if the ConnType is valid.
func (t ConnType) IsValid() bool {
	return t >= ConnTypeMainMgr && t <= ConnTypeNextApp
}

// String returns a string representation of the ConnInfo.
func (c ConnInfo) String() string {
	// ensure c.Version is <= 24 characters
	if len(c.Version) > 24 {
		c.Version = c.Version[:24]
	}

	if !c.Type.IsValid() {
		panic(fmt.Sprintf("invalid connection type: 0x%0x", c.Type))
	}

	id := base64.RawURLEncoding.EncodeToString(c.ID[:])
	return fmt.Sprintf("GoAlert %s SWO:%c:%s", c.Version, c.Type, id)
}

// ParseConnInfo parses a connection string into a ConnInfo.
func ParseConnInfo(s string) (*ConnInfo, error) {
	if !strings.HasPrefix(s, "GoAlert ") {
		return nil, fmt.Errorf("missing 'GoAlert' prefix: %q", s)
	}
	s = strings.TrimPrefix(s, "GoAlert ")

	parts := strings.Split(s, ":")
	if len(parts) != 3 {
		return nil, fmt.Errorf("incorrect number of segments: %q", s)
	}

	if !strings.HasSuffix(parts[0], " SWO") {
		return nil, fmt.Errorf("missing 'SWO' suffix: %q", s)
	}
	parts[0] = strings.TrimSuffix(parts[0], " SWO")

	var info ConnInfo
	info.Version = parts[0]

	if len(parts[1]) != 1 {
		return nil, fmt.Errorf("invalid connection type: %q", s)
	}
	info.Type = ConnType(parts[1][0])
	if !info.Type.IsValid() {
		return nil, fmt.Errorf("invalid connection type: %q", s)
	}

	id, err := base64.RawURLEncoding.DecodeString(parts[2])
	if err != nil {
		return nil, fmt.Errorf("invalid ID '%s': %w", parts[2], err)
	}
	if len(id) != 16 {
		return nil, fmt.Errorf("invalid ID '%s': incorrect length", parts[2])
	}
	copy(info.ID[:], id)

	return &info, nil
}