Class: Quark::MdkDiscovery::Cluster
- Inherits:
-
DatawireQuarkCore::QuarkObject
- Object
- DatawireQuarkCore::QuarkObject
- Quark::MdkDiscovery::Cluster
- Extended by:
- DatawireQuarkCore::Static
- Defined in:
- lib/mdk_discovery.rb
Overview
a single service. Each service provider is represented by a Node.
Constant Summary
Constants included from DatawireQuarkCore::Static
DatawireQuarkCore::Static::Unassigned
Instance Attribute Summary collapse
-
#_counter ⇒ Object
Returns the value of attribute _counter.
-
#_failurepolicies ⇒ Object
Returns the value of attribute _failurepolicies.
-
#_fpfactory ⇒ Object
Returns the value of attribute _fpfactory.
-
#_registeredVersions ⇒ Object
Returns the value of attribute _registeredVersions.
-
#_waiting ⇒ Object
Returns the value of attribute _waiting.
-
#nodes ⇒ Object
Returns the value of attribute nodes.
Instance Method Summary collapse
- #__init_fields__ ⇒ Object
- #_addRequest(version, factory) ⇒ Object
-
#_copyNode(node) ⇒ Object
Create a Node for external use.
- #_getClass ⇒ Object
- #_getField(name) ⇒ Object
- #_setField(name, value) ⇒ Object
-
#add(node) ⇒ Object
Add a Node to the cluster (or, if it’s already present in the cluster, update its properties).
-
#choose ⇒ Object
Choose a single Node to talk to.
-
#chooseVersion(version) ⇒ Object
Choose a compatible version of a service to talk to.
-
#failurePolicy(node) ⇒ Object
Get the FailurePolicy for a Node.
-
#initialize(fpfactory) ⇒ Cluster
constructor
A new instance of Cluster.
-
#isEmpty ⇒ Object
Returns true if and only if this Cluster contains no Nodes.
-
#matchingVersionRegistered(version) ⇒ Object
Return whether node with semantically matching version was registered at some point.
-
#remove(node) ⇒ Object
Remove a Node from the cluster, if it’s present.
-
#toString ⇒ Object
Return a string representation of the Cluster.
Methods included from DatawireQuarkCore::Static
_lazy_statics, static, unlazy_statics
Methods inherited from DatawireQuarkCore::QuarkObject
Constructor Details
#initialize(fpfactory) ⇒ Cluster
Returns a new instance of Cluster.
1144 1145 1146 1147 1148 1149 1150 |
# File 'lib/mdk_discovery.rb', line 1144 def initialize(fpfactory) self.__init_fields__ (self)._fpfactory = fpfactory nil end |
Instance Attribute Details
#_counter ⇒ Object
Returns the value of attribute _counter.
1135 1136 1137 |
# File 'lib/mdk_discovery.rb', line 1135 def _counter @_counter end |
#_failurepolicies ⇒ Object
Returns the value of attribute _failurepolicies.
1135 1136 1137 |
# File 'lib/mdk_discovery.rb', line 1135 def _failurepolicies @_failurepolicies end |
#_fpfactory ⇒ Object
Returns the value of attribute _fpfactory.
1135 1136 1137 |
# File 'lib/mdk_discovery.rb', line 1135 def _fpfactory @_fpfactory end |
#_registeredVersions ⇒ Object
Returns the value of attribute _registeredVersions.
1135 1136 1137 |
# File 'lib/mdk_discovery.rb', line 1135 def _registeredVersions @_registeredVersions end |
#_waiting ⇒ Object
Returns the value of attribute _waiting.
1135 1136 1137 |
# File 'lib/mdk_discovery.rb', line 1135 def _waiting @_waiting end |
#nodes ⇒ Object
Returns the value of attribute nodes.
1135 1136 1137 |
# File 'lib/mdk_discovery.rb', line 1135 def nodes @nodes end |
Instance Method Details
#__init_fields__ ⇒ Object
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 |
# File 'lib/mdk_discovery.rb', line 1404 def __init_fields__() self.nodes = ::DatawireQuarkCore::List.new([]) self._waiting = ::DatawireQuarkCore::List.new([]) self._failurepolicies = {} self._counter = 0 self._fpfactory = nil self._registeredVersions = ::DatawireQuarkCore::List.new([]) nil end |
#_addRequest(version, factory) ⇒ Object
1288 1289 1290 1291 1292 1293 |
# File 'lib/mdk_discovery.rb', line 1288 def _addRequest(version, factory) (@_waiting) << (::Quark.mdk_discovery._Request.new(version, factory)) nil end |
#_copyNode(node) ⇒ Object
Create a Node for external use.
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 |
# File 'lib/mdk_discovery.rb', line 1169 def _copyNode(node) result = ::Quark.mdk_discovery.Node.new() (result).id = (node).id (result).address = (node).address (result).version = (node).version (result).service = (node).service (result).properties = (node).properties (result)._policy = self.failurePolicy(node) return result nil end |
#_getClass ⇒ Object
1348 1349 1350 1351 1352 1353 |
# File 'lib/mdk_discovery.rb', line 1348 def _getClass() return "mdk_discovery.Cluster" nil end |
#_getField(name) ⇒ Object
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 |
# File 'lib/mdk_discovery.rb', line 1355 def _getField(name) if ((name) == ("nodes")) return (self).nodes end if ((name) == ("_waiting")) return (self)._waiting end if ((name) == ("_failurepolicies")) return (self)._failurepolicies end if ((name) == ("_counter")) return (self)._counter end if ((name) == ("_fpfactory")) return (self)._fpfactory end if ((name) == ("_registeredVersions")) return (self)._registeredVersions end return nil nil end |
#_setField(name, value) ⇒ Object
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 |
# File 'lib/mdk_discovery.rb', line 1380 def _setField(name, value) if ((name) == ("nodes")) (self).nodes = ::DatawireQuarkCore.cast(value) { ::DatawireQuarkCore::List } end if ((name) == ("_waiting")) (self)._waiting = ::DatawireQuarkCore.cast(value) { ::DatawireQuarkCore::List } end if ((name) == ("_failurepolicies")) (self)._failurepolicies = ::DatawireQuarkCore.cast(value) { ::Hash } end if ((name) == ("_counter")) (self)._counter = ::DatawireQuarkCore.cast(value) { ::Integer } end if ((name) == ("_fpfactory")) (self)._fpfactory = ::DatawireQuarkCore.cast(value) { ::Quark.mdk_discovery.FailurePolicyFactory } end if ((name) == ("_registeredVersions")) (self)._registeredVersions = ::DatawireQuarkCore.cast(value) { ::DatawireQuarkCore::List } end nil end |
#add(node) ⇒ Object
Add a Node to the cluster (or, if it’s already present in the cluster, update its properties). At present, this involves a linear search, so very large Clusters are unlikely to perform well.
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 |
# File 'lib/mdk_discovery.rb', line 1242 def add(node) kdx = 0 foundVersion = false while ((kdx) < ((@_registeredVersions).size)) do if (((@_registeredVersions)[kdx]) == ((node).version)) foundVersion = true break end kdx = (kdx) + (1) end if (!(foundVersion)) (@_registeredVersions) << ((node).version) end if (!((@_failurepolicies).key?((node).address))) (@_failurepolicies)[(node).address] = ((self)._fpfactory.create()) end if ((((self)._waiting).size) > (0)) waiting = (self)._waiting (self)._waiting = ::DatawireQuarkCore::List.new([]) jdx = 0 while ((jdx) < ((waiting).size)) do req = (waiting)[jdx] if (::Quark.mdk_util.versionMatch((req).version, (node).version)) if (!(((req).factory).promise.value().hasValue())) (req).factory.resolve(self._copyNode(node)) end else ((self)._waiting) << (req) end jdx = (jdx) + (1) end end idx = 0 while ((idx) < ((@nodes).size)) do if (((((@nodes)[idx]).address) == ((node).address)) || (((@nodes)[idx].getId()) == ((node).id))) (@nodes)[idx] = (node) return end idx = (idx) + (1) end (@nodes) << (node) nil end |
#choose ⇒ Object
Choose a single Node to talk to. At present this is a simple round robin.
1159 1160 1161 1162 1163 1164 |
# File 'lib/mdk_discovery.rb', line 1159 def choose() return self.chooseVersion(nil) nil end |
#chooseVersion(version) ⇒ Object
Choose a compatible version of a service to talk to.
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 |
# File 'lib/mdk_discovery.rb', line 1196 def chooseVersion(version) if (((@nodes).size) == (0)) return ::DatawireQuarkCore.cast(nil) { ::Quark.mdk_discovery.Node } end start = (@_counter) % ((@nodes).size) @_counter = (@_counter) + (1) count = 0 while ((count) < ((@nodes).size)) do choice = ((start) + (count)) % ((@nodes).size) candidate = (@nodes)[choice] policy = ((self)._failurepolicies)[(candidate).address] if ((::Quark.mdk_util.versionMatch(version, (candidate).version)) && (policy.available())) return self._copyNode(candidate) end count = (count) + (1) end return ::DatawireQuarkCore.cast(nil) { ::Quark.mdk_discovery.Node } nil end |
#failurePolicy(node) ⇒ Object
Get the FailurePolicy for a Node.
1186 1187 1188 1189 1190 1191 |
# File 'lib/mdk_discovery.rb', line 1186 def failurePolicy(node) return ((self)._failurepolicies)[(node).address] nil end |
#isEmpty ⇒ Object
Returns true if and only if this Cluster contains no Nodes.
1318 1319 1320 1321 1322 1323 |
# File 'lib/mdk_discovery.rb', line 1318 def isEmpty() return ((@nodes).size) <= (0) nil end |
#matchingVersionRegistered(version) ⇒ Object
Return whether node with semantically matching version was registered at some point.
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 |
# File 'lib/mdk_discovery.rb', line 1223 def matchingVersionRegistered(version) idx = 0 while ((idx) < ((@_registeredVersions).size)) do if (::Quark.mdk_util.versionMatch(version, (@_registeredVersions)[idx])) return true end idx = (idx) + (1) end return false nil end |
#remove(node) ⇒ Object
Remove a Node from the cluster, if it’s present. If it’s not present, do nothing. Note that it is possible to remove all the Nodes and be left with an empty cluster.
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
# File 'lib/mdk_discovery.rb', line 1300 def remove(node) idx = 0 while ((idx) < ((@nodes).size)) do ep = (@nodes)[idx] if ((ep.getId()) == (node.getId())) (@nodes).delete_at(idx) return end idx = (idx) + (1) end nil end |
#toString ⇒ Object
Return a string representation of the Cluster.
WARNING: every Node is represented in the string. Large Clusters will produce unusably large strings.
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 |
# File 'lib/mdk_discovery.rb', line 1331 def toString() result = "Cluster(" idx = 0 while ((idx) < ((@nodes).size)) do if ((idx) > (0)) result = (result) + (", ") end result = (result) + ((@nodes)[idx].toString()) idx = (idx) + (1) end result = (result) + (")") return result nil end |