Exemple #1
0
    def __init__(self, ctx, cm_host, platform):
        self.cm_host = cm_host
        self.platform = platform
        # collection mgr sockets (well known ports)
        self.dealer_socket = ctx.socket(zmq.DEALER)
        self.sub_socket = ctx.socket(zmq.SUB)
        self.dealer_socket.linger = 0
        self.sub_socket.linger = 0
        self.sub_socket.setsockopt_string(zmq.SUBSCRIBE, '')
        self.dealer_socket.connect("tcp://%s:%d" %
                                   (cm_host, CollectMsg.router_port(platform)))
        self.sub_socket.connect("tcp://%s:%d" %
                                (cm_host, CollectMsg.pub_port(platform)))
        self.dealer_port = Collection.parse_port(
            self.dealer_socket.getsockopt(zmq.LAST_ENDPOINT))
        self.sub_port = Collection.parse_port(
            self.sub_socket.getsockopt(zmq.LAST_ENDPOINT))
        logging.debug('collect_dealer_port = %d' % self.dealer_port)
        logging.debug('collect_sub_port = %d' % self.sub_port)

        self.poller = zmq.Poller()
        self.poller.register(self.dealer_socket, zmq.POLLIN)
        self.poller.register(self.sub_socket, zmq.POLLIN)
