Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
zkc.zookeeper( function(zk) {
console.log("creating temp node");
zk.a_create("/chh/temp/node","stuff", ZooKeeper.ZOO_SEQUENCE + ZooKeeper.ZOO_EPHEMERAL ,function (rc, error, path) {
if (rc != 0) {
console.log ("zk node create result: %d, error: '%s', path=%s", rc, error, path);
} else {
// console.log("================== %s", path );
var me = path.match(/[^\/]+$/)[0];
console.log("My name is: %s", me);
var dir = "/chh/temp";
election(zk,dir,me);
}
});
});
var lw = function(type, state, p){
if( type == ZooKeeper.ZOO_CHANGED_EVENT ) {
zk.aw_get( path, lw, function() {} );
} else if( type == ZooKeeper.ZOO_DELETED_EVENT ) {
// Our sentinal disappeared - this could mean that
// we are now the leader, or it could mean that we
// need to watch someone else.
election(zk,dir,me);
} else if( type == ZooKeeper.ZOO_SESSION_EVENT ) {
//console.log("Session problem!");
// zk will keep our callback around - no need to re-establish.
} else {
console.log( "Unkown type: " + type );
//zk.aw_get( path, lw, function() {} );
}
};
zk.aw_get( path, lw, function() {} );
var lw = function(type, state, p){
if( type == ZooKeeper.ZOO_CHANGED_EVENT ) {
zk.aw_get( path, lw, function() {} );
} else if( type == ZooKeeper.ZOO_DELETED_EVENT ) {
// Our sentinal disappeared - this could mean that
// we are now the leader, or it could mean that we
// need to watch someone else.
zk_election(zk,dir,me,cb);
} else if( type == ZooKeeper.ZOO_SESSION_EVENT ) {
//console.log("Session problem!");
// zk will keep our callback around - no need to re-establish.
} else {
console.log( "Unkown type: " + type );
throw new Error("Unknown change type");
}
};
zk.aw_get( path, lw, function() {} );
var lw = function(type, state, p){
if( type == ZooKeeper.ZOO_CHANGED_EVENT ) {
zk.aw_get( path, lw, function() {} );
} else if( type == ZooKeeper.ZOO_DELETED_EVENT ) {
// Our sentinal disappeared - this could mean that
// we are now the leader, or it could mean that we
// need to watch someone else.
election(zk,dir,me);
} else if( type == ZooKeeper.ZOO_SESSION_EVENT ) {
//console.log("Session problem!");
// zk will keep our callback around - no need to re-establish.
} else {
console.log( "Unkown type: " + type );
//zk.aw_get( path, lw, function() {} );
}
};
zk.aw_get( path, lw, function() {} );
var lw = function(type, state, p){
if( type == ZooKeeper.ZOO_CHANGED_EVENT ) {
zk.aw_get( path, lw, function() {} );
} else if( type == ZooKeeper.ZOO_DELETED_EVENT ) {
// Our sentinal disappeared - this could mean that
// we are now the leader, or it could mean that we
// need to watch someone else.
zk_election(zk,dir,me,cb);
} else if( type == ZooKeeper.ZOO_SESSION_EVENT ) {
//console.log("Session problem!");
// zk will keep our callback around - no need to re-establish.
} else {
console.log( "Unkown type: " + type );
throw new Error("Unknown change type");
}
};
zk.aw_get( path, lw, function() {} );
var lw = function(type, state, p){
if( type == ZooKeeper.ZOO_CHANGED_EVENT ) {
zk.aw_get( path, lw, function() {} );
} else if( type == ZooKeeper.ZOO_DELETED_EVENT ) {
// Our sentinal disappeared - this could mean that
// we are now the leader, or it could mean that we
// need to watch someone else.
zk_election(zk,dir,me,cb);
} else if( type == ZooKeeper.ZOO_SESSION_EVENT ) {
//console.log("Session problem!");
// zk will keep our callback around - no need to re-establish.
} else {
console.log( "Unkown type: " + type );
throw new Error("Unknown change type");
}
};
zk.aw_get( path, lw, function() {} );
var lw = function(type, state, p){
if( type == ZooKeeper.ZOO_CHANGED_EVENT ) {
zk.aw_get( path, lw, function() {} );
} else if( type == ZooKeeper.ZOO_DELETED_EVENT ) {
// Our sentinal disappeared - this could mean that
// we are now the leader, or it could mean that we
// need to watch someone else.
election(zk,dir,me);
} else if( type == ZooKeeper.ZOO_SESSION_EVENT ) {
//console.log("Session problem!");
// zk will keep our callback around - no need to re-establish.
} else {
console.log( "Unkown type: " + type );
//zk.aw_get( path, lw, function() {} );
}
};
zk.aw_get( path, lw, function() {} );
process.on('SIGINT',function(){
console.log("Exiting")
if(zk != undefined && zk != null)
zk.close();
zk = new ZooKeeper({
connect: process.argv[2],
timeout: 22000,
debug_level: ZooKeeper.ZOO_LOG_LEVEL_WARN,
host_order_deterministic: false
})
zk.connect(function(err){
zk.a_get(zkPath,false,function(rc,error,stat,data){
zk.a_delete_(zkPath, stat.version,function(rc,error){
zk.close()
})
})
})
setTimeout(function(){
process.exit(0)
},1000)
})
}
function become_leader(zk,me) {
console.log(">>>>>>>>>>>>>>>>>>>>> I AM THE LEADER <<<<<<<<<<<<<<<<<<<<<<<<<<<");
zk.a_create("/chh/xxx/leader", me, ZooKeeper.ZOO_EPHEMERAL ,function (rc, error, path) {
});
}
function new_zk(hosts,zkclient){
var zk = new ZK();
zk.init ({connect:hosts, timeout:20000, debug_level:ZK.ZOO_LOG_LEVEL_WARNING, host_order_deterministic:false});
zk.on (ZK.on_connected, function (zkk) {
console.log ("zk session established, id=%s", zkk.client_id);
});
zk.on(ZK.on_closed,function(zkk){
//re-initialize
console.log("zk session close,re-init it");
zkclient.zk=new_zk(hosts,zkclient);
});
return zk;
}