Example #1
0
 def __init__(self, host, port):
     self.connection = BlockingProtoBufConnection(StorageResponseHeader)
     log.msg('self.connection.start(%s,%d)' % (host, port))
     self.connection.start(host, port)
     log.msg('finished')
     self.cont = False
     self.work_queue = Queue()  # threadsafe queue
     reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
Example #2
0
    def redirect(self, redirectMsg):
        master = self.protocol.factory.master

        connection = BlockingProtoBufConnection(DictionaryResponseHeader)
        connection.start(master['host'], master['port'])
        connection.sendMsg(redirectMsg)
        msg = connection.readMsg()
        connection.stop()

        return msg
Example #3
0
class AdminStorageClient(object):
    def __init__(self, host, port):
        self.connection = BlockingProtoBufConnection(StorageAdminResponse)
        self.connection.start(host, port)

    def _send(self, msg, opp):
        container = StorageAdminRequestContainer()
        container.operation = opp
        container.messageData = msg.SerializeToString()
        self.connection.sendMsg(container)

    def _checkResponse(self):
        response = self.connection.readMsg()
        if response.status == StorageAdminResponse.OK:
            return True
        print response.errorMsg
        return False

    def setXORServer(self, host, port):
        serverMsg = StorageAdminServerLocation()
        serverMsg.host = host
        serverMsg.port = port
        self._send(serverMsg, StorageAdminRequestContainer.SET_XOR_SERVER)
        return self._checkResponse()

    def recoverDataFrom(self, hostA, portA, hostB, portB):
        recoveryMsg = StorageAdminRecoveryOperation()
        recoveryMsg.serverA.host = hostA
        recoveryMsg.serverA.port = portA
        recoveryMsg.serverB.host = hostB
        recoveryMsg.serverB.port = portB
        self._send(recoveryMsg, StorageAdminRequestContainer.RECOVER_FROM)
        return self._checkResponse()

    def stop(self):
        self.connection.stop()
Example #4
0
class XORPartnerConnection(object):
    def __init__(self, host, port):
        self.connection = BlockingProtoBufConnection(StorageResponseHeader)
        log.msg('self.connection.start(%s,%d)' % (host, port))
        self.connection.start(host, port)
        log.msg('finished')
        self.cont = False
        self.work_queue = Queue()  # threadsafe queue
        reactor.addSystemEventTrigger('before', 'shutdown', self.stop)

    def sendXORUpdate(self, offset, bytes, callback):
        self.work_queue.put((offset, bytes, callback))

    def _handleXORUpdate(self, offset, bytes, callback):
        # send header
        msg = HashedStorageHeader()
        msg.header.operation = StorageHeader.XOR_WRITE
        msg.header.offset = offset
        msg.header.length = len(bytes)
        signAndTimestampHashedStorageHeader(msg)
        self.connection.sendMsg(msg)
        # send actual data
        self.connection.sendRawBytes(bytes)
        # read response message, and signal callback
        response = self.connection.readMsg()
        if response.status == StorageResponseHeader.OK:
            reactor.callFromThread(callback, offset, len(bytes), None)
        else:
            reactor.callFromThread(callback.offset, len(bytes),
                                   response.errorMsg)

    """
    Blocking request (with timeout) that tries to
    perform a piece of work that is inside the que.
    """

    def _handleOneRequest(self):
        try:  # blocking for 1 second, after this Empty is thrown
            task = self.work_queue.get(True, 1)
            self._handleXORUpdate(*task)
        except Empty:
            return

    """
    Worker function that continues working until
    stop is called and the queue eventueally becomes
    empty.
    """

    def _workerFunction(self):
        while self.cont or not self.work_queue.empty():
            self._handleOneRequest()
        self.connection.stop()

    """
    Starts
    """

    def start(self):
        if self.cont:
            raise Exception("Already running")
        self.cont = True
        reactor.callInThread(self._workerFunction)

    """
    Stop
    """

    def stop(self):
        self.cont = False
Example #5
0
 def __init__(self):
     self.connection = BlockingProtoBufConnection(DictionaryResponseHeader)
     self.cont = False
     self.work_queue = Queue()  # threadsafe queue
     reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
