building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈...

75
Building fast, scalable game server in node.js 网网网网网网网 网网网 @ 网网网网网 @xiecc

Upload: griffin-lofthus

Post on 15-Dec-2015

371 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Building fast, scalable game server in node.js

网易杭州研究院谢骋超@ 圈圈套圈圈@xiecc

Page 2: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Introduction to pomelo

https://github.com/NetEase/pomelo

Fast, scalable, distributed game server framework in node.js

Open sourced in

2012.11.20

Page 3: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Github trending Repos---2nd day

Page 4: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Github---most popular

Page 5: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Scope of application

GameWeb, socail, mobile gameMedium size client side game

Realtime webMore scalable than other frameworks

Page 6: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

What is this lecture about How to create game using pomelo?

No

Scalable game server architectureExtensible frameworkPerformance

Page 7: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

CategoryScalabable game server

architectureExtensible game server frameworkPerformance

Page 8: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Scalability---Web and Game serverWeb server unlimited scalability

Game server

World of tanks(bigworld) : 74,536 online usersMMORPG : 7k-8k maximum

Page 9: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scale?

Long connection VS request/response

Game server and realtime web

Long connection: pull/push

Response time

Web response time : 2000ms Game 、 realtime web : 100ms

Page 10: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

How to solveNode.js to rescue

Perfect for:Fast scalable network applicationsReal-time application

Erlang and node.js

Page 11: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scaleWeb app ( not realtime ), no realtime

interactNo coordinates, no adjacencyPartition randomly, stateless

Game server, realtime interactHave coordinate, no adjacencyPartition by area, stateful

Page 12: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

How to solveParitition by area

One area in one process ( or many areas in one process )

The scalability of server is limited by process

area area2area1

Process1 Process2

Page 13: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scale

MESSAGES IN

1

MESSAGES OUT

1

Broadcast

1 1

Page 14: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scale

MESSAGES IN

2

MESSAGES OUT

4

1 2

12

Page 15: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scale

MESSAGES IN

4

MESSAGES OUT

161 4

14

11

4 4

Page 16: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scale

MESSAGES IN

100

MESSAGES OUT

100001 100

1100

11

100 100

Page 17: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scale

MESSAGES IN

1000

MESSAGES OUT

10000001 1000

11000

11

1000 1000

Page 18: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

How to solve --- broadcastAOI --- area of interested module:

pomelo-aoi

Page 19: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

How to solveSplit process, seperate load , frontend is

stateless

frontend

backend

Single Process

broadcast

Game logic

Page 20: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Why does game server not scaleTick

setInterval(tick, 100)

What does every tick do?Update every entity in the

scene(disappear , move, revive)Refresh mobDriving ai logic(monster, player)

Tick must be far less than 100ms

Page 21: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Problem of tick The entity number should be limited

Pay attention to update algorithm: AI etc.

GC, full gc should never happenV8 is good at GC when memory is under

500MMemory must be limitedTry to divide process

Multi-thread may have some logic problem node.js is single thread

Page 22: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

At last--- runtime architecture

Page 23: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Runtime architecture--lordofpomelo

Page 24: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Problem of runtime architecture?How many codes for this complicated

architecture?A lot of servers and server types, how to

manage?The server side rpc is complicated, how to

simplify? A lot of processes

How many servers do we need ?How to spot the problem on multiple

servers ? Is it too heavy, not efficient?

Page 25: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

With pomeloAchieve this architecture---almost zero

CodeServer types and servers extention ---

simpleServers invocation --- Simple , zero config,

no stubA lot of processes

One machine, small amount of resourcesSingle console , quick spot problem, no

different to single processLightweight, extremely quick to start up

Page 26: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Scalability and node.js

Node.js shine

A lot of network I/O, broadcast

Multi-process, single thread

Lightweight

Page 27: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

CategoryScalability of Game serverExtensible game server frameworkPerformance

Page 28: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Framework --- ExtensibilityDifference between framework and project

Base architecture over functionExtensible : config(DSL) , extention pointsEverything is replacable : underlying

protocal, router, application component, service, admin console

Modularize---reusable module

Pomelo specific---servers management

Page 29: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Category --- extensibilityServer abstractionApp abstractionApp extention pointModularize

Page 30: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Abstract of ServersPomelo --- distributed(multi-process) app

architecture

why?

StateWeb app---stateless , nginx or apache

handle processes

App servers interactionWeb app has not interaction

Before node.js, too heavy for multiple processes

Page 31: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Abstract of servers

frontend

frontend

backend

backend

backend

backend

forward message

push message

by channelrpc

master

Page 32: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Abstract of serversDuck type

frontend

connector

backend

area cha

t

status

Page 33: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Abstract of servers

servers

The Duck

Page 34: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Abstract of serversEasy to extend

Page 35: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Convention over configurationrpc --- based on server abstract

Page 36: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Abstract of Application We need an expressive DSL

