-
Notifications
You must be signed in to change notification settings - Fork 155
/
resolver.go
145 lines (130 loc) · 4.21 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package segfetcher
import (
"context"
"time"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/private/serrors"
seg "github.com/scionproto/scion/pkg/segment"
"github.com/scionproto/scion/private/pathdb"
"github.com/scionproto/scion/private/pathdb/query"
"github.com/scionproto/scion/private/revcache"
)
// ErrInvalidRequest indicates an invalid request.
var ErrInvalidRequest = serrors.New("invalid request")
// Resolver resolves segments that are locally cached.
type Resolver interface {
// Resolve resolves requests. It loads the segments that are locally available
// from the DB and returns the set of requests that have to be requested at a
// remote server.
Resolve(ctx context.Context, reqs Requests, refresh bool) (Segments, Requests, error)
}
// LocalInfo provides information about which segments are always locally
// stored.
type LocalInfo interface {
// IsSegLocal returns whether the requested segment is always locally stored.
IsSegLocal(req Request) bool
}
// NewResolver creates a new resolver with the given DB.
func NewResolver(DB pathdb.ReadWrite, revCache revcache.RevCache,
localInfo LocalInfo) *DefaultResolver {
return &DefaultResolver{
DB: DB,
RevCache: revCache,
LocalInfo: localInfo,
}
}
// DefaultResolver is the default resolver implementation.
type DefaultResolver struct {
DB pathdb.ReadWrite
RevCache revcache.RevCache
LocalInfo LocalInfo
}
// Resolve resolves requests. It loads the segments that are locally available
// from the DB and returns the set of requests that have to be requested at a
// remote server.
func (r *DefaultResolver) Resolve(ctx context.Context,
reqs Requests, refresh bool) (Segments, Requests, error) {
var segs Segments
var fetchReqs Requests
for i := range reqs {
segsi, err := r.resolveSegment(ctx, reqs[i], refresh)
if err != nil {
return nil, nil, err
}
if segsi != nil {
segs = append(segs, segsi...)
} else {
fetchReqs = append(fetchReqs, reqs[i])
}
}
return segs, fetchReqs, nil
}
// resolveSegment loads the segments for this request from the DB.
// Returns nil if the segments are not local information and are not
// available/up to date from the cache.
func (r *DefaultResolver) resolveSegment(ctx context.Context,
req Request, refresh bool) (Segments, error) {
local := r.LocalInfo.IsSegLocal(req)
if !local {
if refresh {
return nil, nil
}
fetch, err := r.needsFetching(ctx, req)
if err != nil || fetch {
return nil, err
}
}
// The segment is local or cached
res, err := r.loadSegment(ctx, req)
if err != nil {
return nil, err
}
allRev, err := r.allRevoked(ctx, res)
if err != nil {
return nil, err
}
// because of revocations our cache is empty, so refetch
if allRev && !local {
return nil, nil
}
return res.SegMetas(), err
}
func (r *DefaultResolver) loadSegment(ctx context.Context, req Request) (query.Results, error) {
start, end := req.Src, req.Dst
consDir := (req.SegType == seg.TypeDown)
if !consDir {
start, end = end, start
}
return r.DB.Get(ctx, &query.Params{
StartsAt: []addr.IA{start},
EndsAt: []addr.IA{end},
SegTypes: []seg.Type{req.SegType},
})
}
func (r *DefaultResolver) needsFetching(ctx context.Context, req Request) (bool, error) {
nq, err := r.DB.GetNextQuery(ctx, req.Src, req.Dst)
return time.Now().After(nq), err
}
func (r *DefaultResolver) allRevoked(ctx context.Context, results query.Results) (bool, error) {
segs := results.Segs()
filtered, err := segs.FilterSegs(func(ps *seg.PathSegment) (bool, error) {
return revcache.NoRevokedHopIntf(ctx, r.RevCache, ps)
})
if err != nil {
return false, err
}
return len(segs) == 0 && filtered > 0, nil
}