0

我正在尝试构建一个没有任何 HTTPS 请求的体系结构,该请求至少需要(100 毫秒)才能将请求传递到后端服务器,所以我决定从 mongo 触发器中获取帮助,现在它需要(20 毫秒)但错过了一些请求。我有 2 个脚本

  1. 测试 Mongo 架构
  2. 监听 mongo 触发器并处理该请求

架构统计

请求数 用的时间 已处理的请求 错过的请求 docker swarm 副本
1000 949.51 996 4 1
3000 4387.24 2948 52 1
5000 10051.78 4878 122 1

工作节点代码:

from pymongo import MongoClient
import pymongo


connectionString = "mongodb://ml1:27017,ml1:27018,ml1:27019/magic_shop?replicaSet=mongodb-replicaset";
client           = MongoClient(connectionString)
db               = client.magic_shop        # test is my database
col              = db.req                   # Here spam is my collection

import socket
machine_name = socket.gethostname()
from datetime import datetime
import time

def processing_request(request):
    '''
    Description:
        Get the string, takes its length and run a loop 10 times of string length, and set the 
        response to processed.
        
    Input:
        request  (str) : Random String
        
    Output:
        Set value back to the mongo DB
            response: Processed
    '''
    for i in range(0,len(request)*10):
        pass
    
    

try:
    with db.req.watch([{'$match': {'operationType': 'insert'}}]) as stream:
        for values in stream:
            request_id = values['fullDocument']['request_id']
            request    = values['fullDocument']['request']
            myquery    = { "request_id": request_id }
            
            # Checking if not processed by other replica (Blocking System)
            if len(list(col.find({ 'request_id': request_id, 'CSR_NAME' : { "$exists": False}}))):
                newvalues  = { "$set": { "CSR_NAME": machine_name, "response": "Processing", "processing_time":datetime.today()} }

                # CSR Responding to the client
                col.update_one(myquery, newvalues)
                print(request_id)
                print(f"Processing {request_id}")
                # Now Processing Request
                processing_request(request)
                # Updating that work is done

                myquery   = { "request_id": request_id }
                newvalues = { "$set": {"response": "Request Processed Have a nice Day sir!", 'response_time':datetime.today()} }
                # CSR Responding to the client
                col.update_one(myquery, newvalues)
            
except pymongo.errors.PyMongoError:
    # The ChangeStream encountered an unrecoverable error or the
    # resume attempt failed to recreate the cursor.
    logging.error('...')

测试代码

from pymongo import MongoClient
from PIL import Image
import io, random
import matplotlib.pyplot as plt
from datetime import datetime



db        = client.magic_shop            # test is my database
col       = db.req                       # Here spam is my collection
brust     = 5000
count     = 0 

for i in range(0, brust):
    random_id = int(random.random()*100000)
    image= {
                'request_id': random_id,
                'request': random.choice(requests),
                'request_time':datetime.today()
                }

    image_id = col.insert_one(image).inserted_id
    count+=1
    
    
print(f"Request Done {count}")

也可以在 Github 上获取完整代码

https://github.com/SohaibAnwaar/Docker-swarm

4

0 回答 0