Example #1
0
    def test_multiple_watchers(self):
        """
        Test whether multiple watchers are correctly called
        """
        cv1, cv2 = threading.Condition(), threading.Condition()

        def watcher1(*args, **kwargs):
            cv1.acquire()
            self.watcher1 = True
            cv1.notify()
            cv1.release()

        def watcher2(*args, **kwargs):
            cv2.acquire()
            self.watcher2 = True
            cv2.notify()
            cv2.release()

        nodename = "/zk-python-multiple-watcher-test"
        self.ensureCreated(nodename, "test")
        cv1.acquire()
        cv2.acquire()
        zookeeper.get(self.handle, nodename, watcher1)
        zookeeper.get(self.handle, nodename, watcher2)
        zookeeper.set(self.handle, nodename, "test")
        cv1.wait(15)
        cv2.wait(15)
        self.assertTrue(self.watcher1 and self.watcher2,
                        "One or more watchers failed to fire")
Example #2
0
    def set(self, path, data='', previous_data=None):
        path = '%s%s' % (self.prefix, path)
        try:
            current_data, current_meta = zookeeper.get(self.handle, path, None)
        except zookeeper.NoNodeException:
            if not previous_data:
                try:
                    zookeeper.create(self.handle, path, data, [ZOO_OPEN_ACL_UNSAFE])
                    return True
                except:
                    self.log.exception('Failed to create a missing key %s', path)
                    return False
            else:
                return False
        except:
            self.log.exception('Failed to set key %s', path)
            return False

        version = None
        if previous_data:
            if current_data != previous_data:
                self.log.error('Previous data constraint failed')
                return False
            version = current_meta['version']

        try:
            if version is None:
                zookeeper.set(self.handle, path, data)
            else:
                zookeeper.set(self.handle, path, data, version)
        except:
            self.log.exception('Set failed')
            return False

        return True
Example #3
0
def zookeeper_node_set(nodepath, nodevalue, perms, **option):
    ephemeral = 0
    if option.has_key('ephemeral') and option['ephemeral']:
        ephemeral = zookeeper.EPHEMERAL

    parent_perms = perms
    if option.has_key('parent_perms'):
        parent_perms = option['parent_perms']

    p = FilePath('/')

    for v in nodepath.split('/'):
        p = p / v

        if not zookeeper.exists(G.zookeeper, p):
            if not option.has_key('nocreate') or not option['nocreate']:
                if p == nodepath:
                    print zookeeper.create(G.zookeeper, p, nodevalue,
                                           [{
                                               "perms": perms,
                                               "scheme": "world",
                                               "id": "anyone"
                                           }], ephemeral)
                    return True
                else:
                    zookeeper.create(G.zookeeper, p, '', [{
                        "perms": parent_perms,
                        "scheme": "world",
                        "id": "anyone"
                    }], 0)
        elif p == nodepath:
            print zookeeper.set(G.zookeeper, p, nodevalue)
            return True

    return False
Example #4
0
    def post(self):
        request_dict = self.request.arguments
	snapshot_id = (request_dict['snapshot_id'])[0]
	print snapshot_id
	data =  ZdSnapshot.get(ZdSnapshot.id == int(snapshot_id) ) 
	zk = zookeeper.init(self.zk_connect(data.cluster_name))
        zookeeper.set(zk, data.path ,data.data)
        zookeeper.close(zk)
	self.write("还原成功!!!!!")
Example #5
0
	def put_ng(self, ng, val):
		# create the new znode that holds the graph data
		payload = { 'graph' : ng , 'data' : val }
		znode_id = zookeeper.create(self.handle, '/ng', json.dumps(payload), [ZOO_OPEN_ACL_UNSAFE], zookeeper.SEQUENCE)
		# now update mNG2znode look-up table:
		(self.NG2znode, stat) = zookeeper.get(self.handle, '/look-up', None)
		self.NG2znode =  json.loads(str(self.NG2znode))
		self.NG2znode[ng] = znode_id
		zookeeper.set(self.handle, '/look-up', json.dumps(self.NG2znode))
