Exemple #1
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('-C',
                        metavar='CM_HOST',
                        default='localhost',
                        help='Collection Manager host')
    args = parser.parse_args()

    # Prepare our context and DEALER socket
    ctx = zmq.Context()
    cmd = ctx.socket(zmq.DEALER)
    cmd.linger = 0
    cmd.RCVTIMEO = 5000  # in milliseconds
    cmd.connect("tcp://%s:%d" % (args.C, CollectMsg.router_port(args.p)))

    # Initiate partition kill
    cmd.send(CollectMsg.KILL)

    try:
        cmmsg = CollectMsg.recv(cmd)
    except Exception as ex:
        print('CollectMsg.recv() exception: %s' % ex)
    else:
        print("Received \"%s\"" % cmmsg.key.decode())
Exemple #2
0
 def platEnter(self, identity):
     # Reset the state to avoid duplicates in case PLAT is repeated
     self.reset()
     # Send PLAT broadcast
     logging.debug("Sending PLAT broadcast")
     platmsg = CollectMsg(key=CollectMsg.PLAT)
     platmsg.send(self.publisher)
     return
Exemple #3
0
 def connectEnter(self, identity):
     # Send CONNECT individually
     logging.debug("Sending CONNECT individually")
     connectMsg = CollectMsg(key=CollectMsg.CONNECT,
                             body=json.dumps(self.entries))
     for key in self.identityDict.keys():
         self.cmd.send(key, zmq.SNDMORE)
         connectMsg.send(self.cmd)
         logging.debug("...sent CONNECT")
     return
Exemple #4
0
    def getMsg(self, msg_type):
        while True:
            items = dict(self.poller.poll(1000))

            # Collection Mgr client: DEALER socket
            if self.dealer_socket in items:
                cmmsg = CollectMsg.recv(self.dealer_socket)
                if cmmsg.key == msg_type:
                    return cmmsg

            # Collection Mgr client: SUB socket
            if self.sub_socket in items:
                cmmsg = CollectMsg.recv(self.sub_socket)
                if cmmsg.key == msg_type:
                    return cmmsg
Exemple #5
0
 def allocEnter(self, identity):
     logging.debug("Sending ALLOC individually")
     for level, nodes in self.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 self.identityDict.items():
                 if item == who:
                     break
                 # should raise error if not found
             body = copy.copy(self.entries)
             body['id'] = nodeid
             allocMsg = CollectMsg(key=CollectMsg.ALLOC,
                                   body=json.dumps(body))
             # perhaps ideally next two lines should be:
             # cmd.send_multipart([identityKey, CollectMsg.ALLOC, json.dumps(body)])
             self.cmd.send(identityKey, zmq.SNDMORE)
             allocMsg.send(self.cmd)
             logging.debug("...sent ALLOC")
     return
Exemple #6
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 #7
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 #8
0
def main():

    # Define commands
    command_dict = {
        'plat': CollectMsg.PLAT,
        'alloc': CollectMsg.ALLOC,
        'connect': CollectMsg.CONNECT,
        'dump': CollectMsg.DUMP,
        'die': CollectMsg.DIE,
        'kill': CollectMsg.KILL,
        'getstate': CollectMsg.GETSTATE
    }

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('command', choices=command_dict.keys())
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform (default 0)')
    parser.add_argument('-C',
                        metavar='COLLECT_HOST',
                        default='localhost',
                        help='collection host (default localhost)')
    parser.add_argument('-P',
                        metavar='PARTITION',
                        default='AMO',
                        help='partition name (default AMO)')
    parser.add_argument('-v', action='store_true', help='be verbose')
    args = parser.parse_args()

    # Prepare our context and DEALER socket
    ctx = zmq.Context()
    cmd_socket = ctx.socket(zmq.DEALER)
    cmd_socket.linger = 0
    cmd_socket.RCVTIMEO = 5000  # in milliseconds
    cmd_socket.connect("tcp://%s:%d" %
                       (args.C, CollectMsg.router_port(args.p)))

    # Compose message
    newmsg = CollectMsg(key=command_dict[args.command])
    #   newmsg['partName'] = args.P
    #   newmsg['platform'] = ('%d' % args.p)

    # Send message
    newmsg.send(cmd_socket)

    # Receive reply
    try:
        cmmsg = CollectMsg.recv(cmd_socket)
    except Exception as ex:
        print(ex)
    else:
        print("Received \"%s\"" % cmmsg.key.decode())

        if (cmmsg.body is not None) and (len(cmmsg.body) > 2):
            # JSON body
            try:
                entries = json.loads(cmmsg.body)
            except Exception as ex:
                print('E: json.loads(): %s' % ex)
            else:
                if args.v:
                    print('Entries:')
                    pprint.pprint(entries)
                # print any error messages found
                for level, nodes in entries.items():
                    for nodeid, node in enumerate(nodes):
                        try:
                            msg = node['errorInfo']['msg']
                        except KeyError:
                            pass
                        else:
                            print('%s%d: %s' % (level, nodeid, msg))
    return
