Пример #1
0
def recalculate_mn_chunk_ranking_table():
    """
    This method recalculates all ranks of masternodes for each chunk. Is tend to be slow (if the number of chunks
    and masternode will be big), so it needs to be called only when new masternode is added.
    There is a sense to limit frequence of calls (say, no more then once a minute or so).
    """
    # calculate each masternode rank for each chunk
    tasks_logger.info(
        'ChunkMnRanked table has {} record. Recalculating...'.format(
            ChunkMnRanked.select().count()))
    subquery = '''
    select chunk_id, masternode_id, row_number() 
    over (partition by chunk_id order by distance asc) as r 
    from chunkmndistance
    '''

    # leave only top `Settings.REPLICATION_FACTOR` masternodes (which are considered as chunk owners).
    sql = '''select chunk_id, masternode_id, r from ({}) as t where t.r<={}'''.format(
        subquery, Settings.REPLICATION_FACTOR)

    # delete old rows
    ChunkMnRanked.delete().execute()

    # insert (chunk, masternode, rank) for all chunk-owners in a separate table for convinience
    insert_sql = '''insert into chunkmnranked (chunk_id, masternode_id, rank) {}'''.format(
        sql)
    MASTERNODE_DB.execute_sql(insert_sql)
    tasks_logger.info('...Done. Now here are {} records'.format(
        ChunkMnRanked.select().count()))
Пример #2
0
 def __receive_rpc_sql(self, sql, *args, **kwargs):
     self.__logger.info('SQL request received')
     if not isinstance(sql, str):
         raise TypeError("SQL must be a string!")
     from core_modules.database import MASTERNODE_DB
     c = MASTERNODE_DB.execute_sql(sql)
     r = c.fetchall()
     result = []
     fields = [x[0] for x in c.description]
     for record in r:
         dict_record = dict()
         for i in range(len(record)):
             dict_record[fields[i]] = record[i]
         result.append(dict_record)
     return {"result": result}