satellite/satellitedb: fix selection query with AOST

Union query for both reputable and new nodes didn't properly work. The
top level query is required to have an `AS OF SYSTEM TIME` statement as
well.

Change-Id: I8ee6dd5b700c2b1ed2aa562962bfa72be7eec30a
This commit is contained in:
Egon Elbre 2021-10-15 16:41:48 +03:00
parent 4a146000cc
commit 51f488fc4b
2 changed files with 21 additions and 9 deletions

View File

@ -457,8 +457,10 @@ func TestNodeSelectionGracefulExit(t *testing.T) {
} {
t.Logf("#%2d. %+v", i, tt)
response, err := satellite.Overlay.Service.FindStorageNodesWithPreferences(ctx, overlay.FindStorageNodesRequest{
response, err := satellite.Overlay.Service.FindStorageNodesWithPreferences(ctx,
overlay.FindStorageNodesRequest{
RequestedCount: tt.RequestCount,
AsOfSystemInterval: -time.Microsecond,
}, &tt.Preferences)
t.Log(len(response), err)
@ -688,8 +690,10 @@ func testDistinctIPs(t *testing.T, ctx *testcontext.Context, planet *testplanet.
}
for _, tt := range tests {
response, err := service.FindStorageNodesWithPreferences(ctx, overlay.FindStorageNodesRequest{
response, err := service.FindStorageNodesWithPreferences(ctx,
overlay.FindStorageNodesRequest{
RequestedCount: tt.requestCount,
AsOfSystemInterval: -time.Microsecond,
}, &tt.preferences)
if tt.shouldFailWith != nil {
assert.Error(t, err)

View File

@ -140,7 +140,7 @@ func (cache *overlaycache) selectStorageNodesOnce(ctx context.Context, reputable
}
}
query := unionAll(newNodeQuery, reputableNodeQuery)
query := unionAll(asOf, newNodeQuery, reputableNodeQuery)
rows, err := cache.db.Query(ctx, cache.db.Rebind(query.query), query.args...)
if err != nil {
@ -282,7 +282,7 @@ func (partial partialQuery) asQuery() query {
}
// unionAll combines multiple partial queries into a single query.
func unionAll(partials ...partialQuery) query {
func unionAll(asOf string, partials ...partialQuery) query {
var queries []string
var args []interface{}
for _, partial := range partials {
@ -302,8 +302,16 @@ func unionAll(partials ...partialQuery) query {
return query{query: queries[0], args: args}
}
union := "(" + strings.Join(queries, ") UNION ALL (") + ")"
if asOf == "" {
return query{
query: "(" + strings.Join(queries, ") UNION ALL (") + ")",
query: union,
args: args,
}
}
return query{
query: "SELECT * FROM (" + union + ") " + asOf,
args: args,
}
}