Exemple #9
0
def main():

    # Define commands
    command_dict = {
        'plat': CollectMsg.STARTPLAT,
        'alloc': CollectMsg.STARTALLOC,
        'connect': CollectMsg.STARTCONNECT,
        'dump': CollectMsg.STARTDUMP,
        'die': CollectMsg.STARTDIE,
        'kill': CollectMsg.STARTKILL,
        'getstate': CollectMsg.GETSTATE
    }

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('command', choices=command_dict.keys())
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform (default 0)')
    parser.add_argument('-C',
                        metavar='COLLECT_HOST',
                        default='localhost',
                        help='collection host (default localhost)')
    parser.add_argument('-P',
                        metavar='PARTITION',
                        default='AMO',
                        help='partition name (default AMO)')
    args = parser.parse_args()

    # Prepare our context and DEALER socket
    ctx = zmq.Context()
    cmd_socket = ctx.socket(zmq.DEALER)
    cmd_socket.linger = 0
    cmd_socket.RCVTIMEO = 5000  # in milliseconds
    cmd_socket.connect("tcp://%s:%d" %
                       (args.C, CollectMsg.router_port(args.p)))

    # Compose message
    newmsg = CollectMsg(key=command_dict[args.command])
    #   newmsg['partName'] = args.P
    #   newmsg['platform'] = ('%d' % args.p)

    # Send message
    newmsg.send(cmd_socket)

    # Receive reply
    try:
        cmmsg = CollectMsg.recv(cmd_socket)
    except Exception as ex:
        print(ex)
    else:
        print("Received \"%s\"" % cmmsg.key.decode())

        if cmmsg.key == CollectMsg.STATE:
            # nodes
            try:
                nodes = json.loads(cmmsg.body)
            except Exception as ex:
                print('E: json.loads()', ex)
            else:
                print('Nodes:')
                pprint.pprint(nodes)

    return
Exemple #10
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 #11
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('-C',
                        metavar='CM_HOST',
                        default='localhost',
                        help='Collection Manager host')
    parser.add_argument('--noheader',
                        action='store_true',
                        help='do not print header')
    args = parser.parse_args()

    # Prepare our context and DEALER socket
    ctx = zmq.Context()
    cmd = ctx.socket(zmq.DEALER)
    cmd.linger = 0
    cmd.RCVTIMEO = 5000  # in milliseconds
    cmd.connect("tcp://%s:%d" % (args.C, CollectMsg.router_port(args.p)))

    cmd.send(CollectMsg.GETSTATE)
    while True:
        try:
            msg = CollectMsg.recv(cmd)
        except Exception as ex:
            print(ex)
            return

        request = msg.key
        if request in [
                CollectMsg.NOPLAT, CollectMsg.PLAT, CollectMsg.ALLOC,
                CollectMsg.CONNECT
        ]:
            # platform FIXME
            platform = '0'

            # partition name FIXME
            partName = '(None)'

            # nodes
            nodes = []
            try:
                nodes = json.loads(msg.body)
            except Exception as ex:
                print('E: json.loads()', ex)
                nodes = []
            displayList = []

            for level, nodelist in nodes.items():
                for node in nodelist:
                    try:
                        host = node['procInfo']['host']
                    except KeyError:
                        host = '(Unknown)'
                    try:
                        pid = node['procInfo']['pid']
                    except KeyError:
                        pid = 0
                    display = "%s/%s/%-16s" % (level, pid, host)
                    if level == 'control':
                        # show control level first
                        displayList.insert(0, display)
                    else:
                        displayList.append(display)

            if not args.noheader:
                print("Platform | Partition      |    Node")
                print("         | id/name        | level/pid/host")
                print(
                    "---------+----------------+------------------------------------"
                )
            print("  %3s     %2s/%-12s" % (platform, platform, partName),
                  end='')
            firstLine = True
            for nn in displayList:
                if firstLine:
                    print("  ", nn)
                    firstLine = False
                else:
                    print("                           ", nn)
            if firstLine:
                print()
            break  # Done
        else:
            print("W: Received key \"%s\"" % request)
            continue
