65
Believe in Erlang in Games!!! Noah Gift Mario Izquierdo James Mayfield High Level Overview of Erlang in Game Backends

Erlang factory slides

Embed Size (px)

Citation preview

Page 1: Erlang factory slides

Believe in Erlang in Games!!!

Noah Gift

Mario IzquierdoJames Mayfield

High Level Overview of Erlang in Game Backends

Page 2: Erlang factory slides

Overview of Our Talk: 3 for 1 Special

● Advocating Erlang in A Game Company: Noah Gift

● RabbitMQ in DIO: Mario Izquierdo● Versu an AI Game Evolution: James

Mayfield

Page 3: Erlang factory slides

You Can Get Fired For Using Erlang

● Erlang is "functional" and this is "complicated" for "regular" programmers

● It has been around for too long

● It is not Java, or Python or Ruby or C++ or "X".

● Your boss is an idiot who can't code like this guy???

Page 4: Erlang factory slides

Always Be Delivering

● On day 1 at work don't suggest rewriting the billing systems in Haskell

● Show up early, stay late, underpromise and overdeliver. Rinse and Repeat * N

● Then suggest Erlang on a small project...

Page 5: Erlang factory slides

Ubiquitous Multiprocessing in Erlang: Not TBD...

● PyCon 2013 Keynote: Async Multiprocessing...TBD

● Event Loops in Ruby, Node and Python can be simple at first, then exponentially difficult

Page 6: Erlang factory slides

More on Event Loops in Scripting Languages

● What do you do when your one processor gets hot and starts to consume a lot of CPU?

● Start to write Erlang in a scripting language on a tight deadline?

● Amdah's Law (sucks)● Energy Efficiency

Page 7: Erlang factory slides

More on Event Loops in Scripting Languages

● Maybe you simply use RabbitMQ to dance around it...notice how the "X" language's largest website always has a secret ingredient...(hint not their language)

● Erlang (RabbitMQ): script language crack.

Page 8: Erlang factory slides

RabbitMQin dio

Mario Izquierdo

Erlang can help you even if you don't know erlang!

Page 9: Erlang factory slides

What is RabbitMQ?

Page 10: Erlang factory slides

What is RabbitMQ?

● A service for messaging that implements the AMQP protocol

● Erlang nodes running Erlang applications (so it must be good)

Think of it as a service (like MySQL), where you can:● Open a connection● Define Exchanges, Queues and Bindings

(instead of tables and indexes)● Send and Receive Messages

(instead of reading/adding records)

Page 11: Erlang factory slides

Queues and Consumers

queue

ConsumerConsumer

Consumer

Round Robin Deliver

subscribe

Page 12: Erlang factory slides

Exchanges and Producers

Exchange Exchange

Producer Producer

Producer

Publishto any exchange

Page 13: Erlang factory slides

Bindings and Routing

Consumer

Producer Consumer

Consumer

Exchange

queue

queue

Publish

SubscribeBindrouting rules

Page 14: Erlang factory slides

Types of Exchanges and bindings

Directuses routing_key

Fanoutbroadcasts to all

Exchange Exchange

queue queue

catdog

dog

dog

queue queue

Page 15: Erlang factory slides

Typical usage of RabbitMQ

● Connect with RabbitMQ● Define an exchange

○ Start publishing messages● Define a queue and its bindings

○ Subscribe to the queue and listen for incoming messages

● Add more consumers (workers) to the same queue for load balancing

● Connect applications written in different languages

● "Send and forget", i.e. logging

Page 16: Erlang factory slides

Real usage example: dio.com

dio is a web app built on ruby, optimized to support multiplayer experiences in real time.

A place (world) instance runs in memory on one of the available GameServers.

Page 17: Erlang factory slides

Dio Architecture

● Browsers send HTTP requests to Rails and Connection Servers

● Connection Servers use HTML5 EventSource for push notifications

● Connection Servers send in-game messages to world instances that are running in Game Servers

● Connection and Game servers need to talk to each other constantly, and they need to scale

Page 18: Erlang factory slides

Dio before RabbitMQ

Game Server

Connection Server

Rails Server

Browser

AMQP

Dio Broker

Browser Browser

Rails Server

Workers

Game Server

Connection Server

Game Server

NGINX

Zmq

WebAPI

Push Notifications

http

Page 19: Erlang factory slides

Dio before RabbitMQ

Game Server

Connection Server

Rails Server

Browser

AMQP

Dio Broker

Browser Browser

Rails Server

Workers

Game Server

Connection Server

Game Server

NGINX

Zmq

RabbitMQ is used only for Chat

Knows about all GameServers and worlds, decides about where to run the next world. Receives GameServers Heartbeats.Routes Connection Server requests to the right GameServer.

Instance 0 used only for WebAPI

Keep a ZeroMQ socket for each GameServer, and other for the Broker. EventSource with the Browsers.

Rails and workers can not talk with

other components

Push Notifications

