Esempio n. 1
0
    def __init__(self, contents):
        self.log = logger.Logger('report_stats.txt')
        self.log.write('KV Consistency Data Report \n\n')
        #headers
        self.log.write(
            'Request # --- Request Type ----- Request Consistency ------ Request Time(secs)'
        )
        self.log.write(
            '__________________________________________________________________________________'
        )

        msg = msg_pb2.Message()
        request_types = {1: 'Get', 2: 'Set'}
        consis_types = {1: 'LINEAR', 2: 'SEQUENT', 3: 'CAUSAL', 4: 'EVENTUAL'}

        for req in contents:
            msg = contents[req][0]
            self.log.write(
                str(req) + '.                ' +
                str(request_types[msg.request]) + '                  ' +
                str(consis_types[msg.consis]) + '              ' +
                str(contents[req][3]))
            self.log.write(
                '__________________________________________________________________________________'
            )
        self.log.output_log()
Esempio n. 2
0
    def service_connection(self, key, mask):
        sock = key.fileobj
        data = key.data
        #self.node_log.write('\nService connection invoked')
        if mask & selectors.EVENT_READ:
            recv_data = sock.recv(1024)  # Should be ready to read
            if recv_data:

                #who is the msg coming from?
                #self.node_log.write('\nConnected ip:' + self.connected_ip)
                cmds = msg_pb2.Message()
                try:
                    cmds.ParseFromString(recv_data)
                    self.node_log.write('Inbound Msg:')
                    self.node_log.write(str(cmds))
                except:
                    self.node_log.write("\nerror cmds: " + repr(cmds))
                    self.node_log.write(
                        '\nmessage error: either not a proto buffer or incorrect type'
                    )

                #TODO do stuff with commands (cmds)
                self.handle_message(cmds)

            else:
                self.node_log.write('closing connection to' + str(data.addr))
                self.sel.unregister(sock)
                sock.close()
        if mask & selectors.EVENT_WRITE:
            if data.outb:
                #self.node_log.write('[Server] sending' + repr(data.outb) + 'to' + str(data.addr))
                sent = sock.send(data.outb)  # Should be ready to write
                data.outb = data.outb[sent:]
Esempio n. 3
0
    def eventual(self, cmds):
        pass
        toMaster = msg_pb2.Message()
        if cmds.rID in self.requests.keys():
            # Do nothing if replica has already seen request
            pass
        else:
            # Handle set request
            if cmds.request == 1:
                newKV = cmds.data.split(" ::: ")
                if len(newKV) == 2:

                    self.allConsisDB[newKV[0]] = newKV[1]
                    self.eventualDB[newKV[0]] = newKV[1]

                    # If msg from master,
                    # then it is the first replica to see it, and we should send a response
                    if cmds.ip == config.MASTER_IP:
                        toMaster = build_msg.build(self.ip, cmds.consis,
                                                   cmds.request, cmds.ack,
                                                   cmds.data, cmds.l_Clock,
                                                   cmds.rID)

                    # Send to other replicas if
                    for ip in self.replicaRoster:
                        if ip == self.ip:
                            pass
                        else:
                            toReplica = build_msg.build(
                                self.ip, cmds.consis, cmds.request, cmds.ack,
                                cmds.data, cmds.l_Clock, cmds.rID)
                            self.start_connections(
                                ip, toReplica.SerializeToString())

                    # Add the request to the requests dictionary
                    self.requests[cmds.rID] = cmds.ip

                else:
                    toMaster = build_msg.build(self.ip, cmds.consis,
                                               cmds.request, 0, cmds.data,
                                               cmds.l_Clock, cmds.rID)

            # Handle get request
            elif cmds.request == 2:
                if cmds.data in self.eventualDB:
                    pass
                    toMaster = build_msg.build(self.ip, cmds.consis,
                                               cmds.request, 1,
                                               self.eventualDB[cmds.data],
                                               self.l_clock, cmds.rID)
                else:
                    toMaster = build_msg.build(self.ip, cmds.consis,
                                               cmds.request, 0, 'Get Failure',
                                               self.l_clock, cmds.rID)

            # Send off to master
            self.start_connections(self.master_ip,
                                   toMaster.SerializeToString())
 def get_url(self, message):
     global elapsed_time
     message = pb2.Message(message=message.message)
     start = monotonic()
     for i in range(NO_OF_ITERATION):
         response = self.stub.getServerResponse(message)
     end = monotonic()
     elapsed_time = (end - start)
     return response
