scala in a wild enterprise

69

Upload: rafael-bagmanov

Post on 22-Jun-2015

1.027 views

Category:

Documents


0 download

DESCRIPTION

Video: (in russian) http://www.youtube.com/watch?v=4pTDhgWW6ko

TRANSCRIPT

Page 1: Scala in a wild enterprise
Page 2: Scala in a wild enterprise
Page 3: Scala in a wild enterprise

Scala in a wild enterpriseRafael Bagmanov ( Grid Dynamics )

the?

Page 4: Scala in a wild enterprise

In what use cases (types of applications) does Scala make the least sense?

Page 5: Scala in a wild enterprise

"Database front end, CRUD apps.

Most of the boring apps that are built with Struts and Spring"

David Pollak "Barriers to scala adoption" Infoq.com

Page 6: Scala in a wild enterprise
Page 7: Scala in a wild enterprise

OpenGenesisgithub.com/griddynamics/OpenGenesis

Page 8: Scala in a wild enterprise

● open source - open-genesis.org

● 2 years of development

● > 50 KLOC of scala code

● successfully deployed to production in one

large american financial institution

● Buzzwords: continuous deployment, cloud,

chef, devops, aws, openstack

OpenGenesisDeployment orchestration tool

Page 9: Scala in a wild enterprise

● Integration with legacy apps and data (lots of

SOAP and xml)

● sophisticated security policies

● IT department separated from development

team

● J2EE Containers everywhere

● Risk averse

Enterprise characteristics

Page 10: Scala in a wild enterprise

Typical "lightweight" j2ee stack

Web layer

Service layer

Data access layer

DB

Spring MVC

Spring

JPA

Page 11: Scala in a wild enterprise

j2ee stack. Scala edition

Web layer

Service layer

Data access layer

DB

Spring MVC + scala magic

Spring + scala implicits

JPA Squeryl

Page 12: Scala in a wild enterprise

j2ee stack. Scala edition

Web layer

Service layer

Data access layer

DB

Spring MVC

Spring

Squeryl

WAR

Page 13: Scala in a wild enterprise

j2ee stack. Scala edition + scala goodness

Web layer

Service layer

Data access layer

DB

Spring MVC

Spring

Squeryl

Workflow distributed engine

Akka

WAR

Page 14: Scala in a wild enterprise

Service layer: Spring

Page 15: Scala in a wild enterprise

● DI fits almost nicely with scala

Spring with scala

Page 16: Scala in a wild enterprise

● DI fits almost nicely with scalaclass GenesisRestController {

@Autowired var genesisService: GenesisService = _

}

class GenesisRestController {

@BeanProperty var genesisService: GenesisService = _

}

class GenesisRestController (genesisService: GenesisService) {}

Spring with scala

Page 17: Scala in a wild enterprise

● DI fits almost nicely with scala○ "Everything is a trait" approach can't be done

Spring with scala

Page 18: Scala in a wild enterprise

● DI fits almost nicely with scala○ "Everything is a trait" approach can't be done

○ Be aware of type inference in @Configuration beantrait Service

class ServiceImpl extends Service

@Configuration

class ServiceContext {

@Bean def service = new ServiceImpl

}

Spring with scala

Page 19: Scala in a wild enterprise

● DI fits almost nicely with scala○ "Everything is a trait" approach can't be done

○ Be aware of type inference in @Configuration beantrait Service

class ServiceImpl extends Service

@Configuration

class ServiceContext {

@Bean def service: Service = new ServiceImpl

}

Spring with scala

Page 20: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

Spring with scala

Page 21: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

springLdapTemplate.authenticate("user", "*", "password", new

AuthenticationErrorCallback {

def execute(e: Exception) {log.error(e)}

})

Spring with scala

Page 22: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

springLdapTemplate.authenticate("user", "*", "password", new

AuthenticationErrorCallback {

def execute(e: Exception) {log.error(e)}

})

Spring with scala

Page 23: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

springLdapTemplate.authenticate("user", "*", "password", new

AuthenticationErrorCallback {

def execute(e: Exception) {log.error(e)}

})

springLdapTemplate.authenticate("user", "*", "password", log.error(_))