Exemple #12
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')
Exemple #13
0
def main():
    global verbose

    # Define commands
    command_dict = {
        'configure': Transition.configure,
        'beginrun': Transition.beginrun,
        'enable': Transition.enable,
        'disable': Transition.disable,
        'endrun': Transition.endrun,
        'unconfigure': Transition.unconfigure,
        'getstate': ControlMsg.GETSTATE
    }

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('command', choices=command_dict.keys())
    parser.add_argument('-p',
                        type=int,
                        choices=range(0, 8),
                        default=0,
                        help='platform')
    parser.add_argument('-C',
                        metavar='COLLECT_HOST',
                        default='localhost',
                        help='collection host')
    parser.add_argument('-v', action='store_true', help='be verbose')
    args = parser.parse_args()
    verbose = args.v

    # Prepare our context and collection mgr DEALER socket
    ctx = zmq.Context()
    collect_dealer_socket = ctx.socket(zmq.DEALER)
    collect_dealer_socket.linger = 0
    collect_dealer_socket.RCVTIMEO = 5000  # in milliseconds
    collect_dealer_socket.connect("tcp://%s:%d" %
                                  (args.C, CollectMsg.router_port(args.p)))

    # Send GETSTATE command to collection mgr
    collect_dealer_socket.send(CollectMsg.GETSTATE)

    # Receive reply
    host = None
    try:
        collectMsg = CollectMsg.recv(collect_dealer_socket)
    except Exception as ex:
        print("CollectMsg.recv", ex)
    else:
        if verbose:
            print("Received \"%s\"" % collectMsg.key.decode())
        # nodes
        try:
            host, router_port, pull_port = getControlPorts(collectMsg.body)
        except KeyError as ex:
            print("KeyError:", ex)
        except Exception as ex:
            print("Exception:", ex)

    # Close zmq socket
    collect_dealer_socket.close()

    if host is None:
        print("Failed to find control level ports in collection")
        return
    else:
        if verbose:
            print("Control level: host=%s  router_port=%d  pull_port=%d" %
                  (host, router_port, pull_port))

        # Prepare our control level DEALER socket
        control_dealer_socket = ctx.socket(zmq.DEALER)
        control_dealer_socket.linger = 0
        control_dealer_socket.RCVTIMEO = 5000  # in milliseconds
        control_dealer_socket.connect("tcp://%s:%d" % (host, router_port))

        # Send command
        control_dealer_socket.send(command_dict[args.command])

        # Receive reply
        try:
            controlMsg = ControlMsg.recv(control_dealer_socket)
        except Exception as ex:
            print("Exception:", ex)
        else:
            print("Received \"%s\"" % controlMsg.key.decode())

        # Close zmq socket
        control_dealer_socket.close()

    return
Exemple #14
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 #15
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 #16
0
def main():

    # Process arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('pvbase', help='EPICS PV base (e.g. DAQ:LAB2:PART:2)')
    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('-u',
                        metavar='UNIQUE_ID',
                        default='control',
                        help='Name')
    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('control level starting')

    ctx = zmq.Context()

    coll = Collection(ctx, args.C, args.p)

    pybody = {}
    pybody['host'] = gethostname()
    pybody['pid'] = getpid()
    idbody = {}
    idbody['procInfo'] = pybody
    mainbody = {}
    mainbody['control'] = idbody
    hellomsg = CollectMsg(key=CollectMsg.HELLO, body=json.dumps(mainbody))
    partition = coll.partitionInfo(hellomsg)
    pprint.pprint(json.loads(partition.body))

    # set up our end of connections, potentially based on the information
    # about who is in the partition (e.g. number of eb/drp nodes)
    # control sockets (ephemeral ports)
    control_router_socket = ctx.socket(zmq.ROUTER)
    control_pull_socket = ctx.socket(zmq.PULL)
    control_router_socket.bind("tcp://*:*")
    control_pull_socket.bind("tcp://*:*")
    control_router_port = Collection.parse_port(
        control_router_socket.getsockopt(zmq.LAST_ENDPOINT))
    control_pull_port = Collection.parse_port(
        control_pull_socket.getsockopt(zmq.LAST_ENDPOINT))
    logging.debug('control_router_port = %d' % control_router_port)
    logging.debug('control_pull_port = %d' % control_pull_port)

    pybody = {}
    pybody['router_port'] = {
        'adrs': gethostname(),
        'port': control_router_port
    }
    pybody['pull_port'] = {'adrs': gethostname(), 'port': control_pull_port}
    connbody = {}
    connbody['connectInfo'] = pybody
    mainbody = {}
    mainbody['control'] = connbody

    portsmsg = CollectMsg(key=CollectMsg.CONNECTINFO,
                          body=json.dumps(mainbody))
    connect_info = coll.connectionInfo(portsmsg)
    pprint.pprint(json.loads(connect_info.body))

    # now make the connections and report to CM when done

    # Control state
    yy = ControlStateMachine(args.pvbase)
    logging.debug("ControlStateMachine state: %s" % yy.state())

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

            # Handle control_pull_socket socket
            if control_pull_socket in items:
                msg = control_pull_socket.recv()
                config = dgram.Dgram(view=msg)
                # now it's in dgram.Dgram object
                ttt = config.seq.timestamp()
                print('Timestamp:', ttt)  # FIXME

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

                if request == ControlMsg.PING:
                    # Send reply to client
                    logging.debug("Sending <PONG> reply")
                    control_router_socket.send(identity, zmq.SNDMORE)
                    cmmsg = ControlMsg(key=ControlMsg.PONG)
                    cmmsg.send(control_router_socket)
                    continue

                if request == ControlMsg.PONG:
                    continue

                if request in [
                        Transition.configure, Transition.beginrun,
                        Transition.enable, Transition.disable,
                        Transition.endrun, Transition.unconfigure,
                        ControlMsg.GETSTATE
                ]:

                    if request != ControlMsg.GETSTATE:
                        oldstate = yy.state()
                        # Do transition
                        yy.on_transition(request)
                        newstate = yy.state()
                        if newstate != oldstate:
                            logging.debug("ControlStateMachine state: %s" %
                                          newstate)

                    # Send reply to client
                    control_router_socket.send(identity, zmq.SNDMORE)
                    cmmsg = ControlMsg(key=yy.state().key())
                    cmmsg.send(control_router_socket)
                    continue

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

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

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

    # Close all sockets associated with this context, and then
    # terminate the context.
    ctx.destroy(0)

    logging.info('control level exiting')
