On Mon, 7 Jul 2014 16:51:27 +0800 "jjf_ntci"<[email protected]> wrote:
> where can I get ryu/app/multiple_controller_zk.py? We have not tested it for long time so I'm not sure if it works. = This patch implements multiple controllers with Zookeeper for high availability. - support OF1.0 + nicira extensions - need to install Zookeeper for coordination Signed-off-by: OHMURA Kei <[email protected]> --- ryu/app/multiple_controllers_zk.py | 121 ++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 ryu/app/multiple_controllers_zk.py diff --git a/ryu/app/multiple_controllers_zk.py b/ryu/app/multiple_controllers_zk.py new file mode 100644 index 0000000..7252b84 --- /dev/null +++ b/ryu/app/multiple_controllers_zk.py @@ -0,0 +1,121 @@ +# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import gevent +import zookeeper + +from ryu.ofproto import ofproto_v1_0 +from ryu.ofproto.ofproto_v1_0 import NX_ROLE_MASTER, NX_ROLE_SLAVE +# from ryu.ofproto import ofproto_v1_2 + +LOG = logging.getLogger('ryu.app.multiple_controllers') + +ZOO_OPEN_ACL_UNSAFE = {"perms": 0x1f, "scheme": "world", "id": "anyone"} +ROOT_PATH = "/multiple_controllers" +MASTER_PATH = ROOT_PATH + "/master" +SLAVE_PATH = ROOT_PATH + "/slave_" +DEFAULT_TIMEOUT = 10 # TODO: XXX + + +class MultipleControllers(object): + OFP_VERSIONS = [ofproto_v1_0.OFP_VERSION] + # OFP_VERSIONS = [ofproto_v1_2.OFP_VERSION] + + def __init__(self, role, zhost): + super(MultipleControllers, self).__init__() + self.role = NX_ROLE_MASTER if role == 'master' else \ + NX_ROLE_SLAVE + self.zhost = zhost + self.dpset = {} + self.cnodes = {} + self.init_zookeeper() + + def role_request(self, dp, role): + msg = dp.ofproto_parser.NXTRoleRequest(dp, role) + dp.send_msg(msg) + + def create_znode(self, path, data, acl, mode): + ret = None + try: + ret = zookeeper.create(self.handle, path, data, acl, mode) + except zookeeper.NodeExistsException: + LOG.debug("%s already exists", path) + + return ret + + def delete_znode(self, path): + try: + zookeeper.delete(self.handle, path) + except zookeeper.NoNodeException: + LOG.debug("%s not exists", path) + + def conn_watcher(self, handle, _type, state, path): + self.async_result.set() + + def controller_watcher(self): + new_nodes = zookeeper.get_children(self.handle, ROOT_PATH, + self.controller_watcher_cb) + if 'master' not in new_nodes: + # fail over + cid = self.create_znode(MASTER_PATH, '', + [ZOO_OPEN_ACL_UNSAFE], + zookeeper.EPHEMERAL) + if cid != None: + self.role = NX_ROLE_MASTER + old_id = self.cid + old_path = ROOT_PATH + '/' + old_id + self.cid = cid[len(ROOT_PATH) + 1:] + self.delete_znode(old_path) + for dp in self.dpset.values(): + self.role_request(dp, self.role) + + self.cnodes = new_nodes + print 'nodes:', self.cnodes + + def controller_watcher_cb(self, handle, type, state, path): + print('controller_watcher_cb handle: %d type: %d state: %s path: %s' % + (handle, type, state, path)) + self.controller_watcher() + + def init_zookeeper(self): + self.async_result = gevent.event.AsyncResult() + self.handle = zookeeper.init(self.zhost, self.conn_watcher) + try: + self.async_result.get(timeout=DEFAULT_TIMEOUT) + except gevent.Timeout: + LOG.debug('connect failure') + raise + + self.create_znode(ROOT_PATH, '', [ZOO_OPEN_ACL_UNSAFE], 0) + if self.role == NX_ROLE_MASTER: + self.cid = self.create_znode(MASTER_PATH, '', + [ZOO_OPEN_ACL_UNSAFE], + zookeeper.EPHEMERAL) + if self.cid == None: + LOG.error('master already exists', self.role) + raise + elif self.role == NX_ROLE_SLAVE: + self.cid = self.create_znode(SLAVE_PATH, '', + [ZOO_OPEN_ACL_UNSAFE], + zookeeper.EPHEMERAL | + zookeeper.SEQUENCE) + + self.cid = self.cid[len(ROOT_PATH) + 1:] + gevent.spawn_later(1, self.controller_watcher) + + def handle_datapath(self, ev): + self.dpset[ev.dp.id] = ev.dp + self.role_request(ev.dp, self.role) -- 1.7.9.5 ------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel ------------------------------------------------------------------------------ Open source business process management suite built on Java and Eclipse Turn processes into business applications with Bonita BPM Community Edition Quickly connect people, data, and systems into organized workflows Winner of BOSSIE, CODIE, OW2 and Gartner awards http://p.sf.net/sfu/Bonitasoft _______________________________________________ Ryu-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/ryu-devel
