2 Distributed Cloud Emulator (dcemulator)
3 (c) 2015 by Manuel Peuster <manuel.peuster@upb.de>
5 from mininet
.node
import Docker
6 from mininet
.link
import Link
9 LOG
= logging
.getLogger("dcemulator")
10 LOG
.setLevel(logging
.DEBUG
)
13 DCDPID_BASE
= 1000 # start of switch dpid's used for data center switches
16 class EmulatorCompute(Docker
):
18 Emulator specific compute node class.
19 Inherits from Dockernet's Docker host class.
20 Represents a single container connected to a (logical)
22 We can add emulator specific helper functions to it.
26 self
, name
, dimage
, **kwargs
):
27 self
.datacenter
= kwargs
.get("datacenter") # pointer to current DC
28 self
.flavor_name
= kwargs
.get("flavor_name")
29 LOG
.debug("Starting compute instance %r in data center %r" % (name
, str(self
.datacenter
)))
30 # call original Docker.__init__
31 Docker
.__init
__(self
, name
, dimage
, **kwargs
)
33 def getNetworkStatus(self
):
35 Helper method to receive information about the virtual networks
36 this compute instance is connected to.
38 # format list of tuples (name, Ip, MAC, isUp, status)
39 return [(str(i
), i
.IP(), i
.MAC(), i
.isUp(), i
.status())
40 for i
in self
.intfList()]
44 Helper method to receive information about this compute instance.
47 status
["name"] = self
.name
48 status
["network"] = self
.getNetworkStatus()
49 status
["image"] = self
.dimage
50 status
["cpu_quota"] = self
.cpu_quota
51 status
["cpu_period"] = self
.cpu_period
52 status
["cpu_shares"] = self
.cpu_shares
53 status
["cpuset"] = self
.cpuset
54 status
["mem_limit"] = self
.mem_limit
55 status
["memswap_limit"] = self
.memswap_limit
56 status
["state"] = self
.dcli
.inspect_container(self
.dc
)["State"]
57 status
["id"] = self
.dcli
.inspect_container(self
.dc
)["Id"]
58 status
["datacenter"] = (None if self
.datacenter
is None
59 else self
.datacenter
.label
)
63 class Datacenter(object):
65 Represents a logical data center to which compute resources
66 (Docker containers) can be added at runtime.
68 Will also implement resource bookkeeping in later versions.
73 def __init__(self
, label
, metadata
={}):
74 self
.net
= None # DCNetwork to which we belong
75 # each node (DC) has a short internal name used by Mininet
76 # this is caused by Mininets naming limitations for swtiches etc.
77 self
.name
= "dc%d" % Datacenter
.DC_COUNTER
78 Datacenter
.DC_COUNTER
+= 1
79 # use this for user defined names that can be longer than self.name
81 # dict to store arbitrary metadata (e.g. latitude and longitude)
82 self
.metadata
= metadata
83 # first prototype assumes one "bigswitch" per DC
85 # keep track of running containers
87 # pointer to assigned resource model
88 self
._resource
_model
= None
93 def _get_next_dc_dpid(self
):
100 Each data center is represented by a single switch to which
101 compute resources can be connected at run time.
103 TODO: This will be changed in the future to support multiple networks
106 self
.switch
= self
.net
.addSwitch(
107 "%s.s1" % self
.name
, dpid
=hex(self
._get
_next
_dc
_dpid
())[2:])
108 LOG
.debug("created data center switch: %s" % str(self
.switch
))
113 def startCompute(self
, name
, image
=None, command
=None, network
=None, flavor_name
="tiny"):
115 Create a new container as compute resource and connect it to this
117 :param name: name (string)
118 :param image: image name (string)
119 :param command: command (string)
120 :param network: networks list({"ip": "10.0.0.254/8"}, {"ip": "11.0.0.254/24"})
121 :param flavor_name: name of the flavor for this compute container
124 assert name
is not None
126 if name
in [c
.name
for c
in self
.net
.getAllContainers()]:
127 raise Exception("Container with name %s already exists." % name
)
128 # set default parameter
132 network
= {} # {"ip": "10.0.0.254/8"}
133 if isinstance(network
, dict):
134 network
= [network
] # if we have only one network, put it in a list
135 if isinstance(network
, list):
139 # allocate in resource resource model and compute resource limits for new container
140 cpu_limit
= mem_limit
= disk_limit
= -1
141 cpu_period
= cpu_quota
= None
142 if self
._resource
_model
is not None:
143 # call allocate in resource model to calculate resource limit for this container
144 (cpu_limit
, mem_limit
, disk_limit
) = alloc
= self
._resource
_model
.allocate(name
, flavor_name
)
145 LOG
.debug("Allocation result: %r" % str(alloc
))
146 # check if we have a cpu_limit given by the used resource model
148 # calculate cpu period and quota for CFS
149 # (see: https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt)
150 # TODO consider multi core machines etc! non trivial!
151 # Attention minimum cpu_quota is 1ms (micro)
152 cpu_period
= 100000 # lets consider a fixed period of 100000 microseconds for now
153 cpu_quota
= cpu_period
* cpu_limit
# calculate the fraction of cpu time for this container
155 "CPU limit: cpu_quota = cpu_period * cpu_limit = %f * %f = %f" % (cpu_period
, cpu_limit
, cpu_quota
))
156 # ATTENTION >= 1000 to avoid a invalid argument system error ... no idea why
159 LOG
.warning("Increased CPU quota for %r to avoid system error." % name
)
160 # check if we have a mem_limit given by the used resource model
163 "MEM limit: mem_limit = %f MB" % mem_limit
)
164 # ATTENTION minimum mem_limit per container is 4MB
167 LOG
.warning("Increased MEM limit for %r because it was less than 4.0 MB." % name
)
168 # create the container
169 d
= self
.net
.addDocker(
174 flavor_name
=flavor_name
,
175 cpu_period
=int(cpu_period
) if cpu_limit
> 0 else None, # set cpu limits if needed
176 cpu_quota
=int(cpu_quota
) if cpu_limit
> 0 else None,
177 mem_limit
="%dm" % int(mem_limit
) if mem_limit
> 0 else None, # set mem limits if needed
178 memswap_limit
="%dm" % int(mem_limit
) if mem_limit
> 0 else None # lets set swap to mem limit for now
180 # connect all given networks
182 # TODO we cannot use TCLink here (see: https://github.com/mpeuster/dockernet/issues/3)
183 self
.net
.addLink(d
, self
.switch
, params1
=nw
, cls
=Link
)
185 self
.containers
[name
] = d
186 return d
# we might use UUIDs for naming later on
188 def stopCompute(self
, name
):
190 Stop and remove a container from this data center.
192 assert name
is not None
193 if name
not in self
.containers
:
194 raise Exception("Container with name %s not found." % name
)
196 link
=None, node1
=self
.containers
[name
], node2
=self
.switch
)
197 self
.net
.removeDocker("%s" % (name
))
198 del self
.containers
[name
]
199 # call resource model and free resources
200 if self
._resource
_model
is not None:
201 self
._resource
_model
.free(name
)
204 def listCompute(self
):
206 Return a list of all running containers assigned to this
209 return list(self
.containers
.itervalues())
213 Return a dict with status information about this DC.
217 "internalname": self
.name
,
218 "switch": self
.switch
.name
,
219 "n_running_containers": len(self
.containers
),
220 "metadata": self
.metadata
223 def assignResourceModel(self
, rm
):
225 Assign a resource model to this DC.
226 :param rm: a BaseResourceModel object
229 if self
._resource
_model
is not None:
230 raise Exception("There is already an resource model assigned to this DC.")
231 self
._resource
_model
= rm
232 self
.net
.rm_registrar
.register(self
, rm
)
233 LOG
.info("Assigned RM: %r to DC: %r" % (rm
, self
))