diff --git a/README.md b/README.md index f911b6b..8da3276 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ trader data verify ... # check canonical bars against the raw they wer trader data audit ... # one integrity report for a range (text or JSON) trader data migrate ... # move an old-layout raw archive to the current layout (dry run unless --apply) trader backtest ... # backtest canonical bars from the data store -trader serve # read-only REST API over the canonical bars (docs/rest-api.md) +trader serve # read-only REST API over the canonical bars (docs/rest-api.md) and the loopback strategy service (ADR-094) ``` `trader data download` fetches bid/ask candles for a UTC range into month diff --git a/cmd/trader/serve.go b/cmd/trader/serve.go index fe215ba..fdffefc 100644 --- a/cmd/trader/serve.go +++ b/cmd/trader/serve.go @@ -17,6 +17,8 @@ import ( "github.com/rustyeddy/trader/internal/store" "github.com/rustyeddy/trader/rest" "github.com/rustyeddy/trader/service" + "github.com/rustyeddy/trader/strategy/proto" + strategyserver "github.com/rustyeddy/trader/strategy/server" ) // envAPIToken is where the bearer token the API requires comes from (like the @@ -25,9 +27,17 @@ const envAPIToken = "TRADER_API_TOKEN" const defaultListen = "127.0.0.1:8080" +// defaultStrategyListen is where the strategy service listens unless told +// otherwise: loopback only, like the API. strategyOff turns it off. +const ( + defaultStrategyListen = "127.0.0.1:8081" + strategyOff = "off" +) + type serveFlags struct { config, dataRoot string listen string + strategyListen string timeout time.Duration } @@ -48,7 +58,13 @@ environment, in which case every request must carry "Authorization: Bearer marketdata.SourcePartition uses `Load` with `SkipStale`). `rest` imports only `service` and the public `marketdata` model, and no provider (a test enforces it); `cmd/trader/serve.go` reads the environment (`TRADER_API_TOKEN`) and refuses a non-loopback address without it. +- `strategy`, `strategy/proto` and `strategy/server` are the public side of external + strategies (ADR-094). `strategy` is the seam a strategy implements and the requested + `Order`; `strategy/proto` is the wire contract and its strict codec; `strategy/server` + is the host: a loopback TCP listener, registration, the RunID and the lock-step + session. The server imports only `strategy` and `strategy/proto` (tests enforce it, and + that no guest package imports it); the engine is attached through the `Admit` and + `Handle` callbacks (#703). `trader serve` starts it on `127.0.0.1:8081` + (`--strategy-listen host:port` changes the address, `off` turns it off; a non-loopback + address is refused). Until the engine is attached every registration is refused. - `service.MigrateLayout` (`trader data migrate`, ADR-092) moves `raw/` to `/raw` with `store.Move` (one rename, never overwrites), the mapping in `layout.LegacyRawDir` and verification by `store.Inventory`; it is a dry run unless diff --git a/docs/arch/adr-094-external-strategy-wire-protocol-v1.org b/docs/arch/adr-094-external-strategy-wire-protocol-v1.org index ef87920..e6bf2ea 100644 --- a/docs/arch/adr-094-external-strategy-wire-protocol-v1.org +++ b/docs/arch/adr-094-external-strategy-wire-protocol-v1.org @@ -149,13 +149,14 @@ Every message is an object with these members: *** =accepted= (Trader to worker) #+begin_src json -{"v":1,"type":"accepted","seq":1,"run_id":"r-20241103-7f3a9c", +{"v":1,"type":"accepted","seq":1,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F", "instruments":["EURUSD","GBPUSD"],"interval":"H1", "warm_bars":100,"reply_timeout_ms":30000} #+end_src Echoes what the host granted (it may grant fewer =warm_bars=) and the reply -timeout that applies. The RunID is unique per run and is the key to everything +timeout that applies. The RunID is a ULID (26 Crockford Base32 characters: a millisecond timestamp and +80 random bits, so RunIDs sort by creation time; the host issues them monotonically). It is unique per run and is the key to everything Trader records about it. *** =bars= (Trader to worker) @@ -163,7 +164,7 @@ Trader records about it. One message per availability instant, never more than one outstanding. #+begin_src json -{"v":1,"type":"bars","seq":2,"run_id":"r-20241103-7f3a9c", +{"v":1,"type":"bars","seq":2,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F", "frame_at":"2024-03-01T11:00:00Z","warm":false, "bars":[ {"instrument":"EURUSD","time":"2024-03-01T10:00:00Z", @@ -201,7 +202,7 @@ Exactly one per =bars=, possibly empty. The orders are submitted in array order, after the worker has seen the whole group. #+begin_src json -{"v":1,"type":"orders","seq":2,"run_id":"r-20241103-7f3a9c", +{"v":1,"type":"orders","seq":2,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F", "in_reply_to":2, "orders":[ {"instrument":"EURUSD","action":"open","direction":"long","type":"market", @@ -215,7 +216,7 @@ An empty reply is =[]=: nothing to do on that frame. *** =done= (Trader to worker) #+begin_src json -{"v":1,"type":"done","seq":812,"run_id":"r-20241103-7f3a9c", +{"v":1,"type":"done","seq":812,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F", "status":"completed","reason":""} #+end_src @@ -226,7 +227,7 @@ closes after a short grace. *** =error= (either direction) #+begin_src json -{"v":1,"type":"error","seq":3,"run_id":"r-20241103-7f3a9c", +{"v":1,"type":"error","seq":3,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F", "code":"invalid_order","fatal":true, "message":"orders[0]: open requires stop_loss"} #+end_src @@ -400,7 +401,7 @@ one element; no frame is sent for an instant at which no instrument has a bar. *** Opening a short with exits #+begin_src json -{"v":1,"type":"orders","seq":3,"run_id":"r-20241103-7f3a9c","in_reply_to":3, +{"v":1,"type":"orders","seq":3,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":3, "orders":[{"instrument":"GBPUSD","action":"open","direction":"short", "type":"limit","price":"1.26600","stop_loss":"1.26900", "take_profit":"1.26000","expires":"2024-03-01T20:00:00Z"}]} @@ -409,13 +410,13 @@ one element; no frame is sent for an instant at which no instrument has a bar. *** Moving a stop, then turning it into a trail #+begin_src json -{"v":1,"type":"orders","seq":5,"run_id":"r-20241103-7f3a9c","in_reply_to":5, +{"v":1,"type":"orders","seq":5,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":5, "orders":[{"instrument":"EURUSD","action":"amend","stop_loss":"1.08764", "ref":"emax-breakeven-1"}]} #+end_src #+begin_src json -{"v":1,"type":"orders","seq":6,"run_id":"r-20241103-7f3a9c","in_reply_to":6, +{"v":1,"type":"orders","seq":6,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":6, "orders":[{"instrument":"EURUSD","action":"amend","trail":"0.00150", "ref":"emax-trail-1"}]} #+end_src @@ -423,7 +424,7 @@ one element; no frame is sent for an instant at which no instrument has a bar. *** Tolerated unknown member (additive evolution) #+begin_src json -{"v":1,"type":"orders","seq":4,"run_id":"r-20241103-7f3a9c","in_reply_to":4, +{"v":1,"type":"orders","seq":4,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":4, "client_note":"ignored","orders":[]} #+end_src diff --git a/strategy/proto/decode_test.go b/strategy/proto/decode_test.go index 0c0e925..bdd8a05 100644 --- a/strategy/proto/decode_test.go +++ b/strategy/proto/decode_test.go @@ -113,11 +113,11 @@ func TestDecode_RegisterRefusals(t *testing.T) { } func TestDecode_Accepted(t *testing.T) { - in := `{"v":1,"type":"accepted","seq":1,"run_id":"r-20241103-7f3a9c","instruments":["EURUSD","GBPUSD"],"interval":"H1","warm_bars":100,"reply_timeout_ms":30000}` + in := `{"v":1,"type":"accepted","seq":1,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","instruments":["EURUSD","GBPUSD"],"interval":"H1","warm_bars":100,"reply_timeout_ms":30000}` m := decode(t, in).(*Accepted) - assert.Equal(t, Header{V: 1, Type: TypeAccepted, Seq: 1, RunID: "r-20241103-7f3a9c"}, m.Header) + assert.Equal(t, Header{V: 1, Type: TypeAccepted, Seq: 1, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, m.Header) assert.Equal(t, []string{"EURUSD", "GBPUSD"}, m.Instruments) assert.Equal(t, "H1", m.Interval) assert.Equal(t, 100, m.WarmBars) diff --git a/strategy/proto/encode_test.go b/strategy/proto/encode_test.go index 547adab..e3711a9 100644 --- a/strategy/proto/encode_test.go +++ b/strategy/proto/encode_test.go @@ -40,28 +40,28 @@ func golden() map[string]Message { From: ts("2024-03-01T00:00:00Z"), To: ts("2024-04-01T00:00:00Z"), WarmBars: 100}}, "register-paper": &Register{Header: Header{Seq: 1}, Name: "emax-go", Capabilities: []string{"bars"}, Run: Run{Mode: ModePaper, Interval: "D1", Instruments: []string{"EURUSD"}, From: ts("2024-03-01T00:00:00Z")}}, - "accepted": &Accepted{Header: Header{Seq: 1, RunID: "r-20241103-7f3a9c"}, Instruments: []string{"EURUSD", "GBPUSD"}, + "accepted": &Accepted{Header: Header{Seq: 1, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, Instruments: []string{"EURUSD", "GBPUSD"}, Interval: "H1", WarmBars: 100, ReplyTimeoutMS: 30000}, - "bars": &Bars{Header: Header{Seq: 2, RunID: "r-20241103-7f3a9c"}, FrameAt: ts("2024-03-01T11:00:00Z"), Bars: []marketdata.Bar{ + "bars": &Bars{Header: Header{Seq: 2, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, FrameAt: ts("2024-03-01T11:00:00Z"), Bars: []marketdata.Bar{ {Instrument: "EURUSD", Time: ts("2024-03-01T10:00:00Z"), AvailableAt: ts("2024-03-01T11:00:00Z"), Volume: 2310, Bid: ohlc("1.08725", "1.0879", "1.0869", "1.0876"), Ask: ohlc("1.08728", "1.08794", "1.08693", "1.08764"), Mid: ohlc("1.087265", "1.08792", "1.086915", "1.08762")}, {Instrument: "GBPUSD", Time: ts("2024-03-01T10:00:00Z"), AvailableAt: ts("2024-03-01T11:00:00Z"), Volume: 1984, Mid: ohlc("1.26412", "1.264825", "1.26372", "1.264575"), Trade: ohlc("1.2641", "1.2648", "1.2637", "1.26455")}}}, - "bars-warm": &Bars{Header: Header{Seq: 2, RunID: "r-20241103-7f3a9c"}, FrameAt: ts("2024-02-29T11:00:00Z"), Warm: true, + "bars-warm": &Bars{Header: Header{Seq: 2, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, FrameAt: ts("2024-02-29T11:00:00Z"), Warm: true, Bars: []marketdata.Bar{{Instrument: "EURUSD", Time: ts("2024-02-29T10:00:00Z"), AvailableAt: ts("2024-02-29T11:00:00Z"), Mid: ohlc("1.08", "1.081", "1.079", "1.0805")}}}, - "orders": &Orders{Header: Header{Seq: 2, RunID: "r-20241103-7f3a9c"}, InReplyTo: 2, Orders: []strategy.Order{ + "orders": &Orders{Header: Header{Seq: 2, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, InReplyTo: 2, Orders: []strategy.Order{ {Instrument: "EURUSD", Action: strategy.Open, Direction: strategy.Long, Type: strategy.Market, Price: pr("1.08764"), StopLoss: pr("1.085"), TakeProfit: pr("1.092"), Trail: pr("0.002"), Ref: "emax-entry-1"}, {Instrument: "GBPUSD", Action: strategy.Open, Direction: strategy.Short, Type: strategy.Limit, Price: pr("1.266"), StopLoss: pr("1.269"), TakeProfit: pr("1.26"), Expires: ts("2024-03-01T20:00:00Z")}, {Instrument: "GBPUSD", Action: strategy.Close, Ref: "emax-exit-7"}}}, - "orders-empty": &Orders{Header: Header{Seq: 3, RunID: "r-20241103-7f3a9c"}, InReplyTo: 3}, - "orders-amend": &Orders{Header: Header{Seq: 5, RunID: "r-20241103-7f3a9c"}, InReplyTo: 5, Orders: []strategy.Order{ + "orders-empty": &Orders{Header: Header{Seq: 3, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, InReplyTo: 3}, + "orders-amend": &Orders{Header: Header{Seq: 5, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, InReplyTo: 5, Orders: []strategy.Order{ {Instrument: "EURUSD", Action: strategy.Amend, StopLoss: pr("1.08764"), Trail: pr("0.0015"), TakeProfit: pr("1.1"), Ref: "emax-breakeven-1"}}}, - "done": &Done{Header: Header{Seq: 812, RunID: "r-20241103-7f3a9c"}, Status: StatusCompleted}, - "error": &Failure{Header: Header{Seq: 3, RunID: "r-20241103-7f3a9c"}, Code: CodeInvalidOrder, Fatal: true, Message: "orders[0]: open requires stop_loss"}, + "done": &Done{Header: Header{Seq: 812, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, Status: StatusCompleted}, + "error": &Failure{Header: Header{Seq: 3, RunID: "01JC8ZXQ5T3V9R0K4M7N2B6D8F"}, Code: CodeInvalidOrder, Fatal: true, Message: "orders[0]: open requires stop_loss"}, "error-before-accepted": &Failure{Header: Header{Seq: 1}, Code: CodeVersion, Fatal: true, Message: "protocol version 2 is not supported"}, } } diff --git a/strategy/proto/testdata/golden/accepted.json b/strategy/proto/testdata/golden/accepted.json index de0dee3..972aabe 100644 --- a/strategy/proto/testdata/golden/accepted.json +++ b/strategy/proto/testdata/golden/accepted.json @@ -1 +1 @@ -{"v":1,"type":"accepted","seq":1,"run_id":"r-20241103-7f3a9c","instruments":["EURUSD","GBPUSD"],"interval":"H1","warm_bars":100,"reply_timeout_ms":30000} +{"v":1,"type":"accepted","seq":1,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","instruments":["EURUSD","GBPUSD"],"interval":"H1","warm_bars":100,"reply_timeout_ms":30000} diff --git a/strategy/proto/testdata/golden/bars-warm.json b/strategy/proto/testdata/golden/bars-warm.json index 4a97400..35a6cd0 100644 --- a/strategy/proto/testdata/golden/bars-warm.json +++ b/strategy/proto/testdata/golden/bars-warm.json @@ -1 +1 @@ -{"v":1,"type":"bars","seq":2,"run_id":"r-20241103-7f3a9c","frame_at":"2024-02-29T11:00:00Z","warm":true,"bars":[{"instrument":"EURUSD","time":"2024-02-29T10:00:00Z","available_at":"2024-02-29T11:00:00Z","mid":{"open":"1.08000","high":"1.08100","low":"1.07900","close":"1.08050"},"volume":0}]} +{"v":1,"type":"bars","seq":2,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","frame_at":"2024-02-29T11:00:00Z","warm":true,"bars":[{"instrument":"EURUSD","time":"2024-02-29T10:00:00Z","available_at":"2024-02-29T11:00:00Z","mid":{"open":"1.08000","high":"1.08100","low":"1.07900","close":"1.08050"},"volume":0}]} diff --git a/strategy/proto/testdata/golden/bars.json b/strategy/proto/testdata/golden/bars.json index 8a411a2..e76aeff 100644 --- a/strategy/proto/testdata/golden/bars.json +++ b/strategy/proto/testdata/golden/bars.json @@ -1 +1 @@ -{"v":1,"type":"bars","seq":2,"run_id":"r-20241103-7f3a9c","frame_at":"2024-03-01T11:00:00Z","warm":false,"bars":[{"instrument":"EURUSD","time":"2024-03-01T10:00:00Z","available_at":"2024-03-01T11:00:00Z","bid":{"open":"1.08725","high":"1.08790","low":"1.08690","close":"1.08760"},"ask":{"open":"1.08728","high":"1.08794","low":"1.08693","close":"1.08764"},"mid":{"open":"1.087265","high":"1.08792","low":"1.086915","close":"1.08762"},"volume":2310},{"instrument":"GBPUSD","time":"2024-03-01T10:00:00Z","available_at":"2024-03-01T11:00:00Z","mid":{"open":"1.26412","high":"1.264825","low":"1.26372","close":"1.264575"},"trade":{"open":"1.26410","high":"1.26480","low":"1.26370","close":"1.26455"},"volume":1984}]} +{"v":1,"type":"bars","seq":2,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","frame_at":"2024-03-01T11:00:00Z","warm":false,"bars":[{"instrument":"EURUSD","time":"2024-03-01T10:00:00Z","available_at":"2024-03-01T11:00:00Z","bid":{"open":"1.08725","high":"1.08790","low":"1.08690","close":"1.08760"},"ask":{"open":"1.08728","high":"1.08794","low":"1.08693","close":"1.08764"},"mid":{"open":"1.087265","high":"1.08792","low":"1.086915","close":"1.08762"},"volume":2310},{"instrument":"GBPUSD","time":"2024-03-01T10:00:00Z","available_at":"2024-03-01T11:00:00Z","mid":{"open":"1.26412","high":"1.264825","low":"1.26372","close":"1.264575"},"trade":{"open":"1.26410","high":"1.26480","low":"1.26370","close":"1.26455"},"volume":1984}]} diff --git a/strategy/proto/testdata/golden/done.json b/strategy/proto/testdata/golden/done.json index 47f6147..a504c6f 100644 --- a/strategy/proto/testdata/golden/done.json +++ b/strategy/proto/testdata/golden/done.json @@ -1 +1 @@ -{"v":1,"type":"done","seq":812,"run_id":"r-20241103-7f3a9c","status":"completed","reason":""} +{"v":1,"type":"done","seq":812,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","status":"completed","reason":""} diff --git a/strategy/proto/testdata/golden/error.json b/strategy/proto/testdata/golden/error.json index 60dec3b..2e0e29e 100644 --- a/strategy/proto/testdata/golden/error.json +++ b/strategy/proto/testdata/golden/error.json @@ -1 +1 @@ -{"v":1,"type":"error","seq":3,"run_id":"r-20241103-7f3a9c","code":"invalid_order","fatal":true,"message":"orders[0]: open requires stop_loss"} +{"v":1,"type":"error","seq":3,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","code":"invalid_order","fatal":true,"message":"orders[0]: open requires stop_loss"} diff --git a/strategy/proto/testdata/golden/orders-amend.json b/strategy/proto/testdata/golden/orders-amend.json index 62258a0..b76067e 100644 --- a/strategy/proto/testdata/golden/orders-amend.json +++ b/strategy/proto/testdata/golden/orders-amend.json @@ -1 +1 @@ -{"v":1,"type":"orders","seq":5,"run_id":"r-20241103-7f3a9c","in_reply_to":5,"orders":[{"instrument":"EURUSD","action":"amend","type":"market","stop_loss":"1.08764","take_profit":"1.10000","trail":"0.00150","ref":"emax-breakeven-1"}]} +{"v":1,"type":"orders","seq":5,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":5,"orders":[{"instrument":"EURUSD","action":"amend","type":"market","stop_loss":"1.08764","take_profit":"1.10000","trail":"0.00150","ref":"emax-breakeven-1"}]} diff --git a/strategy/proto/testdata/golden/orders-empty.json b/strategy/proto/testdata/golden/orders-empty.json index 9f6a97e..2cd23a3 100644 --- a/strategy/proto/testdata/golden/orders-empty.json +++ b/strategy/proto/testdata/golden/orders-empty.json @@ -1 +1 @@ -{"v":1,"type":"orders","seq":3,"run_id":"r-20241103-7f3a9c","in_reply_to":3,"orders":[]} +{"v":1,"type":"orders","seq":3,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":3,"orders":[]} diff --git a/strategy/proto/testdata/golden/orders.json b/strategy/proto/testdata/golden/orders.json index 7f1071c..c29dc33 100644 --- a/strategy/proto/testdata/golden/orders.json +++ b/strategy/proto/testdata/golden/orders.json @@ -1 +1 @@ -{"v":1,"type":"orders","seq":2,"run_id":"r-20241103-7f3a9c","in_reply_to":2,"orders":[{"instrument":"EURUSD","action":"open","direction":"long","type":"market","price":"1.08764","stop_loss":"1.08500","take_profit":"1.09200","trail":"0.00200","ref":"emax-entry-1"},{"instrument":"GBPUSD","action":"open","direction":"short","type":"limit","price":"1.26600","stop_loss":"1.26900","take_profit":"1.26000","expires":"2024-03-01T20:00:00Z"},{"instrument":"GBPUSD","action":"close","type":"market","ref":"emax-exit-7"}]} +{"v":1,"type":"orders","seq":2,"run_id":"01JC8ZXQ5T3V9R0K4M7N2B6D8F","in_reply_to":2,"orders":[{"instrument":"EURUSD","action":"open","direction":"long","type":"market","price":"1.08764","stop_loss":"1.08500","take_profit":"1.09200","trail":"0.00200","ref":"emax-entry-1"},{"instrument":"GBPUSD","action":"open","direction":"short","type":"limit","price":"1.26600","stop_loss":"1.26900","take_profit":"1.26000","expires":"2024-03-01T20:00:00Z"},{"instrument":"GBPUSD","action":"close","type":"market","ref":"emax-exit-7"}]} diff --git a/strategy/server/runid.go b/strategy/server/runid.go new file mode 100644 index 0000000..9fdc798 --- /dev/null +++ b/strategy/server/runid.go @@ -0,0 +1,104 @@ +package server + +import ( + "crypto/rand" + "errors" + "fmt" + "sync" + "time" +) + +// ErrRunIDExhausted reports that 2^80 RunIDs were issued within one millisecond. +var ErrRunIDExhausted = errors.New("run id: entropy exhausted within one millisecond") + +// crockford is the ULID alphabet (Crockford Base32 without I, L, O and U). +const crockford = "0123456789ABCDEFGHJKMNPQRSTVWXYZ" + +// RunIDLen is the length of a RunID: a ULID in its 26-character text form. +const RunIDLen = 26 + +// RunIDGenerator issues ULIDs (https://github.com/ulid/spec): a 48-bit +// millisecond timestamp and 80 bits of entropy, so RunIDs sort by creation time +// and need no central counter. It is monotonic: IDs issued within one +// millisecond, or after the clock steps back, increment the previous entropy, so +// they still sort in issue order and never repeat. It is safe for concurrent use. +type RunIDGenerator struct { + // Now and Entropy are injectable for tests; nil means time.Now and crypto/rand. + Now func() time.Time + Entropy func([]byte) error + + mu sync.Mutex + ms int64 + rand [10]byte + have bool +} + +// NewRunID issues the next RunID. Its signature is Config.NewRunID. +func (g *RunIDGenerator) NewRunID() (string, error) { + now := time.Now + if g.Now != nil { + now = g.Now + } + ms := now().UnixMilli() + if ms < 0 || ms >= 1<<48 { + return "", fmt.Errorf("run id: time %d ms is outside the 48-bit ULID range", ms) + } + + g.mu.Lock() + defer g.mu.Unlock() + if g.have && ms <= g.ms { + // Same millisecond, or the clock went back: keep the old timestamp (g.ms) and + // count up so the order and uniqueness hold. + overflow := true + for i := len(g.rand) - 1; i >= 0; i-- { + g.rand[i]++ + if g.rand[i] != 0 { + overflow = false + break + } + } + if overflow { + return "", ErrRunIDExhausted + } + } else { + fill := g.Entropy + if fill == nil { + fill = func(b []byte) error { _, err := rand.Read(b); return err } + } + var e [10]byte + if err := fill(e[:]); err != nil { + return "", fmt.Errorf("run id: %w", err) + } + g.rand, g.ms, g.have = e, ms, true + } + return encodeULID(g.ms, g.rand), nil +} + +// encodeULID writes the 128 bits (timestamp then entropy) as 26 Crockford digits, +// most significant first; the first digit carries only the top 3 bits. +func encodeULID(ms int64, e [10]byte) string { + var v [16]byte + for i := 0; i < 6; i++ { + v[i] = byte(ms >> (8 * (5 - i))) + } + copy(v[6:], e[:]) + var out [RunIDLen]byte + // Walk the 130-bit (2 zero bits + 128) value five bits at a time. + for i := 0; i < RunIDLen; i++ { + var d byte + for b := 0; b < 5; b++ { + bit := i*5 + b - 2 // index into the 128 bits; negative for the 2 padding bits + d <<= 1 + if bit >= 0 { + d |= (v[bit/8] >> (7 - bit%8)) & 1 + } + } + out[i] = crockford[d] + } + return string(out[:]) +} + +// RandomRunID issues a RunID from a process-wide RunIDGenerator. +func RandomRunID() (string, error) { return defaultRunIDs.NewRunID() } + +var defaultRunIDs RunIDGenerator diff --git a/strategy/server/runid_test.go b/strategy/server/runid_test.go new file mode 100644 index 0000000..0bba6a2 --- /dev/null +++ b/strategy/server/runid_test.go @@ -0,0 +1,116 @@ +package server + +import ( + "errors" + "regexp" + "sort" + "sync" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func fixedEntropy(b byte) func([]byte) error { + return func(p []byte) error { + for i := range p { + p[i] = b + } + return nil + } +} + +func TestEncodeULID_KnownValues(t *testing.T) { + assert.Equal(t, "00000000000000000000000000", encodeULID(0, [10]byte{})) + assert.Equal(t, "7ZZZZZZZZZZZZZZZZZZZZZZZZZ", encodeULID(1<<48-1, [10]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255})) + // The ULID spec's example timestamp, 2016-07-30T23:54:10.385Z. + assert.Equal(t, "01ARYZ6S41", encodeULID(1469918176385, [10]byte{})[:10]) + // One low entropy bit is the last digit. + assert.Equal(t, "00000000000000000000000001", encodeULID(0, [10]byte{9: 1})) + // The first entropy bit is the top bit of the 11th digit (G = 16). + assert.Equal(t, "0000000000G000000000000000", encodeULID(0, [10]byte{0: 0x80})) +} + +func TestRunIDGenerator_IsAULIDAndSortsByTime(t *testing.T) { + now := time.UnixMilli(1700000000000) + g := &RunIDGenerator{Now: func() time.Time { return now }, Entropy: fixedEntropy(7)} + a, err := g.NewRunID() + require.NoError(t, err) + assert.Regexp(t, regexp.MustCompile(`^[0-9A-HJKMNP-TV-Z]{26}$`), a) + now = now.Add(time.Millisecond) + b, err := g.NewRunID() + require.NoError(t, err) + assert.Less(t, a, b, "a later millisecond sorts later") + assert.Equal(t, RunIDLen, len(a)) +} + +func TestRunIDGenerator_IsMonotonicWithinAMillisecond(t *testing.T) { + now := time.UnixMilli(1700000000000) + g := &RunIDGenerator{Now: func() time.Time { return now }, Entropy: fixedEntropy(0)} + var ids []string + for range 5 { + id, err := g.NewRunID() + require.NoError(t, err) + ids = append(ids, id) + } + assert.True(t, sort.StringsAreSorted(ids), "same-millisecond IDs sort in issue order: %v", ids) + assert.Len(t, map[string]bool{ids[0]: true, ids[1]: true, ids[2]: true, ids[3]: true, ids[4]: true}, 5) +} + +func TestRunIDGenerator_AClockThatStepsBackNeitherRepeatsNorReorders(t *testing.T) { + now := time.UnixMilli(1700000005000) + g := &RunIDGenerator{Now: func() time.Time { return now }, Entropy: fixedEntropy(1)} + a, _ := g.NewRunID() + now = time.UnixMilli(1700000000000) + b, err := g.NewRunID() + require.NoError(t, err) + assert.Greater(t, b, a) +} + +func TestRunIDGenerator_Errors(t *testing.T) { + t.Run("entropy failure", func(t *testing.T) { + g := &RunIDGenerator{Entropy: func([]byte) error { return errors.New("no entropy") }} + _, err := g.NewRunID() + assert.ErrorContains(t, err, "no entropy") + }) + t.Run("exhausted within one millisecond", func(t *testing.T) { + now := time.UnixMilli(1700000000000) + g := &RunIDGenerator{Now: func() time.Time { return now }, Entropy: fixedEntropy(0xFF)} + _, err := g.NewRunID() // entropy is already the maximum + require.NoError(t, err) + _, err = g.NewRunID() + assert.ErrorIs(t, err, ErrRunIDExhausted) + }) + t.Run("time out of range", func(t *testing.T) { + g := &RunIDGenerator{Now: func() time.Time { return time.UnixMilli(-1) }} + _, err := g.NewRunID() + assert.Error(t, err) + g = &RunIDGenerator{Now: func() time.Time { return time.UnixMilli(1 << 48) }} + _, err = g.NewRunID() + assert.Error(t, err) + }) +} + +func TestRandomRunID_IsUniqueAndSafeConcurrently(t *testing.T) { + var mu sync.Mutex + seen := map[string]bool{} + var wg sync.WaitGroup + for range 8 { + wg.Add(1) + go func() { + defer wg.Done() + for range 250 { + id, err := RandomRunID() + if !assert.NoError(t, err) { + return + } + mu.Lock() + seen[id] = true + mu.Unlock() + } + }() + } + wg.Wait() + assert.Len(t, seen, 2000) +} diff --git a/strategy/server/server.go b/strategy/server/server.go new file mode 100644 index 0000000..8bf2a0a --- /dev/null +++ b/strategy/server/server.go @@ -0,0 +1,282 @@ +// Package server is the host side of ADR-094: it listens on loopback for +// workers, accepts one registration per connection as one run, and drives the +// lock-step exchange of bars and orders. It knows nothing about the engine, an +// account or a broker: the host supplies those decisions through Config (Admit +// decides whether and how a run is allowed, Handle runs it), so nothing a worker +// sends can choose an account, a size or a broker action. +// +// It imports only the standard library, marketdata, strategy and strategy/proto, +// and strategy/client must never import it, so a guest does not link host code. +package server + +import ( + "context" + "errors" + "fmt" + "io" + "log/slog" + "net" + "sync" + "time" + + "github.com/rustyeddy/trader/strategy/proto" +) + +// Defaults of ADR-094 section 7. +const ( + DefaultRegisterTimeout = 10 * time.Second + DefaultReplyTimeout = 30 * time.Second +) + +// Grant is what the host decides about an admitted run. The worker asks; the +// host answers, and everything else about the run (the account, the risk policy, +// the data) stays with the host. +type Grant struct { + // Instruments are the instruments of the run, in delivery order. Empty means + // those the worker registered. + Instruments []string + // WarmBars is the warm-up the host will deliver (it may be fewer than asked). + WarmBars int +} + +// Admitter decides whether a registration may start a run: whether the +// instruments exist, whether the mode is supported, whether policy allows it. It +// returns Reject(...) to refuse the registration (the worker gets error rejected); +// any other error is the host's own failure (error internal). It runs after +// Register.Validate has passed. +type Admitter func(ctx context.Context, reg *proto.Register) (Grant, error) + +// Handler runs one admitted run over the session. It is called after the worker +// has been told the RunID. A nil return finishes the run completed unless the +// handler finished it already; an error finishes it failed. It must return when +// ctx is done. +type Handler func(ctx context.Context, s *Session) error + +// Config configures a Server. Admit and Handle are required. +type Config struct { + Admit Admitter + Handle Handler + + // RegisterTimeout is how long a connection may take to send its register. + // ReplyTimeout is how long a worker may take to answer one bars message. Zero + // means the default of ADR-094. + RegisterTimeout time.Duration + ReplyTimeout time.Duration + // CloseGrace is how long a connection is kept open after the last message so + // the worker can close first. Zero means a second. + CloseGrace time.Duration + // MaxFrame is the largest frame accepted. Zero means proto.DefaultMaxFrame. + MaxFrame int + // NewRunID issues a RunID, a ULID string. Zero means RandomRunID. It must not + // repeat. + NewRunID func() (string, error) + // Logger receives the server's log lines. Nil discards them: a library does + // not choose the process's logger. + Logger *slog.Logger +} + +// Server accepts workers. +type Server struct { + cfg Config + log *slog.Logger +} + +// ErrRejected marks a registration the host refuses. +var ErrRejected = errors.New("registration rejected") + +// Reject returns the error an Admitter gives to refuse a registration. The worker +// is sent the text. +func Reject(format string, args ...any) error { + return fmt.Errorf("%w: %s", ErrRejected, fmt.Sprintf(format, args...)) +} + +// New returns a Server for cfg. +func New(cfg Config) (*Server, error) { + if cfg.Admit == nil || cfg.Handle == nil { + return nil, errors.New("server: Admit and Handle are required") + } + if cfg.RegisterTimeout <= 0 { + cfg.RegisterTimeout = DefaultRegisterTimeout + } + if cfg.ReplyTimeout <= 0 { + cfg.ReplyTimeout = DefaultReplyTimeout + } + if cfg.CloseGrace <= 0 { + cfg.CloseGrace = time.Second + } + if cfg.MaxFrame <= 0 { + cfg.MaxFrame = proto.DefaultMaxFrame + } + if cfg.NewRunID == nil { + cfg.NewRunID = RandomRunID + } + log := cfg.Logger + if log == nil { + log = slog.New(slog.NewTextHandler(io.Discard, nil)) + } + return &Server{cfg: cfg, log: log}, nil +} + +// IsLoopbackAddr reports whether a listen address (host:port) binds to the +// loopback interface only. The host must be an IP literal or "localhost"; an empty +// host or 0.0.0.0 listens on every interface and is not loopback. +func IsLoopbackAddr(addr string) (bool, error) { + host, _, err := net.SplitHostPort(addr) + if err != nil { + return false, fmt.Errorf("listen address %q: %w", addr, err) + } + if host == "localhost" { + return true, nil + } + ip := net.ParseIP(host) + return ip != nil && ip.IsLoopback(), nil +} + +// Listen opens a TCP listener on addr, refusing an address that is not loopback +// (v1 has neither TLS nor a token, ADR-094 section 9). +func Listen(addr string) (net.Listener, error) { + local, err := IsLoopbackAddr(addr) + if err != nil { + return nil, err + } + if !local { + return nil, fmt.Errorf("listen address %q is not loopback: the strategy protocol has no TLS or token in v1", addr) + } + return net.Listen("tcp", addr) +} + +// Serve accepts connections on ln until ctx is done or ln fails, then waits for +// the runs in flight (which are told to cancel). It refuses a listener that is not +// bound to loopback, whoever opened it. It closes ln. +func (s *Server) Serve(ctx context.Context, ln net.Listener) error { + defer func() { _ = ln.Close() }() + if tcp, ok := ln.Addr().(*net.TCPAddr); !ok || !tcp.IP.IsLoopback() { + return fmt.Errorf("server: listener %s is not a loopback TCP address", ln.Addr()) + } + ctx, cancel := context.WithCancel(ctx) + stop := context.AfterFunc(ctx, func() { _ = ln.Close() }) + var wg sync.WaitGroup + // On every exit, tell the runs to cancel and close the listener before waiting + // for them: a worker that never answers must not hold shutdown. + defer func() { + cancel() + stop() + _ = ln.Close() + wg.Wait() + }() + for { + conn, err := ln.Accept() + if err != nil { + if ctx.Err() != nil { + return nil + } + return fmt.Errorf("server: accept: %w", err) + } + wg.Add(1) + go func() { + defer wg.Done() + s.serveConn(ctx, conn) + }() + } +} + +// serveConn is one connection's whole life: register, admission, the run. +func (s *Server) serveConn(ctx context.Context, conn net.Conn) { + defer func() { _ = conn.Close() }() + log := s.log.With("peer", conn.RemoteAddr().String()) + + reg, err := s.readRegister(conn) + if err != nil { + if !errors.Is(err, errSilentClose) { + s.sendError(conn, 1, "", err) + } + log.Info("registration refused", "err", err) + return + } + grant, err := s.cfg.Admit(ctx, reg) + if err != nil { + s.sendError(conn, 1, "", admitError(err)) + log.Info("registration not admitted", "name", reg.Name, "err", err) + return + } + if len(grant.Instruments) == 0 { + grant.Instruments = reg.Run.Instruments + } + runID, err := s.cfg.NewRunID() + if err != nil { + s.sendError(conn, 1, "", &proto.Error{Code: proto.CodeInternal, Msg: "could not issue a run id"}) + log.Error("run id", "err", err) + return + } + accepted := &proto.Accepted{Header: proto.Header{Seq: 1, RunID: runID}, Instruments: grant.Instruments, + Interval: reg.Run.Interval, WarmBars: grant.WarmBars, ReplyTimeoutMS: int(s.cfg.ReplyTimeout / time.Millisecond)} + _ = conn.SetWriteDeadline(time.Now().Add(s.cfg.ReplyTimeout)) + if err := proto.Write(conn, accepted, s.cfg.MaxFrame); err != nil { + log.Info("accepted not delivered", "run", runID, "err", err) + return + } + + sess := newSession(conn, s.cfg, runID, reg, grant, log.With("run", runID)) + log.Info("run accepted", "run", runID, "name", reg.Name, "build", reg.Build, "mode", reg.Run.Mode) + s.run(ctx, sess) +} + +func (s *Server) run(ctx context.Context, sess *Session) { + err := s.cfg.Handle(ctx, sess) + switch { + case sess.isClosed(): + case ctx.Err() != nil: + _ = sess.Finish(proto.StatusCancelled, "the server is shutting down") + case err != nil: + _ = sess.Finish(proto.StatusFailed, err.Error()) + default: + _ = sess.Finish(proto.StatusCompleted, "") + } + sess.log.Info("run ended", "status", sess.status(), "err", err) +} + +// errSilentClose marks a connection that ended before it said anything: there is +// no one to tell. +var errSilentClose = errors.New("connection closed before a register") + +func (s *Server) readRegister(conn net.Conn) (*proto.Register, error) { + _ = conn.SetReadDeadline(time.Now().Add(s.cfg.RegisterTimeout)) + m, err := proto.Read(conn, s.cfg.MaxFrame) + if err != nil { + switch { + case errors.Is(err, io.EOF): + return nil, errSilentClose + case isTimeout(err): + return nil, &proto.Error{Code: proto.CodeTimeout, Msg: "no register within " + s.cfg.RegisterTimeout.String()} + } + var pe *proto.Error + if errors.As(err, &pe) { + return nil, pe + } + return nil, errSilentClose // a transport failure: nobody to tell + } + reg, ok := m.(*proto.Register) + if !ok { + return nil, &proto.Error{Code: proto.CodeProtocol, Msg: fmt.Sprintf("expected register, got %s", m.Head().Type)} + } + if reg.Seq != 1 { + return nil, &proto.Error{Code: proto.CodeProtocol, Msg: fmt.Sprintf("register has seq %d, want 1", reg.Seq)} + } + if err := reg.Validate(); err != nil { + return nil, err + } + return reg, nil +} + +func admitError(err error) *proto.Error { + if errors.Is(err, ErrRejected) { + return &proto.Error{Code: proto.CodeRejected, Msg: err.Error()} + } + return &proto.Error{Code: proto.CodeInternal, Msg: "the host could not admit the run"} +} + +// sendError tells the peer why it is being dropped. It is best effort: the peer +// may already be gone. +func (s *Server) sendError(conn net.Conn, seq uint64, runID string, err error) { + sendFailure(conn, s.cfg.MaxFrame, seq, runID, err) +} diff --git a/strategy/server/server_test.go b/strategy/server/server_test.go new file mode 100644 index 0000000..c6dd420 --- /dev/null +++ b/strategy/server/server_test.go @@ -0,0 +1,646 @@ +package server + +import ( + "context" + "encoding/binary" + "errors" + "fmt" + "go/parser" + "go/token" + "io" + "log/slog" + "net" + "path/filepath" + "regexp" + "strconv" + "strings" + "sync" + "testing" + "time" + + "github.com/rustyeddy/trader/marketdata" + "github.com/rustyeddy/trader/num" + "github.com/rustyeddy/trader/strategy" + "github.com/rustyeddy/trader/strategy/proto" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// rig is a running Server on a loopback port, with a worker's view of it. +type rig struct { + t *testing.T + addr string + cancel context.CancelFunc + done chan struct{} // closed when Serve has returned + err error // what Serve returned, valid once done is closed + srv *Server +} + +var quiet = slog.New(slog.NewTextHandler(io.Discard, nil)) + +func ts(s string) time.Time { + t, err := time.Parse(time.RFC3339, s) + if err != nil { + panic(err) + } + return t +} + +func admitAll(context.Context, *proto.Register) (Grant, error) { return Grant{}, nil } + +func idleHandler(ctx context.Context, _ *Session) error { return nil } + +func start(t *testing.T, cfg Config) *rig { + t.Helper() + if cfg.Admit == nil { + cfg.Admit = admitAll + } + if cfg.Handle == nil { + cfg.Handle = idleHandler + } + if cfg.Logger == nil { + cfg.Logger = quiet + } + if cfg.CloseGrace == 0 { + cfg.CloseGrace = 20 * time.Millisecond + } + srv, err := New(cfg) + require.NoError(t, err) + ln, err := Listen("127.0.0.1:0") + require.NoError(t, err) + ctx, cancel := context.WithCancel(context.Background()) + r := &rig{t: t, addr: ln.Addr().String(), cancel: cancel, done: make(chan struct{}), srv: srv} + go func() { r.err = srv.Serve(ctx, ln); close(r.done) }() + t.Cleanup(func() { + cancel() + select { + case <-r.done: + case <-time.After(10 * time.Second): + t.Error("the server did not stop") + } + }) + return r +} + +func (r *rig) dial() net.Conn { + r.t.Helper() + c, err := net.Dial("tcp", r.addr) + require.NoError(r.t, err) + r.t.Cleanup(func() { _ = c.Close() }) + return c +} + +func validRegister() *proto.Register { + return &proto.Register{Header: proto.Header{Seq: 1}, Name: "emax", Version: "0.3", Build: "git:abc", + Capabilities: []string{"bars", "warm"}, + Run: proto.Run{Mode: proto.ModeBacktest, Interval: "H1", Instruments: []string{"EURUSD", "GBPUSD"}, + From: ts("2024-03-01T00:00:00Z"), To: ts("2024-04-01T00:00:00Z"), WarmBars: 10}} +} + +func send(t *testing.T, c net.Conn, m proto.Message) { + t.Helper() + require.NoError(t, c.SetWriteDeadline(time.Now().Add(5*time.Second))) + require.NoError(t, proto.Write(c, m, proto.DefaultMaxFrame)) +} + +func sendRaw(t *testing.T, c net.Conn, payload string) { + t.Helper() + require.NoError(t, proto.WriteFrame(c, []byte(payload), proto.DefaultMaxFrame)) +} + +func recv(t *testing.T, c net.Conn) proto.Message { + t.Helper() + require.NoError(t, c.SetReadDeadline(time.Now().Add(5*time.Second))) + m, err := proto.Read(c, proto.DefaultMaxFrame) + require.NoError(t, err) + return m +} + +func recvError(t *testing.T, c net.Conn) *proto.Failure { + t.Helper() + f, ok := recv(t, c).(*proto.Failure) + require.True(t, ok, "expected an error message") + return f +} + +// expectClosed asserts the server has closed the connection. +func expectClosed(t *testing.T, c net.Conn) { + t.Helper() + require.NoError(t, c.SetReadDeadline(time.Now().Add(5*time.Second))) + _, err := proto.ReadFrame(c, proto.DefaultMaxFrame) + require.ErrorIs(t, err, io.EOF, "the connection is closed") +} + +// register dials, registers and returns the connection and the accepted reply. +func (r *rig) register(reg *proto.Register) (net.Conn, *proto.Accepted) { + r.t.Helper() + c := r.dial() + send(r.t, c, reg) + a, ok := recv(r.t, c).(*proto.Accepted) + require.True(r.t, ok, "expected accepted") + return c, a +} + +func TestNew_NeedsAnAdmitterAndAHandlerAndSetsDefaults(t *testing.T) { + _, err := New(Config{Handle: idleHandler}) + require.Error(t, err) + _, err = New(Config{Admit: admitAll}) + require.Error(t, err) + + s, err := New(Config{Admit: admitAll, Handle: idleHandler}) + + require.NoError(t, err) + assert.Equal(t, DefaultRegisterTimeout, s.cfg.RegisterTimeout) + assert.Equal(t, DefaultReplyTimeout, s.cfg.ReplyTimeout) + assert.Equal(t, time.Second, s.cfg.CloseGrace) + assert.Equal(t, proto.DefaultMaxFrame, s.cfg.MaxFrame) + assert.Equal(t, proto.DefaultMaxFrame, s.cfg.MaxFrame) + assert.NotNil(t, s.cfg.NewRunID) + assert.NotNil(t, s.log) +} + +func TestDefaults_AreTheOnesOfTheADR(t *testing.T) { + assert.Equal(t, 10*time.Second, DefaultRegisterTimeout) + assert.Equal(t, 30*time.Second, DefaultReplyTimeout) +} + +func TestServer_AcceptsARegistrationAndIssuesARunID(t *testing.T) { + seen := make(chan *Session, 1) + var admitted *proto.Register + r := start(t, Config{ + ReplyTimeout: 7 * time.Second, + Admit: func(_ context.Context, reg *proto.Register) (Grant, error) { + admitted = reg + return Grant{}, nil + }, + Handle: func(_ context.Context, s *Session) error { seen <- s; return nil }, + }) + + conn, accepted := r.register(validRegister()) + + assert.Equal(t, proto.Header{V: 1, Type: proto.TypeAccepted, Seq: 1, RunID: accepted.RunID}, accepted.Header) + assert.Regexp(t, regexp.MustCompile(`^[0-9A-HJKMNP-TV-Z]{26}$`), accepted.RunID) + assert.Equal(t, []string{"EURUSD", "GBPUSD"}, accepted.Instruments, "an empty grant means what the worker asked for") + assert.Equal(t, "H1", accepted.Interval) + assert.Zero(t, accepted.WarmBars, "the host grants warm-up; it is not echoed from the request") + assert.Equal(t, 7000, accepted.ReplyTimeoutMS) + require.NotNil(t, admitted) + assert.Equal(t, "emax", admitted.Name) + assert.Equal(t, "git:abc", admitted.Build) + + s := <-seen + assert.Equal(t, accepted.RunID, s.RunID(), "the run context carries the same RunID") + assert.Equal(t, "emax", s.Register().Name) + done := recv(t, conn).(*proto.Done) + assert.Equal(t, proto.StatusCompleted, done.Status, "a handler that returns nil completes the run") + assert.Equal(t, accepted.RunID, done.RunID) + assert.Equal(t, uint64(2), done.Seq) +} + +func TestServer_TheGrantDecidesInstrumentsAndWarmUp(t *testing.T) { + r := start(t, Config{Admit: func(context.Context, *proto.Register) (Grant, error) { + return Grant{Instruments: []string{"GBPUSD"}, WarmBars: 4}, nil + }}) + + _, accepted := r.register(validRegister()) + + assert.Equal(t, []string{"GBPUSD"}, accepted.Instruments) + assert.Equal(t, 4, accepted.WarmBars) +} + +func TestServer_RefusesWhatTheHostRefusesAndNeverStartsTheRun(t *testing.T) { + called := false + r := start(t, Config{ + Admit: func(context.Context, *proto.Register) (Grant, error) { + return Grant{}, Reject("instrument %s is not available", "XYZ") + }, + Handle: func(context.Context, *Session) error { called = true; return nil }, + }) + c := r.dial() + send(t, c, validRegister()) + + f := recvError(t, c) + + assert.Equal(t, proto.CodeRejected, f.Code) + assert.True(t, f.Fatal) + assert.Contains(t, f.Message, "instrument XYZ is not available") + assert.Empty(t, f.RunID, "no run exists") + assert.Equal(t, uint64(1), f.Seq) + expectClosed(t, c) + assert.False(t, called) +} + +func TestServer_AHostFailureWhileAdmittingIsInternalAndLeaksNothing(t *testing.T) { + r := start(t, Config{Admit: func(context.Context, *proto.Register) (Grant, error) { + return Grant{}, errors.New("database password is hunter2") + }}) + c := r.dial() + send(t, c, validRegister()) + + f := recvError(t, c) + + assert.Equal(t, proto.CodeInternal, f.Code) + assert.NotContains(t, f.Message, "hunter2") + expectClosed(t, c) +} + +func TestServer_ARegistrationThatFailsValidationNeverReachesTheAdmitter(t *testing.T) { + admitted := false + r := start(t, Config{Admit: func(context.Context, *proto.Register) (Grant, error) { admitted = true; return Grant{}, nil }}) + for name, edit := range map[string]func(*proto.Register){ + "live is reserved": func(m *proto.Register) { m.Run.Mode = proto.ModeLive }, + "no bars capability": func(m *proto.Register) { m.Capabilities = []string{"warm"} }, + "an end before a start": func(m *proto.Register) { m.Run.To = m.Run.From.Add(-time.Hour) }, + } { + t.Run(name, func(t *testing.T) { + c := r.dial() + m := validRegister() + edit(m) + send(t, c, m) + + assert.Equal(t, proto.CodeRejected, recvError(t, c).Code) + expectClosed(t, c) + }) + } + assert.False(t, admitted) +} + +func TestServer_ARunObjectWithAnUnknownMemberIsRejected(t *testing.T) { + r := start(t, Config{}) + c := r.dial() + sendRaw(t, c, `{"v":1,"type":"register","seq":1,"name":"x","capabilities":["bars"],"run":{"mode":"backtest","interval":"H1","instrument":["EURUSD"],"from":"2024-03-01T00:00:00Z","to":"2024-04-01T00:00:00Z"}}`) + + f := recvError(t, c) + + assert.Equal(t, proto.CodeRejected, f.Code) + assert.Contains(t, f.Message, `"instrument"`) +} + +func TestServer_ProtocolViolationsBeforeAcceptedAreErrorsThenClose(t *testing.T) { + r := start(t, Config{}) + zeroLen := func(c net.Conn) { + _, err := c.Write([]byte{0, 0, 0, 0}) + require.NoError(t, err) + } + huge := func(c net.Conn) { + _, err := c.Write(binary.BigEndian.AppendUint32(nil, 0xFFFFFFFF)) + require.NoError(t, err) + } + for name, tc := range map[string]struct { + do func(c net.Conn) + code proto.Code + }{ + "wrong version": {func(c net.Conn) { sendRaw(t, c, `{"v":2,"type":"register","seq":1}`) }, proto.CodeVersion}, + "not a register": {func(c net.Conn) { + send(t, c, &proto.Done{Header: proto.Header{Seq: 1, RunID: "r"}, Status: "completed"}) + }, proto.CodeProtocol}, + "a register with seq 2": {func(c net.Conn) { m := validRegister(); m.Seq = 2; send(t, c, m) }, proto.CodeProtocol}, + "not JSON": {func(c net.Conn) { sendRaw(t, c, `hello`) }, proto.CodeProtocol}, + "duplicate member": {func(c net.Conn) { sendRaw(t, c, `{"v":1,"v":1}`) }, proto.CodeProtocol}, + "zero length frame": {zeroLen, proto.CodeProtocol}, + "a hostile length": {huge, proto.CodeProtocol}, + "a register with a run_id": {func(c net.Conn) { + sendRaw(t, c, `{"v":1,"type":"register","seq":1,"run_id":"r","name":"x","capabilities":["bars"],"run":{}}`) + }, proto.CodeProtocol}, + } { + t.Run(name, func(t *testing.T) { + c := r.dial() + tc.do(c) + + f := recvError(t, c) + + assert.Equal(t, tc.code, f.Code) + assert.True(t, f.Fatal) + expectClosed(t, c) + }) + } +} + +func TestServer_ARegistrationThatNeverComesTimesOut(t *testing.T) { + r := start(t, Config{RegisterTimeout: 150 * time.Millisecond}) + c := r.dial() + + f := recvError(t, c) + + assert.Equal(t, proto.CodeTimeout, f.Code) + expectClosed(t, c) +} + +func TestServer_AHalfSentRegisterTimesOutToo(t *testing.T) { + r := start(t, Config{RegisterTimeout: 150 * time.Millisecond}) + c := r.dial() + _, err := c.Write([]byte{0, 0, 1, 0, '{'}) // promises 256 bytes, sends one + + require.NoError(t, err) + assert.Equal(t, proto.CodeTimeout, recvError(t, c).Code) +} + +func TestServer_AConnectionThatHangsUpFirstIsForgottenAndTheServerGoesOn(t *testing.T) { + r := start(t, Config{}) + c := r.dial() + require.NoError(t, c.Close()) + half := r.dial() + _, err := half.Write([]byte{0, 0}) + require.NoError(t, err) + require.NoError(t, half.Close()) + + _, accepted := r.register(validRegister()) + + assert.NotEmpty(t, accepted.RunID) +} + +func TestServer_EveryRunGetsItsOwnRunID(t *testing.T) { + r := start(t, Config{}) + ids := map[string]bool{} + for range 5 { + _, a := r.register(validRegister()) + ids[a.RunID] = true + } + + assert.Len(t, ids, 5) +} + +func TestServer_RunsAreIndependentAndConcurrent(t *testing.T) { + var mu sync.Mutex + in := 0 + release := make(chan struct{}) + r := start(t, Config{Handle: func(ctx context.Context, s *Session) error { + mu.Lock() + in++ + mu.Unlock() + select { + case <-release: + case <-ctx.Done(): + } + return nil + }}) + c1, a1 := r.register(validRegister()) + c2, a2 := r.register(validRegister()) + require.Eventually(t, func() bool { mu.Lock(); defer mu.Unlock(); return in == 2 }, 5*time.Second, 10*time.Millisecond) + + close(release) + + assert.NotEqual(t, a1.RunID, a2.RunID) + assert.Equal(t, a1.RunID, recv(t, c1).(*proto.Done).RunID) + assert.Equal(t, a2.RunID, recv(t, c2).(*proto.Done).RunID) +} + +func TestServer_TheRunIDGeneratorIsInjectable(t *testing.T) { + r := start(t, Config{NewRunID: func() (string, error) { return "r-fixed", nil }}) + + _, a := r.register(validRegister()) + + assert.Equal(t, "r-fixed", a.RunID) +} + +func TestServer_AFailureToIssueARunIDIsInternal(t *testing.T) { + r := start(t, Config{NewRunID: func() (string, error) { return "", errors.New("entropy") }}) + c := r.dial() + send(t, c, validRegister()) + + assert.Equal(t, proto.CodeInternal, recvError(t, c).Code) + expectClosed(t, c) +} + +func TestServer_HandlerOutcomesBecomeDone(t *testing.T) { + for name, tc := range map[string]struct { + handle func(context.Context, *Session) error + status string + reason string + }{ + "nil completes": {func(context.Context, *Session) error { return nil }, proto.StatusCompleted, ""}, + "an error fails": {func(context.Context, *Session) error { return errors.New("engine stopped: bad bar") }, proto.StatusFailed, "engine stopped: bad bar"}, + "the handler finishes itself": {func(_ context.Context, s *Session) error { return s.Finish(proto.StatusCancelled, "owner halt") }, proto.StatusCancelled, "owner halt"}, + } { + t.Run(name, func(t *testing.T) { + r := start(t, Config{Handle: tc.handle}) + c, _ := r.register(validRegister()) + + d := recv(t, c).(*proto.Done) + + assert.Equal(t, tc.status, d.Status) + assert.Equal(t, tc.reason, d.Reason) + expectClosed(t, c) + }) + } +} + +func TestServer_ShuttingDownCancelsRunsAndTellsTheWorker(t *testing.T) { + started := make(chan struct{}) + r := start(t, Config{Handle: func(ctx context.Context, s *Session) error { + close(started) + <-ctx.Done() + return ctx.Err() + }}) + c, _ := r.register(validRegister()) + <-started + + r.cancel() + + d := recv(t, c).(*proto.Done) + assert.Equal(t, proto.StatusCancelled, d.Status) + select { + case <-r.done: + assert.NoError(t, r.err, "a shutdown is not an error") + case <-time.After(10 * time.Second): + t.Fatal("Serve did not return") + } +} + +func TestServer_StopsAcceptingAfterShutdown(t *testing.T) { + r := start(t, Config{}) + r.cancel() + <-r.done + + _, err := net.DialTimeout("tcp", r.addr, time.Second) + + assert.Error(t, err) +} + +func TestIsLoopbackAddr(t *testing.T) { + for addr, want := range map[string]bool{ + "127.0.0.1:9000": true, "127.9.9.9:1": true, "[::1]:9000": true, "localhost:9000": true, + "0.0.0.0:9000": false, ":9000": false, "[::]:9000": false, "192.168.1.5:9000": false, + "10.0.0.1:1": false, "example.com:80": false, "8.8.8.8:53": false, + } { + got, err := IsLoopbackAddr(addr) + require.NoError(t, err, addr) + assert.Equal(t, want, got, addr) + } + for _, bad := range []string{"", "127.0.0.1", "nonsense"} { + _, err := IsLoopbackAddr(bad) + assert.Error(t, err, bad) + } +} + +func TestListen_RefusesAnythingButLoopback(t *testing.T) { + for _, addr := range []string{"0.0.0.0:0", ":0", "[::]:0", "192.168.1.5:0", "example.com:0"} { + ln, err := Listen(addr) + + require.Error(t, err, addr) + assert.Nil(t, ln, addr) + } + _, err := Listen("not-an-address") + assert.Error(t, err) + + ln, err := Listen("127.0.0.1:0") + require.NoError(t, err) + assert.True(t, ln.Addr().(*net.TCPAddr).IP.IsLoopback()) + require.NoError(t, ln.Close()) +} + +func TestServe_RefusesAListenerThatIsNotLoopbackWhoeverOpenedIt(t *testing.T) { + s, err := New(Config{Admit: admitAll, Handle: idleHandler, Logger: quiet}) + require.NoError(t, err) + ln, err := net.Listen("tcp", "0.0.0.0:0") + require.NoError(t, err) + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + err = s.Serve(ctx, ln) + + require.Error(t, err, "a listener that is not loopback must be refused, not served") + assert.Contains(t, err.Error(), "loopback") + _, derr := net.DialTimeout("tcp", ln.Addr().String(), 200*time.Millisecond) + assert.Error(t, derr, "Serve closed the listener it refused") +} + +func TestServe_RefusesANonTCPListener(t *testing.T) { + s, err := New(Config{Admit: admitAll, Handle: idleHandler, Logger: quiet}) + require.NoError(t, err) + ln, err := net.Listen("unix", t.TempDir()+"/s.sock") + require.NoError(t, err) + + assert.Error(t, s.Serve(context.Background(), ln)) +} + +func TestReject_WrapsErrRejected(t *testing.T) { + err := Reject("no %s", "room") + + assert.ErrorIs(t, err, ErrRejected) + assert.Contains(t, err.Error(), "no room") +} + +// sampleBars is a bars message for a frame, for the exchange tests. +func sampleBars(frame string, instruments ...string) *proto.Bars { + b := &proto.Bars{FrameAt: ts(frame)} + for _, in := range instruments { + b.Bars = append(b.Bars, marketdata.Bar{Instrument: in, Time: ts(frame).Add(-time.Hour), AvailableAt: ts(frame), + Mid: marketdata.OHLC{Open: num.MustParsePrice("1.1"), High: num.MustParsePrice("1.1"), Low: num.MustParsePrice("1.1"), Close: num.MustParsePrice("1.1")}}) + } + return b +} + +func goodOrder(instrument string) strategy.Order { + return strategy.Order{Instrument: instrument, Action: strategy.Open, Direction: strategy.Long, Type: strategy.Market, + Price: num.MustParsePrice("1.1"), StopLoss: num.MustParsePrice("1.09"), Ref: "t"} +} + +func num0() num.Price { return num.Price{} } + +func sprintf(format string, args ...any) string { return fmt.Sprintf(format, args...) } + +// importsOf lists the module-internal imports of the non-test Go files of dir. +func importsOf(t *testing.T, dir string) map[string][]string { + t.Helper() + files, err := filepath.Glob(filepath.Join(dir, "*.go")) + require.NoError(t, err) + out := map[string][]string{} + for _, file := range files { + if strings.HasSuffix(file, "_test.go") { + continue + } + f, err := parser.ParseFile(token.NewFileSet(), file, nil, parser.ImportsOnly) + require.NoError(t, err) + for _, imp := range f.Imports { + path, err := strconv.Unquote(imp.Path.Value) + require.NoError(t, err) + out[file] = append(out[file], path) + } + } + return out +} + +// TestServer_ImportsOnlyTheStandardLibraryAndTheProtocol keeps the host a leaf: it +// knows nothing of the engine, an account, a broker, a provider or the store. +func TestServer_ImportsOnlyTheStandardLibraryAndTheProtocol(t *testing.T) { + const mod = "github.com/rustyeddy/trader" + allowed := map[string]bool{mod + "/strategy": true, mod + "/strategy/proto": true} + imports := importsOf(t, ".") + require.NotEmpty(t, imports) + for file, paths := range imports { + for _, path := range paths { + if path == mod || strings.HasPrefix(path, mod+"/") { + assert.True(t, allowed[path], "%s imports %s", file, path) + } + } + } +} + +// TestServer_NoGuestPackageImportsTheHost keeps a guest from linking host code: +// strategy, strategy/proto and strategy/client must not import strategy/server. +func TestServer_NoGuestPackageImportsTheHost(t *testing.T) { + const host = "github.com/rustyeddy/trader/strategy/server" + checked := 0 + for _, dir := range []string{"..", filepath.Join("..", "proto"), filepath.Join("..", "client")} { + for file, paths := range importsOf(t, dir) { + checked++ + assert.NotContains(t, paths, host, file) + } + } + require.Positive(t, checked) +} + +// failingListener accepts its first n connections from the real listener, then +// fails Accept as a broken listener would. +type failingListener struct { + net.Listener + mu sync.Mutex + n int +} + +func (f *failingListener) Accept() (net.Conn, error) { + f.mu.Lock() + ok := f.n > 0 + f.n-- + f.mu.Unlock() + if !ok { + return nil, errors.New("accept broke") + } + return f.Listener.Accept() +} + +func TestServer_AListenerFailureDoesNotWaitForASilentWorker(t *testing.T) { + handling := make(chan struct{}) + srv, err := New(Config{Admit: admitAll, Logger: quiet, ReplyTimeout: time.Minute, CloseGrace: 20 * time.Millisecond, + Handle: func(ctx context.Context, s *Session) error { + close(handling) + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }}) + require.NoError(t, err) + ln, err := Listen("127.0.0.1:0") + require.NoError(t, err) + + served := make(chan error, 1) + go func() { served <- srv.Serve(context.Background(), &failingListener{Listener: ln, n: 1}) }() + + c, err := net.Dial("tcp", ln.Addr().String()) + require.NoError(t, err) + t.Cleanup(func() { _ = c.Close() }) + send(t, c, validRegister()) + recv(t, c) // accepted + select { + case <-handling: + case <-time.After(5 * time.Second): + t.Fatal("the run never started") + } + recv(t, c) // bars; the worker now stays silent + + select { + case err := <-served: + assert.ErrorContains(t, err, "accept") + case <-time.After(5 * time.Second): + t.Fatal("Serve waited for the silent worker instead of cancelling it") + } +} diff --git a/strategy/server/session.go b/strategy/server/session.go new file mode 100644 index 0000000..8ce5a40 --- /dev/null +++ b/strategy/server/session.go @@ -0,0 +1,278 @@ +package server + +import ( + "context" + "errors" + "fmt" + "io" + "log/slog" + "net" + "sync" + "time" + + "github.com/rustyeddy/trader/strategy/proto" +) + +// Errors of a run. +var ( + // ErrClosed is returned by a Session that has ended: finished, failed, or + // dropped. Nothing more can be exchanged. + ErrClosed = errors.New("the run has ended") + // ErrDisconnected is returned when the worker's connection ends mid-run. The + // run is failed closed; what was recorded before is kept. + ErrDisconnected = errors.New("worker disconnected") +) + +// WorkerError is returned when the worker itself ends the run with an error +// message. +type WorkerError struct{ Failure *proto.Failure } + +func (e *WorkerError) Error() string { + return fmt.Sprintf("worker ended the run: %s: %s", e.Failure.Code, e.Failure.Message) +} + +// Session is one admitted run over one connection. It carries the RunID and +// drives the strict lock-step: one bars message out, one orders message back. It +// is used by one goroutine at a time (the Handler's). +type Session struct { + conn net.Conn + cfg Config + log *slog.Logger + runID string + reg *proto.Register + grant Grant + + mu sync.Mutex + closed bool + outcome string + trSeq uint64 // next seq Trader sends + wkSeq uint64 // next seq expected from the worker + instrSet map[string]bool +} + +func newSession(conn net.Conn, cfg Config, runID string, reg *proto.Register, grant Grant, log *slog.Logger) *Session { + set := map[string]bool{} + for _, in := range grant.Instruments { + set[in] = true + } + // accepted was seq 1; register was the worker's seq 1. + return &Session{conn: conn, cfg: cfg, log: log, runID: runID, reg: reg, grant: grant, trSeq: 2, wkSeq: 2, instrSet: set} +} + +// RunID is the unique id of the run. It is on every message after accepted. +func (s *Session) RunID() string { return s.runID } + +// Register is what the worker registered: its name, build, config and run. +func (s *Session) Register() *proto.Register { return s.reg } + +// Grant is what the host granted at admission. +func (s *Session) Grant() Grant { return s.grant } + +func (s *Session) isClosed() bool { + s.mu.Lock() + defer s.mu.Unlock() + return s.closed +} + +// status is how the run ended, for the log: "" while it is open. +func (s *Session) status() string { + s.mu.Lock() + defer s.mu.Unlock() + return s.outcome +} + +// Exchange sends one bars message and returns the worker's orders for it. It +// stamps the header (version, type, seq, RunID), so the caller sets only FrameAt, +// Warm and Bars. The reply must be an orders message for exactly this bars +// message, in sequence, with this RunID, every order valid for the frame +// (Orders.Validate) and on an instrument of the run, and none at all on a warm-up +// frame. Anything else, a reply timeout, a decode failure or a disconnect ends the +// run: the worker is sent the error and the connection is closed, and the error +// is returned. ctx being done ends it as cancelled and returns ctx's error. +func (s *Session) Exchange(ctx context.Context, bars *proto.Bars) (*proto.Orders, error) { + s.mu.Lock() + defer s.mu.Unlock() + if s.closed { + return nil, ErrClosed + } + if err := ctx.Err(); err != nil { + s.endLocked("cancelled", func() { s.sendDone(proto.StatusCancelled, "run cancelled") }) + return nil, err + } + + out := *bars + out.Header = proto.Header{V: proto.Version, Type: proto.TypeBars, Seq: s.trSeq, RunID: s.runID} + s.trSeq++ + _ = s.conn.SetWriteDeadline(time.Now().Add(s.cfg.ReplyTimeout)) + if err := proto.Write(s.conn, &out, s.cfg.MaxFrame); err != nil { + var pe *proto.Error + if errors.As(err, &pe) { // a bars message the codec itself refused: the host's bug + s.failLocked(proto.CodeInternal, pe.Msg) + return nil, &proto.Error{Code: proto.CodeInternal, Msg: pe.Msg} + } + s.dropLocked("write failed") + return nil, fmt.Errorf("%w: %v", ErrDisconnected, err) + } + + _ = s.conn.SetReadDeadline(time.Now().Add(s.cfg.ReplyTimeout)) + stop := context.AfterFunc(ctx, func() { _ = s.conn.SetReadDeadline(time.Now()) }) + m, err := proto.Read(s.conn, s.cfg.MaxFrame) + stop() + if err != nil { + return nil, s.readFailedLocked(ctx, err) + } + return s.checkReplyLocked(m, &out) +} + +func (s *Session) readFailedLocked(ctx context.Context, err error) error { + var pe *proto.Error + switch { + case isTimeout(err) && ctx.Err() != nil: + s.endLocked("cancelled", func() { s.sendDone(proto.StatusCancelled, "run cancelled") }) + return ctx.Err() + case isTimeout(err): + s.failLocked(proto.CodeTimeout, "no orders within "+s.cfg.ReplyTimeout.String()) + return &proto.Error{Code: proto.CodeTimeout, Msg: "no orders within " + s.cfg.ReplyTimeout.String()} + case errors.As(err, &pe): + s.failLocked(pe.Code, pe.Msg) + return pe + default: // end of stream or a transport failure: the worker is gone + s.dropLocked("disconnected") + return fmt.Errorf("%w: %v", ErrDisconnected, err) + } +} + +// checkReplyLocked applies ADR-094 sections 3 to 6 to the worker's message. +func (s *Session) checkReplyLocked(m proto.Message, sent *proto.Bars) (*proto.Orders, error) { + fail := func(code proto.Code, format string, args ...any) (*proto.Orders, error) { + msg := fmt.Sprintf(format, args...) + s.failLocked(code, msg) + return nil, &proto.Error{Code: code, Msg: msg} + } + switch m := m.(type) { + case *proto.Failure: + if h := m.Header; h.Seq != s.wkSeq { + return fail(proto.CodeProtocol, "error has seq %d, want %d", h.Seq, s.wkSeq) + } else if h.RunID != s.runID { + return fail(proto.CodeProtocol, "error has run_id %q, want %q", h.RunID, s.runID) + } + s.dropLocked("worker error") + return nil, &WorkerError{Failure: m} + case *proto.Orders: + h := m.Header + switch { + case h.Seq != s.wkSeq: + return fail(proto.CodeProtocol, "orders has seq %d, want %d", h.Seq, s.wkSeq) + case h.RunID != s.runID: + return fail(proto.CodeProtocol, "orders has run_id %q, want %q", h.RunID, s.runID) + case m.InReplyTo != sent.Seq: + return fail(proto.CodeProtocol, "orders answers seq %d, but the bars sent was seq %d", m.InReplyTo, sent.Seq) + case sent.Warm && len(m.Orders) > 0: + return fail(proto.CodeInvalidOrder, "orders on a warm-up frame: a warm-up reply has none") + } + if err := m.Validate(sent.FrameAt); err != nil { + return fail(proto.CodeOf(err), "%s", err.(*proto.Error).Msg) + } + for i, o := range m.Orders { + if !s.instrSet[o.Instrument] { + return fail(proto.CodeInvalidOrder, "orders[%d].instrument: %q is not an instrument of the run", i, o.Instrument) + } + } + s.wkSeq++ + return m, nil + default: + return fail(proto.CodeProtocol, "expected orders, got %s", m.Head().Type) + } +} + +// Finish ends the run: it sends done with the status (completed, cancelled or +// failed) and the reason, then closes the connection after giving the worker a +// moment to close first. A session that has already ended returns ErrClosed. +func (s *Session) Finish(status, reason string) error { + s.mu.Lock() + defer s.mu.Unlock() + if s.closed { + return ErrClosed + } + switch status { + case proto.StatusCompleted, proto.StatusCancelled, proto.StatusFailed: + default: + return fmt.Errorf("server: %q is not a done status", status) + } + s.endLocked(status, func() { s.sendDone(status, reason) }) + return nil +} + +// Fail ends the run with an error message to the worker and closes the connection. +// The host uses it for its own failures; the session uses it for the worker's. +func (s *Session) Fail(code proto.Code, message string) error { + s.mu.Lock() + defer s.mu.Unlock() + if s.closed { + return ErrClosed + } + if !code.Valid() { + code = proto.CodeInternal + } + s.failLocked(code, message) + return nil +} + +func (s *Session) failLocked(code proto.Code, message string) { + s.endLocked("failed: "+string(code), func() { + sendFailure(s.conn, s.cfg.MaxFrame, s.nextSeq(), s.runID, &proto.Error{Code: code, Msg: message}) + }) + s.log.Warn("run failed", "code", code, "reason", message) +} + +// dropLocked closes a connection that is already gone or not worth talking to. +func (s *Session) dropLocked(why string) { + s.endLocked("failed: "+why, func() {}) + s.log.Warn("run dropped", "reason", why) +} + +// endLocked marks the run over, sends the farewell, and closes the connection. +func (s *Session) endLocked(outcome string, farewell func()) { + if s.closed { + return + } + s.closed = true + s.outcome = outcome + farewell() + s.drainAndClose() +} + +func (s *Session) nextSeq() uint64 { + n := s.trSeq + s.trSeq++ + return n +} + +func (s *Session) sendDone(status, reason string) { + _ = s.conn.SetWriteDeadline(time.Now().Add(2 * time.Second)) + _ = proto.Write(s.conn, &proto.Done{Header: proto.Header{Seq: s.nextSeq(), RunID: s.runID}, Status: status, Reason: reason}, s.cfg.MaxFrame) +} + +// drainAndClose gives the worker a short grace to close its side first, then +// closes the connection. +func (s *Session) drainAndClose() { + _ = s.conn.SetReadDeadline(time.Now().Add(s.cfg.CloseGrace)) + _, _ = io.Copy(io.Discard, io.LimitReader(s.conn, 64<<10)) + _ = s.conn.Close() +} + +// sendFailure writes an error message, best effort. +func sendFailure(conn net.Conn, max int, seq uint64, runID string, err error) { + code, msg := proto.CodeInternal, err.Error() + var pe *proto.Error + if errors.As(err, &pe) { + code, msg = pe.Code, pe.Msg + } + _ = conn.SetWriteDeadline(time.Now().Add(2 * time.Second)) + _ = proto.Write(conn, &proto.Failure{Header: proto.Header{Seq: seq, RunID: runID}, Code: code, Fatal: true, Message: msg}, max) +} + +func isTimeout(err error) bool { + var ne net.Error + return errors.As(err, &ne) && ne.Timeout() +} diff --git a/strategy/server/session_test.go b/strategy/server/session_test.go new file mode 100644 index 0000000..91a34e5 --- /dev/null +++ b/strategy/server/session_test.go @@ -0,0 +1,410 @@ +package server + +import ( + "context" + "errors" + "net" + "testing" + "time" + + "github.com/rustyeddy/trader/strategy" + "github.com/rustyeddy/trader/strategy/proto" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// exchange runs script as the Handler of one run against a real worker +// connection driven by worker, and returns what the script returned. The worker +// sees accepted first, then whatever the script sends. +func exchange(t *testing.T, cfg Config, script func(ctx context.Context, s *Session) error, worker func(c net.Conn, runID string)) error { + t.Helper() + result := make(chan error, 1) + cfg.Handle = func(ctx context.Context, s *Session) error { + err := script(ctx, s) + result <- err + return err + } + r := start(t, cfg) + c, accepted := r.register(validRegister()) + worker(c, accepted.RunID) + select { + case err := <-result: + return err + case <-time.After(10 * time.Second): + t.Fatal("the handler did not return") + return nil + } +} + +func reply(runID string, seq, inReplyTo uint64, orders ...strategy.Order) *proto.Orders { + return &proto.Orders{Header: proto.Header{Seq: seq, RunID: runID}, InReplyTo: inReplyTo, Orders: orders} +} + +func TestSession_ExchangeIsStrictLockStepWithSequenceAndRunID(t *testing.T) { + var runID string + var got1, got2 *proto.Orders + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + var err error + if got1, err = s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD", "GBPUSD")); err != nil { + return err + } + got2, err = s.Exchange(ctx, sampleBars("2024-03-01T12:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + runID = id + b1 := recv(t, c).(*proto.Bars) + assert.Equal(t, proto.Header{V: 1, Type: proto.TypeBars, Seq: 2, RunID: id}, b1.Header, "after accepted, seq 2, the RunID, stamped by the host") + assert.Equal(t, ts("2024-03-01T11:00:00Z"), b1.FrameAt) + assert.Len(t, b1.Bars, 2) + send(t, c, reply(id, 2, 2, goodOrder("EURUSD"))) + b2 := recv(t, c).(*proto.Bars) + assert.Equal(t, uint64(3), b2.Seq) + assert.Equal(t, id, b2.RunID) + send(t, c, reply(id, 3, 3)) + d := recv(t, c).(*proto.Done) + assert.Equal(t, proto.StatusCompleted, d.Status) + assert.Equal(t, uint64(4), d.Seq) + assert.Equal(t, id, d.RunID, "the RunID is on every message") + }) + + require.NoError(t, err) + require.Len(t, got1.Orders, 1) + assert.Equal(t, "EURUSD", got1.Orders[0].Instrument) + assert.Equal(t, "t", got1.Orders[0].Ref) + assert.Empty(t, got2.Orders) + assert.NotEmpty(t, runID) +} + +func TestSession_TheCallerDoesNotSetTheHeader(t *testing.T) { + in := sampleBars("2024-03-01T11:00:00Z", "EURUSD") + in.Header = proto.Header{Seq: 99, RunID: "wrong", Type: proto.TypeDone} + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { _, err := s.Exchange(ctx, in); return err }, + func(c net.Conn, id string) { + b := recv(t, c).(*proto.Bars) + assert.Equal(t, proto.Header{V: 1, Type: proto.TypeBars, Seq: 2, RunID: id}, b.Header) + send(t, c, reply(id, 2, 2)) + }) + + require.NoError(t, err) + assert.Equal(t, uint64(99), in.Seq, "the caller's message is not modified") +} + +func TestSession_AccessorsAndGrant(t *testing.T) { + var s0 *Session + _ = exchange(t, Config{Admit: func(context.Context, *proto.Register) (Grant, error) { + return Grant{Instruments: []string{"EURUSD"}, WarmBars: 3}, nil + }}, + func(_ context.Context, s *Session) error { s0 = s; return nil }, + func(c net.Conn, id string) { recv(t, c) }) + + assert.Equal(t, []string{"EURUSD"}, s0.Grant().Instruments) + assert.Equal(t, 3, s0.Grant().WarmBars) + assert.Equal(t, "emax", s0.Register().Name) + assert.NotEmpty(t, s0.RunID()) + assert.True(t, s0.isClosed()) + assert.Equal(t, "completed", s0.status()) +} + +// The worker's replies that break the protocol or the order rules end the run: the +// worker is told, the connection closes, the handler gets the error. +func TestSession_AReplyThatBreaksTheRulesEndsTheRun(t *testing.T) { + noStop := goodOrder("EURUSD") + noStop.StopLoss = num0() + for name, tc := range map[string]struct { + msg func(runID string) proto.Message + code proto.Code + raw string + }{ + "wrong run_id": {func(id string) proto.Message { return reply("r-other", 2, 2) }, proto.CodeProtocol, ""}, + "a worker error with the wrong run_id": {func(id string) proto.Message { + return &proto.Failure{Header: proto.Header{Seq: 2, RunID: "r-other"}, Code: proto.CodeInternal, Message: "boom"} + }, proto.CodeProtocol, ""}, + "a worker error with the wrong seq": {func(id string) proto.Message { + return &proto.Failure{Header: proto.Header{Seq: 5, RunID: id}, Code: proto.CodeInternal, Message: "boom"} + }, proto.CodeProtocol, ""}, + "a seq gap": {func(id string) proto.Message { return reply(id, 3, 2) }, proto.CodeProtocol, ""}, + "a repeated seq": {func(id string) proto.Message { return reply(id, 1, 2) }, proto.CodeProtocol, ""}, + "answering the wrong frame": {func(id string) proto.Message { return reply(id, 2, 1) }, proto.CodeProtocol, ""}, + "answering a future frame": {func(id string) proto.Message { return reply(id, 2, 9) }, proto.CodeProtocol, ""}, + "a register again": {func(id string) proto.Message { m := validRegister(); m.Seq = 2; return m }, proto.CodeProtocol, ""}, + "a done from the worker": {func(id string) proto.Message { + return &proto.Done{Header: proto.Header{Seq: 2, RunID: id}, Status: "completed"} + }, proto.CodeProtocol, ""}, + "an open with no stop": {func(id string) proto.Message { return reply(id, 2, 2, noStop) }, proto.CodeInvalidOrder, ""}, + "an unregistered instrument": {func(id string) proto.Message { return reply(id, 2, 2, goodOrder("USDJPY")) }, proto.CodeInvalidOrder, ""}, + "a valid and an invalid": {func(id string) proto.Message { return reply(id, 2, 2, goodOrder("EURUSD"), noStop) }, proto.CodeInvalidOrder, ""}, + "an expiry before the frame": {func(id string) proto.Message { + o := goodOrder("EURUSD") + o.Expires = ts("2024-03-01T10:00:00Z") + return reply(id, 2, 2, o) + }, proto.CodeInvalidOrder, ""}, + "a host-owned member": {nil, proto.CodeInvalidOrder, `{"v":1,"type":"orders","seq":2,"run_id":"%s","in_reply_to":2,"orders":[{"instrument":"EURUSD","action":"close","units":1000}]}`}, + "an unknown order member": {nil, proto.CodeInvalidOrder, `{"v":1,"type":"orders","seq":2,"run_id":"%s","in_reply_to":2,"orders":[{"instrument":"EURUSD","action":"close","stoploss":"1.0"}]}`}, + "not JSON": {nil, proto.CodeProtocol, `garbage%.0s`}, + "another version": {nil, proto.CodeVersion, `{"v":2,"type":"orders","seq":2,"run_id":"%s"}`}, + "a duplicate member": {nil, proto.CodeProtocol, `{"v":1,"v":1,"type":"orders","seq":2,"run_id":"%s"}`}, + } { + t.Run(name, func(t *testing.T) { + var failure *proto.Failure + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + recv(t, c) + if tc.msg != nil { + send(t, c, tc.msg(id)) + } else { + sendRaw(t, c, sprintf(tc.raw, id)) + } + failure = recvError(t, c) + expectClosed(t, c) + }) + + var pe *proto.Error + require.ErrorAs(t, err, &pe) + assert.Equal(t, tc.code, pe.Code) + assert.Equal(t, tc.code, failure.Code) + assert.True(t, failure.Fatal) + assert.Equal(t, uint64(3), failure.Seq, "the error continues Trader's sequence") + }) + } +} + +func TestSession_ARefusedBatchSubmitsNothing(t *testing.T) { + var orders *proto.Orders + noStop := goodOrder("EURUSD") + noStop.StopLoss = num0() + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + var err error + orders, err = s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + recv(t, c) + send(t, c, reply(id, 2, 2, goodOrder("EURUSD"), noStop)) + recvError(t, c) + }) + + require.Error(t, err) + assert.Nil(t, orders, "no order of a refused message is handed over") +} + +func TestSession_AWarmUpFrameTakesNoOrders(t *testing.T) { + warm := sampleBars("2024-02-29T11:00:00Z", "EURUSD") + warm.Warm = true + t.Run("an empty reply is fine", func(t *testing.T) { + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { _, err := s.Exchange(ctx, warm); return err }, + func(c net.Conn, id string) { + b := recv(t, c).(*proto.Bars) + assert.True(t, b.Warm) + send(t, c, reply(id, 2, 2)) + }) + require.NoError(t, err) + }) + t.Run("an order is refused", func(t *testing.T) { + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { _, err := s.Exchange(ctx, warm); return err }, + func(c net.Conn, id string) { + recv(t, c) + send(t, c, reply(id, 2, 2, goodOrder("EURUSD"))) + assert.Equal(t, proto.CodeInvalidOrder, recvError(t, c).Code) + }) + assert.Equal(t, proto.CodeInvalidOrder, proto.CodeOf(err)) + }) +} + +func TestSession_ASlowReplyTimesOut(t *testing.T) { + var failure *proto.Failure + err := exchange(t, Config{ReplyTimeout: 150 * time.Millisecond}, + func(ctx context.Context, s *Session) error { + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + recv(t, c) // the bars, then silence + failure = recvError(t, c) + expectClosed(t, c) + }) + + assert.Equal(t, proto.CodeTimeout, proto.CodeOf(err)) + assert.Equal(t, proto.CodeTimeout, failure.Code) +} + +func TestSession_ADisconnectFailsTheRunClosed(t *testing.T) { + for name, hangUp := range map[string]func(c net.Conn){ + "after reading the bars": func(c net.Conn) { recv(t, c); _ = c.Close() }, + "in the middle of the reply": func(c net.Conn) { + recv(t, c) + _, _ = c.Write([]byte{0, 0, 0, 50, '{'}) + _ = c.Close() + }, + } { + t.Run(name, func(t *testing.T) { + var s0 *Session + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + s0 = s + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { hangUp(c) }) + + require.Error(t, err) + assert.True(t, errors.Is(err, ErrDisconnected) || proto.CodeOf(err) == proto.CodeProtocol, "%v", err) + assert.True(t, s0.isClosed()) + _, again := s0.Exchange(context.Background(), sampleBars("2024-03-01T12:00:00Z", "EURUSD")) + assert.ErrorIs(t, again, ErrClosed) + }) + } +} + +func TestSession_ADisconnectBeforeTheBarsAreWrittenIsReported(t *testing.T) { + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + time.Sleep(200 * time.Millisecond) // the worker is gone by now + for range 50 { + if _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")); err != nil { + return err + } + } + return nil + }, + func(c net.Conn, id string) { _ = c.Close() }) + + require.ErrorIs(t, err, ErrDisconnected) +} + +func TestSession_AWorkerErrorEndsTheRun(t *testing.T) { + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + recv(t, c) + send(t, c, &proto.Failure{Header: proto.Header{Seq: 2, RunID: id}, Code: proto.CodeInternal, Fatal: true, Message: "strategy panicked"}) + expectClosed(t, c) + }) + + var we *WorkerError + require.ErrorAs(t, err, &we) + assert.Equal(t, "strategy panicked", we.Failure.Message) + assert.Contains(t, err.Error(), "strategy panicked") +} + +func TestSession_CancellingTheContextEndsTheRunCancelled(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + var d *proto.Done + err := exchange(t, Config{}, + func(_ context.Context, s *Session) error { + time.AfterFunc(100*time.Millisecond, cancel) + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + recv(t, c) // never answered + d = recv(t, c).(*proto.Done) + expectClosed(t, c) + }) + + require.ErrorIs(t, err, context.Canceled) + assert.Equal(t, proto.StatusCancelled, d.Status) +} + +func TestSession_ACancelledContextBeforeTheCallSendsNothing(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + err := exchange(t, Config{}, + func(_ context.Context, s *Session) error { + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + d := recv(t, c).(*proto.Done) + assert.Equal(t, proto.StatusCancelled, d.Status, "the first thing the worker sees is done") + }) + + require.ErrorIs(t, err, context.Canceled) +} + +func TestSession_FinishIsOnceAndValidatesTheStatus(t *testing.T) { + var first, second, bad error + _ = exchange(t, Config{}, + func(_ context.Context, s *Session) error { + bad = s.Finish("ok", "") + first = s.Finish(proto.StatusFailed, "stopped") + second = s.Finish(proto.StatusCompleted, "") + return nil + }, + func(c net.Conn, id string) { + d := recv(t, c).(*proto.Done) + assert.Equal(t, proto.StatusFailed, d.Status) + assert.Equal(t, "stopped", d.Reason) + expectClosed(t, c) + }) + + assert.Error(t, bad) + assert.NoError(t, first) + assert.ErrorIs(t, second, ErrClosed) +} + +func TestSession_FailSendsTheErrorAndClosesOnce(t *testing.T) { + var first, second, unknownCode error + err := exchange(t, Config{}, + func(ctx context.Context, s *Session) error { + first = s.Fail(proto.CodeInternal, "engine stopped") + second = s.Fail(proto.CodeInternal, "again") + _, unknownCode = s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return nil + }, + func(c net.Conn, id string) { + f := recvError(t, c) + assert.Equal(t, proto.CodeInternal, f.Code) + assert.Equal(t, "engine stopped", f.Message) + assert.Equal(t, id, f.RunID) + expectClosed(t, c) + }) + + require.NoError(t, err) + assert.NoError(t, first) + assert.ErrorIs(t, second, ErrClosed) + assert.ErrorIs(t, unknownCode, ErrClosed) +} + +func TestSession_FailWithAnUnknownCodeIsInternal(t *testing.T) { + _ = exchange(t, Config{}, + func(_ context.Context, s *Session) error { return s.Fail("nonsense", "x") }, + func(c net.Conn, id string) { assert.Equal(t, proto.CodeInternal, recvError(t, c).Code) }) +} + +func TestSession_ABarsMessageTheCodecRefusesIsTheHostsBug(t *testing.T) { + big := sampleBars("2024-03-01T11:00:00Z", "EURUSD", "EURUSD", "EURUSD", "EURUSD") + err := exchange(t, Config{MaxFrame: 700}, + func(ctx context.Context, s *Session) error { _, err := s.Exchange(ctx, big); return err }, + func(c net.Conn, id string) {}) + + assert.Equal(t, proto.CodeInternal, proto.CodeOf(err), "a frame over the host's own limit is not the worker's fault") +} + +func TestSession_OversizedReplyIsAProtocolError(t *testing.T) { + err := exchange(t, Config{MaxFrame: 1 << 12}, + func(ctx context.Context, s *Session) error { + _, err := s.Exchange(ctx, sampleBars("2024-03-01T11:00:00Z", "EURUSD")) + return err + }, + func(c net.Conn, id string) { + recv(t, c) + _, werr := c.Write([]byte{0, 1, 0, 0}) // 65536 bytes promised, over the 4096 limit + require.NoError(t, werr) + assert.Equal(t, proto.CodeProtocol, recvError(t, c).Code) + }) + + assert.Equal(t, proto.CodeProtocol, proto.CodeOf(err)) +}