def __init__(self, vbaware, verbose=False):
     #have a queue , in case of not my vbucket error
     #let's reinitialize the config/memcached socket connections ?
     self.queue = Queue(10000)
     self.status = "initialized"
     self.vbaware = vbaware
     self.reconfig_callback = self.vbaware.reconfig_vbucket_map
     self.start_connection_callback = self.vbaware.start_vbucket_connection
     self.restart_connection_callback =\
         self.vbaware.restart_vbucket_connection
     self.verbose = verbose
     self.log = logger("CommandDispatcher")
     self._dispatcher_stopped_event = Event()
 def cas(self, key, expiry, flags, old_value, value):
     event = Event()
     item = {"operation": "cas", "key": key, "expiry": expiry,
             "flags": flags, "old_value": old_value, "value": value,
             "event": event, "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
 def touch(self, key, expiry):
     event = Event()
     item = {"operation": "touch", "key": key, "expiry": expiry,
             "event": event,
             "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
 def __init__(self, vbaware, verbose=False):
     # have a queue , in case of not my vbucket error
     # let's reinitialize the config/memcached socket connections ?
     self.queue = Queue(1000000)
     self.status = "initialized"
     self.vbaware = vbaware
     self.reconfig_callback = self.vbaware.reconfig_vbucket_map
     self.start_connection_callback = self.vbaware.start_vbucket_connection
     self.restart_connection_callback = self.vbaware.restart_vbucket_connection
     self.verbose = verbose
     self.log = logger("CommandDispatcher")
     self._dispatcher_stopped_event = Event()
 def flush(self, wait_time=0):
     event = Event()
     item = {"operation": "flush", "expiry": wait_time, "event": event,
             "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
 def incr(self, key, amount=1, init=0, expiry=0):
     event = Event()
     item = {"operation": "incr", "key": key, "amount": amount,
             "init": init, "expiry": expiry, "event": event, "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
 def prepend(self, key, value, cas=0):
     event = Event()
     item = {"operation": "prepend", "key": key, "cas": cas, "value": value,
             "event": event, "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
 def delete(self, key, cas=0):
     event = Event()
     item = {"operation": "delete", "key": key, "cas": cas, "event": event,
             "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
 def get(self, key):
     event = Event()
     item = {"operation": "get", "key": key, "event": event, "response": {}}
     self.dispatcher.put(item)
     return self._respond(item, event)
class CommandDispatcher(object):
    #this class contains a queue where request

    def __init__(self, vbaware, verbose=False):
        #have a queue , in case of not my vbucket error
        #let's reinitialize the config/memcached socket connections ?
        self.queue = Queue(10000)
        self.status = "initialized"
        self.vbaware = vbaware
        self.reconfig_callback = self.vbaware.reconfig_vbucket_map
        self.start_connection_callback = self.vbaware.start_vbucket_connection
        self.restart_connection_callback =\
            self.vbaware.restart_vbucket_connection
        self.verbose = verbose
        self.log = logger("CommandDispatcher")
        self._dispatcher_stopped_event = Event()

    def put(self, item):
        try:
            self.queue.put(item, False)
        except Full:
            #TODO: add a better error message here
            raise Exception("queue is full")

    def shutdown(self):
        if self.status != "shutdown":
            self.status = "shutdown"
            if self.verbose:
                self.log.info("dispatcher shutdown command received")
        self._dispatcher_stopped_event.wait(2)

    def reconfig_completed(self):
        self.status = "ok"

    def dispatch(self):
        while (self.status != "shutdown" or (self.status == "shutdown" and
               self.queue.qsize() > 0)):
            #wait if its reconfiguring the vbucket-map
            if self.status == "vbucketmap-configuration":
                continue
            try:
                item = self.queue.get(block=True, timeout=5)
                if item:
                    try:
                        self.do(item)
                        # do will only raise not_my_vbucket_exception,
                        # EOF and socket.error
                    except MemcachedError, ex:
                        # if we get a not_my_vbucket then requeue item
                        #  with fast forward map vbucket
                        self.log.error(ex)
                        if 'vbucket' in ex:
                            self.reconfig_callback(ex.vbucket)
                            self.start_connection_callback(ex.vbucket)
                        else:
                            raise Empty
                        item["fastforward"] = True
                        self.queue.put(item)
                    except EOFError, ex:
                        # we go an EOF error, restart the connection
                        self.log.error(ex)
                        if 'vbucket' in ex:
                            self.restart_connection_callback(ex.vbucket)
                        else:
                            raise Empty
                        self.queue.put(item)
                    except socket.error, ex:
                        # we got a socket error, restart the connection
                        self.log.error(ex)
                        if 'vbucket' in ex:
                            self.restart_connection_callback(ex.vbucket)
                        else:
                            raise Empty
                        self.queue.put(item)