Skip to content

Commit

Permalink
Move admin handler to their own dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jwolski committed Aug 26, 2015
1 parent db6491a commit a20ae4f
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 178 deletions.
27 changes: 27 additions & 0 deletions server/admin/debug-clear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
ringpop.clearDebugFlags();
callback(null, null, 'ok');
};
};
32 changes: 32 additions & 0 deletions server/admin/debug-set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

var safeParse = require('../../lib/util.js').safeParse;

module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
var body = safeParse(arg2.toString());
if (body && body.debugFlag) {
ringpop.setDebugFlag(body.debugFlag);
}
callback(null, null, 'ok');
};
};
33 changes: 4 additions & 29 deletions server/admin-join-handler.js → server/admin/gossip.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,9 @@
// THE SOFTWARE.
'use strict';

var errors = require('../lib/errors.js');
var sendJoin = require('../lib/swim/join-sender.js').joinCluster;

module.exports = function handleAdminJoin(opts, callback) {
var ringpop = opts.ringpop;

if (!ringpop.membership.localMember) {
process.nextTick(function() {
callback(errors.InvalidLocalMemberError());
});
return;
}

// Handle rejoin for member that left.
if (ringpop.membership.localMember.status === 'leave') {
// Assert local member is alive.
ringpop.membership.makeAlive(ringpop.whoami(), Date.now());

module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
ringpop.gossip.start();
ringpop.suspicion.reenable();

callback(null, null, 'rejoined');
return;
}

sendJoin({
ringpop: ringpop,
maxJoinDuration: ringpop.maxJoinDuration,
joinSize: ringpop.joinSize
}, callback);
callback(null, null, 'ok');
};
};
59 changes: 59 additions & 0 deletions server/admin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

module.exports = {
debugClear: {
endpoint: '/admin/debugClear',
handler: require('./debug-clear.js')
},
debugSet: {
endpoint: '/admin/debugSet',
handler: require('./debug-set.js')
},
gossip: {
endpoint: '/admin/gossip',
handler: require('./gossip.js')
},
join: {
endpoint: '/admin/join',
handler: require('./join.js')
},
leave: {
endpoint: '/admin/leave',
handler: require('./leave.js')
},
lookup: {
endpoint: '/admin/lookup',
handler: require('./lookup.js')
},
reload: {
endpoint: '/admin/reload',
handler: require('./reload.js')
},
stats: {
endpoint: '/admin/stats',
handler: require('./stats.js')
},
tick: {
endpoint: '/admin/tick',
handler: require('./tick.js')
}
};
61 changes: 61 additions & 0 deletions server/admin/join.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

var errors = require('../../lib/errors.js');
var sendJoin = require('../../lib/swim/join-sender.js').joinCluster;

module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
if (!ringpop.membership.localMember) {
process.nextTick(function() {
callback(errors.InvalidLocalMemberError());
});
return;
}

// Handle rejoin for member that left.
if (ringpop.membership.localMember.status === 'leave') {
// Assert local member is alive.
ringpop.membership.makeAlive(ringpop.whoami(), Date.now());

ringpop.gossip.start();
ringpop.suspicion.reenable();

callback(null, null, 'rejoined');
return;
}

sendJoin({
ringpop: ringpop,
maxJoinDuration: ringpop.maxJoinDuration,
joinSize: ringpop.joinSize
}, function onJoin(err, candidateHosts) {
if (err) {
callback(err);
return;
}

callback(null, null, JSON.stringify({
candidateHosts: candidateHosts
}));
});
};
};
58 changes: 29 additions & 29 deletions server/admin-leave-handler.js → server/admin/leave.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,43 @@
// THE SOFTWARE.
'use strict';

var errors = require('../lib/errors.js');
var errors = require('../../lib/errors.js');
var TypedError = require('error/typed');

var RedundantLeaveError = TypedError({
type: 'ringpop.invalid-leave.redundant',
message: 'A node cannot leave its cluster when it has already left.'
});

module.exports = function handleAdminLeave(opts, callback) {
var ringpop = opts.ringpop;
module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
if (typeof callback !== 'function') {
callback = function noop() {};
}

if (!ringpop.membership.localMember) {
process.nextTick(function() {
callback(errors.InvalidLocalMemberError());
});
return;
}

if (ringpop.membership.localMember.status === 'leave') {
process.nextTick(function() {
callback(RedundantLeaveError());
});
return;
}

// TODO Explicitly infect other members (like admin join)?
ringpop.membership.makeLeave(ringpop.whoami(),
ringpop.membership.localMember.incarnationNumber);

ringpop.gossip.stop();
ringpop.suspicion.stopAll();

if (typeof callback !== 'function') {
callback = function noop() {};
}

if (!ringpop.membership.localMember) {
process.nextTick(function() {
callback(errors.InvalidLocalMemberError());
});
return;
}

if (ringpop.membership.localMember.status === 'leave') {
process.nextTick(function() {
callback(RedundantLeaveError());
callback(null, null, 'ok');
});
return;
}

// TODO Explicitly infect other members (like admin join)?
ringpop.membership.makeLeave(ringpop.whoami(),
ringpop.membership.localMember.incarnationNumber);

ringpop.gossip.stop();
ringpop.suspicion.stopAll();

process.nextTick(function() {
callback(null, null, 'ok');
});
};
};
29 changes: 29 additions & 0 deletions server/admin/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
var key = arg2.toString();
callback(null, null, JSON.stringify({
dest: ringpop.lookup(key)
}));
};
};
33 changes: 33 additions & 0 deletions server/admin/reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';

var safeParse = require('../../lib/util.js').safeParse;

module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
var body = safeParse(arg2.toString());
if (body && body.file) {
ringpop.reload(body.file, function(err) {
callback(err);
});
}
};
};
8 changes: 4 additions & 4 deletions server/admin-lookup-handler.js → server/admin/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
// THE SOFTWARE.
'use strict';

module.exports = function handleAdminLookup(opts, callback) {
callback(null, null, {
dest: opts.ringpop.lookup(opts.key)
});
module.exports = function createHandler(ringpop) {
return function handle(arg1, arg2, hostInfo, callback) {
callback(null, null, JSON.stringify(ringpop.getStats()));
};
};
Loading

0 comments on commit a20ae4f

Please sign in to comment.