From d7d0e775efcb5e4651662517eb41a52f7d57296c Mon Sep 17 00:00:00 2001 From: Josh Allmann Date: Sat, 5 Dec 2026 00:58:05 -0800 Subject: [PATCH] Filter signer discovery by orchestrator address --- docs/discovery.md | 4 ++-- docs/http-api.md | 16 ++++++++----- docs/signer.md | 4 ++-- signer/discovery_test.go | 52 ++++++++++++++++++++++++++++++---------- signer/service.go | 21 ++++++++++++++-- 5 files changed, 72 insertions(+), 25 deletions(-) diff --git a/docs/discovery.md b/docs/discovery.md index 6a6011e..1187acb 100644 --- a/docs/discovery.md +++ b/docs/discovery.md @@ -71,5 +71,5 @@ Orchestrators = ["http://127.0.0.1:8939"] DiscoveryGrants = ["127.0.0.1:8939"] ``` -The signer's existing application and GPU filters apply. Returned runner URLs -continue to point to the owning orchestrator. +The signer supports application, GPU, and orchestrator Ethereum address filters. +Returned runner URLs point to the owning orchestrator. diff --git a/docs/http-api.md b/docs/http-api.md index 29490fa..266dc1b 100644 --- a/docs/http-api.md +++ b/docs/http-api.md @@ -111,13 +111,17 @@ response's `address` retains the orchestrator HTTP(S) base URL described above. `GET /discover-orchestrators` returns cached runner inventories. -After [deduplicating runners](#discovery-http), it filters by exact `app` and GPU -name (`gpu`). Repeated values match any value within a filter; both filters must -match when supplied together. `caps` and payment price ceilings do not apply. +After [deduplicating runners](#discovery-http), the signer matches `app` and GPU +name (`gpu`) exactly. It matches `orchestrator` against the advertised Ethereum +address case-insensitively, including the `0x` prefix. A match can use any of a +filter's repeated values and must satisfy all supplied filters. An `orchestrator` +filter excludes entries without an address. Empty or invalid values match no +address. +`caps` and payment price ceilings do not apply. Results contain `address`, matching `runners`, and `last_seen`, the successful -poll time of the cached source in UTC. At least one usable cached inventory yields -HTTP 200 (possibly `[]`); none yields 503. Cached results can remain available -while a source is unreachable. +poll time of the cached source in UTC. A usable cache with no matches yields HTTP +200 and `[]`. The signer returns 503 when no usable cache is available. Cached +results can remain available while a source is unreachable. Runner details are abbreviated in this example. diff --git a/docs/signer.md b/docs/signer.md index b905f50..4870905 100644 --- a/docs/signer.md +++ b/docs/signer.md @@ -71,10 +71,10 @@ the last inventory until `DiscoveryMaxFailedRefreshes` consecutive failures (default `6`, nonnegative) remove it; `0` evicts on the first failure. Recovered sources reappear on subsequent polls. -Query by application and GPU; omit either filter to broaden the results: +Query by application, GPU, and orchestrator Ethereum address. Each filter is optional. ```sh -curl -fsS 'http://127.0.0.1:8937/discover-orchestrators?app=example&gpu=H100' +curl -fsS 'http://127.0.0.1:8937/discover-orchestrators?app=example&gpu=H100&orchestrator=0x1111111111111111111111111111111111111111' ``` Results describe matching runners, prices, capacity, and application URLs. diff --git a/signer/discovery_test.go b/signer/discovery_test.go index e576b25..0f0d58d 100644 --- a/signer/discovery_test.go +++ b/signer/discovery_test.go @@ -29,10 +29,16 @@ func discoveryResponse(s *Service, query string) *httptest.ResponseRecorder { func TestSignerDiscovery(t *testing.T) { const recipient = "0x111111111111111111111111111111111111AbCd" - const advertised = `[{"address":"http://localhost:9000","orchestrator":"` + recipient + `","runners":[{"url":"http://localhost:9000/app","observed_at":"2026-01-01T00:00:00Z","app":"retained","version":"v1","metadata":"model-x","mode":"single-shot","gpu":{"id":"0","name":" H100 ","vram_mb":80000},"capacity":3,"capacity_used":1,"capacity_available":2,"price_info":{"price":10,"price_usd":0.1,"currency":"wei","unit":"fixed"}},{"url":"http://localhost:9000/cpu","observed_at":"2026-01-01T00:00:00Z","app":"cpu","mode":"single-shot","capacity":1,"price_info":{"price":1,"currency":"wei","unit":"fixed"}}]}]` + const orchestratorField = `"orchestrator":"` + recipient + `",` + const advertised = `[{"address":"http://localhost:9000",` + orchestratorField + `"runners":[{"url":"http://localhost:9000/app","observed_at":"2026-01-01T00:00:00Z","app":"retained","version":"v1","metadata":"model-x","mode":"single-shot","gpu":{"id":"0","name":" H100 ","vram_mb":80000},"capacity":3,"capacity_used":1,"capacity_available":2,"price_info":{"price":10,"price_usd":0.1,"currency":"wei","unit":"fixed"}},{"url":"http://localhost:9000/cpu","observed_at":"2026-01-01T00:00:00Z","app":"cpu","mode":"single-shot","capacity":1,"price_info":{"price":1,"currency":"wei","unit":"fixed"}}]}]` + const otherRecipient = "0x2222222222222222222222222222222222222222" + const missingRecipient = "0x3333333333333333333333333333333333333333" const maxBytes = 3 << 20 bodies := map[string]string{ "empty": "[]", "good": advertised, "failed": "", "malformed": "not JSON", + "legacy": strings.NewReplacer(orchestratorField, "", ":9000", ":9001").Replace(advertised), + "other": strings.NewReplacer(recipient, otherRecipient, ":9000", ":9002").Replace(advertised), + "stale": strings.NewReplacer(recipient, otherRecipient, "2026-01-01", "2025-12-31").Replace(advertised), "lenient": strings.NewReplacer( `"price":10,`, `"price":"10",`, `"price_usd":0.1,`, `"price_usd":"0.1",`, @@ -102,6 +108,7 @@ func TestSignerDiscovery(t *testing.T) { apps []string }{ {nil, "", 503, nil}, + {nil, "orchestrator=" + recipient, 503, nil}, {[]string{"empty"}, "", 200, nil}, {[]string{"invalid"}, "", 503, nil}, {[]string{"metadata"}, "", 200, nil}, {[]string{"failed"}, "", 503, nil}, {[]string{"malformed"}, "", 503, nil}, @@ -114,6 +121,17 @@ func TestSignerDiscovery(t *testing.T) { {[]string{"good"}, "app=cpu", 200, []string{"cpu"}}, {[]string{"good"}, "app=retained&gpu=L40S", 200, nil}, {[]string{"good"}, "app=cpu&gpu=H100", 200, nil}, + {[]string{"legacy", "other", "good"}, "orchestrator=" + recipient, 200, []string{"retained", "cpu"}}, + {[]string{"stale", "good"}, "orchestrator=" + recipient, 200, []string{"retained", "cpu"}}, + {[]string{"stale", "good"}, "orchestrator=" + otherRecipient, 200, nil}, + {[]string{"collected"}, "orchestrator=" + missingRecipient + "&orchestrator=" + strings.ToLower(recipient) + "&app=retained&gpu=H100", 200, []string{"retained"}}, + {[]string{"good"}, "orchestrator=" + missingRecipient + "&app=retained&gpu=H100", 200, nil}, + {[]string{"good"}, "orchestrator=" + recipient + "&app=cpu&gpu=H100", 200, nil}, + {[]string{"legacy", "good"}, "orchestrator=", 200, nil}, + {[]string{"good"}, "orchestrator=invalid", 200, nil}, + {[]string{"good"}, "orchestrat%6Fr=%ZZ", 200, nil}, + {[]string{"good"}, "orchestrator=%ZZ&orchestrator=" + recipient, 200, []string{"retained", "cpu"}}, + {[]string{"good"}, "app=cpu&other=orchestrator=%ZZ", 200, []string{"cpu"}}, } { t.Run(strings.Join(test.sources, ",")+"?"+test.query, func(t *testing.T) { urls := make([]*url.URL, len(test.sources)) @@ -139,6 +157,19 @@ func TestSignerDiscovery(t *testing.T) { require.Equal(t, want, result[0].Runners, "preserve advertised fields and URLs") }) } + t.Run("multiple orchestrators", func(t *testing.T) { + urls := []*url.URL{sources["legacy"], sources["other"], sources["good"]} + require.NoError(t, s.SetDiscovery(urls, endpoint.Hostname())) + w := discoveryResponse(s, "orchestrator="+strings.ToUpper(recipient)+"&orchestrator="+otherRecipient) + require.Equal(t, http.StatusOK, w.Code) + var result []discoveredOrchestrator + require.NoError(t, json.Unmarshal(w.Body.Bytes(), &result)) + var recipients []string + for _, entry := range result { + recipients = append(recipients, entry.Orchestrator) + } + require.ElementsMatch(t, []string{recipient, otherRecipient}, recipients) + }) } type discoveryCloseTracker struct { @@ -199,10 +230,14 @@ func TestSignerDiscoveryFreshnessMerge(t *testing.T) { s := newService(nil) defer s.Close() require.NoError(t, s.SetDiscovery([]*url.URL{testURL(t, "https://first.example"), testURL(t, "https://second.example")})) - const old = `[{"address":"https://owner-old.example","runners":[{"url":"https://runner.example/app","app":"old","gpu":{"name":"H100"},"observed_at":"2026-01-01T00:00:00Z","capacity":99}]}]` + const oldRecipient = "0x1111111111111111111111111111111111111111" + const old = `[{"address":"https://owner-old.example","orchestrator":"` + oldRecipient + `","runners":[{"url":"https://runner.example/app","app":"old","gpu":{"name":"H100"},"observed_at":"2026-01-01T00:00:00Z","capacity":99}]}]` const fresh = `[{"address":"https://owner-new.example","runners":[{"url":"https://runner.example/app","app":"new","gpu":{"name":"L40S"},"observed_at":"2026-01-01T02:00:00.123+01:00","capacity":1},{"url":"https://other.example/app","app":"new","gpu":{"name":"L40S"},"observed_at":"2026-01-01T00:00:00Z"}]}]` var offline atomic.Bool s.collector.Client.Transport = webhookTransport(func(r *http.Request) (*http.Response, error) { + if r.URL.RawQuery != "" { + t.Errorf("discovery request forwarded filters: %s", r.URL.RawQuery) + } body := old if r.URL.Host == "second.example" { if offline.Load() { @@ -210,16 +245,7 @@ func TestSignerDiscoveryFreshnessMerge(t *testing.T) { } body = fresh } - // Model a source that honors filters: forwarding them would hide the newer record. - var entries []discoveredOrchestrator - require.NoError(t, json.Unmarshal([]byte(body), &entries)) - app, gpu := r.URL.Query().Get("app"), r.URL.Query().Get("gpu") - entries[0].Runners = slices.DeleteFunc(entries[0].Runners, func(runner discoveredRunner) bool { - return app != "" && runner.App != app || gpu != "" && runner.GPU.Name != gpu - }) - data, err := json.Marshal(entries) - require.NoError(t, err) - return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(data)))}, nil + return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(body))}, nil }) w := discoveryResponse(s, "") require.Equal(t, 200, w.Code) @@ -231,7 +257,7 @@ func TestSignerDiscoveryFreshnessMerge(t *testing.T) { require.Len(t, result[0].Runners, 2) require.Equal(t, 1, result[0].Runners[0].Capacity) require.Equal(t, "2026-01-01T02:00:00.123+01:00", result[0].Runners[0].ObservedAt) - for _, query := range []string{"app=old", "gpu=H100", "app=old&gpu=H100"} { + for _, query := range []string{"app=old", "gpu=H100", "app=old&gpu=H100", "orchestrator=" + oldRecipient} { require.JSONEq(t, "[]", discoveryResponse(s, query).Body.String(), "resolve duplicates before filtering") } offline.Store(true) diff --git a/signer/service.go b/signer/service.go index caf9bfc..90f3eb4 100644 --- a/signer/service.go +++ b/signer/service.go @@ -262,8 +262,20 @@ func (s *Service) discover(w http.ResponseWriter, r *http.Request) { signerError(w, http.StatusServiceUnavailable, "orchestrator discovery unavailable") return } - appFilter := r.URL.Query()["app"] - gpuFilter := r.URL.Query()["gpu"] + query, queryErr := url.ParseQuery(r.URL.RawQuery) + appFilter := query["app"] + gpuFilter := query["gpu"] + orchestratorFilter, filterByOrchestrator := query["orchestrator"] + if queryErr != nil && !filterByOrchestrator { + // ParseQuery drops malformed values, so recover whether the filter was supplied. + for part := range strings.SplitSeq(r.URL.RawQuery, "&") { + key, _, _ := strings.Cut(part, "=") + if key, err := url.QueryUnescape(key); err == nil && key == "orchestrator" { + filterByOrchestrator = true + break + } + } + } inventories := []discovery.Entry[discoveredRunner]{} observations := map[string]time.Time{} for _, snapshot := range snapshots { @@ -276,6 +288,11 @@ func (s *Service) discover(w http.ResponseWriter, r *http.Request) { } result := make([]discoveredOrchestrator, 0) for _, entry := range discovery.Merge(inventories) { + if filterByOrchestrator && !slices.ContainsFunc(orchestratorFilter, func(address string) bool { + return entry.Orchestrator != "" && strings.EqualFold(entry.Orchestrator, address) + }) { + continue + } filtered := make([]discoveredRunner, 0, len(entry.Runners)) for _, runner := range entry.Runners { if len(appFilter) > 0 && !slices.Contains(appFilter, runner.App) {