-
Notifications
You must be signed in to change notification settings - Fork 2
/
antidoteclient.go
188 lines (166 loc) · 4.09 KB
/
antidoteclient.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
package antidoteclient
import (
"fmt"
"math/rand"
"net"
"time"
"gopkg.in/fatih/pool.v2"
)
const INITIAL_POOL_SIZE = 1
const MAX_POOL_SIZE = 50
// Represents connections to the Antidote database.
// Allows to start/create transaction.
type Client struct {
pools []pool.Pool
}
// Represents an Antidote server.
// The port needs to be the port of the protocol-buffer interface (usually 8087)
type Host struct {
Name string
Port int
}
// Recreates a new Antidote client connected to the given Antidote servers.
// Remember to close the client to clean-up the connections in the connection pool
func NewClient(hosts ...Host) (client *Client, err error) {
pools := make([]pool.Pool, len(hosts))
for i, h := range hosts {
p, err := pool.NewChannelPool(INITIAL_POOL_SIZE, MAX_POOL_SIZE, func() (net.Conn, error) { return net.Dial("tcp", fmt.Sprintf("%s:%d", h.Name, h.Port)) })
if err != nil {
return nil, err
}
pools[i] = p
}
client = &Client{
pools: pools,
}
return
}
// Call close after using the client to clean up the connections int he connection pool and release resources.
func (client *Client) Close() {
for _, p := range client.pools {
p.Close()
}
}
func (client *Client) getConnection() (c *connection, err error) {
// maybe make this global?
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for _, i := range r.Perm(len(client.pools)) {
p := client.pools[i]
con, err := p.Get()
if err != nil {
return nil, err
}
c = &connection{
Conn: con,
pool: p,
}
return c, nil
}
err = fmt.Errorf("All connections dead")
return
}
// a close already puts the connection back into the right pool
type connection struct {
net.Conn
pool pool.Pool
}
// Starts an interactive transaction and registers it on the Antidote server.
// The connection used to issue reads and updates is sticky;
// interactive transactions are only valid local to the server they are started on.
func (client *Client) StartTransaction() (tx *InteractiveTransaction, err error) {
con, err := client.getConnection()
if err != nil {
return
}
readwrite := uint32(0)
blue := uint32(0)
apbtxn := &ApbStartTransaction{
Properties: &ApbTxnProperties{ReadWrite: &readwrite, RedBlue: &blue},
}
err = apbtxn.encode(con)
if err != nil {
return
}
apbtxnresp, err := decodeStartTransactionResp(con)
if err != nil {
return
}
txndesc := apbtxnresp.TransactionDescriptor
tx = &InteractiveTransaction{
con: con,
txID: txndesc,
}
return
}
// Creates a static transaction object. Does not communicate with the Antidote server.
func (client *Client) CreateStaticTransaction() *StaticTransaction {
return &StaticTransaction{client: client}
}
// Creates a data center with the given node names
func (client *Client) CreateDc(nodeNames []string) (err error) {
con, err := client.getConnection()
if err != nil {
return
}
createDc := &ApbCreateDC{
Nodes: nodeNames,
}
err = createDc.encode(con)
if err != nil {
return
}
resp, err := decodeApbCreateDCResp(con)
if err != nil {
return
}
if !*resp.Success {
return fmt.Errorf("Could not create DC, error code %v", *resp.Errorcode)
}
return
}
// Get a connection descriptor for the data center
// The descriptor can then be used with ConnectToDCs
func (client *Client) GetConnectionDescriptor() (descriptor []byte, err error) {
con, err := client.getConnection()
if err != nil {
return
}
getCD := &ApbGetConnectionDescriptor{
}
err = getCD.encode(con)
if err != nil {
return
}
resp, err := decodeApbGetConnectionDescriptorResp(con)
if err != nil {
return
}
if !*resp.Success {
err = fmt.Errorf("Could not create DC, error code %v", *resp.Errorcode)
return
}
descriptor = resp.Descriptor_
return
}
func (client *Client) ConnectToDCs(descriptors [][]byte) (err error) {
con, err := client.getConnection()
if err != nil {
return
}
getCD := &ApbConnectToDCs{
Descriptors: descriptors,
}
err = getCD.encode(con)
if err != nil {
return
}
resp, err := decodeApbConnectToDCsResp(con)
if err != nil {
return
}
if !*resp.Success {
err = fmt.Errorf("Could not create DC, error code %v", *resp.Errorcode)
return
}
return
}