79
Hot-or-Not Elixir with José Valim Author: Paul Zenden

Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Hot-or-Not Elixirwith José Valim

Author: Paul Zenden

Page 2: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sioux 2017 2

|>

|> elixirwith {:ok, :”José Valim”} <-

creator_of_elixir()do

hot_or_not()end

Page 3: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Timetable

18:00h Introduction

18:05h Elixir (José Valim)

19:30h Break

20:00h Demonstrator &

Elixir in real practice (Sioux team)

20:45h Q & A

21:00h Drinks

#End of Program

Sioux 2017 3

Page 4: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

The Free Lunch Is OverA fundamental turn towards concurrency

Sioux 2017 4

https://herbsutter.com/welcome-to-the-jungle/

http://www.gotw.ca/publications/concurrency-ddj.htm

Next gen CPU no longer faster!

New performance drivers:

- Hyperthreading

- Cache

- Multi-Core

Page 5: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Software expectations double every year!

› Embrace concurrency (to exploit new hw)

› More, complex features

› Always available

› Scalable

› Responsive

› DevOps

› Zero downtime deployment

› …

Sioux 2017 5

Page 6: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

José Valim will explain how can help us …

Sioux 2017 6

… to cope with these challenges.

Founder and Lead Developer at Plataformatec.

Creator of Elixir

Page 7: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sioux 2017 7

Page 8: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

@elixirlang / elixir-lang.org

Page 9: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Rails 2.2 threadsafe

Page 10: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Functional programming

Page 11: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

• Explicit instead of implicit state • Transformation instead of mutation

Page 12: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools
Page 13: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Switch

Switch

Page 14: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Switch

Browser

Endpoint

Server

Page 16: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

http://blog.whatsapp.com/index.php/2012/01/1-million-is-so-2011/

2 million connections on a single node

Page 17: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Intel Xeon CPU X5675 @ 3.07GHz 24 CPU - 96GB Using 40% of CPU and Memory

Page 18: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

• Functional • Concurrent • Distributed

Page 19: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Idioms

Page 20: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sequential code

Page 21: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sequential code

elixir

Page 22: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

elixir

Page 23: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

elixir

Page 24: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

WebDB MailerStats

Page 25: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

WebDB MailerStats

Sup

Page 26: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

WebDB MailerStats

Sup

App

Page 27: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Observer Demo

Page 28: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Applications

• Introspection & Monitoring • Visibility of the application state • Easy to break into "components" • Reasoning when things go wrong

Page 29: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

• Processes • Supervisors • Applications • Message passing

Page 30: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

• Concurrent • Fail fast • Fault tolerant • Distributed?

Page 31: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

elixir

Page 32: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

app2@localapp1@local

elixir

Page 33: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

• Compatibility • Extensibility • Productivity

Page 34: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

goal #1

Compatibility

Page 35: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

goal #2

Extensibility

Page 36: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Now we need to go meta. We should now think of a language design as being a

pattern for language designs. A tool for making more tools of the same kind.

Guy Steele - “Growing a language” at ACM OOPSLA 1998

Page 37: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

defmodule MathTest do use ExUnit.Case

test "basic operations" do assert 1 + 1 == 2 end end

elixir

Page 38: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools
Page 39: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

from p in Post, where: p.published_at < now and p.author == “José”, order: p.created_at

elixir

Page 40: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

goal #3

Productivity

Page 41: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

• First-class documentation • Tooling (ExUnit, IEx, Mix) • Hex packages

Page 42: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools
Page 43: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools
Page 44: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools
Page 45: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

hex.pm

Page 46: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Demo time!

Page 47: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

elixir-lang.org

Page 48: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools
Page 49: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

consulting and software engineering

Built and designed at

Page 50: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

consulting and software engineering

Elixir coaching

Elixir design review

Custom development

Page 51: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

@elixirlang / elixir-lang.org

Page 52: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Hot-or-Not Elixirwith José Valim

Author: Paul Zenden

Demo

Elixir in practice

Page 53: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Color SorterElixir in practice

Sioux 2017 2

Goals:

• Learn Elixir

• Apply Elixir in

embedded system

• Concurrent behavior

• Distributed application

• Complex control

• Test support

• Documentation

• (Failure recovery)

Team:

• Koen Rutten

• Jochem Berndsen

• Han van Venrooij

• Philippe Dirkse

• Paul Zenden

Robot 1:

Blue

Robot 2:

Yellow

Robot 3:

Red

Belt + Color

identification

Gripper

Motor

Lift

Motor

Lift

Down

Sensor

Turn

Motor

Turn

Home

Sensor

Mindstorm

Page 54: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Functions

Sioux 2017 3

Robot:

- Connect with Belt controller

- Pick up block

- Drop off block

- Move lift up/down

- Rotate arm left/right

- Safety zone handling

- Notify belt when block

picked/dropped

Belt:

- Move belt to next position

- Identify color

- Keep track of blocks on belt

- Notify robots to pick/drop block

Page 55: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Safety Layer – Limited Rotations

Sioux 2017 4

Safety

Zone

Safety

Zone

Distributed

communication about

which robot may enter

the safety zone.

Distributed

communication about

which robot may enter

the safety zone.Allowed

rotation

Page 56: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Demo first – Technicalities next

Sioux 2017 5

Page 57: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

System levels

Sioux 2017 6

Lego Mindstorms

Brick

300 MHz ARM926EJ-S

64 MB DRAM /16 MB Flash

MicroSD

Linux Kernel 4.4 – ev3dev patches

GPIO, I2C, SPI – ev3dev drivers

Ethernet/Wifi: USB dongle