Example #6
0
    def write(self, path, contents, ephemeral=False, exclusive=False):
        """ 
        Writes the contents to the path in zookeeper. It will create the path in
        zookeeper if it does not already exist.

        This method will return True if the value is written, False otherwise.
        (The value will not be written if the exclusive is True and the node
        already exists.)
        """
        partial_path = ''

        # We start from the second element because we do not want to inclued
        # the initial empty string before the first "/" because all paths begin
        # with "/". We also don't want to include the final node because that
        # is dealt with later.

        for path_part in path.split("/")[1:-1]:
            partial_path = partial_path + "/" + path_part
            if not(zookeeper.exists(self.handle, partial_path)):
                try:
                    zookeeper.create(self.handle, partial_path, '', [self.acl], 0)
                except zookeeper.NodeExistsException:
                    pass

        exists = zookeeper.exists(self.handle, path)

        # Don't create it if we're exclusive.
        if exists and exclusive:
            return False

        # We make sure that we have the creation flags for ephemeral nodes,
        # otherwise they could be associated with previous connections that
        # have not yet timed out.
        if ephemeral and exists:
            try:
                zookeeper.delete(self.handle, path)
            except zookeeper.NoNodeException:
                pass
            exists = False

        if exists:
            zookeeper.set(self.handle, path, contents)
            return True
        else:
            flags = (ephemeral and zookeeper.EPHEMERAL or 0)
            try:
                zookeeper.create(self.handle, path, contents, [self.acl], flags)
                return True
            except zookeeper.NodeExistsException:
                if not(exclusive):
                    # Woah, something happened between the top and here.
                    # We just restart and retry the whole routine.
                    self.write(path, contents, ephemeral=ephemeral)
                    return True
                else:
                    return False
Example #7
0
  def update(self):
    setvalue = str(self.aID) + "," + str(self.agent_state[1]) + "," + str(self.agent_state[2]) + "," + str(self.agent_state[3])
    #IBR: following is not in romi's code, but i think it is needed:
    print("updating... car {0}".format(self.aID))
    self.agent_state = [self.clock(), self.position(),self.velocity(),self.acceleration()]
    self.agent[1] = self.agent_state
    zookeeper.set(self.handle, "/root/segment/" + str(self.aID), str(self.agent_state)) #Update values stored in zookeeper

    t_update = Timer(self.self.TA, self.update) #Set timer to TA = period of agentstate update
    t_update.start()
Example #8
0
  def __updateNode(self, path, value):
    """ Try to update path first.

    If the path doesn't exists, this method will try to create it.
    """
    self.__waitForConnect()
    try:
      zookeeper.set(self.handle, path, value)
    except zookeeper.NoNodeException:
      self.__forceCreatePath(path, value)
Example #9
0
    def post(self):
	request_dict = self.request.arguments
	node_value = (request_dict['node_value'])[0]
	node_name = (request_dict['node_name'])[0]
        cluster_name  = (request_dict['cluster_name'])[0]
        zk=zookeeper.init(self.zk_connect(cluster_name))
	zookeeper.set(zk,node_name,node_value)
	zookeeper.close(zk)
	logging.info('%s修改了集群%s的节点:%s值为%s'%(self.get_current_user(),cluster_name,node_name ,node_value ))
	self.write("修改成功")
Example #10
0
  def __updateNode(self, path, value):
    """ Try to update path first.

    If the path doesn't exists, this method will try to create it.
    """
    self.__waitForConnect()
    try:
      zookeeper.set(self.handle, path, value)
    except zookeeper.NoNodeException:
      self.__forceCreatePath(path, value)
