-
Notifications
You must be signed in to change notification settings - Fork 211
/
admin_service.go
175 lines (163 loc) · 4.89 KB
/
admin_service.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
package grpcserver
import (
"context"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spf13/afero"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/spacemeshos/go-spacemesh/checkpoint"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/sql"
)
const (
chunksize = 1024
defaultNumAtxs = 4
)
// AdminService exposes endpoints for node administration.
type AdminService struct {
db *sql.Database
dataDir string
recover func()
p peers
}
// NewAdminService creates a new admin grpc service.
func NewAdminService(db *sql.Database, dataDir string, p peers) *AdminService {
return &AdminService{
db: db,
dataDir: dataDir,
recover: func() {
go func() {
// Allow time for the response to be sent.
time.Sleep(time.Second)
os.Exit(0)
}()
},
p: p,
}
}
// RegisterService registers this service with a grpc server instance.
func (a AdminService) RegisterService(server *Server) {
pb.RegisterAdminServiceServer(server.GrpcServer, a)
}
func (a AdminService) CheckpointStream(req *pb.CheckpointStreamRequest, stream pb.AdminService_CheckpointStreamServer) error {
// checkpoint data can be more than 4MB, it can cause stress
// - on the client side (default limit on the receiving end)
// - locally as the node already loads db query result in memory
snapshot := types.LayerID(req.SnapshotLayer)
numAtxs := int(req.NumAtxs)
if numAtxs < defaultNumAtxs {
numAtxs = defaultNumAtxs
}
err := checkpoint.Generate(stream.Context(), afero.NewOsFs(), a.db, a.dataDir, snapshot, numAtxs)
if err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to create checkpoint: %s", err.Error()))
}
fname := checkpoint.SelfCheckpointFilename(a.dataDir, snapshot)
if err := stream.SendHeader(metadata.MD{}); err != nil {
return status.Errorf(codes.Unavailable, "can't send header")
}
f, err := os.Open(fname)
if err != nil {
return status.Errorf(codes.Internal, fmt.Sprintf("failed to open file %s: %s", fname, err.Error()))
}
defer f.Close()
var (
buf = make([]byte, chunksize)
chunk int
)
for {
select {
case <-stream.Context().Done():
return nil
default:
chunk, err = f.Read(buf)
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return status.Errorf(codes.Internal, fmt.Sprintf("failed to read from file %s: %s", fname, err.Error()))
}
if err = stream.Send(&pb.CheckpointStreamResponse{Data: buf[:chunk]}); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
}
}
}
func (a AdminService) Recover(ctx context.Context, _ *pb.RecoverRequest) (*emptypb.Empty, error) {
ctxzap.Info(ctx, "going to recover from checkpoint")
a.recover()
return &emptypb.Empty{}, nil
}
func (a AdminService) EventsStream(req *pb.EventStreamRequest, stream pb.AdminService_EventsStreamServer) error {
sub, buf, err := events.SubscribeUserEvents(events.WithBuffer(1000))
if err != nil {
return status.Errorf(codes.FailedPrecondition, err.Error())
}
defer sub.Close()
// send empty header after subscribing to the channel.
// this is optional but allows subscriber to wait until stream is fully initialized.
if err := stream.SendHeader(metadata.MD{}); err != nil {
return status.Errorf(codes.Unavailable, "can't send header")
}
buf.Iterate(func(ev events.UserEvent) bool {
err = stream.Send(ev.Event)
return err == nil
})
if err != nil {
return fmt.Errorf("send buffered to stream: %w", err)
}
for {
select {
case <-stream.Context().Done():
return nil
case <-sub.Full():
return status.Errorf(codes.Canceled, "buffer is full")
case ev := <-sub.Out():
if err := stream.Send(ev.Event); err != nil {
return fmt.Errorf("send to stream: %w", err)
}
}
}
}
func (a AdminService) PeerInfoStream(_ *emptypb.Empty, stream pb.AdminService_PeerInfoStreamServer) error {
for _, p := range a.p.GetPeers() {
select {
case <-stream.Context().Done():
return nil
default:
info := a.p.ConnectedPeerInfo(p)
// There is no guarantee that the peers originally returned will still
// be connected by the time we call ConnectedPeerInfo.
if info == nil {
continue
}
connections := make([]*pb.ConnectionInfo, len(info.Connections))
for j, c := range info.Connections {
connections[j] = &pb.ConnectionInfo{
Address: c.Address.String(),
Uptime: durationpb.New(c.Uptime),
Outbound: c.Outbound,
}
}
err := stream.Send(&pb.PeerInfo{
Id: info.ID.String(),
Connections: connections,
Tags: info.Tags,
})
if err != nil {
return fmt.Errorf("send to stream: %w", err)
}
}
}
return nil
}