26
Erlang at Nu Echo Some lessons learned Dominique Boucher Dominique Boucher Nu Echo

Erlang at Nu Echo

Embed Size (px)

DESCRIPTION

Slides from a talk at the Erlang Montreal user group.

Citation preview

Page 1: Erlang at Nu Echo

Erlang at Nu EchoSome lessons learned

Dominique Boucher Dominique Boucher Nu Echo

Page 2: Erlang at Nu Echo

Agenda

Who we are, what we doWho we are, what we doNuGram Hosted ServerWhy ErlangArchitectureSome lessons

Page 3: Erlang at Nu Echo

Who we are / what we do

• Speech applicationsSpeech applications expertsLots of R&D• Lots of R&D

• Many tools to support our own practice• Grammar tools• Benchmarking/tuning tools• Testing/monitoring toolsTesting/monitoring tools

Page 4: Erlang at Nu Echo

Some of our tools

NuBot

NuGram IDE

Page 5: Erlang at Nu Echo

NuGram Hosted Server

Hosted infrastructure for deploying andHosted infrastructure for deploying and sharing speech grammars– Supports static AND dynamic grammars– Supports static AND dynamic grammars– Embeds the NuGram technologies

• Dynamic grammar instantiation• Dynamic grammar instantiation• Grammar conversion to ABNF/GrXML/GSL• Text-based semantic interpretationp

• To complement telephony in the cloudTo complement telephony in the cloud(eg. Tropo)

Page 6: Erlang at Nu Echo

NuGram Hosted Server

ComponentsRESTf l API– RESTful API– www.grammarserver.com/api

C it b it– Community website– www.grammarserver.com

– NuGram product website– nugram.nuecho.com

Page 7: Erlang at Nu Echo

NuGram Hosted Server

NuGramHosted Server

Your application

(Java Ruby PythonHosted Server (Java, Ruby, Python, JavaScript, Erlang, etc.)

TelephonyPlatform

Page 8: Erlang at Nu Echo

NuGram Hosted ServerAn example in Ruby

dyngrammar = "#ABNF 1.0 UTF-8;..."

server = GrammarServer.new()session = server.create session("username", "passwrd")session server.create_session( username , passwrd )session.upload("digits2.abnf", dyngrammar);grammar = session.instantiate("digits2.abnf", {})interpretation

i t t(' t th f ') t j= grammar.interpret('one two three four').to_jsonsession.disconnect

Page 9: Erlang at Nu Echo

Why Erlang?

• Initial requirementsInitial requirements• High-performance HTTP server

• Scalability Fault tolerance• Scalability, Fault-tolerance

• Hot code swapping

Di t ib t d d t b• Distributed database

• The Erlang appeal• Single language, single platform

• Ideal projectp j• for experimenting with a different technology

Page 10: Erlang at Nu Echo

Components• Behaviours for building robust

applications• Behaviours for building robust

applicationsOTP

(Open Telecom Pl tf ) applicationsapplicationsPlatform)

• High performance HTTP Server• High performance HTTP ServerYaws • High-performance HTTP Server• High-performance HTTP ServerYaws

• Distributed, fault-tolerant, soft realtime DB

• Distributed, fault-tolerant, soft realtime DBMnesia

• Java API to expose Java/Kawa code as a regular Erlang node

• Java API to expose Java/Kawa code as a regular Erlang nodeJinterface

Page 11: Erlang at Nu Echo

High-level architecture

JInterface JInterface JInterface

www.grammarserver.com(http + https)

Yaw

s

nugram.nuecho.com(http + https) Y

MnesiaRESTful API(http + https)

Page 12: Erlang at Nu Echo

Session handlingJava/Kawa Worker

Java/Kawa WorkerJava Thread

Erlang Node

Ja a ead

Session

Dispatcher

Mne

sia

Yaws

Dispatcher

Request Response

Page 13: Erlang at Nu Echo

Session handlingJava/Kawa Worker

Java/Kawa WorkerJava Thread

Erlang NodeErlang Node

Ja a eadca

ted)

Session

cate

d)

Dispatchersia

(rep

lic

DispatcherDispatcher sia

(rep

lic

Yaws

Dispatcher

Mne

s

Yaws

Dispatcher

Yaws

Dispatcher

Mne

s

Request Response

Page 14: Erlang at Nu Echo

Session handling

• Sending messages to remote nodes isSending messages to remote nodes is completely transparentE l ( i lifi d)Example (simplified)

Process = db:get session(SessionID)Process = db:get_session(SessionID),Process ! Request.

Page 15: Erlang at Nu Echo

Session handling

• Session timeouts handled by theSession timeouts handled by the session processE lExample

receive…

after Timeout ->db:expire_session(self())

end

Page 16: Erlang at Nu Echo

Mnesia

• StoresStores…• User accounts• All grammars

(templates and instantiated ones)• Node IDs of all Java workers• All session data

Page 17: Erlang at Nu Echo

What we’ve developed

Account management Registration, email confirmation, etc.

BBcode validation & conversion for comments

Twitter widgetA process caches the tweetsA process caches the tweets and updates it every 5 minutes

Rendering of tweetse de g o ee s

RSS reader / widgetA process caches the feed contentA process caches the feed content and updates it every hour

Page 18: Erlang at Nu Echo

What we’ve developed

Code-based representation pof the web site structure

Tree like structureTree-like structureDocuments, modules, sections

Supports the addition on new “modules”

Lots of unit tests!

Page 19: Erlang at Nu Echo

Web site structure

-define(SITE_CONFIGURATION,{ i d{section, "product_app", "Home",[{module, "welcome",

"Welcome - NuGram Platform", {nugram_site, welcome_with_news, []}},

{document, "system_error", "Warning - Request not completed","system_error.html"},

{section, "nugram_platform", "NuGram Platform",[{document, "overview",

"Overview", "nugram_overview.html"},

{document, "faq", "FAQ", "faq.html"},{ , q , , q },{document, "abnf_tutorial",

"Grammar Language Tutorial", "abnf_tutorial.html"},

……

Page 20: Erlang at Nu Echo

Lessons learned

• Erlang strengths • Glueing things togetherg g g• Data transformations

• bbcode XML• bbcode, XML

• Protocol handling / processing byte streamsstreams

• Like encoding detection

G t d i d l t i t• Great dynamic development environment• REPL + connecting to a running node

Page 21: Erlang at Nu Echo

Lessons learned

E l k• Erlang weaknesses• Sequential computations

harder to express• A matter of style

• String manipulationg p• Somewhat better now (support for encodings)

Page 22: Erlang at Nu Echo

Lessons learned

• Learn and use OTP from day one• Servers,

supervisors,applicationsapplications

• Worth the investment

E l t itt f d• Example: twitter feed server• First version without OTP

crashed way too often making the platform unreliable• crashed way too often, making the platform unreliable

• Second version using gen_server

Page 23: Erlang at Nu Echo

Lessons learned

• Dynamically-typed languages makeDynamically typed languages make non-trivial refactorings difficult and error-proneerror-prone• Not specific to Erlang,

b t litbut a reality• Dializer can help

• Hot code loading is greatHot code loading is great• But tricky to do right

Page 24: Erlang at Nu Echo

Lessons learned

• Know your toolsKnow your tools• Toolbar, tv,

appmon, pmanpp , p

D 't i t th h l• Don't reinvent the wheel• There are a lot more libraries now

• we use Yaws, oauth, smtp_fsm

• rebar - great build tool

Page 25: Erlang at Nu Echo

We learned a lot!

And we had a lot of fun()!()

Page 26: Erlang at Nu Echo

halt()halt().