Exemple #2
0
def main():

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform (default 0)')
    parser.add_argument('-v', action='store_true', help='be verbose')
    args = parser.parse_args()

    if args.v:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s - %(levelname)s - %(message)s')
    else:
        logging.basicConfig(level=logging.WARNING,
                            format='%(asctime)s - %(levelname)s - %(message)s')

    logging.info('collectMgr starting')

    timer1started = False
    # CM state
    cmstate = CMState(platform=args.p)

    identityDict = {}

    pongCount = 0
    # context and sockets
    ctx = zmq.Context()
    cmd = ctx.socket(zmq.ROUTER)
    cmd.bind("tcp://*:%d" % CollectMsg.router_port(cmstate.platform()))
    publisher = ctx.socket(zmq.PUB)
    publisher.bind("tcp://*:%d" % CollectMsg.pub_port(cmstate.platform()))
    timerReceive = ctx.socket(zmq.PAIR)
    timerEndpoint = "inproc://timer"
    timerReceive.sndtimeo = 0
    timerReceive.bind(timerEndpoint)

    if cmstate.heartbeatInterval() > 0:
        # create timer thread
        timers = [{
            'period': cmstate.heartbeatInterval(),
            'offset': 0,
            'msg': CollectMsg.STARTPING
        }, {
            'period': cmstate.heartbeatInterval(),
            'offset': 1,
            'msg': CollectMsg.STARTEXP
        }]

        timer1 = ZTimer("Timer-1", ctx, timerEndpoint, timers)
        timer1.start()
        timer1started = True

    poller = zmq.Poller()
    poller.register(cmd, zmq.POLLIN)
    poller.register(timerReceive, zmq.POLLIN)
    try:
        while True:
            items = dict(poller.poll(1000))

            # Execute timer request
            if timerReceive in items:
                request = timerReceive.recv()
                logging.debug('Received <%s> from timer' % request.decode())

                if request == CollectMsg.STARTPING:
                    # Send PING broadcast
                    #cmmsg = CollectMsg(key=CollectMsg.PING)
                    #cmmsg.send(publisher)
                    #logging.debug("Published <PING>")
                    continue
                elif request == CollectMsg.STARTEXP:
                    # Remove expired keys
                    exlist = cmstate.expired_keys(cmstate.nodeTimeout())
                    if len(exlist) > 0:
                        pass
                        #removed = cmstate.remove_keys(exlist)
                        #for rr in removed:
                        #    logging.warning("Node timed out: %s" % rr)
                        #logging.warning("Removed %d nodes after %ds timeout" % (len(removed), cmstate.nodeTimeout()))
                    continue

            # Execute state cmd request
            if cmd in items:
                msg = cmd.recv_multipart()
                identity = msg[0]
                request = msg[1]
                logging.debug('Received <%s> from cmd' % request.decode())

                if request == CollectMsg.GETSTATE:

                    # Send STATE reply to client
                    logging.debug("Sending STATE reply")
                    cmd.send(identity, zmq.SNDMORE)
                    print('cmstate.entries:', cmstate.entries)
                    try:
                        testbody = json.dumps(cmstate.entries)
                    except Exception as ex:
                        logging.error(ex)
                        testbody = ''
                    logging.debug('GETSTATE: sending body=<%s>' % testbody)
                    statemsg = CollectMsg(key=CollectMsg.STATE, body=testbody)
                    statemsg.send(cmd)
                    continue

                if request == CollectMsg.STARTPLAT:
                    # Reset the CM state
                    cmstate.reset()
                    identityDict = {}

                    # Assign partition name
                    try:
                        prop = json.loads(msg[4])
                    except Exception as ex:
                        logging.error(ex)
                        prop = {}
                    try:
                        cmstate._partName = prop['partName']
                    except KeyError:
                        logging.error(
                            "STARTPLAT message: No partName property")
                        cmstate._partName = 'Unassigned'
                    logging.debug("Partition name: %s" % cmstate._partName)

                    # Send PLAT broadcast
                    logging.debug("Sending PLAT broadcast")
                    cmmsg = CollectMsg(key=CollectMsg.PLAT)
                    cmmsg.send(publisher)

                    # Send PLATSTARTED reply to client
                    logging.debug("Sending PLATSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.PLATSTARTED)
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTALLOC:
                    # Send ALLOC individually
                    logging.debug("Sending ALLOC individually")

                    for level, nodes in cmstate.entries.items():
                        for nodeid, node in enumerate(nodes):
                            # map to the identity
                            # could also create a reverse-mapping dict
                            who = (level, nodeid)
                            for identityKey, item in identityDict.items():
                                if item == who:
                                    break
                                # should raise error if not found
                            body = copy.copy(cmstate.entries)
                            body['id'] = nodeid
                            cmmsg = CollectMsg(key=CollectMsg.ALLOC,
                                               body=json.dumps(body))
                            # perhaps ideally next two lines should be:
                            # cmd.send_multipart([identityKey, CollectMsg.ALLOC, json.dumps(body)])
                            cmd.send(identityKey, zmq.SNDMORE)
                            cmmsg.send(cmd)
                            print('---', cmmsg.body)
                            logging.debug("...sent ALLOC")

                    # Send ALLOCSTARTED reply to client
                    logging.debug("Sending ALLOCSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.ALLOCSTARTED)
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTCONNECT:
                    # Send CONNECT individually
                    logging.debug("Sending CONNECT individually")
                    cmmsg = CollectMsg(key=CollectMsg.CONNECT,
                                       body=json.dumps(cmstate.entries))
                    for key in identityDict.keys():
                        cmd.send(key, zmq.SNDMORE)
                        cmmsg.send(cmd)
                        print('---', cmmsg.body)
                        logging.debug("...sent CONNECT")

                    # Send CONNECTSTARTED reply to client
                    logging.debug("Sending CONNECTSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.CONNECTSTARTED)
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTKILL:
                    # Send KILL broadcast
                    logging.debug("Sending KILL broadcast")
                    cmmsg = CollectMsg(key=CollectMsg.KILL)
                    #                   cmmsg['platform'] = cmstate.platform()
                    cmmsg.send(publisher)

                    # reset the CM state
                    cmstate.reset()

                    # Send KILLSTARTED reply to client
                    logging.debug("Sending KILLSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.KILLSTARTED)
                    #                   cmmsg['platform'] = cmstate.platform()
                    cmmsg.send(cmd)
                    continue

                elif request == CollectMsg.STARTDIE:
                    # Send DIE broadcast
                    logging.debug("Sending DIE broadcast")
                    cmmsg = CollectMsg(key=CollectMsg.DIE)
                    cmmsg.send(publisher)

                    # Send DIESTARTED reply to client
                    logging.debug("Sending DIESTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.DIESTARTED)
                    cmmsg.send(cmd)
                    continue

                elif request == CollectMsg.HELLO:
                    logging.debug("Loading HELLO properties with JSON")
                    if len(msg) == 3:
                        try:
                            hellodict = json.loads(msg[2])
                        except Exception as ex:
                            logging.error(ex)
                            hellodict = {}
                        logging.debug("HELLO msg[2] = %s" % hellodict)
                    else:
                        logging.error("Got HELLO msg of len %d, expected 3" %
                                      len(msg))
                        hellodict = {}

                    # remove any duplicates before adding new entry
                    #exlist = cmstate.find_duplicates(hellodict)
                    #if len(exlist) > 0:
                    #    removed = cmstate.remove_keys(exlist)
                    #    for rr in removed:
                    #        logging.warning("Node duplicated: %s" % rr)
                    #    logging.warning("Removed %d duplicate nodes" % len(removed))

                    # add new entry
                    #cmstate[identity] = hellodict
                    for level, item in hellodict.items():
                        keys = cmstate.keys()
                        if level not in keys:
                            cmstate[level] = []
                        identityDict[identity] = (level, len(cmstate[level]))
                        cmstate[level].append(item)
                        break
                    #try:
                    # update timestamp
                    #    cmstate.timestamp(identity)
                    #except:
                    #    logging.debug("HELLO timestamp failed")

                    continue

                elif request == CollectMsg.CONNECTINFO:
                    logging.debug("Loading CONNECTINFO with JSON")
                    if len(msg) == 3:
                        try:
                            connectInfo = json.loads(msg[2])
                        except Exception as ex:
                            logging.error(ex)
                            connectInfo = {}
                        logging.debug("CONNECTINFO msg[2] = %s" % connectInfo)
                    else:
                        logging.error(
                            "Got CONNECTINFO msg of len %d, expected 3" %
                            len(msg))
                        connectInfo = {}

                    try:
                        level, index = identityDict[identity]
                        cmstate[level][index].update(connectInfo[level])
                    except Exception as ex:
                        logging.error(ex)

                    continue

                elif request == CollectMsg.PONG:
                    pongCount += 1
                    logging.debug("PONG #%d" % pongCount)
                    if identity in cmstate.keys():
                        try:
                            # update timestamp
                            cmstate.timestamp(identity)
                        except:
                            logging.debug("PONG timestamp failed")
                    continue

                elif request == CollectMsg.STARTDUMP:
                    # Send reply to client
                    logging.debug("Sending DUMPSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.DUMPSTARTED)
                    cmmsg.send(cmd)

                    # Dump state to console
                    print("platform:", cmstate.platform())
                    print("partName:", cmstate.partName())
                    #                   print("heartbeatInterval:", cmstate.heartbeatInterval())
                    #                   print("nodeTimeout:", cmstate.nodeTimeout())
                    print("Nodes:")
                    pprint.pprint(cmstate.entries)
                    continue

                else:
                    logging.warning("Unknown msg <%s>" % request.decode())
                    # Send reply to client
                    logging.debug("Sending <HUH?> reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.HUH)
                    cmmsg.send(cmd)
                    continue

    except KeyboardInterrupt:
        logging.debug("Interrupt received")

    # Clean up
    logging.debug("Clean up")
    if timer1started:
        try:
            timerReceive.send(b"")  # signal timer to exit
        except zmq.Again:
            pass
        timer1.join()  # join timer thread

    # terminate zmq context
    ctx.destroy()

    logging.info('collectMgr exiting')
Exemple #3
0
def main():

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform (default 0)')
    parser.add_argument('-v', action='store_true', help='be verbose')
    args = parser.parse_args()

    if args.v:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s - %(levelname)s - %(message)s')
    else:
        logging.basicConfig(level=logging.WARNING,
                            format='%(asctime)s - %(levelname)s - %(message)s')

    logging.info('cmserver starting')

    timer1started = False
    # CM state
    cmstate = CMState(platform=args.p)

    pongCount = 0
    # context and sockets
    ctx = zmq.Context()
    cmd = ctx.socket(zmq.ROUTER)
    cmd.bind("tcp://*:%d" % CollectMsg.router_port(cmstate.platform()))
    publisher = ctx.socket(zmq.PUB)
    publisher.bind("tcp://*:%d" % CollectMsg.pub_port(cmstate.platform()))
    timerReceive = ctx.socket(zmq.PAIR)
    timerEndpoint = "inproc://timer"
    timerReceive.sndtimeo = 0
    timerReceive.bind(timerEndpoint)

    if cmstate.heartbeatInterval() > 0:
        # create timer thread
        timers = [{
            'period': cmstate.heartbeatInterval(),
            'offset': 0,
            'msg': CollectMsg.STARTPING
        }, {
            'period': cmstate.heartbeatInterval(),
            'offset': 1,
            'msg': CollectMsg.STARTEXP
        }]

        timer1 = ZTimer("Timer-1", ctx, timerEndpoint, timers)
        timer1.start()
        timer1started = True

    sequence = 0

    poller = zmq.Poller()
    poller.register(cmd, zmq.POLLIN)
    poller.register(timerReceive, zmq.POLLIN)
    try:
        while True:
            items = dict(poller.poll(1000))

            # Execute timer request
            if timerReceive in items:
                request = timerReceive.recv()
                logging.debug('Received <%s> from timer' % request.decode())

                if request == CollectMsg.STARTPING:
                    # Send PING broadcast
                    cmmsg = CollectMsg(sequence, key=CollectMsg.PING)
                    cmmsg.send(publisher)
                    logging.debug("Published <PING>")
                    continue
                elif request == CollectMsg.STARTEXP:
                    # Remove expired keys
                    exlist = cmstate.expired_keys(cmstate.nodeTimeout())
                    if len(exlist) > 0:
                        removed = cmstate.remove_keys(exlist)
                        for rr in removed:
                            logging.warning("Node timed out: %s" % rr)
                        logging.warning("Removed %d nodes after %ds timeout" %
                                        (len(removed), cmstate.nodeTimeout()))
                    continue

            # Execute state cmd request
            if cmd in items:
                msg = cmd.recv_multipart()
                identity = msg[0]
                request = msg[1]
                logging.debug('Received <%s> from cmd' % request.decode())

                if request == CollectMsg.GETSTATE:

                    # Send STATE reply to client
                    logging.debug("Sending STATE reply")
                    cmd.send(identity, zmq.SNDMORE)
                    print('cmstate.nodes():', cmstate.nodes())
                    try:
                        #                       testbody = json.dumps([{'key5': 5, 'key7': 7}, "Hi"])
                        testbody = json.dumps(cmstate.nodes())
                    except Exception as ex:
                        logging.error(ex)
                        testbody = ''
                    cmmsg = CollectMsg(sequence,
                                       key=CollectMsg.STATE,
                                       body=testbody)
                    cmmsg['platform'] = cmstate.platform()
                    cmmsg['partName'] = cmstate.partName()
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTPLAT:
                    # Assign partition name
                    try:
                        prop = json.loads(msg[4])
                    except Exception as ex:
                        logging.error(ex)
                        prop = {}
                    try:
                        cmstate._partName = prop['partName']
                    except KeyError:
                        logging.error(
                            "STARTPLAT message: No partName property")
                        cmstate._partName = 'Unassigned'
                    logging.debug("Partition name: %s" % cmstate._partName)

                    # Send PLAT broadcast
                    logging.debug("Sending PLAT broadcast")
                    cmmsg = CollectMsg(sequence, key=CollectMsg.PLAT)
                    cmmsg.send(publisher)

                    # Send PLATSTARTED reply to client
                    logging.debug("Sending PLATSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.PLATSTARTED)
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTALLOC:
                    # Send ALLOC individually
                    logging.debug("Sending ALLOC individually")
                    cmmsg = CollectMsg(sequence, key=CollectMsg.ALLOC)
                    for key in cmstate.entries.keys():
                        # skip allocating this entry if property select=0
                        try:
                            select = cmstate.entries[key]['select']
                        except KeyError:
                            pass
                        else:
                            if select == 0:
                                continue

                        cmd.send(key, zmq.SNDMORE)
                        cmmsg.send(cmd)

                    # Send ALLOCSTARTED reply to client
                    logging.debug("Sending ALLOCSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.ALLOCSTARTED)
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTCONNECT:
                    # Send CONNECT individually
                    logging.debug("Sending CONNECT individually")
                    cmmsg = CollectMsg(sequence, key=CollectMsg.CONNECT)
                    for key in cmstate.entries.keys():
                        # skip connecting this entry if property select=0
                        try:
                            select = cmstate.entries[key]['select']
                        except KeyError:
                            pass
                        else:
                            if select == 0:
                                continue

                        cmd.send(key, zmq.SNDMORE)
                        cmmsg.send(cmd)

                    # Send CONNECTSTARTED reply to client
                    logging.debug("Sending CONNECTSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.CONNECTSTARTED)
                    cmmsg.send(cmd)
                    continue

                if request == CollectMsg.STARTKILL:
                    # Send KILL broadcast
                    logging.debug("Sending KILL broadcast")
                    cmmsg = CollectMsg(sequence, key=CollectMsg.KILL)
                    cmmsg['platform'] = cmstate.platform()
                    cmmsg.send(publisher)

                    # reset the CM state
                    cmstate.reset()

                    # Send KILLSTARTED reply to client
                    logging.debug("Sending KILLSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.KILLSTARTED)
                    cmmsg['platform'] = cmstate.platform()
                    cmmsg.send(cmd)
                    continue

                elif request == CollectMsg.STARTDIE:
                    # Send DIE broadcast
                    logging.debug("Sending DIE broadcast")
                    cmmsg = CollectMsg(sequence, key=CollectMsg.DIE)
                    cmmsg.send(publisher)

                    # Send DIESTARTED reply to client
                    logging.debug("Sending DIESTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.DIESTARTED)
                    cmmsg.send(cmd)
                    continue

                elif request == CollectMsg.HELLO:
                    try:
                        prop = json.loads(msg[4])
                    except Exception as ex:
                        logging.error(ex)
                        prop = {}
                    logging.debug("properties = %s" % prop)

                    # remove any duplicates before adding new entry
                    exlist = cmstate.find_duplicates(prop)
                    if len(exlist) > 0:
                        removed = cmstate.remove_keys(exlist)
                        for rr in removed:
                            logging.warning("Node duplicated: %s" % rr)
                        logging.warning("Removed %d duplicate nodes" %
                                        len(removed))

                    # add new entry
                    cmstate[identity] = prop
                    try:
                        # update timestamp
                        cmstate.timestamp(identity)
                    except:
                        logging.debug("HELLO timestamp failed")

                    continue

                elif request == CollectMsg.PORTS:
                    try:
                        prop = json.loads(msg[4])
                    except:
                        prop = {}

                    if 'ports' in prop:
                        try:
                            cmstate[identity]['ports'] = prop['ports']
                        except:
                            logging.debug("Setting PORTS property failed")
                    else:
                        logging.error("PORTS message: No ports property")
                    continue

                elif request == CollectMsg.PONG:
                    pongCount += 1
                    logging.debug("PONG #%d" % pongCount)
                    if identity in cmstate.keys():
                        try:
                            # update timestamp
                            cmstate.timestamp(identity)
                        except:
                            logging.debug("PONG timestamp failed")
                    continue

                elif request == CollectMsg.STARTDUMP:
                    # Send reply to client
                    logging.debug("Sending DUMPSTARTED reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.DUMPSTARTED)
                    cmmsg.send(cmd)

                    # Dump state to console
                    print("platform:", cmstate.platform())
                    print("partName:", cmstate.partName())
                    print("heartbeatInterval:", cmstate.heartbeatInterval())
                    print("nodeTimeout:", cmstate.nodeTimeout())
                    print("Nodes:")
                    pprint.pprint(cmstate.entries)
                    continue

                else:
                    logging.warning("Unknown msg <%s>" % request.decode())
                    # Send reply to client
                    logging.debug("Sending <HUH?> reply")
                    cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(sequence, key=CollectMsg.HUH)
                    cmmsg.send(cmd)
                    continue

    except KeyboardInterrupt:
        logging.debug("Interrupt received")

    # Clean up
    logging.debug("Clean up")
    try:
        timerReceive.send(b"")  # signal timer to exit
    except zmq.Again:
        pass

    time.sleep(.25)

    # close zmq sockets
    cmd.close()
    publisher.close()
    timerReceive.close()

    # terminate zmq context
    ctx.term()

    if timer1started:
        timer1.join()  # join timer thread

    logging.info('cmserver exiting')
Exemple #4
0
def main():

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('clientId',
                        type=int,
                        choices=range(0, 10),
                        help='client ID')
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform (default 0)')
    parser.add_argument('-C',
                        metavar='CM_HOST',
                        default='localhost',
                        help='Collection Manager host')
    parser.add_argument('-v', action='store_true', help='be verbose')
    parser.add_argument('--unselect',
                        action='store_true',
                        help='avoid allocation')
    args = parser.parse_args()

    clientId = args.clientId
    verbose = args.v
    unselect = args.unselect

    # Prepare our context and sockets
    ctx = zmq.Context()
    collect_cmd = ctx.socket(zmq.DEALER)
    collect_cmd.linger = 0
    collect_cmd.connect("tcp://%s:%d" %
                        (args.C, CollectMsg.router_port(args.p)))
    collect_sub = ctx.socket(zmq.SUB)
    collect_sub.linger = 0
    collect_sub.setsockopt_string(zmq.SUBSCRIBE, '')
    collect_sub.connect("tcp://%s:%d" % (args.C, CollectMsg.pub_port(args.p)))

    poller = zmq.Poller()
    # Listen to both DEALER and SUB sockets
    poller.register(collect_sub, zmq.POLLIN)
    poller.register(collect_cmd, zmq.POLLIN)

    alarm = time.time() + 1.
    while True:
        tickless = 1000 * max(0, alarm - time.time())
        try:
            items = dict(poller.poll(tickless))
        except:
            break  # Interrupted

        if collect_sub in items:
            cmmsg = CollectMsg.recv(collect_sub)
            if cmmsg.key == CollectMsg.PING:
                if verbose:
                    print("Received PING, sending PONG")
                collect_cmd.send(CollectMsg.PONG)

            elif cmmsg.key == CollectMsg.PLAT:
                if verbose:
                    print("Received PLAT, sending HELLO (platform=%d)" %
                          args.p)

                # start composing JSON message
                pybody = {}
                pyinfo = {}

                # Create simulated HELLO entry
                #  {'test': {'procInfo': {'host': 'psbuild-rhel6', 'pid': 31314}}}

                pyinfo['host'] = socket.gethostname()
                pyinfo['pid'] = getpid()
                pybody['test'] = {'procInfo': pyinfo}

                jsonbody = json.dumps(pybody)
                hellomsg = CollectMsg(key=CollectMsg.HELLO, body=jsonbody)
                try:
                    hellomsg.send(collect_cmd)
                except Exception as ex:
                    print('E: hellomsg.send()', ex)

            elif cmmsg.key == CollectMsg.DIE:
                if verbose:
                    print("Received DIE, exiting")
                break  # Interrupted

            elif cmmsg.key == CollectMsg.KILL:
                if verbose:
                    print("Received KILL, ignoring")

            elif verbose:
                print("Client ID %d: Received key=\"%s\" on SUB socket" %
                      (clientId, cmmsg.key))

        if collect_cmd in items:
            cmmsg = CollectMsg.recv(collect_cmd)
            if cmmsg.key == CollectMsg.ALLOC:
                if verbose:
                    print("Received ALLOC, sending CONNECTINFO")

                pybody = {}
                pyinfo = {}

                # Create simulated CONNECTINFO entry
                #  {'test': {'connectInfo': {'test_port': {'adrs': 'psbuild-rhel6', 'port': 55500}}}}

                pyinfo['test_port'] = {
                    'adrs': socket.gethostname(),
                    'port': 55500 + clientId
                }
                pybody['test'] = {'connectInfo': pyinfo}

                jsonbody = json.dumps(pybody)
                connectInfoMsg = CollectMsg(key=CollectMsg.CONNECTINFO,
                                            body=jsonbody)
                connectInfoMsg.send(collect_cmd)

            elif cmmsg.key == CollectMsg.CONNECT:
                if verbose:
                    print("Received CONNECT")
                try:
                    pybody = json.loads(cmmsg.body)
                except Exception as ex:
                    print("CONNECT: json.loads() exception: %s" % ex)
                    print("CONNECT: cmmsg.body = <%s>" % cmmsg.body)
                    pybody = {}
                if verbose:
                    print("--------------------------")
                    pprint.pprint(pybody)
                    print("--------------------------")

            elif verbose:
                print("Client ID %d: Received key=\"%s\" on DEALER socket" %
                      (clientId, cmmsg.key))

    print("Interrupted")
    sys.exit(0)
Exemple #5
0
def main():

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('clientId', type=int, choices=range(0, 10), help='client ID')
    parser.add_argument('-p', type=int, choices=range(0, 8), default=0, help='platform (default 0)')
    parser.add_argument('-C', metavar='CM_HOST', default='localhost', help='Collection Manager host')
    parser.add_argument('-v', action='store_true', help='be verbose')
    parser.add_argument('--unselect', action='store_true', help='avoid allocation')
    args = parser.parse_args()

    clientId = args.clientId
    verbose = args.v
    unselect = args.unselect

    # Prepare our context and sockets
    ctx = zmq.Context()
    cmd = ctx.socket(zmq.DEALER)
    cmd.linger = 0
    cmd.connect("tcp://%s:%d" % (args.C, CollectMsg.router_port(args.p)))
    subscriber = ctx.socket(zmq.SUB)
    subscriber.linger = 0
    subscriber.setsockopt_string(zmq.SUBSCRIBE, '')
    subscriber.connect("tcp://%s:%d" % (args.C, CollectMsg.pub_port(args.p)))

    poller = zmq.Poller()
    # Listen to both DEALER and SUB sockets
    poller.register(subscriber, zmq.POLLIN)
    poller.register(cmd, zmq.POLLIN)

    alarm = time.time()+1.
    while True:
        tickless = 1000*max(0, alarm - time.time())
        try:
            items = dict(poller.poll(tickless))
        except:
            break           # Interrupted

        if subscriber in items:
            cmmsg = CollectMsg.recv(subscriber)
            if cmmsg.key == CollectMsg.PING:
                if verbose:
                    print( "Received PING, sending PONG")
                cmd.send(CollectMsg.PONG)

            elif cmmsg.key == CollectMsg.PLAT:
                if verbose:
                    print( "Received PLAT, sending HELLO (platform=%d)" % args.p)
                newmsg = CollectMsg(0, key=CollectMsg.HELLO)
                # Create simulated ports entry based on clientId N, platform P
                # {
                #   'platform' : P
                #   'group'    : N
                #   'uid'      : N
                #   'level'    : N
                #   'pid'      : 25NNN
                #   'ip'       : '172.NN.NN.NN'
                #   'ether'    : 'NN:NN:NN:NN:NN:NN'
                # }
                newmsg['platform'] = args.p
                if unselect:
                    newmsg['select'] = 0
                newmsg['group'] = newmsg['uid'] = newmsg['level'] = \
                    clientId
                newmsg['pid'] = \
                    25000 + (111 * clientId)
                newmsg['ip'] = \
                    '172' + (3 * ('.%d%d' % (clientId, clientId)))
                newmsg['ether'] = \
                    '%d%d' % (clientId, clientId) + \
                               (5 * (':%d%d' % (clientId, clientId)))
                try:
                    newmsg.send(cmd)
                except Exception as ex:
                    print('E: newmsg.send()', ex)

            elif cmmsg.key == CollectMsg.DIE:
                if verbose:
                    print( "Received DIE, exiting")
                break       # Interrupted

            elif cmmsg.key == CollectMsg.KILL:
                if verbose:
                    print( "Received KILL, ignoring")

            elif verbose:
                print( "Client ID %d: Received key=\"%s\" on SUB socket" % (clientId, cmmsg.key))

        if cmd in items:
            cmmsg = CollectMsg.recv(cmd)
            if cmmsg.key == CollectMsg.ALLOC:
                if verbose:
                    print( "Received ALLOC, sending PORTS")
                newmsg = CollectMsg(0, key=CollectMsg.PORTS)
                # Create simulated ports entry based on clientId N
                # {
                #   'name' : 'port<N>',
                #   'host' : 'HHHHH',
                #   'port' : 5550<N>
                # }
                ports = {}
                ports['name'] = 'port%d' % clientId
                ports['host'] = socket.gethostname()
                ports['port'] = 55500 + clientId
                newmsg['ports'] = [ ports ]
                newmsg.send(cmd)

            elif verbose:
                print( "Client ID %d: Received key=\"%s\" on DEALER socket" % (clientId, cmmsg.key))

    print ("Interrupted")
    sys.exit(0)
Exemple #6
0
def main():

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform (default 0)')
    parser.add_argument('-v', action='store_true', help='be verbose')
    args = parser.parse_args()

    if args.v:
        logging.basicConfig(level=logging.DEBUG,
                            format='%(asctime)s - %(levelname)s - %(message)s')
    else:
        logging.basicConfig(level=logging.WARNING,
                            format='%(asctime)s - %(levelname)s - %(message)s')

    logging.info('collectMgr starting')

    # Collect state

    states = [
        State(name='NOPLAT', on_enter='noplatEnter'),
        State(name='PLAT', on_enter='platEnter'),
        State(name='ALLOC', on_enter='allocEnter'),
        State(name='CONNECT', on_enter='connectEnter')
    ]
    transitions = [{
        'trigger': 'PLAT',
        'source': ['NOPLAT', 'PLAT'],
        'dest': 'PLAT'
    }, {
        'trigger': 'ALLOC',
        'conditions': ['nonEmptyVerify', 'procInfoVerify'],
        'source': 'PLAT',
        'dest': 'ALLOC'
    }, {
        'trigger': 'CONNECT',
        'conditions': 'connectInfoVerify',
        'source': 'ALLOC',
        'dest': 'CONNECT'
    }, {
        'trigger': 'KILL',
        'before': 'killBefore',
        'source': states,
        'dest': 'NOPLAT'
    }]
    collectState = CollectState(platform=args.p)
    collectMachine = Machine(collectState,
                             states,
                             transitions=transitions,
                             initial='NOPLAT')
    logging.debug("Initial collectState.state: %s" % collectState.state)

    # context and sockets
    ctx = zmq.Context()
    collectState.cmd = ctx.socket(zmq.ROUTER)
    collectState.cmd.bind("tcp://*:%d" %
                          CollectMsg.router_port(collectState.platform()))
    collectState.publisher = ctx.socket(zmq.PUB)
    collectState.publisher.bind("tcp://*:%d" %
                                CollectMsg.pub_port(collectState.platform()))

    poller = zmq.Poller()
    poller.register(collectState.cmd, zmq.POLLIN)
    try:
        while True:
            items = dict(poller.poll(1000))

            # Execute state cmd request
            if collectState.cmd in items:
                msg = collectState.cmd.recv_multipart()
                identity = msg[0]
                request = msg[1]
                logging.debug('Received <%s> from cmd' % request.decode())

                collectTrigger = collectState.getTrigger(request.decode())
                if collectTrigger:
                    # Handle state transition by calling trigger
                    try:
                        collectTrigger(identity)
                    except MachineError as ex:
                        logging.error('Transition failed: %s' % ex)

                    # *fall through* to GETSTATE for reply after transition
                    request = CollectMsg.GETSTATE

                if request == CollectMsg.GETSTATE:
                    # Send state reply to client
                    logging.debug("Sending state reply")
                    collectState.cmd.send(identity, zmq.SNDMORE)
                    # print('collectState.entries:', collectState.entries)
                    try:
                        testbody = json.dumps(collectState.entries)
                    except Exception as ex:
                        logging.error(ex)
                        testbody = ''
                    logging.debug('GETSTATE: sending body=<%s>' % testbody)
                    statemsg = CollectMsg(key=collectState.state.encode(),
                                          body=testbody)
                    statemsg.send(collectState.cmd)
                    continue

                elif request == CollectMsg.DIE:
                    # Send DIE broadcast
                    logging.debug("Sending DIE broadcast")
                    cmmsg = CollectMsg(key=CollectMsg.DIE)
                    cmmsg.send(collectState.publisher)

                    # Send DIESTARTED reply to client
                    logging.debug("Sending DIESTARTED reply")
                    collectState.cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.DIESTARTED)
                    cmmsg.send(collectState.cmd)
                    continue

                elif request == CollectMsg.HELLO:
                    if collectState.state not in ['PLAT']:
                        logging.warning("Dropped HELLO message (state=%s)" %
                                        collectState.state)
                        continue

                    logging.debug("Loading HELLO properties with JSON")
                    if len(msg) == 3:
                        try:
                            hellodict = json.loads(msg[2])
                        except Exception as ex:
                            logging.error(ex)
                            hellodict = {}
                        logging.debug("HELLO msg[2] = %s" % hellodict)
                    else:
                        logging.error("Got HELLO msg of len %d, expected 3" %
                                      len(msg))
                        hellodict = {}

                    # add new entry
                    #collectState[identity] = hellodict
                    for level, item in hellodict.items():
                        keys = collectState.entries.keys()
                        if level not in keys:
                            collectState.entries[level] = []
                        collectState.identityDict[identity] = (
                            level, len(collectState.entries[level]))
                        collectState.entries[level].append(item)
                        break

                    continue

                elif request == CollectMsg.CONNECTINFO:
                    logging.debug("Loading CONNECTINFO with JSON")
                    if len(msg) == 3:
                        try:
                            connectInfo = json.loads(msg[2])
                        except Exception as ex:
                            logging.error(ex)
                            connectInfo = {}
                        logging.debug("CONNECTINFO msg[2] = %s" % connectInfo)
                    else:
                        logging.error(
                            "Got CONNECTINFO msg of len %d, expected 3" %
                            len(msg))
                        connectInfo = {}

                    try:
                        level, index = collectState.identityDict[identity]
                        collectState.entries[level][index].update(
                            connectInfo[level])
                    except Exception as ex:
                        logging.error(ex)

                    continue

                elif request == CollectMsg.DUMP:
                    # Send reply to client
                    logging.debug("Sending DUMPSTARTED reply")
                    collectState.cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.DUMPSTARTED)
                    cmmsg.send(collectState.cmd)

                    # Dump state to console
                    print("platform:", collectState.platform())
                    print("partName:", collectState.partName())
                    print("Nodes:")
                    pprint.pprint(collectState.entries)
                    continue

                else:
                    logging.warning("Unknown msg <%s>" % request.decode())
                    # Send reply to client
                    logging.debug("Sending <HUH?> reply")
                    collectState.cmd.send(identity, zmq.SNDMORE)
                    cmmsg = CollectMsg(key=CollectMsg.HUH)
                    cmmsg.send(collectState.cmd)
                    continue

    except KeyboardInterrupt:
        logging.debug("Interrupt received")

    # Clean up
    logging.debug("Clean up")

    # terminate zmq context
    ctx.destroy()

    logging.info('collectMgr exiting')