FlexibleSupport multi-servers configSupport a lot of extention points

Json and XML does not fit our needs.

Page 37: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Application DSL

Page 38: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Application DSL --- configureMultiple server config:

app.configure(‘production|development’, function() {});

app.configure(‘production|development’, ‘chat’, function() {});

app.configure(‘production’, ‘connector|area|auth’, function(){});

Page 39: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Filterapp.filter(pomelo.filters.timout());

Page 40: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Filter

Page 41: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

RouterRoute to specific server based on session state, dynamic

app.route(‘chat’, routeUtil.chat);

Page 42: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Extensibility– request and transparent routeRequest and transparent route

Page 43: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Extensibility --- Component

app.load(component, options};

app.configure(‘production’, ‘area’, function() {

app.load(pomelo.sync, {path:__dirname, dbclient:dbclient’

});});

Page 44: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Extensibility--- App component

app

handler

rpcproxy

rpcserve

r

connector

client

remotepeer

client

client

Pomelo is a collection of components

Page 45: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Component

Page 46: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Modularize---npm module based design James Halliday(substack) --- linux

philosophy

Page 47: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Modularize---npm module based design

Pomelo

pomelo-sync

pomelo-loader

pomelo-rpc

pomelo-protocol

pomelo-monitor

pomelo-logger

pomelo-aoi

pomelo-pathfinding

pomelo-bt

pomelo-admin-web

pomelo-admin

Page 48: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Admin console

pomelopomelo-admin

pomelo-admin-web

Pomelo

Monitor logic

Master, servers

Monitor log

Rpc, request Admin console

Client js

Page 49: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Admin console extensibility

Page 50: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Extensibility ---Node.js shine again

Strong DSL ability

Dynamic , easy for COC

Module organization , npm , all components are loosely coupled

Page 51: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

CategoryScalabable game server architectureExtensible game server frameworkPerformance

Page 52: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- overviewThe indicator

The max online usersResponse time/throughputSingle area or game?

The variationGame logic: round or realtime, room or

infiniteMap size, character densityBalance of areasTest parameters: Think time, test action

Page 53: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- targetArea online users

next-gen: Socket.io: 25,000 concurrent users

But in the real worldThe real online data: maximum 1,000 concurrent

users per area, 8,000 concurrent users per group game servers

Page 54: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- toolsStress testing for websocket--pomelo-robot

master

agent agent

robot robot robot robot robot

Page 55: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- tools

Stress test console

Page 56: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- tools, profilerServer profiler, choosing servers

Page 57: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- stress testing

Stress on single area, increasing step by step

Real game logic simulationRoam, fight, pickThink time: 2s~4s

Page 58: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- hardwareCPU , 24 cores

Mem, 48G

Page 59: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- stress testing

Page 60: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- stress testing

Page 61: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- progress6 roundsOnline users: 200 to 1000…Response time: less than 200ms

Enter scene: 200msOther requests: 100ms

Page 62: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- broadcast200 online users, connector 100% cpu

Page 63: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Areaconnectorsclients

channel

uids

connector1

connector2

client1

client2

clientn

regroup

uids1

uids2

… …

broadcast

tick: 20ms

Performance --- channel, where is wrong?

tick:20ms

serializedeserialize

serialize

serialize

serialize

deserialize

Page 64: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- connectorConnector--- the middle manWhat do I need data for?

connector1

connector2

Message in

forward

Parse the route

area{route:’area.playerHandler.hello’, data:{…}}

area

forward

broadcast

Client

encode

Decode: only route

Stringify

parse

Serializedeserialize

Page 65: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- the packagePomelo-protocal

Only parse head for route information :

\0\0\0\3\34connector.loginHandler.login{“username”:”xcc”,……..}

Page 66: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance---CPU场景及出生点寻路问题怪数量及动态寻路AOI 计算大量的解压包dataApi 查找换成 MAP

Page 67: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance---IO网络传送数据路过大数据未批量发送用户断开空转数据同步日志过于频繁

Page 68: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance---Memory数据抽取模式(拉推)冗余数据去除内存泄漏及 GC

Page 69: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- the result1600 onlines

Page 70: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- the result1600 onlines , server loadIsn’t that amazing? no

Page 71: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- the result800 onlines, fight each other

Page 72: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Performance --- the resultServer load, fight each other

Page 73: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

TODOPerformance

Servers rpc , sock.io tcpNetwork protocal, json is wasteful

Fault-tolerantDifferent clients support

Page 74: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

SitesPomelo home : http://pomelo.netease.comGithub:

https://github.com/NetEase/pomeloDemo:

http://pomelo.netease.com/lordofpomeloWeibo : @pomelonode @ 圈圈套圈圈

Page 75: Building fast, scalable game server in node.js 网易杭州研究院 谢骋超 @ 圈圈套圈圈 @xiecc

Q&A