Esempio n. 5
0
def build(ip, consis, request, ack, data, l_Clock, rID = 0, ma_Timestamp = 0):
    message = msg_pb2.Message()
    message.ip = ip
    message.consis = consis
    message.request = request
    message.ack = ack
    message.data = data
    message.l_Clock = l_Clock
    message.rID = rID
    message.ma_Timestamp = ma_Timestamp
    return message
Esempio n. 6
0
    def tob(self, cmds):
        msgQ = PriorityQueue()
        outb = msg_pb2.Message()

        #check if request is in the dict
        if (cmds.rID in self.requests.keys()):
            # this is an acknowledgement message
            #add to request's msgQ
            msgQ = self.requests[cmds.rID]
            msgQ.put((msgQ.queue[0][0] + cmds.l_Clock, cmds))
            #update requests dict
            self.requests[cmds.rID] = msgQ

        else:
            #this is a command message
            #update local requests dict
            if (cmds.ip == self.master_ip):
                outb = build_msg.build(self.ip, cmds.consis, cmds.request, 1,
                                       cmds.data, self.l_clock, cmds.rID)
                #msgQ.put((self.l_clock, outb))
                self.requests[cmds.rID] = msgQ

                if (cmds.consis == 1):
                    # add to linearPQ
                    self.linearPQ.put((self.l_clock, cmds.rID, cmds))
                else:
                    # add to seq
                    self.seqPQ.put((self.l_clock, cmds.rID, cmds))
                self.broadcast(outb)
                return 0

            else:
                # handle replica message
                msgQ.put((cmds.l_Clock, cmds))
                self.requests[cmds.rID] = msgQ

                if (cmds.consis == 1):
                    # add to linearPQ
                    self.linearPQ.put((cmds.l_Clock, cmds.rID, cmds))
                else:
                    # add to seq
                    self.seqPQ.put((cmds.l_Clock, cmds.rID, cmds))
        self.broadcast(cmds)
Esempio n. 7
0
    def start_connections(self, host, open_msg):  # open_msg has to be in bytes
        #increment Logical clock
        self.l_clock += 1

        server_addr = (host, config.PORT)
        for i in range(0, 1):
            connid = i + 1
            self.node_log.write('starting connection' + str(connid) + 'to' +
                                str(server_addr))
            msg = msg_pb2.Message()
            msg.ParseFromString(open_msg)
            self.node_log.write('Outbound Msg:' + '\n' + str(msg))
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setblocking(False)
            sock.connect_ex(server_addr)
            events = selectors.EVENT_READ | selectors.EVENT_WRITE
            data = types.SimpleNamespace(connid=connid,
                                         addr=server_addr,
                                         inb=b'',
                                         outb=b'')
            data.outb += open_msg
            self.sel.register(sock, events, data=data)
    alpha_numeric_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvxyz"
    sb = ""
    for i in range(n):
        INDEX = int(len(alpha_numeric_string) * random.random())
        sb += alpha_numeric_string[INDEX]
    return sb


def get_numeric(n):
    return round(random.random() * 100, n)


