我正在学习ZeroMQ并从头开始制作指南中的所有示例(在NodeJS中)。但我正在堆叠一个创建处理作业的本地集群,如果它不能再处理,则将作业发送到通过其他套接字连接的“云对等体”(“Putting it All Together” example)。
我请求您帮助调试代码,以查看为什么客户的消息未被云端对等处理(并返回):
如果您clone and execute my repo(cd
到第3章,然后是两个终端,例如node peering3.js me you
和node peering3.js you me
,您可以关注谁发送并接收(Get
)数据。
您可以使用NBR_CLIENTS
和NBR_WORKERS
(第12行和第13行)进行游戏,看到作业未正确发送/返回...
如果您可以查看我的代码,我将非常感激!
提前致谢...
答案 0 :(得分:0)
您在云代理之间进行寻址时遇到问题。您正在使用本地workers
ID在云代理之间进行寻址。代码评论以突出问题。
// - Route any request locally if we can, else to cloud.
localfe.on('message', function() {
var args = Array.apply(null, arguments);
console.log('LocalFE: Get ' + args.toString());
if (localCapacity > 0) {
console.log('LocalFE: Send '+ workers.shift() + ',\'\',' + args[0]+ ',\'\','+ args[2] + ' to LocalBE');
localCapacity--;
statebe.send(localCapacity);
localbe.send([workers.shift(), '', args[0], '', args[2]]);
} else {
// Route to random broker peer automatically
var randomPeer = randomBetween(3, argc);
var peer = process.argv[randomPeer];
/////////////////
// why are you referencing `workers` here, that is only for local workers
// You correctly route to `peer` here, though, so that should be fine
// however, you've removed a local worker from the queue erroneously
/////////////////
console.log('LocalFE: Send '+ workers.shift() + ',\'\',' + args[0]+ ',\'\','+ args[2] + ' to CloudBE at peer ' + peer);
cloudbe.send([peer, '', args[0], '', args[2]]);
}
});
... cloudfe
然后正确地将消息路由到本地工作者,但它在workers
队列之前没有检查可用的工作人员,所以如果所有工作人员都这样做工作人员被带走,然后你被塞了,本地workers
队列将是空的,并且消息不会去任何地方。您也失去了对云端对等方的引用,因此当消息回来时,无法知道它需要返回到云端对等方:
cloudfe.on('message', function() {
var args = Array.apply(null, arguments);
console.log('CloudFE: Get ' + args.toString());
/////////////////
// if `workers` is already empty, `shift()`ing it will get you `undefined`
// also, you're removing the ID from the queue, which causes problems below
/////////////////
console.log('CloudFE: Send '+ workers.shift() + ',\'\',' + args[2]+ ',\'\','+ args[4] + ' to LocalBE');
localCapacity--;
statebe.send(localCapacity);
/////////////////
// you're now sending it to a *different* worker than you logged above
// and you've removed *two* workers from the queue instead of one
// as said above, if `workers` is already empty, you'll route it nowhere
// and lose the message
// further, nowhere in here are you logging the identity of the cloud peer, so you've
// lost the ability to route it back to the cloud peer that has the client
/////////////////
localbe.send([workers.shift(), '', args[2], '', args[4]]);
});
......工作人员应该处理消息并将其成功发送回本地经纪人,至少对于前两个消息(不是5个消息,因为我们只将它发送给工作人员2和4)。但是,我们不仅失去了之前向我们发送消息的云代理商的参考,我们甚至不会在我们从工作人员收到消息时尝试将其发回:
// Reply from local worker.
localbe.on('message', function() {
var args = Array.apply(null, arguments);
//console.log('LocalBE: Get ' + args.toString());
workers.push(args[0]); // Add its identity to the array.
// We broadcast new capacity messages to other peers.
localCapacity++;
statebe.send(localCapacity);
// If it's not READY message, route the reply to client.
if (args[2].toString() != WORKER_READY) {
console.log('LocalBE: Send ' + args[2].toString() + ',\'\', ' + args[4].toString());
/////////////////
// you're attempting to send it directly back to the client, but the client
// you're addressing is not `connect()`ed to this broker, it's connected to
// the cloud broker, so it goes nowhere
/////////////////
localfe.send([args[2], '', args[4]]);
}
});
所以:
shift()
发送给云代理商时,不要localfe
您的工作人员排队 - 您甚至可能在失去工作机会之前失去所有本地工作人员< / LI>
cloudfe
上收到消息的开始,shift()
将的工作人员ID 一次转换为本地变量并在任何需要的地方使用该局部变量setTimeout()
再试一次,或将其发送给新的云端。为了简单起见,我建议使用前者,否则您必须在消息中跟踪整列云端对等ID。