http

Relies in ZeroMQ to send messages

Page 20: Erlang factory slides

Dio before RabbitMQ

Game Server

Connection Server

Rails Server

Browser

AMQP

Dio Broker

Browser Browser

Rails Server

Workers

Game Server

Connection Server

Game Server

NGINX

http

Relies in ZeroMQ to send messages

But it's ok, omnipresent and omnipotent Broker will keep the evil away,right?

GOD

notice our friend was already here

Integrate other systems is really hard

Page 21: Erlang factory slides

Pentagram Antipattern

Build a complex system with a lot of interdependencies and you will cast the devil● Dependence on black magic● Dependence on priests● Multiple global-aware Gods

Fix it with SCIENCE!● Study the problem● Burn Churches● Use the right tool for the right problem

Page 22: Erlang factory slides

Dio before RabbitMQ: Problems● ZeroMQ has bad support in ruby, the libraries are outdated.● ZeroMQ sockets are very low level, actually they are a very weird mix of

a low level library with black magic, they are never disconnected.● Too many sockets: it needs an all-to-all socket connections between all

game servers, connection servers and broker (Pentagram).● Hard to scale● Limited communications: for example if we want to send a message

from Rails to a Game Server, or from a Worker to a ConnectionServer. Too hard that seems impossible, so we end up doing hacks like short polling mongodb or adding HTTP methods in the ConnectionServers (Gandalf).

● Broker is too complex. It is needed for routing and also to decide where to run new world instances. Needs to know about every GameServer and needs to be in sync, which makes it very error prone and unstable (GOD)

Page 23: Erlang factory slides

Game Server

Connection Server

Browser

Rails Server

AMQP

NGINXPush Notifi.

Connection Server

Rails Server

Game Server

Browser

http

Browser

Workers

Dio with RabbitMQ

Page 24: Erlang factory slides

Game Server

Connection Server

Browser

Rails Server

AMQP

NGINXPush Notifi.

Connection Server

Rails Server

Game Server

Browser

http

Holds the connection with the browsers to push notifications.Also works as ClientProxy

HTML and JavaScript client.Send messages through HTTP.Receive messages through HTML5 SSE.

Browser

Serves HTML and assets.

Authentication.Authorization.

Pages outside stories.Not a RabbitMQ

consumer.

Run world instances (several instances on each GameServer), in

real time, in memory.

Subscribes to RabbitMQ by world_id channel.

Publish to avatar_id channel

RabbitMQ exchanges and queues.

Used for messaging, load balance world instances in the GameServers and allows any component to talk with any other component.

For example send a push notification to the browser from a Sidekiq worker.

Workers

Dio with RabbitMQ

Page 25: Erlang factory slides

Game Server

Connection Server

Browser

Rails Server

AMQP

NGINXPush Notifi.

Connection Server

Rails Server

Game Server

Browser

http

Browser

Workers

Dio with RabbitMQ

Dio Broker

GO TO HELL

GOD

Page 26: Erlang factory slides

RabbitMQ

Who would you prefer to worship?

ARCANE GODS

defined by some dudesin a reactive way

to remove the fearsof the people of their time

Dio Broker

RabbitMQ GOD

supported by a big community,widely used,

known practices,and written in erlang

Page 27: Erlang factory slides

Good choice

RabbitMQ

RabbitMQ ROCKS

ME TOO

AGREE

Page 28: Erlang factory slides

Dio with RabbitMQ: Advantages● Consistency: AMQP is going to be the protocol for messaging.● Flexibility: is easy to add more components when needed, and they instantly are connected

with any other component.● Visibility: RabbitMQ is more easy to monitor than a lot bunch of sockets.● Less restrictions: Any system can be a producer and send messages to RabbitMQ, so if we

need to send a message to a world from Rails is easy, and if we need to send a push notification to the user from rails or from a worker is also easy.

● Support: RabbitMQ has much better support in ruby than ZeroMQ.● More Features: RabbitMQ can accomplish a lot more. ZeroMQ is like a simple abstraction over

sockets whether RabbitMQ is a full messaging system (similar to compare the file system with a database).

● Routing out of the box: for example a Game Server can subscribe for messages to the words that are running on it, and the Connection Server just publishes to that world, no need to know what Game Server is running that world.

● No complex Broker and automatic load balancing: We can use RabbitMQ queues to round-robin new worlds, and the Game Servers can subscribe/unsubscribe to that queue when they are available or overloaded. So they don't need to know about other Game Servers.

Page 29: Erlang factory slides

Game Server

Connection Server

AMQP

Connection Server

Game Server

Browser

Rails Server

NGINX

Rails Server

Browser

http

Browser

Workers

Messagingexamples

Page 30: Erlang factory slides

Game Messages

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av2

Av3

Av1

Av2 Av3

world_instances.direct

World1

Av1

Page 31: Erlang factory slides

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av1Av3

Av1

Av2Av3

world_instances.direct

World1

