-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
165 lines (141 loc) · 3.27 KB
/
server.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
package main
import (
"TigerDB/cache"
"TigerDB/client"
"TigerDB/proto"
"context"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"time"
"go.uber.org/zap"
)
type ServerOpts struct {
ListenAddr string
IsLeader bool
LeaderAddr string
}
type Server struct {
ServerOpts
members map[*client.Client]struct{}
cache cache.Cacher
logger *zap.SugaredLogger
}
func NewServer(opts ServerOpts, c cache.Cacher) *Server {
l, _ := zap.NewProduction()
lsugar := l.Sugar()
return &Server{
ServerOpts: opts,
cache: c,
members: make(map[*client.Client]struct{}),
logger: lsugar,
}
}
func (s *Server) Start() error {
ln, err := net.Listen("tcp", s.ListenAddr)
if err != nil {
return fmt.Errorf("listen error: %s", err)
}
if !s.IsLeader && len(s.LeaderAddr) != 0 {
go func() {
if err := s.dialLeader(); err != nil {
log.Println(err)
}
}()
}
s.logger.Infow("server starting", "addr", s.ListenAddr, "leader", s.IsLeader)
for {
conn, err := ln.Accept()
if err != nil {
log.Printf("accept error: %s\n", err)
continue
}
go s.handleConn(conn)
}
}
func (s *Server) dialLeader() error {
conn, err := net.Dial("tcp", s.LeaderAddr)
if err != nil {
return fmt.Errorf("faild to dial the leader: [%s]", s.LeaderAddr)
}
s.logger.Infow("connected to leader", "addr", s.LeaderAddr)
binary.Write(conn, binary.LittleEndian, proto.CmdJoin)
s.handleConn(conn)
return nil
}
func (s *Server) handleConn(conn net.Conn) {
defer conn.Close()
for {
cmd, err := proto.ParseCommand(conn)
if err != nil {
if err == io.EOF {
break
}
log.Println("parse command error: ", err)
break
}
go s.handleCommand(conn, cmd)
}
}
func (s *Server) handleCommand(conn net.Conn, cmd any) {
switch v := cmd.(type) {
case *proto.CommandSet:
s.handleSetCommand(conn, v)
case *proto.CommandGet:
s.handleGetCommand(conn, v)
case *proto.CommandJoin:
s.handleJoinCommand(conn, v)
}
}
func (s *Server) handleJoinCommand(conn net.Conn, cmd *proto.CommandJoin) error {
fmt.Println("member just joined the cluster: ", conn.RemoteAddr())
s.members[client.NewClientFromConn(conn)] = struct{}{}
return nil
}
func (s *Server) handleSetCommand(conn net.Conn, cmd *proto.CommandSet) error {
log.Printf("SET %s to %s", cmd.Key, cmd.Value)
go func() {
for member := range s.members {
if err := member.Set(context.TODO(), cmd.Key, cmd.Value, cmd.TTL); err != nil {
log.Println("write failed on a follower: ", err)
}
}
}()
resp := proto.ResponseSet{}
if err := s.cache.Set(cmd.Key, cmd.Value, time.Duration(cmd.TTL)); err != nil {
resp.Status = proto.StatusError
serialize, _ := resp.Bytes()
_, err := conn.Write(serialize)
return err
}
resp.Status = proto.StatusOK
serialize, _ := resp.Bytes()
_, err := conn.Write(serialize)
return err
}
func (s *Server) handleGetCommand(conn net.Conn, cmd *proto.CommandGet) error {
resp := proto.ResponseGet{}
value, err := s.cache.Get(cmd.Key)
if err != nil {
resp.Status = proto.StatusKeyNotFound
serialize, err := resp.Bytes()
if err != nil {
return err
}
_, err = conn.Write(serialize)
return err
}
resp.Status = proto.StatusOK
resp.Value = value
serialize, err := resp.Bytes()
if err != nil {
return err
}
_, err = conn.Write(serialize)
if err != nil {
return err
}
return nil
}