Exemple #17
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('-C',
                        metavar='CM_HOST',
                        default='localhost',
                        help='Collection Manager host')
    parser.add_argument('--noheader',
                        action='store_true',
                        help='do not print header')
    args = parser.parse_args()

    # Prepare our context and DEALER socket
    ctx = zmq.Context()
    cmd = ctx.socket(zmq.DEALER)
    cmd.linger = 0
    cmd.RCVTIMEO = 5000  # in milliseconds
    cmd.connect("tcp://%s:%d" % (args.C, CollectMsg.router_port(args.p)))

    cmd.send(CollectMsg.GETSTATE)
    while True:
        try:
            msg = CollectMsg.recv(cmd)
        except Exception as ex:
            print(ex)
            return

        request = msg.key
        if request == CollectMsg.STATE:
            props = msg.properties

            # platform
            platform = '0'
            try:
                platform = props['platform']
            except KeyError:
                print('E: platform key not found')

            # partition name
            partName = '(None)'
            try:
                partName = props['partName']
            except KeyError:
                print('E: partName key not found')

            # nodes
            nodes = []
            try:
                nodes = json.loads(msg.body)
            except Exception as ex:
                print('E: json.loads()', ex)
                nodes = []
            displayList = []
            for nn in nodes:
                try:
                    level = nn['level']
                except KeyError:
                    print('E: level key not found')
                    level = 0
                try:
                    pid = nn['pid']
                except KeyError:
                    print('E: pid key not found')
                    pid = 0
                try:
                    ip = nn['ip']
                except KeyError:
                    print('E: ip key not found')
                    pid = '0.0.0.0'
                try:
                    portDisplay = "%s" % nn['ports']
                except KeyError:
                    portDisplay = ""
                display = "%s/%s/%-16s  %s" % (level, pid, ip, portDisplay)
                displayList.append(display)

            if not args.noheader:
                print(
                    "Platform | Partition      |    Node                 | Ports"
                )
                print("         | id/name        |  level/ pid /    ip     |")
                print(
                    "---------+----------------+-------------------------+----------"
                )
            print("  %3s     %2s/%-12s" % (platform, platform, partName),
                  end='')
            firstLine = True
            for nn in sorted(displayList):
                if firstLine:
                    print("  ", nn)
                    firstLine = False
                else:
                    print("                           ", nn)
            if firstLine:
                print()
            break  # Done
        else:
            print("W: Received key \"%s\"" % request)
            continue
Exemple #18
0
 def killBefore(self, identity):
     # Send KILL broadcast
     logging.debug("Sending KILL broadcast")
     cmmsg = CollectMsg(key=CollectMsg.KILL)
     cmmsg.send(self.publisher)
     return