-
Notifications
You must be signed in to change notification settings - Fork 658
/
service.go
163 lines (130 loc) · 4.96 KB
/
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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package admin
import (
"errors"
"net/http"
"github.com/gorilla/rpc/v2"
"github.com/ava-labs/avalanchego/api"
"github.com/ava-labs/avalanchego/api/server"
"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/profiler"
cjson "github.com/ava-labs/avalanchego/utils/json"
)
const (
maxAliasLength = 512
// Name of file that stacktraces are written to
stacktraceFile = "stacktrace.txt"
)
var errAliasTooLong = errors.New("alias length is too long")
// Admin is the API service for node admin management
type Admin struct {
log logging.Logger
profiler profiler.Profiler
chainManager chains.Manager
httpServer *server.Server
}
// NewService returns a new admin API service
func NewService(log logging.Logger, chainManager chains.Manager, httpServer *server.Server, profileDir string) (*common.HTTPHandler, error) {
newServer := rpc.NewServer()
codec := cjson.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
if err := newServer.RegisterService(&Admin{
log: log,
chainManager: chainManager,
httpServer: httpServer,
profiler: profiler.New(profileDir),
}, "admin"); err != nil {
return nil, err
}
return &common.HTTPHandler{Handler: newServer}, nil
}
// StartCPUProfiler starts a cpu profile writing to the specified file
func (service *Admin) StartCPUProfiler(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: StartCPUProfiler called")
reply.Success = true
return service.profiler.StartCPUProfiler()
}
// StopCPUProfiler stops the cpu profile
func (service *Admin) StopCPUProfiler(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: StopCPUProfiler called")
reply.Success = true
return service.profiler.StopCPUProfiler()
}
// MemoryProfile runs a memory profile writing to the specified file
func (service *Admin) MemoryProfile(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: MemoryProfile called")
reply.Success = true
return service.profiler.MemoryProfile()
}
// LockProfile runs a mutex profile writing to the specified file
func (service *Admin) LockProfile(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: LockProfile called")
reply.Success = true
return service.profiler.LockProfile()
}
// AliasArgs are the arguments for calling Alias
type AliasArgs struct {
Endpoint string `json:"endpoint"`
Alias string `json:"alias"`
}
// Alias attempts to alias an HTTP endpoint to a new name
func (service *Admin) Alias(_ *http.Request, args *AliasArgs, reply *api.SuccessResponse) error {
service.log.Info("Admin: Alias called with URL: %s, Alias: %s", args.Endpoint, args.Alias)
if len(args.Alias) > maxAliasLength {
return errAliasTooLong
}
reply.Success = true
return service.httpServer.AddAliasesWithReadLock(args.Endpoint, args.Alias)
}
// AliasChainArgs are the arguments for calling AliasChain
type AliasChainArgs struct {
Chain string `json:"chain"`
Alias string `json:"alias"`
}
// AliasChain attempts to alias a chain to a new name
func (service *Admin) AliasChain(_ *http.Request, args *AliasChainArgs, reply *api.SuccessResponse) error {
service.log.Info("Admin: AliasChain called with Chain: %s, Alias: %s", args.Chain, args.Alias)
if len(args.Alias) > maxAliasLength {
return errAliasTooLong
}
chainID, err := service.chainManager.Lookup(args.Chain)
if err != nil {
return err
}
if err := service.chainManager.Alias(chainID, args.Alias); err != nil {
return err
}
reply.Success = true
return service.httpServer.AddAliasesWithReadLock("bc/"+chainID.String(), "bc/"+args.Alias)
}
// GetChainAliasesArgs are the arguments for calling GetChainAliases
type GetChainAliasesArgs struct {
Chain string `json:"chain"`
}
// GetChainAliasesReply are the aliases of the given chain
type GetChainAliasesReply struct {
Aliases []string `json:"aliases"`
}
// GetChainAliases returns the aliases of the chain
func (service *Admin) GetChainAliases(r *http.Request, args *GetChainAliasesArgs, reply *GetChainAliasesReply) error {
service.log.Info("Admin: GetChainAliases called with Chain: %s", args.Chain)
id, err := ids.FromString(args.Chain)
if err != nil {
return err
}
reply.Aliases = service.chainManager.Aliases(id)
return nil
}
// Stacktrace returns the current global stacktrace
func (service *Admin) Stacktrace(_ *http.Request, _ *struct{}, reply *api.SuccessResponse) error {
service.log.Info("Admin: Stacktrace called")
reply.Success = true
stacktrace := []byte(logging.Stacktrace{Global: true}.String())
return perms.WriteFile(stacktraceFile, stacktrace, perms.ReadWrite)
}