Skip to content

Commit 2e1bf83

Browse files
committed
Fixes, updates and refactor.
1 parent 8706546 commit 2e1bf83

File tree

88 files changed

+2274
-323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+2274
-323
lines changed

Gopkg.lock

Lines changed: 12 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
[[constraint]]
4141
name = "github.com/strabox/caravela"
42-
version = "0.2.1"
42+
branch = "master"
4343

4444
[prune]
4545
go-tests = true

configuration.toml

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
[Caravela]
2-
RefreshingInterval = "30s"
3-
OffersStrategy = "chord-smart"
42
Simulation = true
53
# Cpu Cores Partitions
64
[[Caravela.CpuCoresPartitions]]
75
Cores = 1
8-
Percentage = 10
6+
Percentage = 50
97
[[Caravela.CpuCoresPartitions]]
108
Cores = 2
11-
Percentage = 40
12-
[[Caravela.CpuCoresPartitions]]
13-
Cores = 4
149
Percentage = 30
1510
[[Caravela.CpuCoresPartitions]]
16-
Cores = 8
11+
Cores = 4
1712
Percentage = 20
1813
# Ram Partitions
1914
[[Caravela.RamPartitions]]
20-
Ram = 128
21-
Percentage = 25
22-
[[Caravela.RamPartitions]]
23-
Ram = 256
24-
Percentage = 25
25-
[[Caravela.RamPartitions]]
2615
Ram = 512
27-
Percentage = 25
16+
Percentage = 50
2817
[[Caravela.RamPartitions]]
2918
Ram = 1024
30-
Percentage = 25
19+
Percentage = 30
20+
[[Caravela.RamPartitions]]
21+
Ram = 2048
22+
Percentage = 20
23+
[Caravela.DiscoveryBackend]
24+
Backend = "chord-smart" # chord-random, chord-smart
25+
[Caravela.DiscoveryBackend.SmartChordBackend]
26+
SupplyingInterval = "30s"
27+
SpreadOffersInterval = "30s"
28+
RefreshingInterval = "30s"
29+
[Caravela.DiscoveryBackend.RandomChordBackend]
30+
RandBackendMaxRetries = 4
3131

3232
[Overlay]
33-
ChordNumSuccessors = 3
34-
ChordHashSizeBits = 128
33+
[Overlay.Chord]
34+
NumSuccessors = 3
35+
HashSizeBits = 128

configuration/configuration.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/BurntSushi/toml"
66
"github.com/strabox/caravela-sim/util"
7+
"github.com/strabox/caravela/api/types"
78
"time"
89
)
910

@@ -18,26 +19,37 @@ const DefaultOutDirectoryPath = "out"
1819

1920
// Configuration structure with initialization parameters for the simulator.
2021
type Configuration struct {
21-
NumberOfNodes int // Number of nodes used in the simulation.
22-
TickInterval duration // Interval between each simulator tick (in simulation time).
23-
MaxTicks int // Maximum number of ticks done by the simulator.
24-
Multithread bool // Used to leverage the multiple cores to speed up the simulation.
25-
RequestFeeder string // Used to feed the simulator with a series of requests.
26-
ResourceGenerator string // Strategy used to generate the resources for each node.
27-
OutDirectoryPath string // Path of the output's directory.
28-
SimulatorLogLevel string // Log's level of the simulator.
29-
CaravelaLogLevel string // Log's level of the CARAVELA's system.
22+
NumberOfNodes int // Number of nodes used in the simulation.
23+
TickInterval duration // Interval between each simulator tick (in simulation time).
24+
MaxTicks int // Maximum number of ticks done by the simulator.
25+
Multithread bool // Used to leverage the multiple cores to speed up the simulation.
26+
RequestFeeder string // Used to feed the simulator with a series of requests.
27+
ResourcesGenerator resourcesGenerator // Strategies used to generate the resources for each node.
28+
OutDirectoryPath string // Path of the output's directory.
29+
SimulatorLogLevel string // Log's level of the simulator.
30+
CaravelaLogLevel string // Log's level of the CARAVELA's system.
31+
}
32+
33+
type resourcesGenerator struct {
34+
ResourceGenerator string
35+
StaticResources types.Resources
3036
}
3137

