def doSomeLogic(self): #for x in xrange(1): now = time.time() ftagent.query("GA01", "2222222222222:") spent = time.time() - now if spent > 0.1: ftlog.info("SPENT PPS...", spent) ftsvr.doRedis("user01", "SET", "zx", 1) for x in xrange(50000): x = x * 2 x = x / 2
def _communicateServer(rsrv, groupId, userId, msgpack, head1, isQuery, timeout=None, notimeoutex=0): if timeout == None: timeout = _runenv._RPC_TIME_OUT if groupId <= 0: groupId = rsrv.sididx rsrv.sididx += 1 dst = rsrv.sids[groupId % rsrv.sidlen] ftlog.debug('_communicateServer->dst=', dst, 'groupId=', userId, 'timeout=', timeout, 'msgpack=[' + msgpack + ']') if isQuery: return wrapper.query(dst, msgpack, head1, str(userId), timeout, notimeoutex=notimeoutex) else: return wrapper.send(dst, msgpack, head1, str(userId))
def _communicateTableServer(userId, roomId, msgpack, head1, isQuery, timeout=None, notimeoutex=0): if timeout == None: timeout = _runenv._RPC_TIME_OUT assert (isinstance(msgpack, (MsgPack, basestring))) assert (isinstance(roomId, (int, long)) and roomId >= 0) assert (isinstance(userId, (int, long)) and userId >= 0) allrooms = gdata.roomIdDefineMap() if roomId in allrooms: roomDef = allrooms[roomId] if roomDef.parentId == 0: ftlog.warn('ERROR, cat not localtion the roomId of->', userId, roomId, msgpack) return else: # this roomId is big roomId assert (isinstance(userId, (int, long)) and userId >= 0) bigrooms = gdata.bigRoomidsMap() if roomId in bigrooms: msgpack, shadowRoomId = __getRoomIdByTableId(msgpack) ftlog.debug('_communicateTableServer allrooms = ', allrooms, 'shadowRoomId =', shadowRoomId) if shadowRoomId: roomDef = allrooms[shadowRoomId] else: ftlog.warn('ERROR, cat not localtion the roomId of->', userId, roomId, msgpack) return else: ftlog.warn('ERROR, cat not localtion the roomId of->', userId, roomId, msgpack) return if isinstance(msgpack, MsgPack): msgpack = msgpack.pack() dst = roomDef.serverId ftlog.debug('_communicateTableServer->dst=', dst, 'head1=', head1, 'roomId=', str(roomId), 'timeout=', timeout, 'msgpack=[' + msgpack + ']') if isQuery: response = wrapper.query(dst, msgpack, head1, str(roomId), timeout, notimeoutex=notimeoutex) ftlog.debug('_communicateTableServer->dst=', dst, 'head1=', head1, 'roomId=', str(roomId), 'response=[' + str(response) + ']') return response else: return wrapper.send(dst, msgpack, head1, str(roomId))
def queryCenterServer(msgpack, logicName): """ 发送查询请求消息至CENTER服务 返回CENTER的响应消息 """ if isinstance(msgpack, MsgPack): msgpack = msgpack.pack() assert (isinstance(msgpack, basestring)) for dst, logics in gdata.centerServerLogics().items(): if logicName in logics: return wrapper.query(dst, msgpack, 'Q7', logicName, _runenv._RPC_TIME_OUT) raise Exception('the center logicName not found !!' + str(logicName))
def doSomeLogic2(self): ftlog.info("MyProto do some logic in tasklet") #test redis aio ftsvr.doRedis("user01", "SET", "zx", 1) #Testing doHttp... #code, page = ftsvr.doHttp("GET", "http://www.google.com", # {'User-Agent': ['Twisted']}) #test connect timeout #code, page = ftsvr.doHttp("GET", "http://news.sina.com.cn/", # {'User-Agent': ['Twisted']}, '', 15, 0.1) #test request timeout #page = ftsvr.doHttp("GET", "http://news.sina.com.cn/sssss", # {'User-Agent': ['Twisted']}) #test 404 code, page = ftsvr.doHttp("POST", "http://127.0.0.1:8003/", {'User-Agent': ['Twisted']}, "HAHAHA") print code, page print "HTTP", code, len(page), page log_data = struct.pack("64s", "HAHAHA") ftlog.sendHttpLog("user01", "chip", log_data) #pipeline需要加锁,否则结果可能混乱,框架中已经加了lock,参见ftsvr代码 count = 1000 pipecount = 100 st = time.time() for x in xrange(count / pipecount): if x % 100 == 0: ftlog.info(x) pl = ftsvr.getRedisPipe("user01") for y in xrange(pipecount): pl.set("zx", 1) result = ftsvr.doRedisPipe(pl) if len(result) != pipecount: ftlog.debug("ERRORPIPELINE") ftlog.info("SetPerS:", count / (time.time() - st)) #test ftagent and udpquery ftlog.debug(ftcon.getConf("server"), caller=self) ftlog.debug(ftsvr.getTaskRunArg()) ftlog.debug(ftsvr.doUdpQuery("LO01", "abcdefgh")) ftlog.debug(ftagent.query("GA01", "TCPTCPSENDBYAGENT")) #test cron _tstr = time.strftime('%Y%m%d', time.localtime()) cron2 = FTCron( '{"times_in_day":{"first":"15:00","interval":5, "count":32},\ "days":{"first":"%s", "interval":"2m", "count":10}}' % (_tstr)) ftlog.debug(cron2.getTimeList(), cron2.getDaysList()) ftlog.debug("SEC=", cron2.getTodayNextLater()) #test timer tr = FTTimer(3, self.timerFunc, 1, 2, 3, aaa=111)
def _communicateRoomServer(userId, roomId, msgpack, head1, isQuery, timeout=None, notimeoutex=0): if timeout == None: timeout = _runenv._RPC_TIME_OUT assert (isinstance(msgpack, (MsgPack, basestring))) assert (isinstance(roomId, (int, long)) and roomId >= 0) allrooms = gdata.roomIdDefineMap() if roomId in allrooms: roomDef = allrooms[roomId] if roomDef.parentId > 0: # this roomId is shadowRoomId ctrlRoomId = roomDef.parentId roomDef = allrooms[ctrlRoomId] msgpack = __changeMsgRoomId(msgpack, ctrlRoomId, roomId) else: # this roomId is big roomId assert (isinstance(userId, (int, long)) and userId >= 0) bigrooms = gdata.bigRoomidsMap() if roomId in bigrooms: ctrlroomIds = bigrooms[roomId] ctrlRoomId = ctrlroomIds[ userId % len(ctrlroomIds)] # ctrlRoom0 做为 ctrlRooms 的调度器 roomDef = allrooms[ctrlRoomId] msgpack = __changeMsgRoomId(msgpack, ctrlRoomId, roomId) else: ftlog.warn('ERROR, cat not localtion the roomId of->', userId, roomId, msgpack) return if isinstance(msgpack, MsgPack): msgpack = msgpack.pack() dst = roomDef.serverId ftlog.debug('_communicateRoomServer->dst=', dst, 'head1=', head1, 'roomId=', str(roomId), 'timeout=', timeout, 'msgpack=[' + msgpack + ']') if isQuery: response = wrapper.query(dst, msgpack, head1, str(roomId), timeout, notimeoutex=notimeoutex) ftlog.debug('_communicateRoomServer->dst=', dst, 'head1=', head1, 'roomId=', str(roomId), 'response=[' + str(response) + ']') return response else: return wrapper.send(dst, msgpack, head1, str(roomId))
def _doRemoteWrite(self, rpc, dstSid, mi, rpcid, groupVal, timeout): self._remoteDeffer = None try: rd = wrapper.query(dstSid, mi, rpcid, str(groupVal), _runenv._RPC_TIME_OUT, returnDeffer=1) rd.addCallback(self._doRemoteSuccessful) rd.addErrback(self._doRemoteError) self._remoteDeffer = rd if _DEBUG: debug('._FutureResultRemote._doRemoteWrite->', groupVal, self._rpc) except FTMsgPackException, e: self._status = FutureResult.STATUS_ERROR self._exception = e ftlog.error() self._remoteDeffer = None self._doFutureDone()
def _communicateRoomServer(userId, roomId, msgpack, head1, isQuery, timeout=None, notimeoutex=0): if timeout == None: timeout = _runenv._RPC_TIME_OUT assert (isinstance(msgpack, (MsgPack, basestring))) assert (isinstance(roomId, (int, long)) and roomId >= 0) allrooms = gdata.roomIdDefineMap() if roomId in allrooms: roomDef = allrooms[roomId] if roomDef.parentId > 0: # this roomId is shadowRoomId ctrlRoomId = roomDef.parentId roomDef = allrooms[ctrlRoomId] msgpack = __changeMsgRoomId(msgpack, ctrlRoomId, roomId) else: # this roomId is big roomId assert (isinstance(userId, (int, long)) and userId >= 0) bigrooms = gdata.bigRoomidsMap() if roomId in bigrooms: ctrlroomIds = bigrooms[roomId] ctrlRoomId = ctrlroomIds[userId % len(ctrlroomIds)] # ctrlRoom0 做为 ctrlRooms 的调度器 roomDef = allrooms[ctrlRoomId] msgpack = __changeMsgRoomId(msgpack, ctrlRoomId, roomId) else: ftlog.warn('ERROR, cat not localtion the roomId of->', userId, roomId, msgpack) return if isinstance(msgpack, MsgPack): msgpack = msgpack.pack() dst = roomDef.serverId ftlog.debug('_communicateRoomServer->dst=', dst, 'head1=', head1, 'roomId=', str(roomId), 'timeout=', timeout, 'msgpack=[' + msgpack + ']') if isQuery: response = wrapper.query(dst, msgpack, head1, str(roomId), timeout, notimeoutex=notimeoutex) ftlog.debug('_communicateRoomServer->dst=', dst, 'head1=', head1, 'roomId=', str(roomId), 'response=[' + str(response) + ']') return response else: return wrapper.send(dst, msgpack, head1, str(roomId))
def _invokeRpcMethod(markParams, argl, argd): ''' 进程内其它方法调用RPC方法的代理方法 ''' rpc = markParams['rpc'] rpcSrvType = markParams['rpcSrvType'] groupIndex = markParams['groupIndex'] future = markParams['future'] groupVal = 0 dstSid = None if rpcSrvType == RPC_FIRST_SERVERID: dstSid = argl[0] else: if groupIndex >= 0: groupVal = argl[groupIndex] if rpcSrvType == gdata.SRV_TYPE_UTIL: dstSid = _getRpcDstServerId(router._utilServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_ROOM: dstSid = getRpcDstRoomServerId(groupVal, 1) elif rpcSrvType == gdata.SRV_TYPE_TABLE: dstSid = getRpcDstRoomServerId(groupVal, 0) elif rpcSrvType == gdata.SRV_TYPE_CONN: dstSid = _getRpcDstServerId(router._connServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_HTTP: dstSid = _getRpcDstServerId(router._httpServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_ROBOT: dstSid = _getRpcDstServerId(router._robotServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_AGENT: dstSid = _getRpcDstServerId(router._agentServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_SDK_HTTP: dstSid = _getRpcDstServerId(router._sdkHttpServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_SDK_GATEWAY: dstSid = _getRpcDstServerId(router._gatewayHttpServer, groupVal) elif rpcSrvType == gdata.SRV_TYPE_CENTER: for dst, logics in gdata.centerServerLogics().items(): if groupVal in logics: dstSid = dst break if not dstSid: dstSid = router._centerServer.sids[0] if not dstSid: raise RpcException('RpcException ' + rpc + '! can not location the target server, rpcSrvType=' + str( rpcSrvType) + ' groupVal=' + str(groupVal)) rpcid = _getRpcId() if dstSid == gdata.serverId(): if markParams['syncCall']: if future: # TODO: Future RPC CALL 1 return _FutureResultLocal(markParams, argl, argd) else: ret = _invokeMethodLocked(markParams, argl, argd) return strutil.cloneData(ret) else: ftt = FTLoopTimer(0.01, 0, _invokeMethodLocked, markParams, argl, argd) ftt.start() return None mi = strutil.dumps({'cmd': _runenv._CMD_RPC_, 'rpc': rpc, 'argl': argl, 'argd': argd }) if markParams['syncCall']: if future: # TODO: Future RPC CALL 1 return _FutureResultRemote(rpc, dstSid, mi, rpcid, str(groupVal), _runenv._RPC_TIME_OUT) else: try: jstr = wrapper.query(dstSid, mi, rpcid, str(groupVal), _runenv._RPC_TIME_OUT) except FTMsgPackException, e: raise e except Exception, e: ftlog.warn('RpcException msg=', mi) raise RpcException('RpcException ' + rpc + ' ! query remote false, ' + str(e)) return _parseRpcResult(mi, jstr, rpc)