-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathnode.go
More file actions
118 lines (99 loc) · 3.06 KB
/
Copy pathnode.go
File metadata and controls
118 lines (99 loc) · 3.06 KB
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
package node
import (
"context"
"fmt"
"github.com/rs/zerolog"
"github.com/tomarrell/lbadd/internal/engine"
"github.com/tomarrell/lbadd/internal/network"
"github.com/tomarrell/lbadd/internal/raft"
"github.com/tomarrell/lbadd/internal/raft/cluster"
"github.com/tomarrell/lbadd/internal/raft/message"
"golang.org/x/sync/errgroup"
)
// Node is a database node. It uses an underlying raft.Server to communicate
// with other nodes, if any.
type Node struct {
log zerolog.Logger
engine engine.Engine
raft raft.Server
cluster cluster.Cluster
}
// New creates a new node that is executing commands on the given executor.
func New(log zerolog.Logger) *Node {
return &Node{
log: log,
}
}
// ListenAndServe starts the node on the given address. The given context must
// be used to stop the server, since there is no stop function. Canceling the
// context or a context timeout will cause the server to attempt a graceful
// shutdown.
func (n *Node) ListenAndServe(ctx context.Context, addr string) error {
n.log.Info().
Str("addr", addr).
Msg("listen and serve")
return fmt.Errorf("unimplemented")
}
// Open opens a new cluster, making this node the only node in the cluster.
// Other clusters can connect to the given address and perform the implemented
// handshake, in order to become nodes in the cluster.
func (n *Node) Open(ctx context.Context, addr string) error {
n.log.Info().
Str("addr", addr).
Msg("open")
if err := n.openCluster(ctx, addr); err != nil {
return fmt.Errorf("open cluster: %w", err)
}
return n.startNode(ctx)
}
// Close closes the node, starting with the underlying raft server, then the
// cluster, then the executor.
func (n *Node) Close() error {
ctx := context.TODO()
errs, _ := errgroup.WithContext(ctx)
errs.Go(n.raft.Close)
errs.Go(n.cluster.Close)
errs.Go(n.engine.Close)
return errs.Wait()
}
func (n *Node) openCluster(ctx context.Context, addr string) error {
if n.cluster != nil {
return ErrOpen
}
cluster := cluster.NewTCPCluster(n.log)
cluster.OnConnect(n.performLogonHandshake)
if err := cluster.Open(ctx, addr); err != nil {
return fmt.Errorf("open cluster: %w", err)
}
return nil
}
func (n *Node) performLogonHandshake(cluster cluster.Cluster, conn network.Conn) {
n.log.Debug().
Str("conn-id", conn.RemoteID().String()).
Msg("perform handshake")
n.log.Info().
Str("conn-id", conn.RemoteID().String()).
Msg("connected")
cluster.AddConnection(conn)
}
func (n *Node) startNode(ctx context.Context) error {
n.raft = raft.NewServer(n.log, n.cluster)
n.raft.OnReplication(n.replicate)
return n.raft.Start(ctx)
}
// replicate returns the number of commands that were executed from the
// given slice of commands. -1 is returned if no commands were executed.
func (n *Node) replicate(input []*message.Command) int {
for i := range input {
cmd := message.ConvertMessageToCommand(input[i])
// Link to the engine's executor must be added here.
_, err := n.engine.Evaluate(cmd)
if err != nil {
n.log.Error().
Err(err).
Msg("failed to replicate input: execute")
return i - 1
}
}
return len(input)
}