Example #11
0
    def _write(self, path, contents, ephemeral, exclusive, sequential,
               mustexist):
        # We start from the second element because we do not want to inclued
        # the initial empty string before the first "/" because all paths begin
        # with "/". We also don't want to include the final node because that
        # is dealt with later.
        partial_path = ''
        for path_part in path.split("/")[1:-1]:
            partial_path = partial_path + "/" + path_part
            if not (zookeeper.exists(self.handle, partial_path)):
                try:
                    zookeeper.create(self.handle, partial_path, '', [self.acl],
                                     0)
                except zookeeper.NodeExistsException:
                    pass

        if sequential:
            exists = False
        else:
            exists = zookeeper.exists(self.handle, path)

        # Don't create it if we're exclusive.
        if exists and exclusive:
            return False

        # Check if we require the node to exist.
        if not exists and mustexist:
            return False

        # We make sure that we have the creation flags for ephemeral nodes,
        # otherwise they could be associated with previous connections that
        # have not yet timed out.
        if ephemeral and exists:
            try:
                zookeeper.delete(self.handle, path)
            except zookeeper.NoNodeException:
                pass
            exists = False

        if exists:
            zookeeper.set(self.handle, path, contents)
            return path
        else:
            flags = 0
            if ephemeral:
                flags = flags | zookeeper.EPHEMERAL
            if sequential:
                flags = flags | zookeeper.SEQUENCE

            # NOTE: We return the final path created.
            return zookeeper.create(self.handle, path, contents, [self.acl],
                                    flags)
Example #12
0
def del_test_data():
	hour = int(get_hour())
	zk = zookeeper.init("zk.staging.srv:8080")
	try:
		if 0 <= hour <= 7:  # 00:00~08:00之间的test配置会被清空
			zk_children = zookeeper.get_children(zk, "/company/cash/jira")
			if 'test' in zk_children:
				zk_test = zookeeper.get(zk, "/company/cash/jira/test")
				# 如果test节点存在且不为空,则清空配置
				if zk_test[0] is not '':
					zookeeper.set(zk, "/company/cash/jira/test", "")
	except Exception, ex:
		print ex.message
Example #13
0
    def set(self, path, value):
        """
        Set the value of the ZooKeeper Node at `path`

        Arguments:
        - `path`: string
        - `value`: string

        Return: None
        Exceptions: NoNodeError
        """
        zookeeper.set(self._zk, path, value)
        return
Example #14
0
def control():
    if request.form.get('restart', default=False):
        nodevalue = zookeeper.get(zk, ROOT_NODE)
        if nodevalue[0] != '':
            config = json.loads(nodevalue[0])
            config['restart'] = True
        else:
            config = {'restart': True}

        zookeeper.set(zk, ROOT_NODE, json.dumps(config))

        flash('Success')

    return render_file('control.html')
Example #15
0
    def _write(self, path, contents, ephemeral, exclusive, sequential, mustexist):
        # We start from the second element because we do not want to inclued
        # the initial empty string before the first "/" because all paths begin
        # with "/". We also don't want to include the final node because that
        # is dealt with later.
        partial_path = ''
        for path_part in path.split("/")[1:-1]:
            partial_path = partial_path + "/" + path_part
            if not(zookeeper.exists(self.handle, partial_path)):
                try:
                    zookeeper.create(self.handle, partial_path, '', [self.acl], 0)
                except zookeeper.NodeExistsException:
                    pass

        if sequential:
            exists = False
        else:
            exists = zookeeper.exists(self.handle, path)

        # Don't create it if we're exclusive.
        if exists and exclusive:
            return False

        # Check if we require the node to exist.
        if not exists and mustexist:
            return False

        # We make sure that we have the creation flags for ephemeral nodes,
        # otherwise they could be associated with previous connections that
        # have not yet timed out.
        if ephemeral and exists:
            try:
                zookeeper.delete(self.handle, path)
            except zookeeper.NoNodeException:
                pass
            exists = False

        if exists:
            zookeeper.set(self.handle, path, contents)
            return path
        else:
            flags = 0
            if ephemeral:
                flags = flags | zookeeper.EPHEMERAL
            if sequential:
                flags = flags | zookeeper.SEQUENCE

            # NOTE: We return the final path created.
            return zookeeper.create(self.handle, path, contents, [self.acl], flags)
