def __init__(self, host, username, password, dbname, rootcc, dbtype='mysql'): """@type rootcc: L{satella.instrumentation.CounterCollection}""" assert dbtype == 'mysql', 'I cannot support other databases!' dd = DatabaseDefinition( MySQLdb.connect, (MySQLdb.OperationalError, MySQLdb.InterfaceError), (host, username, password, dbname)) self.cp = ConnectionPool(dd) # Set up instrumentation insmgr = CounterCollection('database') self.cursors_counter = PulseCounter('cursors', resolution=60, units=u'cursors per minute', description='SQL cursors created') insmgr.add(self.cursors_counter) rootcc.add(insmgr)
def test_add_different_objects_same_names(self): insmgr = CounterCollection('test_namespace', description=u'test') ctr = NumericValueCounter('a_counter') atr = NumericValueCounter('a_counter') insmgr.add(ctr) self.assertRaises(ObjectExists, insmgr.add, atr)
def test_add_remove_collection(self): """Tests adding and removing a collection from a collection""" insmgr = CounterCollection('test_namespace') ctr = CounterCollection('nested_child') insmgr.add(ctr) self.assertRaises(ObjectExists, insmgr.add, ctr) insmgr.remove(ctr) self.assertRaises(ObjectNotExists, insmgr.remove, ctr)
def __init__(self, host, username, password, dbname, rootcc, dbtype='mysql'): """@type rootcc: L{satella.instrumentation.CounterCollection}""" assert dbtype == 'mysql', 'I cannot support other databases!' dd = DatabaseDefinition(MySQLdb.connect, (MySQLdb.OperationalError, MySQLdb.InterfaceError), (host, username, password, dbname)) self.cp = ConnectionPool(dd) # Set up instrumentation insmgr = CounterCollection('database') self.cursors_counter = PulseCounter('cursors', resolution=60, units=u'cursors per minute', description='SQL cursors created') insmgr.add(self.cursors_counter) rootcc.add(insmgr)
def __init__(self, pdbhelper, rootcc, qmangr, to_eventprocessor, cshardmgr_interface): """@type pdbhelper: L{lobbyapp.playerdb.api.PDBHelperInterface} implementation. @type qmangr: L{lobbyapp.queuemangr.QueueManager} @param to_eventprocessor: Queue that can be used to send orders to EventProcessor @type to_eventprocessor: L{Queue.Queue} @param cshardmgr_interface: interface to CShardMgr @type cshardmgr_interface: tuple(str, int)""" Monitor.__init__(self) self.pirs = {} #: pid => PlayerInformationRecord self.cshardmgr_interface = cshardmgr_interface self.pdbhelper = pdbhelper self.qmangr = qmangr self.alphas = [] # all existing AlphaMatches self.betagammas = [] # all existing BetaGammas self.to_eventprocessor = to_eventprocessor cc = CounterCollection('player_database') self.transactions_counter = PulseCounter('transactions', resolution=60, units=u'transactions per minute', description=u'player database transactions performed') alphas_waiting = CallbackCounter('alphas_waiting', self.alphas.__len__, description=u'matches being prepared') betagammas = CallbackCounter('betagammas', self.betagammas.__len__, description=u'matches being played') cc.add(self.transactions_counter) cc.add(alphas_waiting) cc.add(betagammas) rootcc.add(cc)
def __init__(self, pdb, rootcc): """ @param pdb: PlayerDatabase @param rootcc: Root CounterCollection """ BaseThread.__init__(self) self.pdb = pdb cc = CounterCollection('alpha_counter', description=u'Module that creates games from hero picking rooms') self.matches_dt = PulseCounter('matches_made', resolution=60, units=u'matches per minute', description=u'successful allocations') self.matches_dt_so = PulseCounter('matches_failed_so', resolution=60, units=u'matches per minute', description=u'fails to allocate due to shard overload') self.matches_dt_fl = PulseCounter('matches_failed_fl', resolution=60, units=u'matches per minute', description=u'fails to allocate due to other reasons') cc.add(self.matches_dt) cc.add(self.matches_dt_so) cc.add(self.matches_dt_fl) rootcc.add(cc)
def __init__(self, server_socket, to_eventprocessor, from_eventprocessor, pdbhelper, rootcc): """@type server_socket: ServerSocket @type to_eventprocessor: L{Queue.Queue} @type from_eventprocessor: L{Queue.Queue} @param rootcc: root counter collection @type rootcc: L{satella.instrumentation.CounterCollection}""" SelectHandlingLayer.__init__(self) # Set up the server socket and attach it to SL self.server_socket = server_socket self.register_channel(server_socket) # dictionary (user pids => respective PlayerSocket) self.authenticated_channels = {} # Queue section self.to_ep = to_eventprocessor self.from_ep = from_eventprocessor # PDB section self.pdbhelper = pdbhelper # Instrumentation section cc = CounterCollection('selectlayer') self.connections_counter = DeltaCounter('connections', units='connections', description='SSL connections to server') self.outbound_events = PulseCounter('events_to_ep', resolution=60, units='events per minute', description='events sent to EventProcessor') cc.add(self.connections_counter) cc.add(self.outbound_events) rootcc.add(cc)
def __init__(self, pdb, rootcc): """ @param pdb: PlayerDatabase @param rootcc: Root CounterCollection """ BaseThread.__init__(self) self.pdb = pdb cc = CounterCollection( 'alpha_counter', description=u'Module that creates games from hero picking rooms') self.matches_dt = PulseCounter('matches_made', resolution=60, units=u'matches per minute', description=u'successful allocations') self.matches_dt_so = PulseCounter( 'matches_failed_so', resolution=60, units=u'matches per minute', description=u'fails to allocate due to shard overload') self.matches_dt_fl = PulseCounter( 'matches_failed_fl', resolution=60, units=u'matches per minute', description=u'fails to allocate due to other reasons') cc.add(self.matches_dt) cc.add(self.matches_dt_so) cc.add(self.matches_dt_fl) rootcc.add(cc)
def __init__(self, qname, ppm, rootcc): """ Constructs the queue @param qname: queue name identifier @type qname: str @param ppm: Players required per match of this type @type ppm: int @param rootcc: Collection counter for queues """ self.qname = qname #: name of the queue self.players_per_match = ppm self.players = [] #: pids of players in queue # ---------------- instrumentation section cc = CounterCollection(self.qname) players_counter = CallbackCounter('players', lambda: len(self.players), description=u'players waiting in this queue') cc.add(players_counter) rootcc.add(cc)
def test_queuemanager(self): q = {'2vs2': { 'players_per_match': 4 } } rootcc = CounterCollection('test') q = QueueManager(q, rootcc) self.assertEquals(q.has_queue('2vs2'), True) self.assertEquals(q.has_queue('dogbert'), False) q.queues['2vs2'].enqueue(2) self.assertRaises(ValueError, q.queues['2vs2'].enqueue, 2)
def test_add_remove_counter(self): """Tests adding and removing a counter from a collection""" insmgr = CounterCollection('test_namespace', description=u'test') ctr = NumericValueCounter('a_counter') atr = NumericValueCounter('b_counter') insmgr.add(ctr) insmgr.add(atr) self.assertRaises(ObjectExists, insmgr.add, ctr) self.assertEquals(insmgr.description, u'test') insmgr.remove(ctr) self.assertRaises(ObjectNotExists, insmgr.remove, ctr) c_atr = insmgr.get('b_counter') self.assertEquals(c_atr, atr) self.assertRaises(ObjectNotExists, insmgr.get, 'c_counter') self.assertEquals(insmgr.get()[0], atr) self.assertEquals(len(insmgr.get()), 1)
def test_mass_disable_enable(self): """Tests enabling and disabling children from a namespace""" insmgr = CounterCollection('test_namespace') ctr = NumericValueCounter('a_counter', units=u'dogs') atr = NumericValueCounter('b_counter', description=u'hello') self.assertEquals(ctr.units, u'dogs') self.assertEquals(atr.description, u'hello') insmgr.add(ctr) insmgr.add(atr) insmgr.disable() self.assertEquals(ctr.enabled, False) self.assertEquals(atr.enabled, False) insmgr.enable() self.assertEquals(ctr.enabled, True) self.assertEquals(atr.enabled, True)
def __init__(self, pdb, rootcc): """ @param pdb: PlayerDatabase @param rootcc: Root CounterCollection """ BaseThread.__init__(self) self.pdb = pdb cc = CounterCollection('match_maker', description=u'Module that organizes matches from enqueued players') self.total_matches_made = DeltaCounter('matches_made', units=u'matches', description=u'total matches made') self.matches_dt = PulseCounter('matches_made_p', resolution=60, units=u'matches per minute', description=u'organized matches per minute') cc.add(self.total_matches_made) cc.add(self.matches_dt) rootcc.add(cc)
def __init__(self, pdbhelper, rootcc, qmangr, to_eventprocessor, cshardmgr_interface): """@type pdbhelper: L{lobbyapp.playerdb.api.PDBHelperInterface} implementation. @type qmangr: L{lobbyapp.queuemangr.QueueManager} @param to_eventprocessor: Queue that can be used to send orders to EventProcessor @type to_eventprocessor: L{Queue.Queue} @param cshardmgr_interface: interface to CShardMgr @type cshardmgr_interface: tuple(str, int)""" Monitor.__init__(self) self.pirs = {} #: pid => PlayerInformationRecord self.cshardmgr_interface = cshardmgr_interface self.pdbhelper = pdbhelper self.qmangr = qmangr self.alphas = [] # all existing AlphaMatches self.betagammas = [] # all existing BetaGammas self.to_eventprocessor = to_eventprocessor cc = CounterCollection('player_database') self.transactions_counter = PulseCounter( 'transactions', resolution=60, units=u'transactions per minute', description=u'player database transactions performed') alphas_waiting = CallbackCounter('alphas_waiting', self.alphas.__len__, description=u'matches being prepared') betagammas = CallbackCounter('betagammas', self.betagammas.__len__, description=u'matches being played') cc.add(self.transactions_counter) cc.add(alphas_waiting) cc.add(betagammas) rootcc.add(cc)
def __init__(self, config, rootcc): """ Initializes queues. @param config: a peculiar set of nested dictionaries describing target classes. See sample config.json for reference @param rootcc: root counter collection @param to_eventprocessor: queue linking to EventProcessor @type to_eventprocessor: L{Queue.Queue} """ cc = CounterCollection('queues') self.queues = {} #: dict(queue name => Queue object) for qname, qvalue in config.iteritems(): if 'players_per_match' not in qvalue: raise ValueError, 'config invalid' self.queues[qname] = Queue(qname, qvalue['players_per_match'], cc) rootcc.add(cc)
def setUp(self): # initialize cshardmgr test socket ss = socket(AF_INET, SOCK_STREAM) ss.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) ss.bind(('127.0.0.1', TESTING_PORT)) ss.listen(10) ss = ServerSocket(ss) sl = SelectLayer([ss], CounterCollection('dontcare')) class SelectLayerThread(BaseThread): def __init__(self, sl): BaseThread.__init__(self) self.sl = sl def run(self): while not self._terminating: self.sl.select() for channel in self.sl.channels: self.sl.close_channel(channel) self.slt = SelectLayerThread(sl).start()
def setUp(self): """Starts the server""" self.dummycert_context = get_dummy_cert() dncert = self.dummycert_context.__enter__() # setup server channel servsock = socket(AF_INET, SOCK_STREAM) servsock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) servsock.bind(('127.0.0.1', TESTING_PORT)) servsock.listen(10) servsock = ssl.wrap_socket(servsock, certfile=dncert) servsock = SSLServerSocket(servsock) self.servsock = servsock jsonsocket.TIME_OUT_SECONDS = 120 # restore sane defaults # our test cases monkeypatch it, so there is a need # to restore it self.events_received, self.events_sent = Queue(), Queue() do_nothing_namespace_manager = CounterCollection('test') phl = PlayersHandlingLayer(servsock, self.events_received, self.events_sent, PDBHelperStandin(), do_nothing_namespace_manager) class PHLingThread(BaseThread): def __init__(self, phl): BaseThread.__init__(self) self.phl = phl def run(self): while not self._terminating: self.phl.select() self.phlt = PHLingThread(phl) self.phlt.start()
from lobbyapp.selectlayer.api import PDBHelperInterface as SelectLayerPDBHelper from lobbyapp.playerdb.api import PDBHelperInterface as PlayerDBPDBHelper from lobbyapp.queuemangr import QueueManager, OpportunisticMatchMaker, AlphaCounter from lobbyapp.playerdb import PlayerDatabase from lobbyapp.statisticsnotary import StatisticsNotary from lobbyapp.eventprocessor import EventProcessor if __name__ == '__main__': # Perform master initialization, reading the config JSON file with open(argv[1], 'r') as k: config = json.load(k) # --------------------------------------------- Instrumentation root nsm = CounterCollection('lobbyapp') # Set up master database connection database_manager = DatabaseManager(config['sqldb_host'], config["sqldb_username"], config["sqldb_password"], config["sqldb_dbname"], nsm, dbtype='mysql') # -------------------------------------------- Socket interfacing part! # Read and initialize socket server interface server_socket = socket(AF_INET, SOCK_STREAM) server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) server_socket.bind((config['inbound_listening_interface'],