Example #1
0
def module_unpack(txn, data):
    print("data: %s" % data, end=' ')
    Logger.info('5', 'receive data: {}'.format(data.decode('UTF-8')))
    example_msg = txn.data()
    example_msg.data = data

    return len(data)
Example #2
0
def module_pack(txn: Txn, txndata: TxnData):
    """Output data pack function.

    It's called after 'module_run' function.
     the workflow (It would no effect if there is no response needed)

    @param txn  Transaction context which is used for getting shared
                transaction data or invoking `service.call()`
    @param data Input data

    @return How many bytes be consumed
    """

    logger.debug("py module pack")

    # Increase counters
    mod_metrics = metrics.module()
    mod_metrics.response.inc(1)

    # Assemble response
    if txn.status() != Txn.TXN_OK:
        txndata.append('error')
    else:
        sharedData = txn.data()
        logger.debug("pack data: %s" % sharedData.data)
        logger.info('ModulePack', 'module_pack: data sz: {}'.format(
            len(sharedData.data)))

        txndata.append(sharedData.data)
Example #3
0
def module_init(config):
    print ("py module init")

    Logger.trace('py module init: trace test')
    Logger.debug('py module init: debug test')
    Logger.info('1', 'py module init: info test')
    Logger.warn('2', 'py module init: warn test', 'no suggestion')
    Logger.error('3', 'py module init: error test', 'no solution')
    Logger.fatal('4', 'py module init: fatal test', 'no solution')
    return True
Example #4
0
def module_init(config):
    """Module Init Entry.

    It's called once when module be loaded

    @param config  A parsed yamlObj

    @return - True:  Initialization success
            - False: Initialization failure
    """

    logger.info('ModuleInit', 'config: {}'.format(pprint.pformat(config)))
    return True
Example #5
0
def module_init(config):
    logger.debug("py module init")
    logger.info('ModuleInit', 'config: {}'.format(pprint.pformat(config)))

    global CFG_MIN_TTL
    global CFG_MAX_TTL
    global CFG_LOW_LATENCY_BAR
    global CFG_LATENCY_FACTOR
    CFG_MIN_TTL = config['min_ttl']
    CFG_MAX_TTL = config['max_ttl']
    CFG_LOW_LATENCY_BAR = config['low_latency_bar']
    CFG_LATENCY_FACTOR = config['latency_factor']

    return True
Example #6
0
def _ranking_response(txn, iostatus, api_name, request_msg, response_msg):
    if iostatus != Txn.IO_OK:
        logger.error("Ranking_E2",
                     "Ranking response IO error: {}".format(iostatus))
        return False

    # print "response: {}".format(response_msg)
    sharedData = txn.data()

    # If the total records count <= 1, just store and return
    nrecords = len(response_msg.result)
    if nrecords <= 1:
        for record in response_msg.result:
            sharedData.rankingRecord.add(ip=record.ip, ttl=record.ttl)
        return True

    # Do the score and rank
    global CFG_LOW_LATENCY_BAR
    global CFG_LATENCY_FACTOR
    scoringResults = ranking.score(response_msg.result)
    rankingResults, filtered = ranking.rank(scoringResults,
                                            CFG_LOW_LATENCY_BAR,
                                            CFG_LATENCY_FACTOR)

    logger.info(
        "Ranking", "question: {} ,total: {} ,filtered {} ,Results: {}".format(
            request_msg.question, len(scoringResults), filtered,
            rankingResults))

    global CFG_MIN_TTL
    for record in rankingResults:
        # Recalculate ttl when there is no scoring information and ttl >
        # CFG_MIN_TTL
        ttl = record['ttl']
        latency = record['avgLatency']
        httpInfoCnt = record['httpInfoCnt']

        new_ttl = CFG_MIN_TTL * httpInfoCnt
        if new_ttl > 0:
            ttl = min([new_ttl, ttl])

        if latency == 0 and ttl > CFG_MIN_TTL:
            ttl = CFG_MIN_TTL
        elif ttl > CFG_MAX_TTL:
            ttl = CFG_MAX_TTL

        sharedData.rankingRecord.add(ip=record['ip'], ttl=ttl)

    return True