Elixir Application

Robot & Belt Application Logic

Robot & Belt Main Functions

Lego Device Handling: Derived from: https://github.com/jfcloutier/ev3/

Nerves

Framework

Frameworks: Networks, I/O, ev3dev, …

Tooling: Cross compilation tools for specific target

Platforms: cross compiled linux, boot directly to Erlang VM

Page 58: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Belt: Application Architecture

Sioux 2017 8

Supervisor Worker

ElixirHoNEv3

(application)

ClaimCoordinator

(left_centre)

ClaimCoordinator

(centre_right)

BeltDeviceWorker

Task

(execute requests)

async

Devices

All communication message based.

3 x Robot

Claim

Obtainer

Robot

Control

Distributed

Belt

Supervisor

BeltState

Wrapper

Belt

ControlHal

Page 59: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Robot: Application Architecture

Sioux 2017 9

ElixirHoNEv3

(application)

RobotDeviceWorker

Task

(execute requests)

async

Devices

All communication message based.

ClaimCoordinator

(left_centre)

ClaimCoordinator

(centre_right)

Belt

Belt

Control

Distributed

Robot

Supervisor

Robot

State

Claim

Obtainer

Robot

ControlHal

Supervisor Worker

Page 60: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Protocols

Sioux 2017 10

Defines client API

Compiler checks whether all functions are provided by BeltDeviceWorker

Hal will accept a notification message, but not explicitly defined (as above).

Task sends notification message directly. Could be improved

Page 61: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Some Code

Sioux 2017 12

Starting application, belt and robot specific children

Page 62: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Some Code

Sioux 2017 13

Synchronous Messages: GenServer.call()

- to be handled by handle_call()

“Direct call”

Asynchronous Messages: GenServer.cast()

- to be handled by handle_cast() Msg

LoopAsync call

Messages: Synchronous & Asynchronous

Note: still message

exchange between

processes.

Page 63: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Some Code

Sioux 2017 14

Robot.Control Robot.Hal RobotDeviceWorker (async) Task

:initialise

:initialise:initialize

<Create task>RobotDeviceTasks.

initialize():movement_done

Belt.Control

{:register_robot, pos}:robot_initialised

Belt Robot

Robot initialization sequence

Page 64: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Some code - Pattern matching

Sioux 2017 16

Page 65: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Some code

Sioux 2017 18

Belt state

Page 66: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Testing

Sioux 2017 19

Tested by

Page 67: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Documentation

Sioux 2017 20

Page 68: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Development, build & deployment

Linux only (due to cross-compilation) if using nerves

Nerves toolchain, commands: powerful

Mix compile, mix firmware, mix firmware.burn

Buildroot linux kernel configuration

Busybox user space commands

Documentation somewhat scattered

Searching, trial & error, active slack channel, github example code

Possible to upload firmware via network (but did not try it)

https://github.com/nerves-project/nerves_firmware_http

https://github.com/nerves-project/nerves_firmware

Sioux 2017 21

Page 69: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Our Experiences - Benefits

Easy to pick up

REPL (read-evaluate-print loop), Elixir toolset (mix)

Syntax

High fun factor (powerful) Focus on essence, less on technical details

Provides a "fighting chance" to develop a proper distributed, fault-tolerant

system Need to develop/learn best practices/patterns

Vibrant community: tooling, libraries and frameworks abound nerves, mix, Phoenix, Ecto, …

Erlang libraries

Erlang VM/OTP: Great!

Sioux 2017 23

Page 70: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Our Experiences - Concerns

No mandatory type system; critical for any non-trivial project

Defining types and type specifications is optional (Need guidelines, best practices)

Dialyzer might provide sufficient support

Not clear whether more complex type specifications are supported

Decomposition design of components (supervisors, workers), including protocol

(messages) is important

IDE support

Partly inherent for dynamic languages

Different level of support for different kind of general editors (emacs, atom, IntelliJ, Sublime, …)

Performance not sure; did no measurements

Calculation intensive might be slow.

IoT, distributed systems probably OK.

Can call C/C++ functions if neededSioux 2017 24

Page 71: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Our Experiences - Conclusion

Great learning experience & fun

Elixir/Erlang looks interesting, especially distributed systems, but you still

have to think about:

Difficult aspects like fault-tolerance, network stability, security, etc.

Decomposition and interface protocol design:

messages should result in atomic actions

Would we apply it in a new project?

Depends highly on the nature of project.

Sioux 2017 25

Page 72: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sioux 2017 26

Page 73: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Active development – vibrant community

Many interesting new applications and developments

Auto-connecting devices, machine learning, game back-end, web services, drones, …

Search for ElixirConf EU 2017, ElixirDaze 2017, Lambda Days 2017, more …

Look at:

https://elixir-lang.org/

https://elixirforum.com/

http://elixir.community/

https://www.erlang-solutions.com/

http://nerves-project.org/

http://www.phoenixframework.org/

https://hex.pm/

Sioux 2017 27

Page 74: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Elixir - Hot-or-Not?

Sioux 2017 28

Page 75: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Thank you, José for Elixir

Sioux 2017 29

Page 76: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

More Hot-or-Not, more Sioux

Q3 2017 > Hot-or-Not “Accelerating Intelligence”

Q4 2017 > Hot-or-Not The Next Generation (workshop)

Go to www.sioux.eu for more information.

Sioux 2017 30

Page 77: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sioux 2017 31

Page 78: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Sioux 2017 32

Page 79: Hot-or-Not Microservices with James Lewis · Now we need to go meta. We should now think of a language design as being a pattern for language designs. A tool for making more tools

Source of your technology

Sioux 2017