-
Notifications
You must be signed in to change notification settings - Fork 154
/
lookup.go
159 lines (137 loc) · 4.28 KB
/
lookup.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2020 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 grpc
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/scionproto/scion/pkg/addr"
"github.com/scionproto/scion/pkg/log"
"github.com/scionproto/scion/pkg/metrics"
"github.com/scionproto/scion/pkg/private/prom"
cppb "github.com/scionproto/scion/pkg/proto/control_plane"
seg "github.com/scionproto/scion/pkg/segment"
"github.com/scionproto/scion/private/revcache"
"github.com/scionproto/scion/private/segment/segfetcher"
"github.com/scionproto/scion/private/tracing"
)
// Lookuper looks up path segments.
type Lookuper interface {
LookupSegments(ctx context.Context, src, dst addr.IA) (segfetcher.Segments, error)
}
// LookupServer handles path segment lookups.
type LookupServer struct {
Lookuper Lookuper
RevCache revcache.RevCache
// Requests aggregates all the incoming requests received by the handler.
// If it is not initialized, nothing is reported.
Requests metrics.Counter
// SegmentsSent aggregates the number of segments that were transmitted in
// response to a segment request.
SegmentsSent metrics.Counter
}
func (s LookupServer) Segments(ctx context.Context,
req *cppb.SegmentsRequest) (*cppb.SegmentsResponse, error) {
src, dst := addr.IA(req.SrcIsdAs), addr.IA(req.DstIsdAs)
labels := requestLabels{
Desc: descLabels{
DstISD: dst.ISD(),
},
}
logger := log.FromCtx(ctx)
span := opentracing.SpanFromContext(ctx)
setQueryTags(span, src, dst)
logger.Debug("Received segment request", "src", src, "dst", dst)
segs, err := s.Lookuper.LookupSegments(ctx, src, dst)
if err != nil {
logger.Debug("Failed to lookup requested segments", "err", err)
s.updateMetric(span, labels.WithResult(segfetcher.ErrToMetricsLabel(err)), err)
if len(segs) == 0 {
// TODO(roosd): Differentiate errors and expose the applicable gRPC
// status codes.
return nil, err
}
// We have some segments and continue with a partial result.
}
labels.Desc.SegType = determineReplyType(segs)
if span != nil {
span.SetTag("seg_type", labels.Desc.SegType)
}
m := map[int32]*cppb.SegmentsResponse_Segments{}
for _, meta := range segs {
s, ok := m[int32(meta.Type)]
if !ok {
s = &cppb.SegmentsResponse_Segments{}
m[int32(meta.Type)] = s
}
s.Segments = append(s.Segments, seg.PathSegmentToPB(meta.Segment))
}
logger.Debug("Replied with segments", "count", len(segs))
s.updateMetric(span, labels.WithResult(prom.Success), nil)
s.incSent(s.SegmentsSent, labels.Desc, len(segs))
return &cppb.SegmentsResponse{
Segments: m,
}, nil
}
func (s LookupServer) updateMetric(span opentracing.Span, l requestLabels, err error) {
if s.Requests != nil {
s.Requests.With(l.Expand()...).Add(1)
}
if span != nil {
tracing.ResultLabel(span, l.Result)
tracing.Error(span, err)
}
}
func (s LookupServer) incSent(c metrics.Counter, labels descLabels, inc int) {
if c != nil {
c.With(labels.Expand()...).Add(float64(inc))
}
}
func setQueryTags(span opentracing.Span, src, dst addr.IA) {
if span != nil {
span.SetTag("query.src", src)
span.SetTag("query.dst", dst)
}
}
type requestLabels struct {
Desc descLabels
Result string
}
func (l requestLabels) Expand() []string {
return append(
l.Desc.Expand(),
prom.LabelResult, l.Result,
)
}
func (l requestLabels) WithResult(result string) requestLabels {
l.Result = result
return l
}
type descLabels struct {
SegType string
DstISD addr.ISD
}
func (l descLabels) Expand() []string {
return []string{
"seg_type", l.SegType,
"dst_isd", l.DstISD.String(),
}
}
// determineReplyType determines which type of segments is in the reply. The
// method assumes that segs only contains one type of segments.
func determineReplyType(segs segfetcher.Segments) string {
if len(segs) > 0 {
return segs[0].Type.String()
}
return "none"
}