Example #16
0
  def UpdateProperty(self, prop, value):
    if prop in self.props:
      if value == self.props[prop]:
        return

    self.props[prop] = value;
    data = ""
    for (k,v) in self.props.items():
      data += k
      data += '='
      data += v
      data += '\r\n'

    print "set zk data " + self.GetPath() + ' : ' + data
    zookeeper.set(self.zk , self.GetPath(), data);
Example #17
0
 def test_sync_getset(self):
     self.assertEqual(self.connected, True, "Not connected!")
     (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None)
     self.assertEqual(data, "on", "Data is not 'on' as expected: " + data)
     ret = zookeeper.set(self.handle, "/zk-python-getsettest",
                         "off", stat["version"])
     (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None)
     self.assertEqual(data, "off", "Data is not 'off' as expected: " + data)        
Example #18
0
  def UpdateProperty(self, prop, value):
    if prop in self.props:
      if value == self.props[prop]:
        return

    self.props[prop] = value;
    data = ""
    for (k,v) in self.props.items():
      data += k
      data += '='
      data += v
      data += '\r\n'

    print INFO(datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S] ") + "set zk data " + self.GetPath() + ":" + data)
    zkinfolog = OpenZkLog("./log.txt")
    zookeeper.set(self.zk , self.GetPath(), data);
    CloseZkLog(zkinfolog)
Example #19
0
def node_modify():
    path = request.args.get('path', '')

    if request.method == 'POST':
        data = request.form.get('data')

        verify = True

        if not zookeeper.exists(zk, CONFIG_NODE / path):
            flash('Path already exists:"%s"' % (CONFIG_NODE / path))
            verify = False

        if verify:
            try:
                zookeeper.set(zk, CONFIG_NODE / path, data)
                return redirect('/node')
            except BaseException, e:
                flash(e.message)
Example #20
0
  def update_node(self, path, value):
    """ Sets the ZooKeeper node at path to value, creating the node if it
      doesn't exist.

    Args:
      path: A PATH_SEPARATOR-separated str that represents the node whose value
        should be updated.
      value: A str representing the value that should be associated with the
        updated node.
    """
    logging.debug("Updating node at {0}, with new value {1}".format(path,
      value))
    self.wait_for_connect()
    try:
      zookeeper.set(self.handle, path, value)
    except zookeeper.NoNodeException:
      self.force_create_path(path, value)
    except zookeeper.ZooKeeperException, zoo_exception:
      logging.error("Problem setting path {0} with {1}, exception {2}"\
        .format(path, value, str(zoo_exception)))
    def test_lose_scope(self):
        """
        The idea is to test that the reference counting doesn't
        fail when we retain no references outside of the module
        """
        self.ensureDeleted("/zk-python-lose-scope-test")
        self.ensureCreated("/zk-python-lose-scope-test")
        def set_watcher():
            def fn(): self.callback_flag = True
            self.callback_flag = False
            zookeeper.exists(self.handle, "/zk-python-lose-scope-test",
                             self.create_callback( lambda handle, type, state, path: fn() )
                             )

        set_watcher()
        gc.collect()
        self.cv.acquire()
        zookeeper.set(self.handle, "/zk-python-lose-scope-test", "test")
        self.cv.wait(15)
        self.assertEqual(self.callback_flag, True)
Example #22
0
  def set(self, value, version, await_update=1):
    """Set the value to store at this node. If this node doesn't exist, this
    will raise NoNodeException; if the given version isn't the most recently
    stored in zookeeper, this will raise BadVersionException. Other server
    errors will raise other exceptions.

    To stomp over the value, regardless of what is stored in zookeeper, set
    version to -1.
    """
    self.__zk._use_socket(lambda z:
        zookeeper.set(z, self.path, value, version))
    self._wait_version(await_update, version+1)
Example #23
0
def server_restart():
    name = request.args.get('name', '')
    if name == '':
        return redirect(request.referrer)

    if not zookeeper.exists(zk, ONLINE_NODE / name):
        flash('"%s" is not online and can not be restart' % (name))
        return redirect(request.referrer)

    nodevalue = zookeeper.get(zk, ROOT_NODE)
    if nodevalue[0] != '':
        config = json.loads(nodevalue[0])
        config['restart'] = name
    else:
        config = {'restart': name}

    zookeeper.set(zk, ROOT_NODE, json.dumps(config))

    flash('Success')

    return redirect(request.referrer)