Example #6
0
class ReplicaNotifier(object):
    def __init__(self):
        self.connection = BlockingProtoBufConnection(DictionaryResponseHeader)
        self.cont = False
        self.work_queue = Queue()  # threadsafe queue
        reactor.addSystemEventTrigger('before', 'shutdown', self.stop)

    def sendReplicaUpdate(self, host, port, msg, callback):
        self.work_queue.put((host, port, msg, callback))

    def _handleReplicaUpdate(self, host, port, msg, callback):
        self.connection.stop()
        self.connection.resetSocket()
        log.msg('self.connection.start(%s,%d)' % (host, port))
        self.connection.start(host, port)

        log.msg("handleReplica: ", msg)

        # send header
        self.connection.sendMsg(msg)
        # read response message, and signal callback
        response = self.connection.readMsg()
        if response.status == DictionaryResponseHeader.OK:
            log.msg("OK")
        else:
            log.msg("ERROR")

    """
    Blocking request (with timeout) that tries to
    perform a piece of work that is inside the que.
    """

    def _handleOneRequest(self):
        try:  # blocking for 1 second, after this Empty is thrown
            task = self.work_queue.get(True, 1)
            self._handleReplicaUpdate(*task)
        except Empty:
            self.stop()
            return

    """
    Worker function that continues working until
    stop is called and the queue eventueally becomes
    empty.
    """

    def _workerFunction(self):
        while self.cont or not self.work_queue.empty():
            self._handleOneRequest()
        self.connection.stop()

    """
    Starts
    """

    def start(self):
        if self.cont:
            raise Exception("Already running")
        self.cont = True
        reactor.callInThread(self._workerFunction)

    """
    Stop
    """

    def stop(self):
        self.cont = False
Example #7
0
 def __init__(self, host, port):
     self.connection = BlockingProtoBufConnection(StorageAdminResponse)
     self.connection.start(host, port)
Example #8
0
class SimpleStorageTestClient(object):
    def __init__(self, host, port):
        self.connection = BlockingProtoBufConnection(StorageResponseHeader)
        self.connection.start(host, port)

    def _sendHeader(self, offset, length, opp):
        # NOTE: in real world this is created by the
        # dictionary service.
        msg = HashedStorageHeader()
        msg.header.operation = opp
        msg.header.offset = offset
        msg.header.length = length
        signAndTimestampHashedStorageHeader(msg)
        #print msg
        self.connection.sendMsg(msg)

    def writeData(self, offset, data):
        #print 'writeData(%d, %s)' % (offset, data)
        self._sendHeader(offset, len(data), StorageHeader.WRITE)
        self.connection.sendRawBytes(data)
        responseHeader = self.connection.readMsg()
        if responseHeader.status == StorageResponseHeader.OK:
            return True
        print responseHeader.errorMsg
        self.stop()
        return False

    def readData(self, offset, length):
        self._sendHeader(offset, length, StorageHeader.READ)
        responseHeader = self.connection.readMsg()
        if responseHeader.status == StorageResponseHeader.OK:
            return self.connection.readNBytes(responseHeader.header.length)
        print responseHeader.errorMsg
        self.stop()
        return None

    def stop(self):
        self.connection.stop()
Example #9
0
 def __init__(self, host, port):
     self.connection = BlockingProtoBufConnection(FreeListResponse)
     self.connection.start(host, port)
Example #10
0
class SimpleFreelistTestClient(object):
    def __init__(self, host, port):
        self.connection = BlockingProtoBufConnection(FreeListResponse)
        self.connection.start(host, port)

    def _handleResponse(self):
        responseHeader = self.connection.readMsg()
        if responseHeader.status == FreeListResponse.OK:
            return responseHeader
        print responseHeader.errorMsg
        return None

    def moveHost(self, fromHost, fromPort, toHost, toPort):
        msg = FreelistRequest()
        msg.operation = FreelistRequest.MOVE_HOST
        msg.moveFrom.host = fromHost
        msg.moveFrom.port = fromPort
        msg.moveTo.host = toHost
        msg.moveTo.port = toPort
        self.connection.sendMsg(msg)
        return self._handleResponse() is not None

    def allocateSpace(self, numberOfBytes):
        assert numberOfBytes > 0, "allocate request should always be larger than 1 byte"
        msg = FreelistRequest()
        msg.operation = FreelistRequest.ALLOCATE
        msg.numberOfBytes = numberOfBytes
        self.connection.sendMsg(msg)
        responseMsg = self._handleResponse()
        if responseMsg is None:
            return False
        return [(loc.host, loc.port, loc.offset, loc.length)
                for loc in responseMsg.freeSpace]

    def releaseSpace(self, releaseTupleList):  #[(host, port, offset, length)]
        msg = FreelistRequest()
        msg.operation = FreelistRequest.RELEASE
        for entry in releaseTupleList:
            l = SpaceLocation()
            l.host, l.port, l.offset, l.length = entry
            msg.releasedSpace.extend([l])
        self.connection.sendMsg(msg)
        return self._handleResponse() is not None

    def stop(self):
        self.connection.stop()
