Skip to content
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

Power management redesign #1891

Merged
merged 13 commits into from
Feb 14, 2023
4 changes: 2 additions & 2 deletions cmds/identityd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func main() {

if farm {
env := environment.MustGet()
fmt.Println(env.FarmerID)
fmt.Println(env.FarmID)
xmonader marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(0)
} else if net {
env := environment.MustGet()
Expand Down Expand Up @@ -355,7 +355,7 @@ func getIdentityMgr(root string, debug bool) (pkg.IdentityManager, error) {

log.Info().
Bool("orphan", env.Orphan).
Uint64("farmer_id", uint64(env.FarmerID)).
Uint64("farmer_id", uint64(env.FarmID)).
muhamadazmy marked this conversation as resolved.
Show resolved Hide resolved
Msg("farmer identified")

return manager, nil
Expand Down
29 changes: 1 addition & 28 deletions cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func action(cli *cli.Context) error {
return err
}

events, err := events.NewRedisStream(sub, msgBrokerCon, node, eventsBlock)
events, err := events.NewRedisStream(sub, msgBrokerCon, env.FarmID, node, eventsBlock)
if err != nil {
return err
}
Expand Down Expand Up @@ -264,33 +264,6 @@ func action(cli *cli.Context) error {
return err
}

// uptime update
go func() {
defer log.Info().Msg("uptime reporting exited permanently")
safeUptime := func(ctx context.Context, redis zbus.Client) (err error) {
defer func() {
if p := recover(); p != nil {
err = fmt.Errorf("uptime reporting has panicked: %+v", p)
}
}()

err = uptime(ctx, id)
return err
}

for {
err := safeUptime(ctx, redis)
if errors.Is(err, context.Canceled) {
log.Info().Msg("stop uptime reporting. context cancelled")
return
} else if err != nil {
log.Error().Err(err).Msg("sending uptime failed")
}
// even there is no error we try again until ctx is cancelled
<-time.After(10 * time.Second)
}
}()

log.Debug().Msg("start message bus")
for {
err := runMsgBus(ctx, sk, env.SubstrateURL, env.RelayURL, msgBrokerCon)
Expand Down
54 changes: 0 additions & 54 deletions cmds/modules/noded/register.go

This file was deleted.

112 changes: 112 additions & 0 deletions cmds/modules/powerd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package powerd

import (
"crypto/ed25519"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/threefoldtech/substrate-client"
"github.com/threefoldtech/zbus"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/events"
"github.com/threefoldtech/zos/pkg/power"
"github.com/threefoldtech/zos/pkg/stubs"
"github.com/threefoldtech/zos/pkg/utils"
"github.com/urfave/cli/v2"
)

const (
module = "power"
)

// Module is entry point for module
var Module cli.Command = cli.Command{
Name: "powerd",
Usage: "handles the node power events",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "broker",
Usage: "connection string to the message `BROKER`",
Value: "unix:///var/run/redis.sock",
},
},
Action: action,
}

func action(cli *cli.Context) error {
var (
msgBrokerCon string = cli.String("broker")
)

ctx, _ := utils.WithSignal(cli.Context)
utils.OnDone(ctx, func(_ error) {
log.Info().Msg("shutting down")
})

env := environment.MustGet()

cl, err := zbus.NewRedisClient(msgBrokerCon)
if err != nil {
return errors.Wrap(err, "failed to connect to message broker server")
}

identity := stubs.NewIdentityManagerStub(cl)
register := stubs.NewRegistrarStub(cl)

nodeID, err := register.NodeID(ctx)
if err != nil {
return errors.Wrap(err, "failed to get node id")
}

twinID, err := register.TwinID(ctx)
if err != nil {
return errors.Wrap(err, "failed to get node id")
}

sk := ed25519.PrivateKey(identity.PrivateKey(ctx))
id, err := substrate.NewIdentityFromEd25519Key(sk)
log.Info().Str("address", id.Address()).Msg("node address")
if err != nil {
return err
}

sub, err := environment.GetSubstrate()
if err != nil {
return err
}

uptime, err := power.NewUptime(sub, id)
if err != nil {
return errors.Wrap(err, "failed to initialize uptime reported")
}

// start uptime reporting
go uptime.Start(cli.Context)

// if the feature is globally enabled try to ensure
// wake on lan is set correctly.
// then override the enabled flag
enabled, err := power.EnsureWakeOnLan(cli.Context)
if err != nil {
return errors.Wrap(err, "failed to enable wol")
}

if !enabled {
// if the zos nics don't support wol we can automatically
// disable the feature
log.Info().Msg("no wol support found by zos nic")
}

consumer, err := events.NewConsumer(msgBrokerCon, module)
if err != nil {
return errors.Wrap(err, "failed to to create event consumer")
}

// start power manager
power, err := power.NewPowerServer(cl, sub, consumer, enabled, env.FarmID, nodeID, twinID, id, uptime)
if err != nil {
return errors.Wrap(err, "failed to initialize power manager")
}

return power.Start(ctx)
}
2 changes: 1 addition & 1 deletion cmds/modules/provisiond/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func action(cli *cli.Context) error {
return errors.Wrap(err, "failed to create substrate users database")
}

admins, err := provision.NewSubstrateAdmins(mgr, uint32(env.FarmerID))
admins, err := provision.NewSubstrateAdmins(mgr, uint32(env.FarmID))
if err != nil {
return errors.Wrap(err, "failed to create substrate admins database")
}
Expand Down
2 changes: 2 additions & 0 deletions cmds/zos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/threefoldtech/zos/cmds/modules/gateway"
"github.com/threefoldtech/zos/cmds/modules/networkd"
"github.com/threefoldtech/zos/cmds/modules/noded"
"github.com/threefoldtech/zos/cmds/modules/powerd"
"github.com/threefoldtech/zos/cmds/modules/provisiond"
"github.com/threefoldtech/zos/cmds/modules/qsfsd"
"github.com/threefoldtech/zos/cmds/modules/storaged"
Expand Down Expand Up @@ -54,6 +55,7 @@ func main() {
&zbusdebug.Module,
&gateway.Module,
&qsfsd.Module,
&powerd.Module,
},
Before: func(c *cli.Context) error {
if c.Bool("debug") {
Expand Down
4 changes: 4 additions & 0 deletions etc/zinit/powerd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exec: powerd --broker unix://var/run/redis.sock
after:
- boot
- noded
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,6 @@ github.com/threefoldtech/0-fs v1.3.1-0.20201203163303-d963de9adea7 h1:64QIPSO1Ac
github.com/threefoldtech/0-fs v1.3.1-0.20201203163303-d963de9adea7/go.mod h1:OPPZiE/GthPR2IepjKSc8wa+t/7wl3dtHQyEdUcftZI=
github.com/threefoldtech/go-substrate-rpc-client/v4 v4.0.6-0.20230102154731-7c633b7d3c71 h1:vvoXPRI10quMOEXHKcakKhBZe+TlLXhNsh1wwX4cyk4=
github.com/threefoldtech/go-substrate-rpc-client/v4 v4.0.6-0.20230102154731-7c633b7d3c71/go.mod h1:5g1oM4Zu3BOaLpsKQ+O8PAv2kNuq+kPcA1VzFbsSqxE=
github.com/threefoldtech/rmb-sdk-go v0.0.0-20230120120324-7c96fec72b33 h1:uSpwP4Du/yKCgpeaw9/wJKH7QmwC0O54QOD8tsTKgwc=
github.com/threefoldtech/rmb-sdk-go v0.0.0-20230120120324-7c96fec72b33/go.mod h1:PgqoUXMfkJEY6TD6Yoy0Gx+w/7hdGoV7Hel21wvNQk8=
github.com/threefoldtech/rmb-sdk-go v0.0.0-20230208121945-1920816a9268 h1:HL0WRgjKn18az5pDDD9XEFkeMTBB1lWJGBF+5Cb8LLg=
github.com/threefoldtech/rmb-sdk-go v0.0.0-20230208121945-1920816a9268/go.mod h1:ddt04wNV0lkrfijSa1x2nbEoyBO41WO0B0BFLIeP6Mg=
github.com/threefoldtech/substrate-client v0.0.0-20230203145052-45be626d311b h1:zeUyutA3Siibb0P9vx3I6P6UIu1WzzOaR39QJm4SLRY=
Expand Down
12 changes: 6 additions & 6 deletions pkg/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type Environment struct {
FlistURL string
BinRepo string

FarmerID pkg.FarmID
Orphan bool
FarmID pkg.FarmID
Orphan bool

FarmSecret string
SubstrateURL []string
Expand Down Expand Up @@ -219,11 +219,11 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {

switch env.RunningMode {
case RunningDev:
env.FarmerID = OrphanageDev
env.FarmID = OrphanageDev
case RunningTest:
env.FarmerID = OrphanageTest
env.FarmID = OrphanageTest
case RunningMain:
env.FarmerID = OrphanageMain
env.FarmID = OrphanageMain
}

} else {
Expand All @@ -232,7 +232,7 @@ func getEnvironmentFromParams(params kernel.Params) (Environment, error) {
if err != nil {
return env, errors.Wrap(err, "wrong format for farm ID")
}
env.FarmerID = pkg.FarmID(id)
env.FarmID = pkg.FarmID(id)
}

// Checking if there environment variable
Expand Down
6 changes: 6 additions & 0 deletions pkg/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ type ContractLockedEvent struct {
TwinId uint32
Lock bool
}

type PowerTargetChangeEvent struct {
FarmID FarmID
NodeID uint32
Target substrate.Power
}
Loading