CQRS, ES, Scala @ Confitura 2012

80
CQRS, ES, Scala?
  • date post

    21-Oct-2014
  • Category

    Software

  • view

    94
  • download

    2

description

CQRS, ES, Scala @ Confitura 2012

Transcript of CQRS, ES, Scala @ Confitura 2012

Page 1: CQRS, ES, Scala @ Confitura 2012

CQRS, ES, Scala?

Page 2: CQRS, ES, Scala @ Confitura 2012

Maciek Próchniak

Topologia algebraiczna

TouK

Funktory

Monady

Drools

CamelServicemix

OSGiActiviti

Scala

hocolim

Page 3: CQRS, ES, Scala @ Confitura 2012
Page 4: CQRS, ES, Scala @ Confitura 2012

● System

● CQRS,ES - z lotu ptaka

● (Funkcyjny) model domeny

● Komunikacja

● Konsumenci zdarzeń

● Czy to się opłaca?

Page 5: CQRS, ES, Scala @ Confitura 2012

Wymagania

Page 6: CQRS, ES, Scala @ Confitura 2012

Alarmy

czujnikipracownicy

korelacje długoterminowe wzorce

Page 7: CQRS, ES, Scala @ Confitura 2012

Wydajność

Korelacje, przetwarzanie

Strumień danych (~300/sec)

Page 8: CQRS, ES, Scala @ Confitura 2012

Dane historyczne

Audyt Raporty

Reagowanie na zmiany

Page 10: CQRS, ES, Scala @ Confitura 2012

UI?

Page 11: CQRS, ES, Scala @ Confitura 2012

CRUD

Page 12: CQRS, ES, Scala @ Confitura 2012

Zadania

Page 13: CQRS, ES, Scala @ Confitura 2012

● System

● CQRS,ES - z lotu ptaka

● (Funkcyjny) model domeny

● Komunikacja

● Konsumenci zdarzeń

● Czy to się opłaca?

Page 14: CQRS, ES, Scala @ Confitura 2012

CQS

Command

Query

Page 15: CQRS, ES, Scala @ Confitura 2012

CQRS

Komenda

Zapytanie

Model domeny

Model odczytu

Page 16: CQRS, ES, Scala @ Confitura 2012

Event Sourcing

CommentAdded

OrderConfirmed

Page 17: CQRS, ES, Scala @ Confitura 2012

Event Sourcing

Page 18: CQRS, ES, Scala @ Confitura 2012

CQRS + ES

Komenda

Zapytanie

Model domeny

Model odczytu

Zdarzenia

Page 19: CQRS, ES, Scala @ Confitura 2012

Architektura z lotu ptaka

Page 20: CQRS, ES, Scala @ Confitura 2012

domena

log zdarzeń raporty

modele odczytu

korelacje

Page 21: CQRS, ES, Scala @ Confitura 2012

A co jeśli...?

Page 22: CQRS, ES, Scala @ Confitura 2012

Będzie zbyt dużo zdarzeń?

Snapshoty

Zmieni się format zdarzeń?

Migracja logu danych

Modyfikacje w locie

Page 23: CQRS, ES, Scala @ Confitura 2012

Model do odczytu "popsuje się"?

Odtwarzamy z logu zdarzeń

Potrzebujemy nowych modeli?

Tworzymy z logu zdarzeń

Page 24: CQRS, ES, Scala @ Confitura 2012

● System

● CQRS,ES - z lotu ptaka

● (Funkcyjny) model domeny

● Komunikacja

● Konsumenci zdarzeń

● Czy to się opłaca?

Page 25: CQRS, ES, Scala @ Confitura 2012

domena

log zdarzeń raporty

modele odczytu

korelacje

Page 26: CQRS, ES, Scala @ Confitura 2012

dane w pamięci

jeden wątek

docelowo: użycie STM

Repozytorium

Page 27: CQRS, ES, Scala @ Confitura 2012

Teraz będzie Scala

Zamknij oczy i myśl o AngliiJavie

Page 28: CQRS, ES, Scala @ Confitura 2012
Page 29: CQRS, ES, Scala @ Confitura 2012

Domena funkcyjna

?

Page 30: CQRS, ES, Scala @ Confitura 2012

Agregat

reguły biznesowe

transakcjespójność

dostęp

zachowanie

