Skip to content

Commit c232814

Browse files
author
Anthony Romano
committed
etcdmain, tcpproxy: srv-priority policy
Adds DNS SRV weighting and priorities to gateway. Partially addresses #4378
1 parent 07ad181 commit c232814

File tree

5 files changed

+115
-40
lines changed

5 files changed

+115
-40
lines changed

etcdmain/gateway.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,28 @@ func stripSchema(eps []string) []string {
9191

9292
return endpoints
9393
}
94-
func startGateway(cmd *cobra.Command, args []string) {
95-
endpoints := gatewayEndpoints
9694

97-
if eps := discoverEndpoints(gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery); len(eps) != 0 {
98-
endpoints = eps
95+
func startGateway(cmd *cobra.Command, args []string) {
96+
srvs := discoverEndpoints(gatewayDNSCluster, gatewayCA, gatewayInsecureDiscovery)
97+
if len(srvs.Endpoints) == 0 {
98+
// no endpoints discovered, fall back to provided endpoints
99+
srvs.Endpoints = gatewayEndpoints
99100
}
100-
101101
// Strip the schema from the endpoints because we start just a TCP proxy
102-
endpoints = stripSchema(endpoints)
102+
srvs.Endpoints = stripSchema(srvs.Endpoints)
103+
if len(srvs.SRVs) == 0 {
104+
for _, ep := range srvs.Endpoints {
105+
h, p, err := net.SplitHostPort(ep)
106+
if err != nil {
107+
plog.Fatalf("error parsing endpoint %q", ep)
108+
}
109+
var port uint16
110+
fmt.Sscanf(p, "%d", &port)
111+
srvs.SRVs = append(srvs.SRVs, &net.SRV{Target: h, Port: port})
112+
}
113+
}
103114

104-
if len(endpoints) == 0 {
115+
if len(srvs.Endpoints) == 0 {
105116
plog.Fatalf("no endpoints found")
106117
}
107118

@@ -113,7 +124,7 @@ func startGateway(cmd *cobra.Command, args []string) {
113124

114125
tp := tcpproxy.TCPProxy{
115126
Listener: l,
116-
Endpoints: endpoints,
127+
Endpoints: srvs.SRVs,
117128
MonitorInterval: getewayRetryDelay,
118129
}
119130

etcdmain/grpc_proxy.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ func startGRPCProxy(cmd *cobra.Command, args []string) {
106106
os.Exit(1)
107107
}
108108

109-
if eps := discoverEndpoints(grpcProxyDNSCluster, grpcProxyCA, grpcProxyInsecureDiscovery); len(eps) != 0 {
110-
grpcProxyEndpoints = eps
109+
srvs := discoverEndpoints(grpcProxyDNSCluster, grpcProxyCA, grpcProxyInsecureDiscovery)
110+
if len(srvs.Endpoints) != 0 {
111+
grpcProxyEndpoints = srvs.Endpoints
111112
}
112113

113114
l, err := net.Listen("tcp", grpcProxyListenAddr)

etcdmain/util.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ import (
2222
"github.com/coreos/etcd/pkg/transport"
2323
)
2424

25-
func discoverEndpoints(dns string, ca string, insecure bool) (endpoints []string) {
25+
func discoverEndpoints(dns string, ca string, insecure bool) (s srv.SRVClients) {
2626
if dns == "" {
27-
return nil
27+
return s
2828
}
2929
srvs, err := srv.GetClient("etcd-client", dns)
3030
if err != nil {
3131
fmt.Fprintln(os.Stderr, err)
3232
os.Exit(1)
3333
}
34-
endpoints = srvs.Endpoints
34+
endpoints := srvs.Endpoints
3535
plog.Infof("discovered the cluster %s from %s", endpoints, dns)
3636
if insecure {
37-
return endpoints
37+
return *srvs
3838
}
3939
// confirm TLS connections are good
4040
tlsInfo := transport.TLSInfo{
@@ -47,5 +47,19 @@ func discoverEndpoints(dns string, ca string, insecure bool) (endpoints []string
4747
plog.Warningf("%v", err)
4848
}
4949
plog.Infof("using discovered endpoints %v", endpoints)
50-
return endpoints
50+
51+
// map endpoints back to SRVClients struct with SRV data
52+
eps := make(map[string]struct{})
53+
for _, ep := range endpoints {
54+
eps[ep] = struct{}{}
55+
}
56+
for i := range srvs.Endpoints {
57+
if _, ok := eps[srvs.Endpoints[i]]; !ok {
58+
continue
59+
}
60+
s.Endpoints = append(s.Endpoints, srvs.Endpoints[i])
61+
s.SRVs = append(s.SRVs, srvs.SRVs[i])
62+
}
63+
64+
return s
5165
}

proxy/tcpproxy/userspace.go

+71-24
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package tcpproxy
1616

1717
import (
18+
"fmt"
1819
"io"
20+
"math/rand"
1921
"net"
2022
"sync"
2123
"time"
@@ -29,6 +31,7 @@ var (
2931

3032
type remote struct {
3133
mu sync.Mutex
34+
srv *net.SRV
3235
addr string
3336
inactive bool
3437
}
@@ -59,26 +62,27 @@ func (r *remote) isActive() bool {
5962

6063
type TCPProxy struct {
6164
Listener net.Listener
62-
Endpoints []string
65+
Endpoints []*net.SRV
6366
MonitorInterval time.Duration
6467

6568
donec chan struct{}
6669

67-
mu sync.Mutex // guards the following fields
68-
remotes []*remote
69-
nextRemote int
70+
mu sync.Mutex // guards the following fields
71+
remotes []*remote
72+
pickCount int // for round robin
7073
}
7174

7275
func (tp *TCPProxy) Run() error {
7376
tp.donec = make(chan struct{})
7477
if tp.MonitorInterval == 0 {
7578
tp.MonitorInterval = 5 * time.Minute
7679
}
77-
for _, ep := range tp.Endpoints {
78-
tp.remotes = append(tp.remotes, &remote{addr: ep})
80+
for _, srv := range tp.Endpoints {
81+
addr := fmt.Sprintf("%s:%d", srv.Target, srv.Port)
82+
tp.remotes = append(tp.remotes, &remote{srv: srv, addr: addr})
7983
}
8084

81-
plog.Printf("ready to proxy client requests to %v", tp.Endpoints)
85+
plog.Printf("ready to proxy client requests to %+v", tp.Endpoints)
8286
go tp.runMonitor()
8387
for {
8488
in, err := tp.Listener.Accept()
@@ -90,10 +94,61 @@ func (tp *TCPProxy) Run() error {
9094
}
9195
}
9296

93-
func (tp *TCPProxy) numRemotes() int {
94-
tp.mu.Lock()
95-
defer tp.mu.Unlock()
96-
return len(tp.remotes)
97+
func (tp *TCPProxy) pick() *remote {
98+
var weighted []*remote
99+
var unweighted []*remote
100+
101+
bestPr := uint16(65535)
102+
w := 0
103+
// find best priority class
104+
for _, r := range tp.remotes {
105+
switch {
106+
case !r.isActive():
107+
case r.srv.Priority < bestPr:
108+
bestPr = r.srv.Priority
109+
w = 0
110+
weighted, unweighted = nil, nil
111+
unweighted = []*remote{r}
112+
fallthrough
113+
case r.srv.Priority == bestPr:
114+
if r.srv.Weight > 0 {
115+
weighted = append(weighted, r)
116+
w += int(r.srv.Weight)
117+
} else {
118+
unweighted = append(unweighted, r)
119+
}
120+
}
121+
}
122+
if weighted != nil {
123+
if len(unweighted) > 0 && rand.Intn(100) == 1 {
124+
// In the presence of records containing weights greater
125+
// than 0, records with weight 0 should have a very small
126+
// chance of being selected.
127+
r := unweighted[tp.pickCount%len(unweighted)]
128+
tp.pickCount++
129+
return r
130+
}
131+
// choose a uniform random number between 0 and the sum computed
132+
// (inclusive), and select the RR whose running sum value is the
133+
// first in the selected order
134+
choose := rand.Intn(w)
135+
for i := 0; i < len(weighted); i++ {
136+
choose -= int(weighted[i].srv.Weight)
137+
if choose <= 0 {
138+
return weighted[i]
139+
}
140+
}
141+
}
142+
if unweighted != nil {
143+
for i := 0; i < len(tp.remotes); i++ {
144+
picked := tp.remotes[tp.pickCount%len(tp.remotes)]
145+
tp.pickCount++
146+
if picked.isActive() {
147+
return picked
148+
}
149+
}
150+
}
151+
return nil
97152
}
98153

99154
func (tp *TCPProxy) serve(in net.Conn) {
@@ -102,10 +157,12 @@ func (tp *TCPProxy) serve(in net.Conn) {
102157
out net.Conn
103158
)
104159

105-
for i := 0; i < tp.numRemotes(); i++ {
160+
for {
161+
tp.mu.Lock()
106162
remote := tp.pick()
107-
if !remote.isActive() {
108-
continue
163+
tp.mu.Unlock()
164+
if remote == nil {
165+
break
109166
}
110167
// TODO: add timeout
111168
out, err = net.Dial("tcp", remote.addr)
@@ -132,16 +189,6 @@ func (tp *TCPProxy) serve(in net.Conn) {
132189
in.Close()
133190
}
134191

135-
// pick picks a remote in round-robin fashion
136-
func (tp *TCPProxy) pick() *remote {
137-
tp.mu.Lock()
138-
defer tp.mu.Unlock()
139-
140-
picked := tp.remotes[tp.nextRemote]
141-
tp.nextRemote = (tp.nextRemote + 1) % len(tp.remotes)
142-
return picked
143-
}
144-
145192
func (tp *TCPProxy) runMonitor() {
146193
for {
147194
select {

proxy/tcpproxy/userspace_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ func TestUserspaceProxy(t *testing.T) {
4242
t.Fatal(err)
4343
}
4444

45+
var port uint16
46+
fmt.Sscanf(u.Port(), "%d", &port)
4547
p := TCPProxy{
4648
Listener: l,
47-
Endpoints: []string{u.Host},
49+
Endpoints: []*net.SRV{{Target: u.Hostname(), Port: port}},
4850
}
4951
go p.Run()
5052
defer p.Stop()

0 commit comments

Comments
 (0)