Spring with scala

Page 24: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

springLdapTemplate.authenticate("user", "*", "password", log.error(_))

implicit def authErrorCallbackWrapper(func:(Exception) => Any) = { new AuthenticationErrorCallback { def execute(exception: Exception): Unit = func(exception) } }

Spring with scala

Page 25: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

● AOP for free (almost)

Spring with scala

Page 26: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

● AOP for free (almost)○ Be aware of naming in debug info def findUsers(projectId: Int) {

dao.findUsers(projectId)

}

def findUsers2(projectId: Int) {

dao.allUsers().filter(_.projectId == projectId)

}

Spring with scala

Page 27: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

● AOP for free (almost)○ Be aware of naming in debug info def findUsers(projectId: Int) { // debugIfo: name "projectId" dao.findUsers(projectId)

}

def findUsers2(projectId: Int) { // debugInfo: name "projectId$" dao.allUsers().filter(_.projectId == projectId)

}

Spring with scala

Page 28: Scala in a wild enterprise

● DI fits almost nicely with scala

● Rich spring templates libraries.

● AOP for free (almost)

● Spring security just works

Spring with scala

Page 29: Scala in a wild enterprise

Persistence layer: Squeryl

Page 30: Scala in a wild enterprise

● Lightweight ORM written in scala

Squeryl

Page 31: Scala in a wild enterprise

● Lightweight ORM written in scala

Squeryl

class Workflow(override val id: Int) extends KeyedEntity[Int]

Page 32: Scala in a wild enterprise

● Lightweight ORM written in scala

Squeryl

class Workflow(override val id: Int) extends KeyedEntity[Int]

object GS extends Schema {

val workflows = table[Workflow]

}

Page 33: Scala in a wild enterprise

● Lightweight ORM written in scala

Squeryl

class Workflow(override val id: Int) extends KeyedEntity[Int]

object GS extends Schema {

val workflows = table[Workflow]

}

def find(workflowId: Int) = from(GS.workflows)(w =>

where(w.id === workflowId)

select (w)

orderBy(w.id desc)

)

Page 34: Scala in a wild enterprise

● Lightweight ORM written in scala○ First class support for scala collections, options, etc

Squeryl

Page 35: Scala in a wild enterprise

● Lightweight ORM written in scala○ First class support for scala collections, options, etc

○ Integrates with spring transaction management

(not out of the box)

Squeryl

Page 36: Scala in a wild enterprise

● Lightweight ORM written in scala○ First class support for scala collections, options, etc

○ Integrates with spring transaction management

(not out of the box)

Squeryl

@Transactional(propagation = REQUIRES_NEW)

def find(workflowId: Int) = from(GS.workflows)(w =>

where(w.id === workflowId)

select (w)

orderBy(w.id desc)

)

Page 37: Scala in a wild enterprise

Squeryl

● Lightweight ORM written in scala

○ Likes heap in the same proportion as Hibernate does

hibernate squeryl

Page 38: Scala in a wild enterprise

● Lightweight ORM written in scala

○ Likes heap in the same proportion as Hibernate does

○ Lot's of "black magic" in source code

Squeryl

Page 39: Scala in a wild enterprise

● Lightweight ORM written in scala

● Internal scala DSL for writing queries○ Type safe queries - compile time syntax check

Squeryl

Page 40: Scala in a wild enterprise

● Lightweight ORM written in scala

● Internal scala DSL for writing queries○ Lot's of "black magic" in source code

Squeryl

Page 41: Scala in a wild enterprise

● Lightweight ORM written in scala

● Internal scala DSL for writing queries○ Lot's of "black magic" in source code

○ Fallback on native sql is not easy

Squeryl

Page 42: Scala in a wild enterprise

● Lightweight ORM written in scala

● Internal scala DSL for writing queries○ Lot's of "black magic" in source code

○ Fallback on native sql is not easy

○ Lots of implicits drives IDE crazy and increases compilation time

Squeryl

Page 43: Scala in a wild enterprise

● Lightweight ORM written in scala

● Internal scala DSL for writing queries○ Lot's of "black magic" in source code

○ Fallback on native sql is not easy