Av2

non_loaded_worlds.fanout REDIS

loading_worldlocks

Load a world instance (1)

Page 32: Erlang factory slides

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av1Av3

Av1

Av2 Av3

world_instances.direct

World1

Av2

non_loaded_worlds.fanout REDIS

loading_worldlocks

Load a world instance (2)

Av4

wants to play inWorld2

Av4

Page 33: Erlang factory slides

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av1Av3

Av3

world_instances.direct

World1

Av2

non_loaded_worlds.fanout REDIS

loading_worldlocks

Load a world instance (3)

Av4

connect world 2

Message is returned because can not route to World2

Av1

Av2

Av4

Page 34: Erlang factory slides

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av1Av3

Av3

world_instances.direct

World1

Av2

non_loaded_worlds.fanout REDIS

loading_worldlocks

Load a world instance (4)

Av4connect

Av1

Av2

Av4

connect

connect

round robin

Page 35: Erlang factory slides

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av1Av3

Av3

world_instances.direct

World1

Av2

non_loaded_worlds.fanout REDIS

loading_worldlocks

Load a world instance (5)

Av4

Av1

Av2

Av4

World 2

World2

World 2

Page 36: Erlang factory slides

Game Server

Connection Server

Connection Server

avatar_clients.direct

Game Server

World1

Av1Av3

Av3

world_instances.direct

World1

Av2

non_loaded_worlds.fanout REDIS

loading_worldlocks

Load a world instance (6)

Av4

Av1

Av2

Av4

World2

World 2

connect ACK

Page 37: Erlang factory slides

Thanks

Questions for the endHave fun!

Page 38: Erlang factory slides

Versu: Evolution of an Erlang Project

James Mayfield

Erlang can help you even if you don't know erlang!

Page 39: Erlang factory slides

What is Versu?

Page 40: Erlang factory slides

What is Versu?

"Versu is an interactive storytelling platform that builds experiences around characters and social interaction"

Page 41: Erlang factory slides

What is Versu?

● Simulator

Page 42: Erlang factory slides

What is Versu?

● Simulator○ Written in C

Page 43: Erlang factory slides

What is Versu?

● Simulator○ Written in C○ Robust, but can crash (content errors)

Page 44: Erlang factory slides

What is Versu?

● "Business Logic"

Page 45: Erlang factory slides

What is Versu?

● "Business Logic"○ Talks to C process via ports

Page 46: Erlang factory slides

What is Versu?

● "Business Logic"○ Talks to C process via ports○ Tracks players and games

Page 47: Erlang factory slides

What is Versu?

● "Business Logic"○ Talks to C process via ports○ Tracks players and games○ Some data transformation of simulator I/O

Page 48: Erlang factory slides

What is Versu?

● Web App

tm

Page 49: Erlang factory slides

What is Versu?

● Web App○ Expose Rest-like interface to simulator functions

tm

Page 50: Erlang factory slides

What is Versu?

● Web App○ Expose Rest-like interface to simulator functions○ UI

tm

Page 51: Erlang factory slides

What is Versu?

● Data Store

Page 52: Erlang factory slides

What is Versu?

● Data Store○ Saved games

Page 53: Erlang factory slides

What is Versu?

● Data Store○ Saved games○ User generated content

Page 54: Erlang factory slides

The Evolution of Versu

The early days...

Page 55: Erlang factory slides

The Evolution of Versu

The early days...

Process SimulatorPort

Misultin

Process

Page 56: Erlang factory slides

The Evolution of Versu

First Steps...

Supervisor

Game Logic Simulator Interface

Simulator

Cowboy

Page 57: Erlang factory slides

The Evolution of Versu

Many sims per node

Supervisor

Supervisor Port Owner

Sim..

Supervisor

Game Logic

Simulator Interface

Supervisor

Supervisor Port Owner

Sim..

Game Logic

Simulator Interface

Cowboy

Page 58: Erlang factory slides

Increased Awesomeness

The Evolution of Versu

S

S WC

S

W W

S

S WC

W W

Erlang VM Node

S

S WC

S

W W

S

S WC

W W

Erlang VM NodeYeeHaw YeeHaw

Load balancer

Page 59: Erlang factory slides

More Increased Awesomeness

The Evolution of Versu

S

S WC

S

W W

S

S WC

W W

Erlang VM Node

S

S WC

S

W W

S

S WC

W W

Erlang VM NodeYeeHaw YeeHaw

Load balancerRiak

Riak Riak

Page 60: Erlang factory slides

Take away

Page 61: Erlang factory slides

Take away

● Happy devs

Page 62: Erlang factory slides

Take away

● Happy devs● Happy ops

Page 63: Erlang factory slides

Take away

● Happy devs● Happy ops● much to learn!

Page 64: Erlang factory slides

Thank You

Page 65: Erlang factory slides

References

● http://www.ibm.com/developerworks/cloud/library/cl-optimizepythoncloud2/index.html