-
Notifications
You must be signed in to change notification settings - Fork 402
/
inspector.go
204 lines (170 loc) · 5.37 KB
/
inspector.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package inspector
import (
"context"
"encoding/binary"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/pb"
"storj.io/common/storj"
"storj.io/common/uuid"
"storj.io/storj/satellite/internalpb"
"storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/overlay"
)
var (
mon = monkit.Package()
// Error wraps errors returned from Server struct methods.
Error = errs.Class("inspector")
)
// Endpoint for checking object and segment health.
//
// architecture: Endpoint
type Endpoint struct {
internalpb.DRPCHealthInspectorUnimplementedServer
log *zap.Logger
overlay *overlay.Service
metabase *metabase.DB
}
// NewEndpoint will initialize an Endpoint struct.
func NewEndpoint(log *zap.Logger, cache *overlay.Service, metabase *metabase.DB) *Endpoint {
return &Endpoint{
log: log,
overlay: cache,
metabase: metabase,
}
}
// ObjectHealth will check the health of an object.
func (endpoint *Endpoint) ObjectHealth(ctx context.Context, in *internalpb.ObjectHealthRequest) (resp *internalpb.ObjectHealthResponse, err error) {
defer mon.Task()(&ctx)(&err)
var segmentHealthResponses []*internalpb.SegmentHealth
var redundancy *pb.RedundancyScheme
limit := int(100)
if in.GetLimit() > 0 {
limit = int(in.GetLimit())
}
var startPosition metabase.SegmentPosition
if in.GetStartAfterSegment() > 0 {
startPosition = metabase.SegmentPositionFromEncoded(uint64(in.GetStartAfterSegment()))
}
projectID, err := uuid.FromBytes(in.GetProjectId())
if err != nil {
return nil, Error.Wrap(err)
}
objectLocation := metabase.ObjectLocation{
ProjectID: projectID,
BucketName: string(in.GetBucket()),
ObjectKey: metabase.ObjectKey(in.GetEncryptedPath()),
}
// TODO add version field to ObjectHealthRequest?
object, err := endpoint.metabase.GetObjectLatestVersion(ctx, metabase.GetObjectLatestVersion{
ObjectLocation: objectLocation,
})
if err != nil {
return nil, Error.Wrap(err)
}
listResult, err := endpoint.metabase.ListSegments(ctx, metabase.ListSegments{
StreamID: object.StreamID,
Cursor: startPosition,
Limit: limit,
})
if err != nil {
return nil, Error.Wrap(err)
}
for _, segment := range listResult.Segments {
if !segment.Inline() {
segmentHealth, err := endpoint.segmentHealth(ctx, segment)
if err != nil {
return nil, Error.Wrap(err)
}
segmentHealthResponses = append(segmentHealthResponses, segmentHealth.GetHealth())
redundancy = segmentHealth.GetRedundancy()
}
}
return &internalpb.ObjectHealthResponse{
Segments: segmentHealthResponses,
Redundancy: redundancy,
}, nil
}
// SegmentHealth will check the health of a segment.
func (endpoint *Endpoint) SegmentHealth(ctx context.Context, in *internalpb.SegmentHealthRequest) (_ *internalpb.SegmentHealthResponse, err error) {
defer mon.Task()(&ctx)(&err)
projectID, err := uuid.FromBytes(in.GetProjectId())
if err != nil {
return nil, Error.Wrap(err)
}
objectLocation := metabase.ObjectLocation{
ProjectID: projectID,
BucketName: string(in.GetBucket()),
ObjectKey: metabase.ObjectKey(in.GetEncryptedPath()),
}
object, err := endpoint.metabase.GetObjectLatestVersion(ctx, metabase.GetObjectLatestVersion{
ObjectLocation: objectLocation,
})
if err != nil {
return nil, Error.Wrap(err)
}
segment, err := endpoint.metabase.GetSegmentByPosition(ctx, metabase.GetSegmentByPosition{
StreamID: object.StreamID,
Position: metabase.SegmentPositionFromEncoded(uint64(in.GetSegmentIndex())),
})
if err != nil {
return nil, Error.Wrap(err)
}
if segment.Inline() {
return nil, Error.New("cannot check health of inline segment")
}
return endpoint.segmentHealth(ctx, segment)
}
func (endpoint *Endpoint) segmentHealth(ctx context.Context, segment metabase.Segment) (_ *internalpb.SegmentHealthResponse, err error) {
health := &internalpb.SegmentHealth{}
var nodeIDs storj.NodeIDList
for _, piece := range segment.Pieces {
nodeIDs = append(nodeIDs, piece.StorageNode)
}
unreliableOrOfflineNodes, err := endpoint.overlay.KnownUnreliableOrOffline(ctx, nodeIDs)
if err != nil {
return nil, Error.Wrap(err)
}
offlineNodes, err := endpoint.overlay.KnownOffline(ctx, nodeIDs)
if err != nil {
return nil, Error.Wrap(err)
}
offlineMap := make(map[storj.NodeID]bool)
for _, id := range offlineNodes {
offlineMap[id] = true
}
unreliableOfflineMap := make(map[storj.NodeID]bool)
for _, id := range unreliableOrOfflineNodes {
unreliableOfflineMap[id] = true
}
redundancy := &pb.RedundancyScheme{
MinReq: int32(segment.Redundancy.RequiredShares),
RepairThreshold: int32(segment.Redundancy.RepairShares),
SuccessThreshold: int32(segment.Redundancy.OptimalShares),
Total: int32(segment.Redundancy.TotalShares),
}
var healthyNodes storj.NodeIDList
var unhealthyNodes storj.NodeIDList
for _, id := range nodeIDs {
if offlineMap[id] {
continue
}
if unreliableOfflineMap[id] {
unhealthyNodes = append(unhealthyNodes, id)
} else {
healthyNodes = append(healthyNodes, id)
}
}
health.HealthyIds = healthyNodes
health.UnhealthyIds = unhealthyNodes
health.OfflineIds = offlineNodes
health.Segment = make([]byte, 8)
binary.LittleEndian.PutUint64(health.Segment, segment.Position.Encode())
return &internalpb.SegmentHealthResponse{
Health: health,
Redundancy: redundancy,
}, nil
}