Scrub: use binary search (#1543)
Use binary search to find the minimal message size, that contains whole RRs and fits the client's buffer. This is better then just setting entire sections to `nil`. Extend the tests to test for additional and answer section truncation. In the first case we *don't* set the TC bit. This function now also set Compression to true.
This commit is contained in:
parent
aa2302e3f4
commit
074d176f03
2 changed files with 101 additions and 27 deletions
|
@ -179,34 +179,86 @@ type Result int
|
||||||
const (
|
const (
|
||||||
// ScrubIgnored is returned when Scrub did nothing to the message.
|
// ScrubIgnored is returned when Scrub did nothing to the message.
|
||||||
ScrubIgnored Result = iota
|
ScrubIgnored Result = iota
|
||||||
// ScrubDone is returned when the reply has been scrubbed.
|
// ScrubExtra is returned when the reply has been scrubbed by removing RRs from the additional section.
|
||||||
ScrubDone
|
ScrubExtra
|
||||||
|
// ScrubAnswer is returned when the reply has been scrubbed by removing RRs from the answer section.
|
||||||
|
ScrubAnswer
|
||||||
)
|
)
|
||||||
|
|
||||||
// Scrub scrubs the reply message so that it will fit the client's buffer. If
|
// Scrub scrubs the reply message so that it will fit the client's buffer. It sets
|
||||||
// even after dropping the additional section it does not fit, the answer will
|
// reply.Compress to true.
|
||||||
// be cleared and the TC bit will be set on the message. Note, the TC bit will
|
// Scrub uses binary search to find a save cut off point in the additional section.
|
||||||
// be set regardless of protocol, even TCP message will get the bit, the client
|
// If even *without* the additional section the reply still doesn't fit we
|
||||||
// should then retry with pigeons. TODO(referral).
|
// repeat this process for the answer section. If we scrub the answer section
|
||||||
|
// we set the TC bit on the reply; indicating the client should retry over TCP.
|
||||||
|
// Note, the TC bit will be set regardless of protocol, even TCP message will
|
||||||
|
// get the bit, the client should then retry with pigeons.
|
||||||
func (r *Request) Scrub(reply *dns.Msg) (*dns.Msg, Result) {
|
func (r *Request) Scrub(reply *dns.Msg) (*dns.Msg, Result) {
|
||||||
|
reply.Compress = true
|
||||||
|
|
||||||
size := r.Size()
|
size := r.Size()
|
||||||
l := reply.Len()
|
rl := reply.Len()
|
||||||
if size >= l {
|
|
||||||
|
if size >= rl {
|
||||||
return reply, ScrubIgnored
|
return reply, ScrubIgnored
|
||||||
}
|
}
|
||||||
// TODO(miek): check for delegation
|
|
||||||
|
|
||||||
// If not delegation, drop additional section.
|
origExtra := reply.Extra
|
||||||
reply.Extra = nil
|
re := len(reply.Extra)
|
||||||
r.SizeAndDo(reply)
|
l, m := 0, 0
|
||||||
l = reply.Len()
|
for l < re {
|
||||||
if size >= l {
|
m = (l + re) / 2
|
||||||
return reply, ScrubDone
|
reply.Extra = origExtra[:m]
|
||||||
|
rl = reply.Len()
|
||||||
|
if rl < size {
|
||||||
|
l = m + 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if rl > size {
|
||||||
|
re = m - 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We may come out of this loop with one rotation too many as we don't break on rl == size.
|
||||||
|
// I.e. m makes it too large, but m-1 works.
|
||||||
|
if rl > size && m > 0 {
|
||||||
|
reply.Extra = origExtra[:m-1]
|
||||||
|
rl = reply.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if rl < size {
|
||||||
|
r.SizeAndDo(reply)
|
||||||
|
return reply, ScrubExtra
|
||||||
|
}
|
||||||
|
|
||||||
|
origAnswer := reply.Answer
|
||||||
|
ra := len(reply.Answer)
|
||||||
|
l, m = 0, 0
|
||||||
|
for l < ra {
|
||||||
|
m = (l + ra) / 2
|
||||||
|
reply.Answer = origAnswer[:m]
|
||||||
|
rl = reply.Len()
|
||||||
|
if rl < size {
|
||||||
|
l = m + 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if rl > size {
|
||||||
|
ra = m - 1
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We may come out of this loop with one rotation too many as we don't break on rl == size.
|
||||||
|
// I.e. m makes it too large, but m-1 works.
|
||||||
|
if rl > size && m > 0 {
|
||||||
|
reply.Answer = origAnswer[:m-1]
|
||||||
|
// No need to recalc length, as we don't use it. We set truncated anyway. Doing
|
||||||
|
// this extra m-1 step does make it fit in the client's buffer however.
|
||||||
|
}
|
||||||
|
|
||||||
|
// It now fits, but Truncated.
|
||||||
|
r.SizeAndDo(reply)
|
||||||
reply.Truncated = true
|
reply.Truncated = true
|
||||||
reply.Answer = nil
|
return reply, ScrubAnswer
|
||||||
return reply, ScrubDone
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type returns the type of the question as a string. If the request is malformed
|
// Type returns the type of the question as a string. If the request is malformed
|
||||||
|
|
|
@ -61,7 +61,7 @@ func TestRequestMalformed(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRequestScrub(t *testing.T) {
|
func TestRequestScrubAnswer(t *testing.T) {
|
||||||
m := new(dns.Msg)
|
m := new(dns.Msg)
|
||||||
m.SetQuestion("large.example.com.", dns.TypeSRV)
|
m.SetQuestion("large.example.com.", dns.TypeSRV)
|
||||||
req := Request{W: &test.ResponseWriter{}, Req: m}
|
req := Request{W: &test.ResponseWriter{}, Req: m}
|
||||||
|
@ -69,24 +69,46 @@ func TestRequestScrub(t *testing.T) {
|
||||||
reply := new(dns.Msg)
|
reply := new(dns.Msg)
|
||||||
reply.SetReply(m)
|
reply.SetReply(m)
|
||||||
for i := 1; i < 200; i++ {
|
for i := 1; i < 200; i++ {
|
||||||
reply.Answer = append(reply.Answer, test.SRV(fmt.Sprintf(
|
reply.Answer = append(reply.Answer, test.SRV(
|
||||||
"large.example.com. 10 IN SRV 0 0 80 10-0-0-%d.default.pod.k8s.example.com.",
|
fmt.Sprintf("large.example.com. 10 IN SRV 0 0 80 10-0-0-%d.default.pod.k8s.example.com.", i)))
|
||||||
i,
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
msg, got := req.Scrub(reply)
|
_, got := req.Scrub(reply)
|
||||||
if want := ScrubDone; want != got {
|
if want := ScrubAnswer; want != got {
|
||||||
t.Errorf("want scrub result %d, got %d", want, got)
|
t.Errorf("want scrub result %d, got %d", want, got)
|
||||||
}
|
}
|
||||||
if want, got := req.Size(), msg.Len(); want < got {
|
if want, got := req.Size(), reply.Len(); want < got {
|
||||||
t.Errorf("want scrub to reduce message length below %d bytes, got %d bytes", want, got)
|
t.Errorf("want scrub to reduce message length below %d bytes, got %d bytes", want, got)
|
||||||
}
|
}
|
||||||
if !msg.Truncated {
|
if !reply.Truncated {
|
||||||
t.Errorf("want scrub to set truncated bit")
|
t.Errorf("want scrub to set truncated bit")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRequestScrubExtra(t *testing.T) {
|
||||||
|
m := new(dns.Msg)
|
||||||
|
m.SetQuestion("large.example.com.", dns.TypeSRV)
|
||||||
|
req := Request{W: &test.ResponseWriter{}, Req: m}
|
||||||
|
|
||||||
|
reply := new(dns.Msg)
|
||||||
|
reply.SetReply(m)
|
||||||
|
for i := 1; i < 200; i++ {
|
||||||
|
reply.Extra = append(reply.Extra, test.SRV(
|
||||||
|
fmt.Sprintf("large.example.com. 10 IN SRV 0 0 80 10-0-0-%d.default.pod.k8s.example.com.", i)))
|
||||||
|
}
|
||||||
|
|
||||||
|
_, got := req.Scrub(reply)
|
||||||
|
if want := ScrubExtra; want != got {
|
||||||
|
t.Errorf("want scrub result %d, got %d", want, got)
|
||||||
|
}
|
||||||
|
if want, got := req.Size(), reply.Len(); want < got {
|
||||||
|
t.Errorf("want scrub to reduce message length below %d bytes, got %d bytes", want, got)
|
||||||
|
}
|
||||||
|
if reply.Truncated {
|
||||||
|
t.Errorf("want scrub to not set truncated bit")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkRequestDo(b *testing.B) {
|
func BenchmarkRequestDo(b *testing.B) {
|
||||||
st := testRequest()
|
st := testRequest()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue