mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
* Initial stab at the protocol * initial protocol sketch for node pool manager * Added http header frame as a message * Force the use of WithAgent variants when creating a server * adds grpc models for node pool manager plus go deps * Naming things is really hard * Merge (and optionally purge) details received by the NPM * WIP: starting to add the runner-side functionality of the new data plane * WIP: Basic startup of grpc server for pure runner. Needs proper certs. * Go fmt * Initial agent for LB nodes. * Agent implementation for LB nodes. * Pass keys and certs to LB node agent. * Remove accidentally left reference to env var. * Add env variables for certificate files * stub out the capacity and group membership server channels * implement server-side runner manager service * removes unused variable * fixes build error * splits up GetCall and GetLBGroupId * Change LB node agent to use TLS connection. * Encode call model as JSON to send to runner node. * Use hybrid client in LB node agent. This should provide access to get app and route information for the call from an API node. * More error handling on the pure runner side * Tentative fix for GetCall problem: set deadlines correctly when reserving slot * Connect loop for LB agent to runner nodes. * Extract runner connection function in LB agent. * drops committed capacity counts * Bugfix - end state tracker only in submit * Do logs properly * adds first pass of tracking capacity metrics in agent * maked memory capacity metric uint64 * maked memory capacity metric uint64 * removes use of old capacity field * adds remove capacity call * merges overwritten reconnect logic * First pass of a NPM Provide a service that talks to a (simulated) CP. - Receive incoming capacity assertions from LBs for LBGs - expire LB requests after a short period - ask the CP to add runners to a LBG - note runner set changes and readvertise - scale down by marking runners as "draining" - shut off draining runners after some cool-down period * add capacity update on schedule * Send periodic capcacity metrics Sending capcacity metrics to node pool manager * splits grpc and api interfaces for capacity manager * failure to advertise capacity shouldn't panic * Add some instructions for starting DP/CP parts. * Create the poolmanager server with TLS * Use logrus * Get npm compiling with cert fixups. * Fix: pure runner should not start async processing * brings runner, nulb and npm together * Add field to acknowledgment to record slot allocation latency; fix a bug too * iterating on pool manager locking issue * raises timeout of placement retry loop * Fix up NPM Improve logging Ensure that channels etc. are actually initialised in the structure creation! * Update the docs - runners GRPC port is 9120 * Bugfix: return runner pool accurately. * Double locking * Note purges as LBs stop talking to us * Get the purging of old LBs working. * Tweak: on restart, load runner set before making scaling decisions. * more agent synchronization improvements * Deal with teh CP pulling out active hosts from under us. * lock at lbgroup level * Send request and receive response from runner. * Add capacity check right before slot reservation * Pass the full Call into the receive loop. * Wait for the data from the runner before finishing * force runner list refresh every time * Don't init db and mq for pure runners * adds shutdown of npm * fixes broken log line * Extract an interface for the Predictor used by the NPM * purge drained connections from npm * Refactor of the LB agent into the agent package * removes capacitytest wip * Fix undefined err issue * updating README for poolmanager set up * ues retrying dial for lb to npm connections * Rename lb_calls to lb_agent now that all functionality is there * Use the right deadline and errors in LBAgent * Make stream error flag per-call rather than global otherwise the whole runner is damaged by one call dropping * abstracting gRPCNodePool * Make stream error flag per-call rather than global otherwise the whole runner is damaged by one call dropping * Add some init checks for LB and pure runner nodes * adding some useful debug * Fix default db and mq for lb node * removes unreachable code, fixes typo * Use datastore as logstore in API nodes. This fixes a bug caused by trying to insert logs into a nil logstore. It was nil because it wasn't being set for API nodes. * creates placement abstraction and moves capacity APIs to NodePool * removed TODO, added logging * Dial reconnections for LB <-> runners LB grpc connections to runners are established using a backoff stategy in event of reconnections, this allows to let the LB up even in case one of the runners go away and reconnect to it as soon as it is back. * Add a status call to the Runner protocol Stub at the moment. To be used for things like draindown, health checks. * Remove comment. * makes assign/release capacity lockless * Fix hanging issue in lb agent when connections drop * Add the CH hash from fnlb Select this with FN_PLACER=ch when launching the LB. * small improvement for locking on reloadLBGmembership * Stabilise the list of Runenrs returned by NodePool The NodePoolManager makes some attempt to keep the list of runner nodes advertised as stable as possible. Let's preserve this effort in the client side. The main point of this is to attempt to keep the same runner at the same inxed in the []Runner returned by NodePool.Runners(lbgid); the ch algorithm likes it when this is the case. * Factor out a generator function for the Runners so that mocks can be injected * temporarily allow lbgroup to be specified in HTTP header, while we sort out changes to the model * fixes bug with nil runners * Initial work for mocking things in tests * fix for anonymouse go routine error * fixing lb_test to compile * Refactor: internal objects for gRPCNodePool are now injectable, with defaults for the real world case * Make GRPC port configurable, fix weird handling of web port too * unit test reload Members * check on runner creation failure * adding nullRunner in case of failure during runner creation * Refactored capacity advertisements/aggregations. Made grpc advertisement post asynchronous and non-blocking. * make capacityEntry private * Change the runner gRPC bind address. This uses the existing `whoAmI` function, so that the gRPC server works when the runner is running on a different host. * Add support for multiple fixed runners to pool mgr * Added harness for dataplane system tests, minor refactors * Add Dockerfiles for components, along with docs. * Doc fix: second runner needs a different name. * Let us have three runners in system tests, why not * The first system test running a function in API/LB/PureRunner mode * Add unit test for Advertiser logic * Fix issue with Pure Runner not sending the last data frame * use config in models.Call as a temporary mechanism to override lb group ID * make gofmt happy * Updates documentation for how to configure lb groups for an app/route * small refactor unit test * Factor NodePool into its own package * Lots of fixes to Pure Runner - concurrency woes with errors and cancellations * New dataplane with static runnerpool (#813) Added static node pool as default implementation * moved nullRunner to grpc package * remove duplication in README * fix go vet issues * Fix server initialisation in api tests * Tiny logging changes in pool manager. Using `WithError` instead of `Errorf` when appropriate. * Change some log levels in the pure runner * fixing readme * moves multitenant compute documentation * adds introduction to multitenant readme * Proper triggering of system tests in makefile * Fix insructions about starting up the components * Change db file for system tests to avoid contention in parallel tests * fixes revisions from merge * Fix merge issue with handling of reserved slot * renaming nulb to lb in the doc and images folder * better TryExec sleep logic clean shutdown In this change we implement a better way to deal with the sleep inside the for loop during the attempt for placing a call. Plus we added a clean way to shutdown the connections with external component when we shut down the server. * System_test mysql port set mysql port for system test to a different value to the one set for the api tests to avoid conflicts as they can run in parallel. * change the container name for system-test * removes flaky test TestRouteRunnerExecution pending resolution by issue #796 * amend remove_containers to remove new added containers * Rework capacity reservation logic at a higher level for now * LB agent implements Submit rather than delegating. * Fix go vet linting errors * Changed a couple of error levels * Fix formatting * removes commmented out test * adds snappy to vendor directory * updates Gopkg and vendor directories, removing snappy and addhing siphash * wait for db containers to come up before starting the tests * make system tests start API node on 8085 to avoid port conflict with api_tests * avoid port conflicts with api_test.sh which are run in parallel * fixes postgres port conflict and issue with removal of old containers * Remove spurious println
573 lines
18 KiB
Go
573 lines
18 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
runner "github.com/fnproject/fn/api/agent/grpc"
|
|
"github.com/fnproject/fn/api/models"
|
|
"github.com/go-openapi/strfmt"
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
"github.com/sirupsen/logrus"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
"google.golang.org/grpc/metadata"
|
|
"google.golang.org/grpc/peer"
|
|
)
|
|
|
|
// callHandle represents the state of the call as handled by the pure runner, and additionally it implements the
|
|
// interface of http.ResponseWriter so that it can be used for streaming the output back.
|
|
type callHandle struct {
|
|
engagement runner.RunnerProtocol_EngageServer
|
|
c *call // the agent's version of call
|
|
input io.WriteCloser
|
|
started bool
|
|
done chan error // to synchronize
|
|
// As the state can be set and checked by both goroutines handling this state, we need a mutex.
|
|
stateMutex sync.Mutex
|
|
// Timings, for metrics:
|
|
receivedTime strfmt.DateTime // When was the call received?
|
|
allocatedTime strfmt.DateTime // When did we finish allocating capacity?
|
|
// Last communication error on the stream (if any). This basically acts as a cancellation flag too.
|
|
streamError error
|
|
// For implementing http.ResponseWriter:
|
|
outHeaders http.Header
|
|
outStatus int
|
|
headerWritten bool
|
|
}
|
|
|
|
func (ch *callHandle) Header() http.Header {
|
|
return ch.outHeaders
|
|
}
|
|
|
|
func (ch *callHandle) WriteHeader(status int) {
|
|
ch.outStatus = status
|
|
ch.commitHeaders()
|
|
}
|
|
|
|
func (ch *callHandle) commitHeaders() error {
|
|
if ch.headerWritten {
|
|
return nil
|
|
}
|
|
ch.headerWritten = true
|
|
logrus.Debugf("Committing call result with status %d", ch.outStatus)
|
|
|
|
var outHeaders []*runner.HttpHeader
|
|
|
|
for h, vals := range ch.outHeaders {
|
|
for _, v := range vals {
|
|
outHeaders = append(outHeaders, &runner.HttpHeader{
|
|
Key: h,
|
|
Value: v,
|
|
})
|
|
}
|
|
}
|
|
|
|
// Only write if we are not in an error situation. If we cause a stream error, then record that but don't cancel
|
|
// the call: basically just blackhole the output and return the write error to cause Submit to fail properly.
|
|
ch.stateMutex.Lock()
|
|
defer ch.stateMutex.Unlock()
|
|
err := ch.streamError
|
|
if err != nil {
|
|
return fmt.Errorf("Bailing out because of communication error: %v", ch.streamError)
|
|
}
|
|
|
|
logrus.Debug("Sending call result start message")
|
|
err = ch.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_ResultStart{
|
|
ResultStart: &runner.CallResultStart{
|
|
Meta: &runner.CallResultStart_Http{
|
|
Http: &runner.HttpRespMeta{
|
|
Headers: outHeaders,
|
|
StatusCode: int32(ch.outStatus),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
logrus.WithError(err).Error("Error sending call result")
|
|
ch.streamError = err
|
|
return err
|
|
}
|
|
logrus.Debug("Sent call result message")
|
|
return nil
|
|
}
|
|
|
|
func (ch *callHandle) Write(data []byte) (int, error) {
|
|
err := ch.commitHeaders()
|
|
if err != nil {
|
|
return 0, fmt.Errorf("Error sending data: %v", err)
|
|
}
|
|
|
|
// Only write if we are not in an error situation. If we cause a stream error, then record that but don't cancel
|
|
// the call: basically just blackhole the output and return the write error to cause Submit to fail properly.
|
|
ch.stateMutex.Lock()
|
|
defer ch.stateMutex.Unlock()
|
|
err = ch.streamError
|
|
if err != nil {
|
|
return 0, fmt.Errorf("Bailing out because of communication error: %v", ch.streamError)
|
|
}
|
|
|
|
logrus.Debugf("Sending call response data %d bytes long", len(data))
|
|
err = ch.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Data{
|
|
Data: &runner.DataFrame{
|
|
Data: data,
|
|
Eof: false,
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
ch.streamError = err
|
|
return 0, fmt.Errorf("Error sending data: %v", err)
|
|
}
|
|
return len(data), nil
|
|
}
|
|
|
|
func (ch *callHandle) Close() error {
|
|
err := ch.commitHeaders()
|
|
if err != nil {
|
|
return fmt.Errorf("Error sending close frame: %v", err)
|
|
}
|
|
|
|
// Only write if we are not in an error situation. If we cause a stream error, then record that but don't cancel
|
|
// the call: basically just blackhole the output and return the write error to cause the caller to fail properly.
|
|
ch.stateMutex.Lock()
|
|
defer ch.stateMutex.Unlock()
|
|
err = ch.streamError
|
|
if err != nil {
|
|
return fmt.Errorf("Bailing out because of communication error: %v", ch.streamError)
|
|
}
|
|
logrus.Debug("Sending call response data end")
|
|
err = ch.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Data{
|
|
Data: &runner.DataFrame{
|
|
Eof: true,
|
|
},
|
|
},
|
|
})
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("Error sending close frame: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// cancel implements the logic for cancelling the execution of a call based on what the state in the handle is.
|
|
func (ch *callHandle) cancel(ctx context.Context, err error) {
|
|
ch.stateMutex.Lock()
|
|
defer ch.stateMutex.Unlock()
|
|
|
|
// Do not double-cancel.
|
|
if ch.streamError != nil {
|
|
return
|
|
}
|
|
|
|
// First, record that there has been an error.
|
|
ch.streamError = err
|
|
// Caller may have died or disconnected. The behaviour here depends on the state of the call.
|
|
// If the call was placed and is running we need to handle it...
|
|
if ch.c != nil {
|
|
// If we've actually started the call we're in the middle of an execution with i/o going back and forth.
|
|
// This is hard to stop. Side effects can be occurring at any point. However, at least we should stop
|
|
// the i/o flow. Recording the stream error in the handle should have stopped the output, but we also
|
|
// want to stop any input being sent through, so we close the input stream and let the function
|
|
// probably crash out. If it doesn't crash out, well, it means the function doesn't handle i/o errors
|
|
// properly and it will hang there until the timeout, then it'll be killed properly by the timeout
|
|
// handling in Submit.
|
|
if ch.started {
|
|
ch.input.Close()
|
|
}
|
|
}
|
|
}
|
|
|
|
type pureRunnerCapacityManager struct {
|
|
totalCapacityUnits uint64
|
|
committedCapacityUnits uint64
|
|
mtx sync.Mutex
|
|
}
|
|
|
|
type capacityDeallocator func()
|
|
|
|
func newPureRunnerCapacityManager(units uint64) pureRunnerCapacityManager {
|
|
return pureRunnerCapacityManager{
|
|
totalCapacityUnits: units,
|
|
committedCapacityUnits: 0,
|
|
}
|
|
}
|
|
|
|
func (prcm *pureRunnerCapacityManager) checkAndReserveCapacity(units uint64) error {
|
|
prcm.mtx.Lock()
|
|
defer prcm.mtx.Unlock()
|
|
if prcm.committedCapacityUnits+units < prcm.totalCapacityUnits {
|
|
prcm.committedCapacityUnits = prcm.committedCapacityUnits + units
|
|
return nil
|
|
}
|
|
return models.ErrCallTimeoutServerBusy
|
|
}
|
|
|
|
func (prcm *pureRunnerCapacityManager) releaseCapacity(units uint64) {
|
|
prcm.mtx.Lock()
|
|
defer prcm.mtx.Unlock()
|
|
if units <= prcm.committedCapacityUnits {
|
|
prcm.committedCapacityUnits = prcm.committedCapacityUnits - units
|
|
return
|
|
}
|
|
panic("Fatal error in pure runner capacity calculation, getting to sub-zero capacity")
|
|
}
|
|
|
|
type pureRunner struct {
|
|
gRPCServer *grpc.Server
|
|
listen string
|
|
a Agent
|
|
inflight int32
|
|
capacity pureRunnerCapacityManager
|
|
}
|
|
|
|
func (pr *pureRunner) ensureFunctionIsRunning(state *callHandle) {
|
|
// Only start it once!
|
|
state.stateMutex.Lock()
|
|
defer state.stateMutex.Unlock()
|
|
if !state.started {
|
|
state.started = true
|
|
go func() {
|
|
err := pr.a.Submit(state.c)
|
|
if err != nil {
|
|
// In this case the function has failed for a legitimate reason. We send a call failed message if we
|
|
// can. If there's a streaming error doing that then we are basically in the "double exception" case
|
|
// and who knows what's best to do. Submit has already finished so we don't need to cancel... but at
|
|
// least we should set streamError if it's not set.
|
|
state.stateMutex.Lock()
|
|
defer state.stateMutex.Unlock()
|
|
if state.streamError == nil {
|
|
err2 := state.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Finished{Finished: &runner.CallFinished{
|
|
Success: false,
|
|
Details: fmt.Sprintf("%v", err),
|
|
}}})
|
|
if err2 != nil {
|
|
state.streamError = err2
|
|
}
|
|
}
|
|
state.done <- err
|
|
return
|
|
}
|
|
// First close the writer, then send the call finished message
|
|
err = state.Close()
|
|
if err != nil {
|
|
// If we fail to close the writer we need to communicate back that the function has failed; if there's
|
|
// a streaming error doing that then we are basically in the "double exception" case and who knows
|
|
// what's best to do. Submit has already finished so we don't need to cancel... but at least we should
|
|
// set streamError if it's not set.
|
|
state.stateMutex.Lock()
|
|
defer state.stateMutex.Unlock()
|
|
if state.streamError == nil {
|
|
err2 := state.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Finished{Finished: &runner.CallFinished{
|
|
Success: false,
|
|
Details: fmt.Sprintf("%v", err),
|
|
}}})
|
|
if err2 != nil {
|
|
state.streamError = err2
|
|
}
|
|
}
|
|
state.done <- err
|
|
return
|
|
}
|
|
// At this point everything should have worked. Send a successful message... and if that runs afoul of a
|
|
// stream error, well, we're in a bit of trouble. Everything has finished, so there is nothing to cancel
|
|
// and we just give up, but at least we set streamError.
|
|
state.stateMutex.Lock()
|
|
defer state.stateMutex.Unlock()
|
|
if state.streamError == nil {
|
|
err2 := state.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Finished{Finished: &runner.CallFinished{
|
|
Success: true,
|
|
Details: state.c.Model().ID,
|
|
}}})
|
|
if err2 != nil {
|
|
state.streamError = err2
|
|
state.done <- err2
|
|
return
|
|
}
|
|
}
|
|
|
|
state.done <- nil
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (pr *pureRunner) handleData(ctx context.Context, data *runner.DataFrame, state *callHandle) error {
|
|
pr.ensureFunctionIsRunning(state)
|
|
|
|
// Only push the input if we're in a non-error situation
|
|
state.stateMutex.Lock()
|
|
defer state.stateMutex.Unlock()
|
|
if state.streamError == nil {
|
|
if len(data.Data) > 0 {
|
|
_, err := state.input.Write(data.Data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if data.Eof {
|
|
state.input.Close()
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pr *pureRunner) handleTryCall(ctx context.Context, tc *runner.TryCall, state *callHandle) (capacityDeallocator, error) {
|
|
state.receivedTime = strfmt.DateTime(time.Now())
|
|
var c models.Call
|
|
err := json.Unmarshal([]byte(tc.ModelsCallJson), &c)
|
|
if err != nil {
|
|
return func() {}, err
|
|
}
|
|
|
|
// Capacity check first
|
|
err = pr.capacity.checkAndReserveCapacity(c.Memory)
|
|
if err != nil {
|
|
return func() {}, err
|
|
}
|
|
|
|
// Proceed!
|
|
var w http.ResponseWriter
|
|
w = state
|
|
inR, inW := io.Pipe()
|
|
agent_call, err := pr.a.GetCall(FromModelAndInput(&c, inR), WithWriter(w))
|
|
if err != nil {
|
|
return func() { pr.capacity.releaseCapacity(c.Memory) }, err
|
|
}
|
|
state.c = agent_call.(*call)
|
|
state.input = inW
|
|
state.allocatedTime = strfmt.DateTime(time.Now())
|
|
|
|
return func() { pr.capacity.releaseCapacity(c.Memory) }, nil
|
|
}
|
|
|
|
// Handles a client engagement
|
|
func (pr *pureRunner) Engage(engagement runner.RunnerProtocol_EngageServer) error {
|
|
// Keep lightweight tabs on what this runner is doing: for draindown tests
|
|
atomic.AddInt32(&pr.inflight, 1)
|
|
defer atomic.AddInt32(&pr.inflight, -1)
|
|
|
|
pv, ok := peer.FromContext(engagement.Context())
|
|
logrus.Debug("Starting engagement")
|
|
if ok {
|
|
logrus.Debug("Peer is ", pv)
|
|
}
|
|
md, ok := metadata.FromIncomingContext(engagement.Context())
|
|
if ok {
|
|
logrus.Debug("MD is ", md)
|
|
}
|
|
|
|
var state = callHandle{
|
|
engagement: engagement,
|
|
c: nil,
|
|
input: nil,
|
|
started: false,
|
|
done: make(chan error),
|
|
streamError: nil,
|
|
outHeaders: make(http.Header),
|
|
outStatus: 200,
|
|
headerWritten: false,
|
|
}
|
|
|
|
grpc.EnableTracing = false
|
|
logrus.Debug("Entering engagement handler")
|
|
|
|
msg, err := engagement.Recv()
|
|
if err != nil {
|
|
// In this case the connection has dropped before we've even started.
|
|
return err
|
|
}
|
|
switch body := msg.Body.(type) {
|
|
case *runner.ClientMsg_Try:
|
|
dealloc, err := pr.handleTryCall(engagement.Context(), body.Try, &state)
|
|
defer dealloc()
|
|
// At the stage of TryCall, there is only one thread running and nothing has happened yet so there should
|
|
// not be a streamError. We can handle `err` by sending a message back. If we cause a stream error by sending
|
|
// the message, we are in a "double exception" case and we might as well cancel the call with the original
|
|
// error, so we can ignore the error from Send.
|
|
if err != nil {
|
|
_ = engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Acknowledged{Acknowledged: &runner.CallAcknowledged{
|
|
Committed: false,
|
|
Details: fmt.Sprintf("%v", err),
|
|
}}})
|
|
state.cancel(engagement.Context(), err)
|
|
return err
|
|
}
|
|
|
|
// If we succeed in creating the call, but we get a stream error sending a message back, we must cancel
|
|
// the call because we've probably lost the connection.
|
|
err = engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Acknowledged{Acknowledged: &runner.CallAcknowledged{
|
|
Committed: true,
|
|
Details: state.c.Model().ID,
|
|
SlotAllocationLatency: time.Time(state.allocatedTime).Sub(time.Time(state.receivedTime)).String(),
|
|
}}})
|
|
if err != nil {
|
|
state.cancel(engagement.Context(), err)
|
|
return err
|
|
}
|
|
|
|
// Then at this point we start handling the data that should be being pushed to us.
|
|
foundEof := false
|
|
for !foundEof {
|
|
msg, err := engagement.Recv()
|
|
if err != nil {
|
|
// In this case the connection has dropped or there's something bad happening. We know we can't even
|
|
// send a message back. Cancel the call, all bets are off.
|
|
state.cancel(engagement.Context(), err)
|
|
return err
|
|
}
|
|
|
|
switch body := msg.Body.(type) {
|
|
case *runner.ClientMsg_Data:
|
|
err := pr.handleData(engagement.Context(), body.Data, &state)
|
|
if err != nil {
|
|
// If this happens, then we couldn't write into the input. The state of the function is inconsistent
|
|
// and therefore we need to cancel. We also need to communicate back that the function has failed;
|
|
// that could also run afoul of a stream error, but at that point we don't care, just cancel the
|
|
// call with the original error.
|
|
_ = state.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Finished{Finished: &runner.CallFinished{
|
|
Success: false,
|
|
Details: fmt.Sprintf("%v", err),
|
|
}}})
|
|
state.cancel(engagement.Context(), err)
|
|
return err
|
|
}
|
|
// Then break the loop if this was the last input data frame, i.e. eof is on
|
|
if body.Data.Eof {
|
|
foundEof = true
|
|
}
|
|
default:
|
|
err := errors.New("Protocol failure in communication with function runner")
|
|
// This is essentially a panic. Try to communicate back that the call has failed, and bail out; that
|
|
// could also run afoul of a stream error, but at that point we don't care, just cancel the call with
|
|
// the catastrophic error.
|
|
_ = state.engagement.Send(&runner.RunnerMsg{
|
|
Body: &runner.RunnerMsg_Finished{Finished: &runner.CallFinished{
|
|
Success: false,
|
|
Details: fmt.Sprintf("%v", err),
|
|
}}})
|
|
state.cancel(engagement.Context(), err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Synchronize to the function running goroutine finishing
|
|
select {
|
|
case <-state.done:
|
|
case <-engagement.Context().Done():
|
|
return engagement.Context().Err()
|
|
}
|
|
|
|
default:
|
|
// Protocol error. This should not happen.
|
|
return errors.New("Protocol failure in communication with function runner")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (pr *pureRunner) Status(ctx context.Context, _ *empty.Empty) (*runner.RunnerStatus, error) {
|
|
return &runner.RunnerStatus{
|
|
Active: atomic.LoadInt32(&pr.inflight),
|
|
}, nil
|
|
}
|
|
|
|
func (pr *pureRunner) Start() error {
|
|
logrus.Info("Pure Runner listening on ", pr.listen)
|
|
lis, err := net.Listen("tcp", pr.listen)
|
|
if err != nil {
|
|
return fmt.Errorf("Could not listen on %s: %s", pr.listen, err)
|
|
}
|
|
|
|
if err := pr.gRPCServer.Serve(lis); err != nil {
|
|
return fmt.Errorf("grpc serve error: %s", err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func CreatePureRunner(addr string, a Agent, cert string, key string, ca string) (*pureRunner, error) {
|
|
if cert != "" && key != "" && ca != "" {
|
|
c, err := creds(cert, key, ca)
|
|
if err != nil {
|
|
logrus.WithField("runner_addr", addr).Warn("Failed to create credentials!")
|
|
return nil, err
|
|
}
|
|
return createPureRunner(addr, a, c)
|
|
}
|
|
|
|
logrus.Warn("Running pure runner in insecure mode!")
|
|
return createPureRunner(addr, a, nil)
|
|
}
|
|
|
|
func creds(cert string, key string, ca string) (credentials.TransportCredentials, error) {
|
|
// Load the certificates from disk
|
|
certificate, err := tls.LoadX509KeyPair(cert, key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Could not load server key pair: %s", err)
|
|
}
|
|
|
|
// Create a certificate pool from the certificate authority
|
|
certPool := x509.NewCertPool()
|
|
authority, err := ioutil.ReadFile(ca)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Could not read ca certificate: %s", err)
|
|
}
|
|
|
|
if ok := certPool.AppendCertsFromPEM(authority); !ok {
|
|
return nil, errors.New("Failed to append client certs")
|
|
}
|
|
|
|
return credentials.NewTLS(&tls.Config{
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
Certificates: []tls.Certificate{certificate},
|
|
ClientCAs: certPool,
|
|
}), nil
|
|
}
|
|
|
|
const megabyte uint64 = 1024 * 1024
|
|
|
|
func createPureRunner(addr string, a Agent, creds credentials.TransportCredentials) (*pureRunner, error) {
|
|
var srv *grpc.Server
|
|
if creds != nil {
|
|
srv = grpc.NewServer(grpc.Creds(creds))
|
|
} else {
|
|
srv = grpc.NewServer()
|
|
}
|
|
memUnits := getAvailableMemoryUnits()
|
|
pr := &pureRunner{
|
|
gRPCServer: srv,
|
|
listen: addr,
|
|
a: a,
|
|
capacity: newPureRunnerCapacityManager(memUnits),
|
|
}
|
|
|
|
runner.RegisterRunnerProtocolServer(srv, pr)
|
|
return pr, nil
|
|
}
|
|
|
|
func getAvailableMemoryUnits() uint64 {
|
|
// To reuse code - but it's a bit of a hack. TODO: refactor the OS-specific get memory funcs out of that.
|
|
throwawayRT := NewResourceTracker().(*resourceTracker)
|
|
return throwawayRT.ramAsyncTotal
|
|
}
|