Commit 54fb2112 authored by Chris O'Haver's avatar Chris O'Haver Committed by GitHub

plugin/forward/grpc: Revert forward/grpc policy dedup (#3919)

* revert de-dup
Signed-off-by: default avatarChris O'Haver <cohaver@infoblox.com>

* unit test
Signed-off-by: default avatarChris O'Haver <cohaver@infoblox.com>

* use roundrobin policy in test
Signed-off-by: default avatarChris O'Haver <cohaver@infoblox.com>
parent bc2ba288
...@@ -14,7 +14,6 @@ import ( ...@@ -14,7 +14,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/debug" "github.com/coredns/coredns/plugin/debug"
clog "github.com/coredns/coredns/plugin/pkg/log" clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/policy"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"
...@@ -29,7 +28,7 @@ type Forward struct { ...@@ -29,7 +28,7 @@ type Forward struct {
concurrent int64 // atomic counters need to be first in struct for proper alignment concurrent int64 // atomic counters need to be first in struct for proper alignment
proxies []*Proxy proxies []*Proxy
p policy.Policy p Policy
hcInterval time.Duration hcInterval time.Duration
from string from string
...@@ -52,7 +51,7 @@ type Forward struct { ...@@ -52,7 +51,7 @@ type Forward struct {
// New returns a new Forward. // New returns a new Forward.
func New() *Forward { func New() *Forward {
f := &Forward{maxfails: 2, tlsConfig: new(tls.Config), expire: defaultExpire, p: new(policy.Random), from: ".", hcInterval: hcInterval, opts: options{forceTCP: false, preferUDP: false, hcRecursionDesired: true}} f := &Forward{maxfails: 2, tlsConfig: new(tls.Config), expire: defaultExpire, p: new(random), from: ".", hcInterval: hcInterval, opts: options{forceTCP: false, preferUDP: false, hcRecursionDesired: true}}
return f return f
} }
...@@ -109,8 +108,8 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg ...@@ -109,8 +108,8 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
} }
// All upstream proxies are dead, assume healthcheck is completely broken and randomly // All upstream proxies are dead, assume healthcheck is completely broken and randomly
// select an upstream to connect to. // select an upstream to connect to.
r := new(policy.Random) r := new(random)
proxy = r.List(f.proxies)[0].([]*Proxy)[0] proxy = r.List(f.proxies)[0]
HealthcheckBrokenCount.Add(1) HealthcheckBrokenCount.Add(1)
} }
...@@ -206,12 +205,7 @@ func (f *Forward) ForceTCP() bool { return f.opts.forceTCP } ...@@ -206,12 +205,7 @@ func (f *Forward) ForceTCP() bool { return f.opts.forceTCP }
func (f *Forward) PreferUDP() bool { return f.opts.preferUDP } func (f *Forward) PreferUDP() bool { return f.opts.preferUDP }
// List returns a set of proxies to be used for this client depending on the policy in f. // List returns a set of proxies to be used for this client depending on the policy in f.
func (f *Forward) List() []*Proxy { func (f *Forward) List() []*Proxy {return f.p.List(f.proxies)}
if len(f.p.List(f.proxies)) == 1 {
return f.p.List(f.proxies)[0].([]*Proxy)
}
return nil
}
var ( var (
// ErrNoHealthy means no healthy proxies left. // ErrNoHealthy means no healthy proxies left.
......
package forward
import (
"testing"
)
func TestList(t *testing.T) {
f := Forward{
proxies: []*Proxy{{addr: "1.1.1.1:53"}, {addr: "2.2.2.2:53"}, {addr: "3.3.3.3:53"}},
p: &roundRobin{},
}
expect := []*Proxy{{addr: "2.2.2.2:53"}, {addr: "1.1.1.1:53"}, {addr: "3.3.3.3:53"}}
got := f.List()
if len(got) != len(expect) {
t.Fatalf("Expected: %v results, got: %v", len(expect), len(got))
}
for i, p := range got {
if p.addr != expect[i].addr {
t.Fatalf("Expected proxy %v to be '%v', got: '%v'", i, expect[i].addr, p.addr)
}
}
}
package policy package forward
import ( import (
"math/rand" "math/rand"
...@@ -7,32 +7,28 @@ import ( ...@@ -7,32 +7,28 @@ import (
// Policy defines a policy we use for selecting upstreams. // Policy defines a policy we use for selecting upstreams.
type Policy interface { type Policy interface {
List(policy ...interface{}) []interface{} List([]*Proxy) []*Proxy
String() string String() string
} }
// Random is a policy that implements random upstream selection. // random is a policy that implements random upstream selection.
type Random struct{} type random struct{}
var _ Policy = &Random{} func (r *random) String() string { return "random" }
// String returns the name of policy Random func (r *random) List(p []*Proxy) []*Proxy {
func (r *Random) String() string { return "random" }
// List returns a set of proxies to be used for this client depending on Random policy.
func (r *Random) List(p ...interface{}) []interface{} {
switch len(p) { switch len(p) {
case 1: case 1:
return p return p
case 2: case 2:
if rand.Int()%2 == 0 { if rand.Int()%2 == 0 {
return []interface{}{p[1], p[0]} // swap return []*Proxy{p[1], p[0]} // swap
} }
return p return p
} }
perms := rand.Perm(len(p)) perms := rand.Perm(len(p))
rnd := make([]interface{}, len(p)) rnd := make([]*Proxy, len(p))
for i, p1 := range perms { for i, p1 := range perms {
rnd[i] = p[p1] rnd[i] = p[p1]
...@@ -40,37 +36,29 @@ func (r *Random) List(p ...interface{}) []interface{} { ...@@ -40,37 +36,29 @@ func (r *Random) List(p ...interface{}) []interface{} {
return rnd return rnd
} }
// RoundRobin is a policy that selects hosts based on round robin ordering. // roundRobin is a policy that selects hosts based on round robin ordering.
type RoundRobin struct { type roundRobin struct {
robin uint32 robin uint32
} }
var _ Policy = &RoundRobin{} func (r *roundRobin) String() string { return "round_robin" }
// String returns the name of policy RoundRobin
func (r *RoundRobin) String() string { return "round_robin" }
// List returns a set of proxies to be used for this client depending on RoundRobin policy. func (r *roundRobin) List(p []*Proxy) []*Proxy {
func (r *RoundRobin) List(p ...interface{}) []interface{} {
poolLen := uint32(len(p)) poolLen := uint32(len(p))
i := atomic.AddUint32(&r.robin, 1) % poolLen i := atomic.AddUint32(&r.robin, 1) % poolLen
robin := []interface{}{p[i]} robin := []*Proxy{p[i]}
robin = append(robin, p[:i]...) robin = append(robin, p[:i]...)
robin = append(robin, p[i+1:]...) robin = append(robin, p[i+1:]...)
return robin return robin
} }
// Sequential is a policy that selects hosts based on sequential ordering. // sequential is a policy that selects hosts based on sequential ordering.
type Sequential struct{} type sequential struct{}
var _ Policy = &Sequential{}
// String returns the name of policy Sequential func (r *sequential) String() string { return "sequential" }
func (r *Sequential) String() string { return "sequential" }
// List returns a set of proxies without filter. func (r *sequential) List(p []*Proxy) []*Proxy {
func (r *Sequential) List(p ...interface{}) []interface{} {
return p return p
} }
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/parse" "github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/policy"
pkgtls "github.com/coredns/coredns/plugin/pkg/tls" pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/transport" "github.com/coredns/coredns/plugin/pkg/transport"
...@@ -221,11 +220,11 @@ func parseBlock(c *caddy.Controller, f *Forward) error { ...@@ -221,11 +220,11 @@ func parseBlock(c *caddy.Controller, f *Forward) error {
} }
switch x := c.Val(); x { switch x := c.Val(); x {
case "random": case "random":
f.p = &policy.Random{} f.p = &random{}
case "round_robin": case "round_robin":
f.p = &policy.RoundRobin{} f.p = &roundRobin{}
case "sequential": case "sequential":
f.p = &policy.Sequential{} f.p = &sequential{}
default: default:
return c.Errf("unknown policy '%s'", x) return c.Errf("unknown policy '%s'", x)
} }
......
...@@ -7,7 +7,6 @@ import ( ...@@ -7,7 +7,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/debug" "github.com/coredns/coredns/plugin/debug"
"github.com/coredns/coredns/plugin/pkg/policy"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"
...@@ -18,7 +17,7 @@ import ( ...@@ -18,7 +17,7 @@ import (
// It has a list of proxies each representing one upstream proxy. // It has a list of proxies each representing one upstream proxy.
type GRPC struct { type GRPC struct {
proxies []*Proxy proxies []*Proxy
p policy.Policy p Policy
from string from string
ignored []string ignored []string
...@@ -94,7 +93,7 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) ( ...@@ -94,7 +93,7 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
// NewGRPC returns a new GRPC. // NewGRPC returns a new GRPC.
func newGRPC() *GRPC { func newGRPC() *GRPC {
g := &GRPC{ g := &GRPC{
p: new(policy.Random), p: new(random),
} }
return g return g
} }
...@@ -127,11 +126,6 @@ func (g *GRPC) isAllowedDomain(name string) bool { ...@@ -127,11 +126,6 @@ func (g *GRPC) isAllowedDomain(name string) bool {
} }
// List returns a set of proxies to be used for this client depending on the policy in p. // List returns a set of proxies to be used for this client depending on the policy in p.
func (g *GRPC) list() []*Proxy { func (g *GRPC) list() []*Proxy { return g.p.List(g.proxies) }
if len(g.p.List(g.proxies)) == 1 {
return g.p.List(g.proxies)[0].([]*Proxy)
}
return nil
}
const defaultTimeout = 5 * time.Second const defaultTimeout = 5 * time.Second
package grpc
import (
"math/rand"
"sync/atomic"
)
// Policy defines a policy we use for selecting upstreams.
type Policy interface {
List([]*Proxy) []*Proxy
String() string
}
// random is a policy that implements random upstream selection.
type random struct{}
func (r *random) String() string { return "random" }
func (r *random) List(p []*Proxy) []*Proxy {
switch len(p) {
case 1:
return p
case 2:
if rand.Int()%2 == 0 {
return []*Proxy{p[1], p[0]} // swap
}
return p
}
perms := rand.Perm(len(p))
rnd := make([]*Proxy, len(p))
for i, p1 := range perms {
rnd[i] = p[p1]
}
return rnd
}
// roundRobin is a policy that selects hosts based on round robin ordering.
type roundRobin struct {
robin uint32
}
func (r *roundRobin) String() string { return "round_robin" }
func (r *roundRobin) List(p []*Proxy) []*Proxy {
poolLen := uint32(len(p))
i := atomic.AddUint32(&r.robin, 1) % poolLen
robin := []*Proxy{p[i]}
robin = append(robin, p[:i]...)
robin = append(robin, p[i+1:]...)
return robin
}
// sequential is a policy that selects hosts based on sequential ordering.
type sequential struct{}
func (r *sequential) String() string { return "sequential" }
func (r *sequential) List(p []*Proxy) []*Proxy {
return p
}
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics" "github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/parse" "github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/policy"
pkgtls "github.com/coredns/coredns/plugin/pkg/tls" pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy"
...@@ -133,11 +132,11 @@ func parseBlock(c *caddy.Controller, g *GRPC) error { ...@@ -133,11 +132,11 @@ func parseBlock(c *caddy.Controller, g *GRPC) error {
} }
switch x := c.Val(); x { switch x := c.Val(); x {
case "random": case "random":
g.p = &policy.Random{} g.p = &random{}
case "round_robin": case "round_robin":
g.p = &policy.RoundRobin{} g.p = &roundRobin{}
case "sequential": case "sequential":
g.p = &policy.Sequential{} g.p = &sequential{}
default: default:
return c.Errf("unknown policy '%s'", x) return c.Errf("unknown policy '%s'", x)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment