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

feat: allow caching of static droplet ips #120

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 6 additions & 3 deletions core/provider/digitalocean/droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (p *Provider) CreateDroplet(ctx context.Context, definition provider.TaskDe

start := time.Now()

err = util.WaitForCondition(ctx, time.Second*100, time.Millisecond*100, func() (bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it really take 5 minutes to get a droplet up and responding?

err = util.WaitForCondition(ctx, time.Second*300, time.Millisecond*300, func() (bool, error) {
d, _, err := p.doClient.Droplets.Get(ctx, droplet.ID)

if err != nil {
Expand Down Expand Up @@ -119,13 +119,16 @@ func (p *Provider) deleteDroplet(ctx context.Context, name string) error {
return nil
}

func (p *Provider) getDroplet(ctx context.Context, name string) (*godo.Droplet, error) {
func (p *Provider) getDroplet(ctx context.Context, name string, returnOnCacheHit bool) (*godo.Droplet, error) {
cachedDroplet, ok := p.droplets.Load(name)

if !ok {
return nil, fmt.Errorf("could not find droplet %s", name)
}

if ok && returnOnCacheHit {
return cachedDroplet, nil
}

droplet, res, err := p.doClient.Droplets.Get(ctx, cachedDroplet.ID)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/provider/digitalocean/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (p *Provider) DestroyTask(ctx context.Context, taskName string) error {
}

func (p *Provider) GetTaskStatus(ctx context.Context, taskName string) (provider.TaskStatus, error) {
droplet, err := p.getDroplet(ctx, taskName)
droplet, err := p.getDroplet(ctx, taskName, false)

if err != nil {
return provider.TASK_STATUS_UNDEFINED, err
Expand Down Expand Up @@ -296,7 +296,7 @@ func (p *Provider) DownloadDir(ctx context.Context, s string, s2 string, s3 stri
}

func (p *Provider) GetIP(ctx context.Context, taskName string) (string, error) {
droplet, err := p.getDroplet(ctx, taskName)
droplet, err := p.getDroplet(ctx, taskName, true)

if err != nil {
return "", err
Expand Down
28 changes: 25 additions & 3 deletions cosmos/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"bytes"
"context"
"fmt"
toml "github.com/pelletier/go-toml/v2"
petritypes "github.com/skip-mev/petri/core/v2/types"
"math/rand"
"reflect"
"strings"
"time"

toml "github.com/pelletier/go-toml/v2"
petritypes "github.com/skip-mev/petri/core/v2/types"
)

type Toml map[string]any
Expand Down Expand Up @@ -164,8 +167,12 @@ func (n *Node) SetDefaultConfigs(ctx context.Context) error {
func (n *Node) SetPersistentPeers(ctx context.Context, peers string) error {
cometBftConfig := make(Toml)

allPeers := strings.Split(peers, ",")

p2pConfig := make(Toml)
p2pConfig["persistent_peers"] = peers

// return the filtered peers
p2pConfig["persistent_peers"] = filterPeers(allPeers)

cometBftConfig["p2p"] = p2pConfig

Expand All @@ -175,3 +182,18 @@ func (n *Node) SetPersistentPeers(ctx context.Context, peers string) error {
cometBftConfig,
)
}

// filter peers returns a random subset of the given peers. The subset is determined as follows:
// 1. If the number of peers is less than or equal to 20, return all peers.
// 2. If the number of peers is greater than 20, return a random subset of 20 peers.
func filterPeers(allPeers []string) string {
if len(allPeers) <= 20 {
return strings.Join(allPeers, ",")
}

rand.Shuffle(len(allPeers), func(i, j int) {
allPeers[i], allPeers[j] = allPeers[j], allPeers[i]
})

return strings.Join(allPeers[:20], ",")
}
Loading