Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support http1 full duplex per workload #14568

Merged
merged 10 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
updates & unit test
  • Loading branch information
skonto committed Jan 15, 2024
commit 7ea197488c81eea84860338c69fc6ebed187a1c4
18 changes: 2 additions & 16 deletions cmd/activator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -238,6 +237,8 @@ func main() {
// NOTE: MetricHandler is being used as the outermost handler of the meaty bits. We're not interested in measuring
// the healthchecks or probes.
ah = activatorhandler.NewMetricHandler(env.PodName, ah)
// We need the context handler to run first so ctx gets the revision info.
Copy link
Contributor Author

@skonto skonto Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as the handler that consumes the body is not run yet. The error we try to eliminate is btw:

2023/11/25 00:07:42 httputil: ReverseProxy read error during body copy: read tcp 10.244.0.32:37488->10.244.0.31:8012: use of closed network connection

Even without the activator wrapper the error at the activator side happens vary rarely, unlike the QP side.

ah = activatorhandler.WrapActivatorHandlerWithFullDuplex(ah, logger)
ah = activatorhandler.NewContextHandler(ctx, ah, configStore)

// Network probe handlers.
Expand All @@ -248,8 +249,6 @@ func main() {
hc := newHealthCheck(sigCtx, logger, statSink)
ah = &activatorhandler.HealthHandler{HealthCheck: hc, NextHandler: ah, Logger: logger}

ah = wrapActivatorHandlerWithFullDuplex(ah, logger)

profilingHandler := profiling.NewHandler(logger, false)
// Watch the logging config map and dynamically update logging levels.
configMapWatcher.Watch(pkglogging.ConfigMapName(), pkglogging.UpdateLevelFromConfigMap(logger, atomicLevel, component))
Expand Down Expand Up @@ -341,16 +340,3 @@ func flush(logger *zap.SugaredLogger) {
os.Stderr.Sync()
metrics.FlushExporter()
}

func wrapActivatorHandlerWithFullDuplex(h http.Handler, logger *zap.SugaredLogger) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
revEnableHTTP1FullDuplex := strings.EqualFold(activatorhandler.RevAnnotation(r.Context(), apiconfig.AllowHTTPFullDuplexFeatureKey), "Enabled")
if revEnableHTTP1FullDuplex {
rc := http.NewResponseController(w)
if err := rc.EnableFullDuplex(); err != nil {
logger.Errorw("Unable to enable full duplex", zap.Error(err))
}
}
h.ServeHTTP(w, r)
})
}
14 changes: 14 additions & 0 deletions pkg/activator/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"knative.dev/pkg/tracing/propagation/tracecontextb3"
"knative.dev/serving/pkg/activator"
activatorconfig "knative.dev/serving/pkg/activator/config"
apiconfig "knative.dev/serving/pkg/apis/config"
pkghttp "knative.dev/serving/pkg/http"
"knative.dev/serving/pkg/networking"
"knative.dev/serving/pkg/queue"
Expand Down Expand Up @@ -150,3 +151,16 @@ func useSecurePort(target string) string {
target = strings.Split(target, ":")[0]
return target + ":" + strconv.Itoa(networking.BackendHTTPSPort)
}

func WrapActivatorHandlerWithFullDuplex(h http.Handler, logger *zap.SugaredLogger) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
revEnableHTTP1FullDuplex := strings.EqualFold(RevAnnotation(r.Context(), apiconfig.AllowHTTPFullDuplexFeatureKey), "Enabled")
if revEnableHTTP1FullDuplex {
rc := http.NewResponseController(w)
if err := rc.EnableFullDuplex(); err != nil {
logger.Errorw("Unable to enable full duplex", zap.Error(err))
}
}
h.ServeHTTP(w, r)
})
}
155 changes: 155 additions & 0 deletions pkg/activator/handler/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ package handler

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"

netprobe "knative.dev/networking/pkg/http/probe"
"knative.dev/pkg/logging"
pkgnet "knative.dev/pkg/network"
rtesting "knative.dev/pkg/reconciler/testing"
"knative.dev/serving/pkg/activator"
apiconfig "knative.dev/serving/pkg/apis/config"
asmetrics "knative.dev/serving/pkg/autoscaler/metrics"
pkghttp "knative.dev/serving/pkg/http"
)
Expand Down Expand Up @@ -114,3 +120,152 @@ func BenchmarkHandlerChain(b *testing.B) {
})
})
}

func TestProxyWithChainHandler(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the issue reproducer utilizing the activator's handler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading this test in isolation it's not clear what it is doing - can you add a detailed comment and maybe link out to the knative and golang issue?

ctx, cancel, _ := rtesting.SetupFakeContextWithCancel(t)
rev := revision(testNamespace, testRevName)
rev.Annotations = map[string]string{apiconfig.AllowHTTPFullDuplexFeatureKey: "Enabled"}
t.Cleanup(cancel)

logger := logging.FromContext(ctx)
configStore := setupConfigStore(t, logger)
revisionInformer(ctx, rev)

// Buffer equal to the activator.
statCh := make(chan []asmetrics.StatMessage)
concurrencyReporter := NewConcurrencyReporter(ctx, activatorPodName, statCh)
go concurrencyReporter.Run(ctx.Done())

// Just read and ignore all stat messages.
go func() {
for {
select {
case <-statCh:
case <-ctx.Done():
return
}
}
}()

// The server responding with the sent body.
echoServer := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Printf("error reading body: %v", err)
http.Error(w, fmt.Sprintf("error reading body: %v", err), http.StatusInternalServerError)
return
}