Example #24
0
    def updateAgentSM(self, aID):
        #t_stamp = rospy.get_rostime()
        t_stamp = self.get_time()
        #print("tstamp sec = {0}, nsec = {1}, ".format(t_stamp.secs,t_stamp.nsecs))
        #print("tstamp = {0}".format(t_stamp))
        #return
        U = self.getUnsafeAgents(aID, t_stamp + TM + 2 * TD + TMan)
        R = self.getReachableAgents(aID, t_stamp + TM + 2 * TD + TMan)

        MR = {}

        # Determine the MO and SM for every turn
        for turn in U.keys():
            MO = True
            SM = U[turn]
            for agent in U[turn]:
                if not (agent in R):
                    MO = False
                    SM = []
                    break
            MR[turn] = [t_stamp, MO, SM]

        #Update MR for agent in zookeeper
        zookeeper.set(self.handle, "/root/mr/" + str(aID), str(MR))
Example #25
0
    def set_data(self, path, data, version=None, return_data=False):
        """Blocking call to set zookeeper node's data.

        Args:
            path: zookeeper node path
            data: zookeeper node data (string)
            version = version if version is not None else -1
            return_data: if True return data

        Raises:
            zookeeper.NoNodeException if node already exists. 
            zookeeper.*Exception for other failure scenarios.
        """
        version = version if version is not None else -1

        if return_data:
            return zookeeper.set2(self.handle, path, data, version)
        else:
            return zookeeper.set(self.handle, path, data, version)
Example #26
0
    def set_data(self, path, data, version=None, return_data=False):
        """Blocking call to set zookeeper node's data.

        Args:
            path: zookeeper node path
            data: zookeeper node data (string)
            version = version if version is not None else -1
            return_data: if True return data

        Raises:
            zookeeper.NoNodeException if node already exists. 
            zookeeper.*Exception for other failure scenarios.
        """
        version = version if version is not None else -1

        if return_data:
            return zookeeper.set2(self.handle, path, data, version)
        else:
            return zookeeper.set(self.handle, path, data, version)
Example #27
0
 def test_sync_getset(self):
     self.assertEqual(self.connected, True, "Not connected!")
     (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None)
     self.assertEqual(data, "on", "Data is not 'on' as expected: " + data)
     ret = zookeeper.set(self.handle, "/zk-python-getsettest",
                         "off", stat["version"])
     (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None)
     self.assertEqual(data, "off", "Data is not 'off' as expected: " + data)
     self.assertRaises(zookeeper.BadVersionException,
                       zookeeper.set,
                       self.handle,
                       "/zk-python-getsettest",
                       "test",
                       stat["version"]+1)
     stat2 = zookeeper.set2(self.handle, "/zk-python-getsettest",
                            "set2", stat["version"])
     self.assertNotEqual(stat2, None, "set2 call failed, return should not be None")
     self.assertEqual(stat2["numChildren"], 0,
                      "set2 call failed, numChildren not 0 in set2 call")
     (data,stat) = zookeeper.get(self.handle, "/zk-python-getsettest", None)
     self.assertEqual(data, "set2", "Data is not 'set2' as expected: " + data)
Example #28
0
def ensure_node(zh, path, content):
    try: # Ensure the node exists
        zookeeper.create(zh, path, content, new_acl_openbar(), 0)
    except zookeeper.NodeExistsException as e:
        zookeeper.set(zh, path, content)
Example #29
0
    def set(self, path, value):
	zookeeper.add_auth(self.handle, 'digest', AUTH, None)
	return zookeeper.set(self.handle, path, value)
Example #30
0
 def set(self, path, value):
     return zookeeper.set(self.handle, path, value)
Example #31
0
 def set(self, path, data="", version=-1):
     """
     设置节点的value,返回更新结果
     """
     return zookeeper.set(self.handle, path, data, version)
