Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
16 changes: 10 additions & 6 deletions docs/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions docs/signer.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 39 additions & 13 deletions signer/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",`,
Expand Down Expand Up @@ -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},
Expand All @@ -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))
Expand All @@ -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 {
Expand Down Expand Up @@ -199,27 +230,22 @@ 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() {
return &http.Response{StatusCode: 500, Body: io.NopCloser(strings.NewReader(""))}, nil
}
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)
Expand All @@ -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)
Expand Down
21 changes: 19 additions & 2 deletions signer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
Loading