3238
// Default creates the configuration structure for a basic/default simulation.
3339
func Default() *Configuration {
3440
return &Configuration{
35-
NumberOfNodes: 2500,
36-
TickInterval: duration{Duration: 10 * time.Second},
37-
MaxTicks: 15,
38-
Multithread: true,
39-
RequestFeeder: "random",
40-
ResourceGenerator: "partition-based",
41+
NumberOfNodes: 2500,
42+
TickInterval: duration{Duration: 10 * time.Second},
43+
MaxTicks: 15,
44+
Multithread: true,
45+
RequestFeeder: "random",
46+
ResourcesGenerator: resourcesGenerator{
47+
ResourceGenerator: "partition-aware",
48+
StaticResources: types.Resources{
49+
CPUs: 4,
50+
RAM: 4096,
51+
},
52+
},
4153
OutDirectoryPath: DefaultOutDirectoryPath,
4254
SimulatorLogLevel: DefaultSimLogLevel,
4355
CaravelaLogLevel: "info",
@@ -112,7 +124,11 @@ func (config *Configuration) Feeder() string {
112124
}
113125

114126
func (config *Configuration) ResourceGen() string {
115-
return config.ResourceGenerator
127+
return config.ResourcesGenerator.ResourceGenerator
128+
}
129+
130+
func (config *Configuration) StaticGeneratorResources() types.Resources {
131+
return config.ResourcesGenerator.StaticResources
116132
}
117133

118134
func (config *Configuration) OutputDirectoryPath() string {
@@ -139,6 +155,7 @@ func (config *Configuration) Print() {
139155
util.Log.Infof("Multithread: %t", config.Multithreaded())
140156
util.Log.Infof("Request Feeder: %s", config.Feeder())
141157
util.Log.Infof("Resource Generator: %s", config.ResourceGen())
158+
util.Log.Infof("Static Gen Resources: <%d;%d>", config.StaticGeneratorResources().CPUs, config.StaticGeneratorResources().RAM)
142159
util.Log.Infof("Output directory: %s", config.OutputDirectoryPath())
143160
util.Log.Infof("Sim's log level: %s", config.SimulatorLogsLevel())
144161
util.Log.Infof("CARAVELA's log level: %s", config.CaravelaLogsLevel())

mocks/caravela/api_remote_client_mock.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package caravela
33
import (
44
"context"
55
"github.com/strabox/caravela-sim/simulation/metrics"
6-
"github.com/strabox/caravela-sim/util"
76
"github.com/strabox/caravela/api/types"
87
"github.com/strabox/caravela/configuration"
98
)
@@ -30,7 +29,7 @@ func NewRemoteClientMock(nodeService NodeService, metricsCollector *metrics.Coll
3029
func (mock *RemoteClientMock) CreateOffer(ctx context.Context, fromSupp *types.Node, toTrader *types.Node,
3130
offer *types.Offer) error {
3231

33-
node, nodeIndex := mock.nodeService.NodeByGUID(toTrader.GUID)
32+
node, nodeIndex := mock.nodeService.NodeByIP(toTrader.IP)
3433

3534
// Collect Metrics
3635
mock.collector.APIRequestReceived(nodeIndex)
@@ -49,7 +48,7 @@ func (mock *RemoteClientMock) RefreshOffer(ctx context.Context, fromTrader, toSu
4948
}
5049

5150
func (mock *RemoteClientMock) RemoveOffer(ctx context.Context, fromSupp, toTrader *types.Node, offer *types.Offer) error {
52-
node, nodeIndex := mock.nodeService.NodeByGUID(toTrader.GUID)
51+
node, nodeIndex := mock.nodeService.NodeByIP(toTrader.IP)
5352

5453
// Collect Metrics
5554
mock.collector.APIRequestReceived(nodeIndex)
@@ -59,18 +58,18 @@ func (mock *RemoteClientMock) RemoveOffer(ctx context.Context, fromSupp, toTrade
5958
}
6059

6160
func (mock *RemoteClientMock) GetOffers(ctx context.Context, fromNode, toTrader *types.Node, relay bool) ([]types.AvailableOffer, error) {
62-
node, nodeIndex := mock.nodeService.NodeByGUID(toTrader.GUID)
61+
node, nodeIndex := mock.nodeService.NodeByIP(toTrader.IP)
6362

6463
// Collect Metrics
65-
mock.collector.IncrMessagesTradedRequest(ctx.Value(types.RequestCtxKey(util.SimRequestIDKey)).(string), 1)
64+
mock.collector.IncrMessagesTradedRequest(types.RequestID(ctx), 1)
6665
mock.collector.APIRequestReceived(nodeIndex)
6766

6867
offers := node.GetOffers(ctx, fromNode, toTrader, relay)
6968
return offers, nil
7069
}
7170

7271
func (mock *RemoteClientMock) AdvertiseOffersNeighbor(ctx context.Context, fromTrader, toNeighborTrader, traderOffering *types.Node) error {
73-
node, nodeIndex := mock.nodeService.NodeByGUID(toNeighborTrader.GUID)
72+
node, nodeIndex := mock.nodeService.NodeByIP(toNeighborTrader.IP)
7473

7574
// Collect Metrics
7675
mock.collector.APIRequestReceived(nodeIndex)
@@ -84,7 +83,7 @@ func (mock *RemoteClientMock) LaunchContainer(ctx context.Context, fromBuyer, to
8483
node, nodeIndex := mock.nodeService.NodeByIP(toSupplier.IP)
8584

8685
// Collect Metrics
87-
mock.collector.IncrMessagesTradedRequest(ctx.Value(types.RequestCtxKey(util.SimRequestIDKey)).(string), 1)
86+
mock.collector.IncrMessagesTradedRequest(types.RequestID(ctx), 1)
8887
mock.collector.APIRequestReceived(nodeIndex)
8988

9089
return node.LaunchContainers(ctx, fromBuyer, offer, containersConfigs)

mocks/docker/client_mock.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const containerIDSize = 64
1414
// ClientMock mocks the interactions with the docker daemon.
1515
// It implements the github.com/strabox/caravela/node/external DockerClient interface.
1616
type ClientMock struct {
17+
maxCPUS int
18+
maxRAM int
1719
numOfContainers int64
1820
containersRunning sync.Map
1921
resourcesGenerator ResourcesGenerator
@@ -22,6 +24,8 @@ type ClientMock struct {
2224
// NewClientMock creates a new docker client mock to be used.
2325
func NewClientMock(resourcesGenerator ResourcesGenerator) *ClientMock {
2426
return &ClientMock{
27+
maxCPUS: 0,
28+
maxRAM: 0,
2529
numOfContainers: 0,
2630
containersRunning: sync.Map{},
2731
resourcesGenerator: resourcesGenerator,
@@ -32,12 +36,19 @@ func (cliMock *ClientMock) ContainersRunning() int64 {
3236
return cliMock.numOfContainers
3337
}
3438

39+
func (cliMock *ClientMock) MaxResourcesAvailable() (int, int) {
40+
return cliMock.maxCPUS, cliMock.maxRAM
41+
}
42+
3543
// ===============================================================================
3644
// = DockerClient Interface =
3745
// ===============================================================================
3846

3947
func (cliMock *ClientMock) GetDockerCPUAndRAM() (int, int) {
40-
return cliMock.resourcesGenerator.Generate()
48+
cpus, ram := cliMock.resourcesGenerator.Generate()
49+
cliMock.maxCPUS += cpus
50+
cliMock.maxRAM += ram
51+
return cpus, ram
4152
}
4253

4354
func (cliMock *ClientMock) CheckContainerStatus(containerID string) (myContainer.ContainerStatus, error) {
@@ -50,7 +61,6 @@ func (cliMock *ClientMock) CheckContainerStatus(containerID string) (myContainer
5061
}
5162

5263
func (cliMock *ClientMock) RunContainer(contConfig types.ContainerConfig) (*types.ContainerStatus, error) {
53-
5464
// Generate a random ID for the container and store it in an HashMap
5565
randomContainerID := util.RandomString(containerIDSize)
5666
cliMock.containersRunning.Store(randomContainerID, nil)
@@ -66,7 +76,7 @@ func (cliMock *ClientMock) RunContainer(contConfig types.ContainerConfig) (*type
6676
func (cliMock *ClientMock) RemoveContainer(containerID string) error {
6777
if _, exist := cliMock.containersRunning.Load(containerID); exist {
6878
cliMock.containersRunning.Delete(containerID)
69-
cliMock.numOfContainers--
79+
atomic.AddInt64(&cliMock.numOfContainers, -1)
7080
}
7181
return nil
7282
}
Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package docker
22

3-
import "github.com/strabox/caravela/configuration"
3+
import (
4+
"github.com/strabox/caravela-sim/util"
5+
"github.com/strabox/caravela/configuration"
6+
)
47

58
type partitionAwareResourceGen struct {
69
caravelaConfigs *configuration.Configuration
@@ -13,12 +16,43 @@ func newPartitionAwareResourceGen(caravelaConfigs *configuration.Configuration)
1316
}
1417

1518
func (p *partitionAwareResourceGen) Generate() (int, int) {
16-
/*
17-
coresPartition := p.caravelaConfigs.CPUCoresPartitions()
18-
for _, partition := range coresPartition {
19-
partition.
19+
coresPartitions := make([]configuration.CPUCoresPartition, len(p.caravelaConfigs.CPUCoresPartitions()))
20+
ramPartitions := make([]configuration.RAMPartition, len(p.caravelaConfigs.RAMPartitions()))
21+
copy(coresPartitions, p.caravelaConfigs.CPUCoresPartitions())
22+
copy(ramPartitions, p.caravelaConfigs.RAMPartitions())
23+
24+
acc := 0
25+
for i := range coresPartitions {
26+
currentPercentage := coresPartitions[i].Percentage
27+
coresPartitions[i].Percentage += acc
28+
acc += currentPercentage
29+
}
30+
31+
acc = 0
32+
for i := range ramPartitions {
33+
currentPercentage := ramPartitions[i].Percentage
34+
ramPartitions[i].Percentage += acc
35+
acc += currentPercentage
36+
}
37+
38+
randInt := util.RandomInteger(1, 100)
39+
prevCores := coresPartitions[0].Cores
40+
for i := range coresPartitions {
41+
if randInt <= coresPartitions[i].Percentage {
42+
prevCores = coresPartitions[i].Cores
43+
break
2044
}
21-
// TODO
22-
*/
23-
return 1, 256
45+
}
46+
47+
randInt = util.RandomInteger(1, 100)
48+
prevRAM := ramPartitions[0].RAM
49+
for i := range ramPartitions {
50+
if randInt <= ramPartitions[i].Percentage {
51+
prevRAM = ramPartitions[i].RAM
52+
break
53+
}
54+
}
55+
56+
util.Log.Debugf(util.LogTag("ResGen")+"MaxRes: <%d;%d>", prevCores, prevRAM)
57+
return prevCores, prevRAM
2458
}

0 commit comments

Comments
 (0)