Page 31: CQRS, ES, Scala @ Confitura 2012

Niezmienialne agregaty

?

Page 32: CQRS, ES, Scala @ Confitura 2012

Event Sourcing

Page 33: CQRS, ES, Scala @ Confitura 2012

domena

log zdarzeń raporty

modele odczytu

korelacje

Page 34: CQRS, ES, Scala @ Confitura 2012

class Update[A,E] { val aggregate : A val event : E}

trait EventHandler { def handle(update:Update[_,_])}

Page 35: CQRS, ES, Scala @ Confitura 2012

Zachowanie a zmiana stanu

NO BEHAVIOR in state changing methods, no conditional logic, no exceptions

- Greg Young

Page 36: CQRS, ES, Scala @ Confitura 2012

class Order { //zachowania def confirm() : List[DomainEvent] def addLine(productId:UID): List[DomainEvent]

//brak logiki def apply(event:DomainEvent):Order }

Page 37: CQRS, ES, Scala @ Confitura 2012

Encje jako ADT - case class

case class Order(id:String, lines:List[OrderLine]){

def addOrderLine(line:OrderLine)= def confirm() = ... def cancel() = ...}

Page 38: CQRS, ES, Scala @ Confitura 2012

Bonus - FSM

Page 39: CQRS, ES, Scala @ Confitura 2012

Bonus - FSM

case class PreOrder(...) extends Order {def confirm : OrderConfirmed

def apply(e:OrderConfirmed) : ConfirmedOrder

}case class ConfirmedOrder(..) extends Order { def send() : OrderSent}

Page 40: CQRS, ES, Scala @ Confitura 2012

Bonus - testy

When did you read test to assure sth hasn't happened?

- Greg Young

Page 41: CQRS, ES, Scala @ Confitura 2012

Bonus - testy

//givenAlarmCreated, AlarmAckonwledged

//whenclearAlarm

//thenAcknowledgedAlarmCleared

Page 42: CQRS, ES, Scala @ Confitura 2012

Walidacja

Page 43: CQRS, ES, Scala @ Confitura 2012

Walidacja

if (total < 0) {throw ValidationError("Not enough");

}

if (total < 0) {validationErrors.add("Not enough");

}if (validationErrors.size() > 0) {

fail()...}

Page 44: CQRS, ES, Scala @ Confitura 2012

Walidacja

if (total < 0) {throw ValidationError("Not enough");

}

if (total < 0) {validationErrors.add("Not enough");

}if (validationErrors.size() > 0) {

fail()...}

Page 45: CQRS, ES, Scala @ Confitura 2012

Walidacja bez wyjątków

Never throw an exception of my own- Joel Spolsky

Page 46: CQRS, ES, Scala @ Confitura 2012

Walidacje - przykład kodu

Funktor walidacjiscalaz.Validation[Error,_]

//przykładowa metodanonEmpty(val:String) : Validation[Error,String]

Page 47: CQRS, ES, Scala @ Confitura 2012

Walidacje - przykład kodu

?Error("empty.name")

for { name <- nonEmpty(command.name)

Page 48: CQRS, ES, Scala @ Confitura 2012

Walidacje - przykład kodu

for { name <- nonEmpty(command.name) size <- geq(0, command.size)

?Error("negative.size")

Page 49: CQRS, ES, Scala @ Confitura 2012

Walidacje - przykład kodu

for { name <- nonEmpty(command.name) size <- geq(0, command.size)} yield addCompany(name,size)

?Error("bad.company")

Page 50: CQRS, ES, Scala @ Confitura 2012

Walidacje - przykład kodu

for { name <- nonEmpty(command.name) size <- geq(0, command.size)} yield addCompany(name,size)

?CompanyAdded(...)

Page 51: CQRS, ES, Scala @ Confitura 2012

Walidacja bez wyjątków

Non-breaking error handling is just an applicative functor on a partially applied disjoint union type constructor with semigroup error elements so what's the big deal?!

- Tony Morris

Page 52: CQRS, ES, Scala @ Confitura 2012

Domena funkcyjna

● Oddzielenie stanu od zachowania

● Testowalność

● Dostęp do technik funkcyjnych

● Zrównoleglanie

Page 53: CQRS, ES, Scala @ Confitura 2012

● System

● CQRS,ES - z lotu ptaka

● (Funkcyjny) model domeny

● Komunikacja

● Konsumenci zdarzeń

● Czy to się opłaca?

Page 54: CQRS, ES, Scala @ Confitura 2012

domena

log zdarzeń raporty

modele odczytu

korelacje

Page 55: CQRS, ES, Scala @ Confitura 2012

Aktor

skrzynka

wiadomości

stan

hierarchie

Page 56: CQRS, ES, Scala @ Confitura 2012
Page 57: CQRS, ES, Scala @ Confitura 2012

Akka

Page 58: CQRS, ES, Scala @ Confitura 2012

Co ze spójnością w GUI?

Page 59: CQRS, ES, Scala @ Confitura 2012

Akka - komponowanie przyszłości

actorRef ! message

actorRef ? message : Future

Page 60: CQRS, ES, Scala @ Confitura 2012

Akka - komponowanie przyszłości

Page 61: CQRS, ES, Scala @ Confitura 2012

Akka - komponowanie przyszłości

Page 62: CQRS, ES, Scala @ Confitura 2012

Akka - komponowanie przyszłości

Page 63: CQRS, ES, Scala @ Confitura 2012

● System

● CQRS,ES - z lotu ptaka

● (Funkcyjny) model domeny

● Komunikacja

● Konsumenci zdarzeń

● Czy to się opłaca?

Page 64: CQRS, ES, Scala @ Confitura 2012

domena

log zdarzeń raporty

modele odczytu

korelacje

Page 65: CQRS, ES, Scala @ Confitura 2012

Modele odczytu

Don’t pay an American developer to do this!!!

- Greg Young

Page 66: CQRS, ES, Scala @ Confitura 2012

SELECT * from CUSTOMER LEFT JOIN PRODUCT ...

Page 67: CQRS, ES, Scala @ Confitura 2012

Skąd brać dane?

WarehouseView

DeviceConnected

Page 68: CQRS, ES, Scala @ Confitura 2012

Skąd brać dane - zdarzenia

OldWarhouseId

WarehouseView

Page 69: CQRS, ES, Scala @ Confitura 2012

Skąd brać dane - inny model

WarehouseView

DeviceView

Page 70: CQRS, ES, Scala @ Confitura 2012

Skąd brać dane - ten sam model

WarehouseView

deviceIds

Page 71: CQRS, ES, Scala @ Confitura 2012

● System

● CQRS,ES - z lotu ptaka

● (Funkcyjny) model domeny

● Komunikacja

● Konsumenci zdarzeń

● Czy to się opłaca?

Page 72: CQRS, ES, Scala @ Confitura 2012

każdy System jest bardziej CRUDowy

niż się to początkowo wydaje

Page 73: CQRS, ES, Scala @ Confitura 2012

CQRS na poziomie autonomicznych serwisów

Page 74: CQRS, ES, Scala @ Confitura 2012

czy klient rozumie koszty i ograniczenia

?

Page 75: CQRS, ES, Scala @ Confitura 2012

EventSourcing

Page 76: CQRS, ES, Scala @ Confitura 2012

EventSourcing+

Scala+

Akka

Page 77: CQRS, ES, Scala @ Confitura 2012

EventSourcing+

Scala+

Akka

Page 78: CQRS, ES, Scala @ Confitura 2012

Literatura

● Debasish Gosh - http://debasishg.blogspot.com/

● Martin Krasser - http://krasserm.blogspot.com/

● Greg Young● Udi Dahan - http://www.udidahan.com

Page 79: CQRS, ES, Scala @ Confitura 2012

Dzięki

Page 80: CQRS, ES, Scala @ Confitura 2012

● http://www.gettyicons.com/free-icons/112/must-have/png/256/cancel_256.png● http://www.softicons.com/free-icons/web-icons/free-icon-set-by-eclipse-saitex/user-icon● http://www.flickr.com/photos/littlejohncollection/4325397661/sizes/l/in/photostream/● http://www.flickr.com/photos/alainpicard/4175215457/sizes/m/in/photostream/● http://www.flickr.com/photos/squeakychu/5793070401/sizes/m/in/photostream/● http://upload.wikimedia.org/wikipedia/commons/thumb/7/73/God2-Sistine_Chapel.png/800px-

God2-Sistine_Chapel.png