Example #11
0
class StorageClient(object):
    
    def __init__(self, locationMessage):
        self.connection = BlockingProtoBufConnection(StorageResponseHeader)
        self.connection.start(locationMessage.host, locationMessage.port)
        self.header = locationMessage.header
    
    def write(self, data):
        assert self.header.header.length == len(data)
        self.connection.sendMsg(self.header)
        self.connection.sendRawBytes(data)
        response = self.connection.readMsg()
        if response.status == StorageResponseHeader.ERROR:
            raise Exception("Write error: %s" % response.errorMsg)
            
    def read(self):
        self.connection.sendMsg(self.header)
        response = self.connection.readMsg()
        if response.status == StorageResponseHeader.ERROR:
            raise Exception("Read error: %s" % response.errorMsg)
        return self.connection.readNBytes(self.header.header.length)
        
    def stop(self):
        self.connection.stop()
Example #12
0
 def __init__(self, locationMessage):
     self.connection = BlockingProtoBufConnection(StorageResponseHeader)
     self.connection.start(locationMessage.host, locationMessage.port)
     self.header = locationMessage.header
Example #13
0
class DictionaryAdminClient(object):
    """
    Host and port are the Admin client to which we want to connect
    """
    def __init__(self, host, port):
        self.connection = BlockingProtoBufConnection(AdminResponse)
        self.connection.start(host, port)

    """
    A message to send to a dictManager
    """

    def _send(self, msg, notification):
        internal = RequestContainer()
        internal.notification = notification
        if msg is not None:
            internal.messageData = msg.SerializeToString()
        self.connection.sendMsg(internal)

    """
    Check response
    """

    def _checkResponse(self):
        response = self.connection.readMsg()
        if response.status == AdminResponse.OK:
            return True
        print response.errorMsg
        return False

    def resetState(self):
        self._send(None, RequestContainer.RESET_STATE)
        return self._checkResponse()

    """
    Give the DictServer a new slave located at host:port
    """

    def notifyMasterOfNewSlave(self, connection):
        serverMsg = DictionaryLocation()
        serverMsg.host = connection.host
        serverMsg.port = connection.clientPort

        self._send(serverMsg, RequestContainer.NEW_SLAVE)
        return self._checkResponse()

    """
    Give the DictServer a new Master located at host:port
    """

    def notifySlaveOfNewMaster(self, connection):
        serverMsg = DictionaryLocation()
        serverMsg.host = connection.host
        serverMsg.port = connection.clientPort

        self._send(serverMsg, RequestContainer.NEW_MASTER)
        return self._checkResponse()

    """
    Tell the dictServer that he is a slave
    """

    def setSlave(self, masterConnection):
        serverMsg = DictionaryLocation()
        serverMsg.host = masterConnection.host
        serverMsg.port = masterConnection.clientPort

        # tell dictserver that he is a slave
        self._send(serverMsg, RequestContainer.IS_SLAVE)
        return self._checkResponse()

    """
    Tell the dictServer that he is a Master
    """

    def setMaster(self):
        # tell dictserver that he is a master
        self._send(None, RequestContainer.IS_MASTER)
        return self._checkResponse()

    def moveHost(self, fromHost, fromPort, toHost, toPort):
        mh = MoveHostOperation()
        mh.moveFrom.host = fromHost
        mh.moveFrom.port = fromPort
        mh.moveTo.host = toHost
        mh.moveTo.port = toPort
        self._send(mh, RequestContainer.MOVE_HOST)

    """
    Close the connection
    """

    def stop(self):
        #print "Con closed"
        self.connection.stop()