-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add the async probing function to the prober library. #3986
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,31 +20,96 @@ import ( | |
"context" | ||
"io/ioutil" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/knative/serving/pkg/network" | ||
) | ||
|
||
// Do sends a single probe to given target, e.g. `http://revision.default.svc.cluster.local:81`. | ||
// headerValue is the value for the `k-network-probe` header. | ||
// Do returns the status code, response body, and the request error, if any. | ||
func Do(ctx context.Context, target, headerValue string) (int, string, error) { | ||
// Do returns whether the probe was successful or not, or there was an error probing. | ||
func Do(ctx context.Context, target, headerValue string) (bool, error) { | ||
req, err := http.NewRequest(http.MethodGet, target, nil) | ||
if err != nil { | ||
return 0, "", errors.Wrapf(err, "%s is not a valid URL", target) | ||
return false, errors.Wrapf(err, "%s is not a valid URL", target) | ||
} | ||
|
||
req.Header.Set(http.CanonicalHeaderKey(network.ProbeHeaderName), headerValue) | ||
req = req.WithContext(ctx) | ||
resp, err := network.AutoTransport.RoundTrip(req) | ||
if err != nil { | ||
return 0, "", err | ||
return false, errors.Wrapf(err, "error roundtripping %s", target) | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return 0, "", err | ||
return false, errors.Wrap(err, "error reading body") | ||
} | ||
return resp.StatusCode, string(body), nil | ||
return resp.StatusCode == http.StatusOK && string(body) == headerValue, nil | ||
} | ||
|
||
// Done is a callback that is executed when the async probe has finished. | ||
// `arg` is given by the caller at the offering time, while `success` and `err` | ||
// are the return values of the `Do` call. | ||
type Done func(arg interface{}, success bool, err error) | ||
|
||
// Manager manages async probes and makes sure we run concurrently only a single | ||
// probe for the same key. | ||
type Manager struct { | ||
cb Done | ||
|
||
// mu guards keys. | ||
mu sync.Mutex | ||
keys sets.String | ||
} | ||
|
||
// New creates a new Manager, that will invoke the given callback when | ||
// async probing is finished. | ||
func New(cb Done) *Manager { | ||
return &Manager{ | ||
keys: sets.NewString(), | ||
cb: cb, | ||
} | ||
} | ||
|
||
// Offer executes asynchronous probe using `target` as the key. | ||
// If a probe with the same key already exists, Offer will return false and the | ||
// call is discarded. If the request is accepted, Offer returns true. | ||
// Otherwise Offer starts a goroutine that periodically executes | ||
// `Do`, until timeout is reached, the probe succeeds, or fails with an error. | ||
// In the end the callback is invoked with the provided `arg` and probing results. | ||
func (m *Manager) Offer(ctx context.Context, target, headerValue string, arg interface{}, period, timeout time.Duration) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why "Offer"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we offer system to make a probe, since the system can reject doing it -- it's not |
||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
if m.keys.Has(target) { | ||
return false | ||
} | ||
m.keys.Insert(target) | ||
m.doAsync(ctx, target, headerValue, arg, period, timeout) | ||
return true | ||
} | ||
|
||
// doAsync starts a go routine that probes the target with given period. | ||
func (m *Manager) doAsync(ctx context.Context, target, headerValue string, arg interface{}, period, timeout time.Duration) { | ||
go func() { | ||
defer func() { | ||
m.mu.Lock() | ||
defer m.mu.Unlock() | ||
m.keys.Delete(target) | ||
}() | ||
var ( | ||
result bool | ||
err error | ||
) | ||
err = wait.PollImmediate(period, timeout, func() (bool, error) { | ||
result, err = Do(ctx, target, headerValue) | ||
return result, err | ||
}) | ||
m.cb(arg, result, err) | ||
}() | ||
vagababov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ package kpa | |
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/knative/pkg/apis" | ||
"github.com/knative/pkg/apis/duck" | ||
|
@@ -78,11 +77,11 @@ func activatorProbe(pa *pav1alpha1.PodAutoscaler) (bool, error) { | |
// Resolve the hostname and port to probe. | ||
svc := network.GetServiceHostname(pa.Status.ServiceName, pa.Namespace) | ||
port := networking.ServicePort(pa.Spec.ProtocolType) | ||
st, body, err := prober.Do(context.Background(), fmt.Sprintf("http://%s:%d/", svc, port), activator.Name) | ||
success, err := prober.Do(context.Background(), fmt.Sprintf("http://%s:%d/", svc, port), activator.Name) | ||
if err != nil { | ||
return false, err | ||
} | ||
return st == http.StatusOK && body == activator.Name, nil | ||
return success, nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. certainly. 🤦♂ |
||
} | ||
|
||
// podScalableTypedInformerFactory returns a duck.InformerFactory that returns | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added to the
Done
definition.