○ Lots of implicits drives IDE crazy and increase compilation time

○ The approach is somewhat flawed (opinion)

Squeryl

Page 44: Scala in a wild enterprise

Web layer: Spring MVClift-json

scala magic

Page 45: Scala in a wild enterprise

● RESTfull web services

Web layer

case class User (

@NotBlank username: String,

@Email @Size(min = 1, max = 256) email: String,

password: Option[String]

)

@Controller

@RequestMapping(Array("/rest/users"))

class UsersController {

@RequestMapping(method = Array(RequestMethod.POST))

@ResponseBody

def create(@RequestBody @Valid request: User): User = userService.create(request)

}

Page 46: Scala in a wild enterprise

● RESTfull web services

● Easy to make Hypermedia REST with

implicits

Web layer

@Controller

@RequestMapping(Array("/rest/users"))

class UsersController {

import com.griddynamics.genesis.rest.links.Hypermedia._

@RequestMapping(method = Array(RequestMethod.POST))

@ResponseBody

def create(@RequestBody @Valid request: User): Hypermedia[User] = {

userService.create(request).withLink("/rest/users", LinkType.SELF)

}

Page 47: Scala in a wild enterprise

Couple of words aboutAKKA

Page 48: Scala in a wild enterprise

Ехал Акка через Акка

Смотрит Акка в Акка Акка

Сунул Акка Акка в Акка

Акка Акка Акка Акка *

* Ode to Akka in russian

Page 49: Scala in a wild enterprise

Greatest challenge:

Page 50: Scala in a wild enterprise

Greatest challenge:People

Page 51: Scala in a wild enterprise

● Hiring is hard

Challenges

Page 52: Scala in a wild enterprise

● Hiring is hard○ Scala is a talent attraction

Challenges

Page 53: Scala in a wild enterprise

● Hiring is hard○ Scala is a talent attraction

○ In avg: 0.5 interview per month

Challenges

Page 54: Scala in a wild enterprise

● Hiring is hard

● Settling team standards and code

convention

Challenges

Page 55: Scala in a wild enterprise

● Hiring is hard

● Settling team standards and code

convention○ Tools are not there yet

Challenges

Page 56: Scala in a wild enterprise

● Hiring is hard

● Settling team standards and code

convention○ Tools are not there yet

○ Between "OCaml" and "Java" fires

Challenges

Page 57: Scala in a wild enterprise

● Hiring is hard

● Settling team standards and code

convention○ Tools are not there yet

○ Between "OCaml" and "Java" fires

○ "Effective scala" by Twitter and "Scala style guide"

might help (a bit)

Challenges

Page 58: Scala in a wild enterprise

The greatest code-review mystery of all times

if (option.isDefined) {

..

}

option.foreach { .. }

option match { case Some(x) => .. case None => ..}

How to deal with scala.Option

?

Page 59: Scala in a wild enterprise

The greatest code-review mystery of all times

if (option.isDefined) {

..

}

option.foreach { .. }

option match { case Some(x) => .. case None => ..}

How to deal with scala.Option

?

Page 60: Scala in a wild enterprise

Recruiting java developers

Page 61: Scala in a wild enterprise

aka

Page 62: Scala in a wild enterprise

5 stages of grief

Page 63: Scala in a wild enterprise

1. Denial

2. Anger

3. Bargaining

4. Depression

5. Acceptance

5 stages of grief

Page 64: Scala in a wild enterprise

1. Denial

2. Anger

3. Bargaining

4. Depression

5. Acceptance

5 stages of grief

Page 65: Scala in a wild enterprise

1. Denial

2. Anger

3. Bargaining

4. Depression

5. Acceptance

5 stages of grief

Page 66: Scala in a wild enterprise

1. Denial

2. Anger

3. Bargaining

4. Depression

5. Acceptance

5 stages of grief

Page 67: Scala in a wild enterprise

1. Denial

2. Anger

3. Bargaining

4. Depression

5. Acceptance

5 stages of grief

Page 68: Scala in a wild enterprise

1. Denial

2. Anger

3. Bargaining

4. Depression

5. Acceptance

5 stages of grief

Page 69: Scala in a wild enterprise