if _, err := w.Write(body); err != nil {
log.Printf("error writing body: %v", err)
}
},
))
defer echoServer.Close()

// The server proxying requests to the echo server.
echoURL, err := url.Parse(echoServer.URL)
if err != nil {
t.Fatalf("Failed to parse echo URL: %v", err)
}

proxy := pkghttp.NewHeaderPruningReverseProxy(echoURL.Host, "", []string{}, false)
proxy.FlushInterval = 0
proxyWithMiddleware := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
})
var ah http.Handler
ah = concurrencyReporter.Handler(proxyWithMiddleware)
ah = NewTracingHandler(ah)
ah, _ = pkghttp.NewRequestLogHandler(ah, io.Discard, "", nil, false)
ah = NewMetricHandler(activatorPodName, ah)
ah = WrapActivatorHandlerWithFullDuplex(ah, logger)
ah = NewContextHandler(ctx, ah, configStore)
ah = &ProbeHandler{NextHandler: ah}
ah = netprobe.NewHandler(ah)
ah = &HealthHandler{HealthCheck: func() error { return nil }, NextHandler: ah, Logger: logger}

bodySize := 32 * 1024
parallelism := 32

proxyServer := httptest.NewServer(ah)

defer proxyServer.Close()

transport := http.DefaultTransport.(*http.Transport).Clone()

// Turning on this will hide the issue
transport.DisableKeepAlives = false
c := &http.Client{
Transport: transport,
}

body := make([]byte, bodySize)
for i := 0; i < cap(body); i++ {
body[i] = 42
}

for i := 0; i < 10; i++ {
var wg sync.WaitGroup
wg.Add(parallelism)
for i := 0; i < parallelism; i++ {
go func(i int) {
defer wg.Done()

for i := 0; i < 1000; i++ {
if err := send(c, proxyServer.URL, body, "test-host"); err != nil {
t.Errorf("error during request: %v", err)
}
}
}(i)
}

wg.Wait()
}

}

func send(client *http.Client, url string, body []byte, rHost string) error {
r := bytes.NewBuffer(body)
req, err := http.NewRequest("POST", url, r)

if rHost != "" {
req.Host = rHost
}

req.Header.Set(activator.RevisionHeaderNamespace, testNamespace)
req.Header.Set(activator.RevisionHeaderName, testRevName)

if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
}
defer resp.Body.Close()

bd := io.Reader(resp.Body)

rec, err := ioutil.ReadAll(bd)

if err != nil {
return fmt.Errorf("failed to read body: %w", err)
}

if _, err = io.Copy(io.Discard, resp.Body); err != nil {
return fmt.Errorf("failed to discard body: %w", err)
}

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

if len(rec) != len(body) {
return fmt.Errorf("unexpected body length: %d", len(rec))
}

return nil
}
7 changes: 4 additions & 3 deletions pkg/http/handler/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ type timeoutWriter struct {
lastWriteTime time.Time
}

var _ http.Flusher = (*timeoutWriter)(nil)
var _ http.ResponseWriter = (*timeoutWriter)(nil)

// Unwrap returns the underlying writer
func (tw *timeoutWriter) Unwrap() http.ResponseWriter {
return tw.w
}

var _ http.Flusher = (*timeoutWriter)(nil)
var _ http.ResponseWriter = (*timeoutWriter)(nil)

func (tw *timeoutWriter) Flush() {
// The inner handler of timeoutHandler can call Flush at any time including after
// timeoutHandler.ServeHTTP has returned. Forwarding this call to the inner
Expand Down
1 change: 1 addition & 0 deletions pkg/http/response_recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func NewResponseRecorder(w http.ResponseWriter, responseCode int) *ResponseRecor
}
}

// Unwrap returns the underlying writer
func (rr *ResponseRecorder) Unwrap() http.ResponseWriter {
return rr.writer
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/queue/sharedmain/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func mainHandler(
if metricsSupported {
composedHandler = requestAppMetricsHandler(logger, composedHandler, breaker, env)
}
composedHandler = queue.ProxyHandler(breaker, stats, false, composedHandler)
composedHandler = queue.ProxyHandler(breaker, stats, tracingEnabled, composedHandler)
composedHandler = queue.ForwardedShimHandler(composedHandler)
composedHandler = handler.NewTimeoutHandler(composedHandler, "request timeout", func(r *http.Request) (time.Duration, time.Duration, time.Duration) {
return timeout, responseStartTimeout, idleTimeout
Expand Down