Skip to content

Remove lock options from the admin API #2150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions api/admin/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"net/http"
"path"
"sync"

"github.com/gorilla/rpc/v2"

Expand All @@ -16,7 +17,6 @@ import (
"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"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/json"
Expand Down Expand Up @@ -53,23 +53,24 @@ type Config struct {
// Admin is the API service for node admin management
type Admin struct {
Config
lock sync.RWMutex
profiler profiler.Profiler
}

// NewService returns a new admin API service.
// All of the fields in [config] must be set.
func NewService(config Config) (*common.HTTPHandler, error) {
newServer := rpc.NewServer()
func NewService(config Config) (http.Handler, error) {
server := rpc.NewServer()
codec := json.NewCodec()
newServer.RegisterCodec(codec, "application/json")
newServer.RegisterCodec(codec, "application/json;charset=UTF-8")
if err := newServer.RegisterService(&Admin{
Config: config,
profiler: profiler.New(config.ProfileDir),
}, "admin"); err != nil {
return nil, err
}
return &common.HTTPHandler{Handler: newServer}, nil
server.RegisterCodec(codec, "application/json")
server.RegisterCodec(codec, "application/json;charset=UTF-8")
return server, server.RegisterService(
&Admin{
Config: config,
profiler: profiler.New(config.ProfileDir),
},
"admin",
)
}

// StartCPUProfiler starts a cpu profile writing to the specified file
Expand All @@ -79,6 +80,9 @@ func (a *Admin) StartCPUProfiler(_ *http.Request, _ *struct{}, _ *api.EmptyReply
zap.String("method", "startCPUProfiler"),
)

a.lock.Lock()
defer a.lock.Unlock()

return a.profiler.StartCPUProfiler()
}

Expand All @@ -89,6 +93,9 @@ func (a *Admin) StopCPUProfiler(_ *http.Request, _ *struct{}, _ *api.EmptyReply)
zap.String("method", "stopCPUProfiler"),
)

a.lock.Lock()
defer a.lock.Unlock()

return a.profiler.StopCPUProfiler()
}

Expand All @@ -99,6 +106,9 @@ func (a *Admin) MemoryProfile(_ *http.Request, _ *struct{}, _ *api.EmptyReply) e
zap.String("method", "memoryProfile"),
)

a.lock.Lock()
defer a.lock.Unlock()

return a.profiler.MemoryProfile()
}

Expand All @@ -109,6 +119,9 @@ func (a *Admin) LockProfile(_ *http.Request, _ *struct{}, _ *api.EmptyReply) err
zap.String("method", "lockProfile"),
)

a.lock.Lock()
defer a.lock.Unlock()

return a.profiler.LockProfile()
}

Expand Down Expand Up @@ -157,6 +170,9 @@ func (a *Admin) AliasChain(_ *http.Request, args *AliasChainArgs, _ *api.EmptyRe
return err
}

a.lock.Lock()
defer a.lock.Unlock()

if err := a.ChainManager.Alias(chainID, args.Alias); err != nil {
return err
}
Expand Down Expand Up @@ -201,6 +217,10 @@ func (a *Admin) Stacktrace(_ *http.Request, _ *struct{}, _ *api.EmptyReply) erro
)

stacktrace := []byte(utils.GetStacktrace(true))

a.lock.Lock()
defer a.lock.Unlock()

return perms.WriteFile(stacktraceFile, stacktrace, perms.ReadWrite)
}

Expand Down Expand Up @@ -233,6 +253,9 @@ func (a *Admin) SetLoggerLevel(_ *http.Request, args *SetLoggerLevelArgs, _ *api
return errNoLogLevel
}

a.lock.Lock()
defer a.lock.Unlock()

var loggerNames []string
if len(args.LoggerName) > 0 {
loggerNames = []string{args.LoggerName}
Expand Down Expand Up @@ -278,6 +301,10 @@ func (a *Admin) GetLoggerLevel(_ *http.Request, args *GetLoggerLevelArgs, reply
zap.String("method", "getLoggerLevels"),
logging.UserString("loggerName", args.LoggerName),
)

a.lock.RLock()
defer a.lock.RUnlock()

reply.LoggerLevels = make(map[string]LogAndDisplayLevels)
var loggerNames []string
// Empty name means all loggers
Expand Down Expand Up @@ -329,6 +356,9 @@ func (a *Admin) LoadVMs(r *http.Request, _ *struct{}, reply *LoadVMsReply) error
zap.String("method", "loadVMs"),
)

a.lock.Lock()
defer a.lock.Unlock()

ctx := r.Context()
loadedVMs, failedVMs, err := a.VMRegistry.ReloadWithReadLock(ctx)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,15 @@ func (n *Node) initAdminAPI() error {
if err != nil {
return err
}
return n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "")
return n.APIServer.AddRoute(
&common.HTTPHandler{
LockOptions: common.NoLock,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main purpose of this PR - converting from common.WriteLock to common.NoLock.

Handler: service,
},
&sync.RWMutex{},
"admin",
"",
)
}

// initProfiler initializes the continuous profiling
Expand Down