Example #32
0
File: zk.py Project: hussam/racs
 def set(self, node, data, version):
     assert isinstance(version,int)
     return z.set(self.handle, node, data, version)
Example #33
0
 def set(self, path="", value=""):
     try:
         zookeeper.set(self.zk, path, value)
     except zookeeper.NoNodeException:
         self.create(path=path, value=value, refactor=False)
         self.set(path=path, value=value, refactor=False)
Example #34
0
 def set(self, path, value):
     zookeeper.add_auth(self.handle, 'digest', AUTH, None)
     return zookeeper.set(self.handle, path, value)
Example #35
0
 def set(self, path, value):
     return zookeeper.set(self.handle, path, value)
Example #36
0
 def modify(self, absname, value):
     return zookeeper.set(self.zkhandle, absname, value)
Example #37
0
                zookeeper.aget(s, path, None, cb)
            elif op == Operation.set:
                cb = SetCallback()
                zookeeper.aset(s, path, data, -1, cb)
            elif op == Operation.delete:
                cb = DeleteCallback()
                zookeeper.adelete(s, path, -1, cb)
            async_results.append(cb)
        else:
            if op == Operation.create:
                zookeeper.create(s, path, data, acl,
                                 zookeeper.EPHEMERAL if ephemeral else 0)
            elif op == Operation.get:
                zookeeper.get(s, path)
            elif op == Operation.set:
                zookeeper.set(s, path, data)
            elif op == Operation.delete:
                zookeeper.delete(s, path)

    for cb in async_results:
        cb.wait()

    # only print node type for creation operation
    node_type = ""
    if op == Operation.create:
        node_type = "ephemeral" if ephemeral else "permanent"

    return "%5s %6s %8d %10s znodes " % \
           ("async" if async else "sync", op, count, node_type)

Example #38
0
                zookeeper.aget(s, path, None, cb)
            elif op == Operation.set:
                cb = SetCallback()
                zookeeper.aset(s, path, data, -1, cb)
            elif op == Operation.delete:
                cb = DeleteCallback()
                zookeeper.adelete(s, path, -1, cb)
            async_results.append(cb)
        else:
            if op == Operation.create:
                zookeeper.create(s, path, data, acl,
                                 zookeeper.EPHEMERAL if ephemeral else 0)
            elif op == Operation.get:
                zookeeper.get(s, path)
            elif op == Operation.set:
                zookeeper.set(s, path, data)
            elif op == Operation.delete:
                zookeeper.delete(s, path)

    for cb in async_results:
        cb.wait()

    # only print node type for creation operation
    node_type = ""
    if op == Operation.create:
        node_type = "ephemeral" if ephemeral else "permanent"

    return "%5s %6s %8d %10s znodes " % \
           ("async" if async else "sync", op, count, node_type)

Example #39
0
 def set(self, path, data="", version=-1):
     return zookeeper.set(self.handle, path, data, version)
Example #40
0
 def set(self, path, data="", version=-1):
     return zookeeper.set(self.handle, path, data, version)
Example #41
0
 def _update_server_status_in_zk(self, servers):
     for s in servers:
         path = '/'.join([site.ZK_ROOT_NODE, 'workers', s])
         zookeeper.set(self._zk, path, '1')
Example #42
0
 def set(self, path, data):
     return zookeeper.set(self.handle, self.root + path, data)
Example #43
0
def ensure_node(zh, path, content):
    try:  # Ensure the node exists
        zookeeper.create(zh, path, content, new_acl_openbar(), 0)
    except zookeeper.NodeExistsException as e:
        zookeeper.set(zh, path, content)
Example #44
0
 def set(self, path, data="", version=-1):
     """
     设置节点的value,返回更新结果
     """
     return zookeeper.set(self.handle, path, data, version)
Example #45
0
 def trigger_watch(client):
     zookeeper.set(self.client2.handle, "/foobar-watched", "abc")
     return watch_deferred
Example #46
0
 def modify(self, absname, value):
     return zookeeper.set(self.zkhandle, absname, value)