if __name__ == '__main__':
    client = MsgClient()
    courses = []
    messageRequest = pb2.Message()
    for i in range(NO_OF_OBJECTS):
        course = Course(id=i,
                        course_name=get_alpha_numeric_string(10),
                        avg_marks=get_numeric(2),
                        year=int((random.random() * 23) + 2000),
                        month=int((random.random() * 12) + 1),
                        day=int((random.random() * 28) + 1),
                        hour=int(random.random() * 24),
                        minute=int(random.random() * 60),
                        second=int(random.random() * 60))
        courses.append(course)
        AddCourse(messageRequest.message.add(), course)
    """results = client.get_url(message=messageRequest)
    for (result, message) in zip(results.messageResponse, messageRequest.message):
        print("{")
Esempio n. 9
0
    def broadcast(self, cmds):
        #logging where we are with our requests
        ''' 
        for req in self.requests:
            if(isinstance(self.requests[req], PriorityQueue)):
                self.node_log.write('\n' + str(req) + ': ' + str(self.requests[req].queue)) 
            else:
                self.node_log.write('\n' + str(req) + ': ' + str(self.requests[req])) 
        self.node_log.write('processed requests:' + '\n') 
        for req in self.processed_reqs:
            if(isinstance(self.processed_reqs[req], PriorityQueue)):
                self.node_log.write('\n' + str(req) + ': ' + str(self.processed_reqs[req].queue))
            else: 
                self.node_log.write('\n' + str(req) + ': ' + str(self.processed_reqs[req].queue)) 
        '''
        # Check the queues if we have any broadcasting to do
        # if linear
        if (cmds.consis == 1):
            #prevent empty queue
            if (len(self.linearPQ.queue) == 0):
                self.node_log.write('**Lin Queue is Empty ****!!!!')
                return 0
            rID = self.linearPQ.queue[0][1]
            orig_msg = self.linearPQ.queue[0][2]
            msgQ = self.requests[rID]
            top_msg = msg_pb2.Message()
            outb = msg_pb2.Message()
            #self.node_log.write('\n' + 'message q: \n' + str(msgQ.queue))
            for msg in msgQ.queue:
                if (msg[1].ip == self.ip and len(msgQ.queue) < 3):
                    #we have already sent a message
                    return 0
            # are we the originator of the next queued broadcast
            if (len(msgQ.queue) == 0):
                outb = build_msg.build(self.ip, orig_msg.consis,
                                       orig_msg.request, 1, orig_msg.data,
                                       self.linearPQ.queue[0][0], orig_msg.rID)
                #update msgQ and requests
                msgQ.put((self.linearPQ.queue[0][0], outb))
                self.requests[rID] = msgQ
                # Broadcast to all
                for ip in self.replicaRoster:
                    if ip == self.ip:
                        pass
                    else:
                        self.start_connections(ip, outb.SerializeToString())
                self.broadcast(cmds)
                return 0

            top_msg = msgQ.queue[0][1]
            # all acks are in and we are originator
            if (len(msgQ.queue) == len(self.replicaRoster)):
                #remove first element
                self.linearPQ.get()
                #if set perform operation
                if (top_msg.request == 1):
                    newKV = top_msg.data.split(" ::: ")
                    if (len(newKV) == 2):
                        self.node_log.write('\n' + 'processing: ' +
                                            str(newKV) + '\n')
                        self.allConsisDB[newKV[0]] = newKV[1]
                        self.linearDB[newKV[0]] = newKV[1]
                        outb = build_msg.build(self.ip, 1, 1, 1,
                                               'Set successful', self.l_clock,
                                               rID)
                    else:
                        outb = build_msg.build(self.ip, 1, 1, 0, 'Set failure',
                                               self.l_clock, rID)

                else:
                    # perform get
                    if (top_msg.data in self.linearDB):
                        outb = build_msg.build(self.ip, orig_msg.consis,
                                               orig_msg.request, 1,
                                               self.linearDB[orig_msg.data],
                                               self.l_clock, orig_msg.rID)
                    else:
                        outb = build_msg.build(self.ip, orig_msg.consis,
                                               orig_msg.request, 0,
                                               'Get Failure', self.l_clock,
                                               orig_msg.rID)
                #if we are originator send to master
                if (top_msg.ip == self.ip):
                    self.start_connections(self.master_ip,
                                           outb.SerializeToString())

                #move request to processed_reqs
                self.processed_reqs[rID] = self.requests.pop(rID)
                #call broadcast again to check if we need to do anything
                self.broadcast(cmds)
            else:

                #These should all be requests that we are not originators on
                #check if we need to broadcast ack
                #if(len(msgQ.queue) == 1):
                # Broadcast to all
                for ip in self.replicaRoster:
                    if ip == self.ip:
                        pass
                    else:
                        outb = build_msg.build(self.ip, top_msg.consis,
                                               top_msg.request, top_msg.ack,
                                               'acknowledge ' + top_msg.data,
                                               self.l_clock, top_msg.rID)
                        #update msg queue and send ack
                        self.start_connections(ip, outb.SerializeToString())
                clock = 0
                clock = self.l_clock + self.linearPQ.queue[0][0]
                msgQ.put((clock, outb))
                self.requests[rID] = msgQ
                self.broadcast(cmds)

        else:

            #sequential tob
            if (len(self.seqPQ.queue) == 0):
                self.node_log.write('**Seq Queue is Empty ****!!!!')
                return 0

            rID = self.seqPQ.queue[0][1]
            orig_msg = self.seqPQ.queue[0][2]
            msgQ = self.requests[rID]
            top_msg = msg_pb2.Message()
            outb = msg_pb2.Message()

            if (orig_msg.request == 2):
                #If the next message in the queue is a get, just do it
                if (top_msg.data in self.sequentialDB):
                    outb = build_msg.build(self.ip, orig_msg.consis,
                                           orig_msg.request, 1,
                                           self.sequentialDB[orig_msg.data],
                                           self.l_clock, orig_msg.rID)
                else:
                    outb = build_msg.build(self.ip, orig_msg.consis,
                                           orig_msg.request, 0, 'Get Failure',
                                           self.l_clock, orig_msg.rID)

                #Remove from queue, send to master, then broadcast again
                fillQueue = PriorityQueue()
                fillQueue.put((rID, orig_msg))
                self.seqPQ.get()
                self.requests.pop(rID)  # remove from requests registry
                self.processed_reqs[rID] = fillQueue
                self.start_connections(self.master_ip,
                                       outb.SerializeToString())
                self.broadcast(cmds)

            else:

                #self.node_log.write('\n' + 'message q: \n' + str(msgQ.queue))
                for msg in msgQ.queue:
                    if (msg[1].ip == self.ip and len(msgQ.queue) < 3):
                        #we have already sent a message
                        return 0
                # are we the originator of the next queued broadcast
                if (len(msgQ.queue) == 0):
                    outb = build_msg.build(self.ip, orig_msg.consis,
                                           orig_msg.request, 1, orig_msg.data,
                                           self.seqPQ.queue[0][0],
                                           orig_msg.rID)
                    #update msgQ and requests
                    msgQ.put((self.seqPQ.queue[0][0], outb))
                    self.requests[rID] = msgQ
                    # Broadcast to all
                    for ip in self.replicaRoster:
                        if ip == self.ip:
                            pass
                        else:
                            self.start_connections(ip,
                                                   outb.SerializeToString())
                    self.broadcast(cmds)
                    return 0

                top_msg = msgQ.queue[0][1]
                # all acks are in and we are originator
                if (len(msgQ.queue) == len(self.replicaRoster)):
                    #remove first element
                    self.seqPQ.get()
                    #if set perform operation
                    newKV = top_msg.data.split(" ::: ")
                    if (len(newKV) == 2):
                        self.node_log.write('\n' + 'processing: ' +
                                            str(newKV) + '\n')
                        self.allConsisDB[newKV[0]] = newKV[1]
                        self.sequentialDB[newKV[0]] = newKV[1]
                        outb = build_msg.build(self.ip, 1, 1, 1,
                                               'Set successful', self.l_clock,
                                               rID)
                    else:
                        outb = build_msg.build(self.ip, 1, 1, 0, 'Set failure',
                                               self.l_clock, rID)

                    #if we are originator send to master
                    if (top_msg.ip == self.ip):
                        self.start_connections(self.master_ip,
                                               outb.SerializeToString())

                    #move request to processed_reqs
                    self.processed_reqs[rID] = self.requests.pop(rID)
                    #call broadcast again to check if we need to do anything
                    self.broadcast(cmds)
                else:

                    #These should all be requests that we are not originators on
                    #check if we need to broadcast ack
                    #if(len(msgQ.queue) == 1):
                    # Broadcast to all
                    for ip in self.replicaRoster:
                        if ip == self.ip:
                            pass
                        else:
                            outb = build_msg.build(
                                self.ip, top_msg.consis, top_msg.request,
                                top_msg.ack, 'acknowledge ' + top_msg.data,
                                self.l_clock, top_msg.rID)
                            #update msg queue and send ack
                            self.start_connections(ip,
                                                   outb.SerializeToString())
                    clock = 0
                    clock = self.l_clock + self.seqPQ.queue[0][0]
                    msgQ.put((clock, outb))
                    self.requests[rID] = msgQ
                    self.broadcast(cmds)