Skip to content

Commit

Permalink
ir: Start metrics and control service before blockchain init
Browse files Browse the repository at this point in the history
Sync may take some time while nothing stops IR from showing some useful info.
Closes #2677.

Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Dec 14, 2023
1 parent 6151f15 commit beee2c3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog for NeoFS Node
### Added

### Fixed
- Metrics availability during startup (#2677)

### Changed

Expand Down
138 changes: 76 additions & 62 deletions pkg/innerring/innerring.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,12 +390,34 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
}
}

isLocalConsensus := isLocalConsensusMode(cfg)

Check warning on line 393 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L393

Added line #L393 was not covered by tests
isAutoDeploy, err := isAutoDeploymentMode(cfg)
if err != nil {
return nil, err
}

isLocalConsensus := isLocalConsensusMode(cfg)
if isLocalConsensus {
if singleAcc == nil {
return nil, fmt.Errorf("missing account with label '%s' in wallet '%s'", singleAccLabel, walletPass)

Check warning on line 401 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L399-L401

Added lines #L399 - L401 were not covered by tests
}

server.key = singleAcc.PrivateKey()
} else {
acc, err := utilConfig.LoadAccount(walletPath, cfg.GetString("wallet.address"), walletPass)
if err != nil {
return nil, fmt.Errorf("ir: %w", err)

Check warning on line 408 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L404-L408

Added lines #L404 - L408 were not covered by tests
}

server.key = acc.PrivateKey()

Check warning on line 411 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L411

Added line #L411 was not covered by tests
}

err = serveControl(server, log, cfg, errChan)
if err != nil {
return nil, err

Check warning on line 416 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L414-L416

Added lines #L414 - L416 were not covered by tests
}

serveMetrics(server, cfg)

Check warning on line 419 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L419

Added line #L419 was not covered by tests

var localWSClient *rpcclient.WSClient // set if isLocalConsensus only

// create morph client
Expand All @@ -406,10 +428,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
return nil, fmt.Errorf("invalid blockchain configuration: %w", err)
}

if singleAcc == nil {
return nil, fmt.Errorf("missing account with label '%s' in wallet '%s'", singleAccLabel, walletPass)
}

if consensusAcc == nil {
return nil, fmt.Errorf("missing account with label '%s' in wallet '%s'", consensusAccLabel, walletPass)
}
Expand Down Expand Up @@ -450,7 +468,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
return nil, fmt.Errorf("build WS client on internal blockchain: %w", err)
}

server.key = singleAcc.PrivateKey()
morphChain.key = server.key
sidechainOpts := make([]client.Option, 3, 4)
sidechainOpts[0] = client.WithContext(ctx)
Expand All @@ -471,16 +488,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
}

// fallback to the pure RPC architecture
acc, err := utilConfig.LoadAccount(
walletPath,
cfg.GetString("wallet.address"),
walletPass,
)
if err != nil {
return nil, fmt.Errorf("ir: %w", err)
}

server.key = acc.PrivateKey()
morphChain.key = server.key
morphChain.withAutoSidechainScope = !isAutoDeploy

Expand Down Expand Up @@ -565,53 +573,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper, errChan chan<-
}
}

controlSvcEndpoint := cfg.GetString("control.grpc.endpoint")
if controlSvcEndpoint != "" {
authKeysStr := cfg.GetStringSlice("control.authorized_keys")
authKeys := make([][]byte, 0, len(authKeysStr))

for i := range authKeysStr {
key, err := hex.DecodeString(authKeysStr[i])
if err != nil {
return nil, fmt.Errorf("could not parse Control authorized key %s: %w",
authKeysStr[i],
err,
)
}

authKeys = append(authKeys, key)
}

lis, err := net.Listen("tcp", controlSvcEndpoint)
if err != nil {
return nil, err
}
var p controlsrv.Prm

p.SetPrivateKey(*server.key)
p.SetHealthChecker(server)

controlSvc := controlsrv.New(p,
controlsrv.WithAllowedKeys(authKeys),
)

grpcControlSrv := grpc.NewServer()
control.RegisterControlServiceServer(grpcControlSrv, controlSvc)

go func() {
errChan <- grpcControlSrv.Serve(lis)
}()

server.registerNoErrCloser(grpcControlSrv.GracefulStop)
} else {
log.Info("no Control server endpoint specified, service is disabled")
}

if cfg.GetString("prometheus.address") != "" {
m := metrics.NewInnerRingMetrics(misc.Version)
server.metrics = &m
}

// create morph listener
server.morphListener, err = createListener(server.morphClient, morphChain)
if err != nil {
Expand Down Expand Up @@ -1273,3 +1234,56 @@ func (s *Server) restartMorph() error {
func (s *Server) restartMainChain() error {
return nil
}

func serveControl(server *Server, log *zap.Logger, cfg *viper.Viper, errChan chan<- error) error {
controlSvcEndpoint := cfg.GetString("control.grpc.endpoint")
if controlSvcEndpoint != "" {
authKeysStr := cfg.GetStringSlice("control.authorized_keys")
authKeys := make([][]byte, 0, len(authKeysStr))

Check warning on line 1242 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1238-L1242

Added lines #L1238 - L1242 were not covered by tests

for i := range authKeysStr {
key, err := hex.DecodeString(authKeysStr[i])
if err != nil {
return fmt.Errorf("could not parse Control authorized key %s: %w",
authKeysStr[i],
err,
)

Check warning on line 1250 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1244-L1250

Added lines #L1244 - L1250 were not covered by tests
}

authKeys = append(authKeys, key)

Check warning on line 1253 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1253

Added line #L1253 was not covered by tests
}

lis, err := net.Listen("tcp", controlSvcEndpoint)
if err != nil {
return err

Check warning on line 1258 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1256-L1258

Added lines #L1256 - L1258 were not covered by tests
}
var p controlsrv.Prm

Check warning on line 1260 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1260

Added line #L1260 was not covered by tests

p.SetPrivateKey(*server.key)
p.SetHealthChecker(server)

Check warning on line 1263 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1262-L1263

Added lines #L1262 - L1263 were not covered by tests

controlSvc := controlsrv.New(p,
controlsrv.WithAllowedKeys(authKeys),
)

Check warning on line 1267 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1265-L1267

Added lines #L1265 - L1267 were not covered by tests

grpcControlSrv := grpc.NewServer()
control.RegisterControlServiceServer(grpcControlSrv, controlSvc)

Check warning on line 1270 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1269-L1270

Added lines #L1269 - L1270 were not covered by tests

go func() {
errChan <- grpcControlSrv.Serve(lis)
}()

Check warning on line 1274 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1272-L1274

Added lines #L1272 - L1274 were not covered by tests

server.registerNoErrCloser(grpcControlSrv.GracefulStop)
} else {
log.Info("no Control server endpoint specified, service is disabled")

Check warning on line 1278 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1276-L1278

Added lines #L1276 - L1278 were not covered by tests
}

return nil

Check warning on line 1281 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1281

Added line #L1281 was not covered by tests
}

func serveMetrics(server *Server, cfg *viper.Viper) {
if cfg.GetString("prometheus.address") != "" {
m := metrics.NewInnerRingMetrics(misc.Version)
server.metrics = &m

Check warning on line 1287 in pkg/innerring/innerring.go

View check run for this annotation

Codecov / codecov/patch

pkg/innerring/innerring.go#L1284-L1287

Added lines #L1284 - L1287 were not covered by tests
}
}

0 comments on commit beee2c3

Please sign in to comment.