-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
190 lines (168 loc) · 3.98 KB
/
client.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
package ecRedis
import (
"net"
"time"
"github.com/buraksezer/consistent"
"github.com/klauspost/reedsolomon"
"github.com/mason-leap-lab/redeo/resp"
"github.com/seiflotfy/cuckoofilter"
)
type Conn struct {
conn net.Conn
W *resp.RequestWriter
R resp.ResponseReader
}
func (c *Conn) Close() {
c.conn.Close()
}
type DataEntry struct {
Cmd string
ReqId string
Begin time.Time
ReqLatency time.Duration
RecLatency time.Duration
Duration time.Duration
AllGood bool
Corrupted bool
}
type Client struct {
//ConnArr []net.Conn
//W []*resp.RequestWriter
//R []resp.ResponseReader
Conns map[string][]*Conn
EC reedsolomon.Encoder
MappingTable map[string]*cuckoo.Filter
Ring *consistent.Consistent
Data DataEntry
DataShards int
ParityShards int
Shards int
}
func NewClient(dataShards int, parityShards int, ecMaxGoroutine int) *Client {
return &Client{
//ConnArr: make([]net.Conn, dataShards+parityShards),
//W: make([]*resp.RequestWriter, dataShards+parityShards),
//R: make([]resp.ResponseReader, dataShards+parityShards),
Conns: make(map[string][]*Conn),
EC: NewEncoder(dataShards, parityShards, ecMaxGoroutine),
MappingTable: make(map[string]*cuckoo.Filter),
DataShards: dataShards,
ParityShards: parityShards,
Shards: dataShards + parityShards,
}
}
func (c *Client) Dial(addrArr []string) bool {
//t0 := time.Now()
members := []consistent.Member{}
for _, host := range addrArr {
member := Member(host)
members = append(members, member)
}
//cfg := consistent.Config{
// PartitionCount: 271,
// ReplicationFactor: 20,
// Load: 1.25,
// Hasher: hasher{},
//}
cfg := consistent.Config{
PartitionCount: 271,
ReplicationFactor: 20,
Load: 1.25,
Hasher: hasher{},
}
c.Ring = consistent.New(members, cfg)
for _, addr := range addrArr {
log.Debug("Dialing %s...", addr)
if err := c.initDial(addr); err != nil {
log.Error("Fail to dial %s: %v", addr, err)
c.Close()
return false
}
}
//time0 := time.Since(t0)
//fmt.Println("Dial all goroutines are done!")
//if err := nanolog.Log(LogClient, "Dial", time0.String()); err != nil {
// fmt.Println(err)
//}
return true
}
//func (c *Client) initDial(address string, wg *sync.WaitGroup) {
func (c *Client) initDial(address string) (err error) {
// initialize parallel connections under address
tmp := make([]*Conn, c.Shards)
c.Conns[address] = tmp
var i int
for i = 0; i < c.Shards; i++ {
err = c.connect(address, i)
if err != nil {
break
}
}
if err == nil {
// initialize the cuckoo filter under address
c.MappingTable[address] = cuckoo.NewFilter(1000000)
}
return
}
func (c *Client) connect(address string, i int) error {
cn, err := net.Dial("tcp", address)
if err != nil {
return err
}
c.Conns[address][i] = &Conn{
conn: cn,
W: NewRequestWriter(cn),
R: NewResponseReader(cn),
}
return nil
}
func (c *Client) disconnect(address string, i int) {
if c.Conns[address][i] != nil {
c.Conns[address][i].Close()
c.Conns[address][i] = nil
}
}
func (c *Client) validate(address string, i int) error {
if c.Conns[address][i] == nil {
return c.connect(address, i)
}
return nil
}
func (c *Client) Close() {
log.Info("Cleaning up...")
for addr, conns := range c.Conns {
for i, _ := range conns {
c.disconnect(addr, i)
}
}
log.Info("Client closed.")
}
type ecRet struct {
Shards int
Rets []interface{}
Err error
}
func newEcRet(shards int) *ecRet {
return &ecRet{
Shards: shards,
Rets: make([]interface{}, shards),
}
}
func (r *ecRet) Len() int {
return r.Shards
}
func (r *ecRet) Set(i int, ret interface{}) {
r.Rets[i] = ret
}
func (r *ecRet) SetError(i int, ret interface{}) {
r.Rets[i] = ret
r.Err = ret.(error)
}
func (r *ecRet) Ret(i int) (ret []byte) {
ret, _ = r.Rets[i].([]byte)
return
}
func (r *ecRet) Error(i int) (err error) {
err, _ = r.Rets[i].(error)
return
}