Example #7
0
def module_pack(txn, txndata):
    mod_metrics = Metrics.module()
    mod_metrics.response.inc(1)

    mod_dymetrics = Metrics.transaction('test')
    mod_dymetrics.response.inc(1)

    if txn.status() != Txn.TXN_OK:
        txndata.append('error')
        Logger.error('6', 'module_pack error', 'no solution')
    else:
        example_msg = txn.data()
        print("pack data: %s" % example_msg.data)
        Logger.info('7', 'module_pack: data sz: {}'.format(len(example_msg.data)))

        txndata.append(example_msg.data)
Example #8
0
def rank(scoringResults, low_latency_bar, latency_factor):
    rankingResults = []

    # 1. checking total record count
    nrecords = len(scoringResults)

    if nrecords <= 1:
        return scoringResults, 0

    # 2. Keep latency <= 20ms, otherwise filter the record shouldn't larger
    # than 1.5x of the first record
    baseRecord = None
    baseRecordLatency = 0

    for scoringRecord in scoringResults:
        latency = scoringRecord['avgLatency']

        # Add record which its latency <= 20ms
        if latency <= low_latency_bar:
            rankingResults.append(scoringRecord)
            continue
        elif baseRecord is None:
            baseRecord = scoringRecord
            baseRecordLatency = baseRecord['avgLatency']

        factor = float(latency) / float(baseRecordLatency)

        if factor <= latency_factor:
            rankingResults.append(scoringRecord)
        else:
            logger.info(
                "RankingUtil",
                "Ranking terminated at latency: {}, factor: {}, base: {}".
                format(latency, factor, baseRecordLatency))
            break

    nRankingRecords = len(rankingResults)

    return rankingResults, nrecords - nRankingRecords
Example #9
0
def module_pack(txn, txndata):
    # logger.debug("response module pack")

    sharedData = txn.data()
    req = DNSRecord.parse(sharedData.rawRequest)
    answer = req.reply()
    question = sharedData.question
    qtype = QTYPE[req.q.qtype]

    module_counter = metrics.module()
    qtype_counter = metrics.qtype(qtype)

    client = txn.client()
    peerName = client.name()
    peerPort = client.port()
    peerType = client.typeName()

    # Assemble response
    if txn.status() != Txn.TXN_OK:
        logger.error(
            'ModulePack',
            'no answer for question: {} ,Peer: {}:{} {} ,type: {}'.format(
                question, peerName, peerPort, peerType,
                qtype), 'Please check previous errors/exceptions')

        # Increase error counter
        module_counter.error.inc(1)
        qtype_counter.error.inc(1)
    else:
        # Assemble DNS response
        nRawRecords = len(sharedData.record)
        nAnswers = len(sharedData.rankingRecord)
        nFiltered = nRawRecords - nAnswers
        ips = []

        for record in sharedData.rankingRecord:
            if req.q.qtype == QTYPE.A:
                answer.add_answer(
                    RR(question,
                       req.q.qtype,
                       rdata=A(record.ip),
                       ttl=record.ttl))
            elif req.q.qtype == QTYPE.AAAA:
                answer.add_answer(
                    RR(question,
                       req.q.qtype,
                       rdata=AAAA(record.ip),
                       ttl=record.ttl))

            ips.append((record.ip, record.ttl))

        # Increase response counter
        module_counter.response.inc(1)
        module_counter.total_records.inc(nAnswers)
        module_counter.total_filtered.inc(nFiltered)

        qtype_counter.response.inc(1)
        qtype_counter.total_records.inc(nAnswers)
        qtype_counter.total_filtered.inc(nFiltered)

        duration = (time.time() - sharedData.startTime) * 1000
        logger.info(
            'ModulePack',
            'Peer: {}:{} {} ,Duration: {:.3f} ms ,filtered: {} ,Question: {} '
            ',type: {}, {} Answers: {}'.format(peerName, peerPort, peerType,
                                               duration, nFiltered, question,
                                               qtype, nAnswers, ips))

    txndata.append(bytes(answer.pack()))
Example #10
0
def module_init(config):
    logger.debug("Response module init")
    logger.info('0', 'config: {}'.format(pprint.pformat(config)))
    return True
Example #11
0
def module_init(config):
    logger.debug("dns_ask module init")
    logger.info('ModuleInit', 'config: {}'.format(pprint.pformat(config)))
    return True