Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Future of Jira Software powered by Kotlin - Dev...

Martin T. Varga
November 16, 2019
130

Future of Jira Software powered by Kotlin - DevFest 2019

Atlassian is on a decomposition journey. We split our monoliths in microservices and we build our microservices in Kotlin. Our services are reactive and resilient and working on them is kool.

This is a story of Jira Software’s transition to Kotlin. How we built a GraphQL gateway in Kotlin with Spring Boot and Project Reactor. Our success in interoperability with Java and how we solved issues when crossing the boundary between the reactive and non-reactive world. It also explains how Kotlin helped our developers become more productive and how resilience helps us build better services for consumers.

Martin T. Varga

November 16, 2019
Tweet

Transcript

  1. Google Pixel 2 Just Black Simply drop your screenshot on

    to this media placeholder TRELLO MOBILE
  2. Jira For cloud and server Confluence For cloud and server

    OpsGenie Our recent acquisition Not just Trello
  3. Server Cloud 1001001 0 0 1 0 0 1 0

    0 1 0 0 1 1101010 1010101 0 0 1 0 0 1 1110001001 0011110010 1100101011 01111110001 Single Code Base
  4. Server Cloud 1001001 0 0 1 0 0 1 0

    0 1 0 0 1 1101010 1010101 0 0 1 0 0 1
  5. 1101010101 010100100 101010010 0101010111 0 1 0 1 0 1

    1011100 1 0 0 1 0 1 1 0 1 0 1 0 1010111 001001
  6. 1101010 1010101 001001 0 1 0 1 0 1 1011100

    1 0 0 1 0 1 1 0 1 0 1 0 1010111 001001 0 1 0 1 0 1 1011100 1 0 0 1 0 1
  7. 1101010 1010101 001001 0 1 0 1 0 1 1011100

    1 0 0 1 0 1 1 0 1 0 1 0 1010111 001001 0 1 0 1 0 1 1011100 1 0 0 1 0 1
  8. links: healthcheck: uri: heartbeat port: 8080 source: url: "[email protected]:atlassianlabs/animal-facts- graphql-gateway-demo.git"

    notifications: email: "[email protected]" resources: - type: dynamo-db name: facts attributes: ReadWriteCapacityMode: ON_DEMAND
  9. Jira Software Tech Stack DynamoDB or PostgreSQL Depends on the

    application. Project Reactor Mature reactive library. Kotlin For all new services. Spring Boot Custom wrapper that makes monitoring etc. easier.
  10. Experience Have we learned Kotlin enough? Was it a success?

    Compatibility How well is it going to play with our current tech stacks? Staffing Are we going to have enough Kotlin developers available? Adopting Kotlin
  11. Velocity Does it make our developers more productive? Industry Is

    it already adopted by the developers community? Adopting Kotlin
  12. We happen to have a variety of languages within our

    stack including Kotlin, Python, and Ruby. Senior Backend Developer Ad
  13. A case for a reactive app A real world example

    of an app that we built recently
  14. Operators Useful for modifying and combining sources. Allows composition of

    libraries that add resilience, tracing etc. Async Calls a service, then does mostly nothing and waits. Perfect fit. Project reactor Clean code No callback hell.
  15. val currentBoard = "5".toMono() val recentBoards = listOf("0", "1", "2").toFlux()

    currentBoard.concatWith(recentBoards) COMPOSITION & CLEAN CODE 5 0 1 2 0 1 2 5
  16. val currentBoard = "5".toMono() val recentBoards = listOf("0", "1", "2").toFlux()

    currentBoard.concatWith(recentBoards) .map { boardService.getName(it) } .take(3) .subscribe { println(it) } COMPOSITION & CLEAN CODE
  17. currentUser.concatWith(recentUsers) .subscriberContext { sc -> sc.put("key", value) } .flatMap {

    userId -> Mono.subscriberContext().map { sc -> userService.getFullName(userId, sc.get("key")) } } .subscribe { println(it) } SUBSCRIBER CONTEXT
  18. Get exactly what you want Query multiple sources at once,

    specify what you want. Strongly typed schema Schema is a living documentation. Mature web clients Clients like Apollo let us remove dependency on Redux in ReactJs web apps. GraphQL
  19. type Query { board(id: ID): Board } type Board {

    id: ID name: String issues: [Issue] } type Issue { id: ID key: String } SCHEMA
  20. { "data": { "dog": { "fact": "Dog fact", "length": 8

    }, "cat": { "fact": "Cat fact", "length": 8 } } }
  21. type Query { cat: Fact dog: Fact } type Fact

    { fact: String length: Int } GRAPHQL SCHEMA
  22. @Component class CatFactFetcher(val webClient: WebClient) : FactFetcher { override fun

    get(environment: DataFetchingEnvironment): CompletionStage<Fact?> = webClient.get().uri("https://catfact.ninja/fact") .retrieve() .bodyToMono(CatFact::class.java) .map { Fact(it.fact, it.length) } .toFuture() } data class CatFact(val fact: String, val length: Int) CAT FACTS FETCHER
  23. @Component class DogFactFetcher(val webClient: WebClient) : FactFetcher { override fun

    get(environment: DataFetchingEnvironment): CompletionStage<Fact?> = webClient.get().uri("https://.../facts/dog") .retrieve() .bodyToMono(DogFact::class.java) .map { Fact(it.fact, it.fact.length) } .toFuture() } data class DogFact(val fact: String) DOG FACTS FETCHER
  24. @Configuration class GraphqlConfig { @Bean fun schema(wiring: RuntimeWiring): GraphQLSchema =

    SchemaGenerator().makeExecutableSchema( parseSchema(“/graphql/schema.graphqls"), wiring ) } GRAPHQL CONFIGURATION
  25. @Configuration class GraphqlConfig { @Bean fun factsWiring( dogFactFetcher: DogFactFetcher, catFactFetcher:

    CatFactFetcher ): RuntimeWiring = RuntimeWiring.newRuntimeWiring() .type( newTypeWiring("Query") .dataFetcher("dog", dogFactFetcher) .dataFetcher("cat", catFactFetcher) ) .build() }
  26. override fun processQuery( query: String ): Mono<ExecutionResult> = Mono.fromFuture( GraphQL

    .newGraphQL(graphQLSchema) .build() .executeAsync( ExecutionInput.newExecutionInput() .query(query) .build() ) ) SERVICE METHOD
  27. @PostMapping( path = ["/graphql"], consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE]

    ) fun graphql( @RequestBody body: Map<String, Any> ): Mono<Map<String, Any>> { val query: String = body["query"] as String? ?: "{}" return graphQLService.processQuery(query) .map { it.toSpecification() } } REACTIVE API
  28. { "data": { "dog": { "fact": "“Him” and “Her” were

    the names of President Lyndon Johnson's two beagles.", "length": 73 }, "cat": { "fact": "Jaguars are the only big cats that don't roar.", "length": 46 } } } RESULT
  29. { "data": { "dog": {…}, "cat": null }, "errors": [

    { "message": "Exception while fetching data (/cat) : Operation timed out", "path": [ "cat" ] } ] }
  30. class CatFactFetcher( val webClient: WebClient ) : FactFetcher { override

    fun get(environment: DataFetchingEnvironment): CompletionStage<Fact?> = webClient.get().uri("https://catfact.ninja/fact") .retrieve() .bodyToMono(CatFact::class.java) .map { … } .timeout(Duration.ofMillis(5000)) .toFuture() } CAT FACTS WITH TIMEOUT
  31. { "data": { "dog": {…}, "cat": null }, "errors": [

    { "message": "Exception while fetching data (/cat) : Did not observe any item or terminal signal within 5000ms in 'map' (and no fallback has been configured)", "path": [ “cat" ] } ] }
  32. class CatFactFetcher( val webClient: WebClient, val catBreaker: CircuitBreaker ) :

    FactFetcher { override fun get(environment: DataFetchingEnvironment): CompletionStage<Fact?> = webClient.get()
 … .timeout(Duration.ofMillis(2500)) .transform( CircuitBreakerOperator.of(catBreaker) ) .toFuture() } CAT FACTS FETCHER WITH CIRCUIT BREAKER
  33. @Bean fun catCircuitBreaker(): CircuitBreaker { val circuitBreakerConfig = CircuitBreakerConfig.custom() .ringBufferSizeInClosedState(3)

    .ringBufferSizeInHalfOpenState(3) .waitDurationInOpenState(Duration.ofMillis(5000)) .failureRateThreshold(0.5f) .build() return circuitBreakerRegistry.circuitBreaker( "CatCCB", circuitBreakerConfig ) }
  34. { "data": { "dog": {…}, "cat": null }, "errors": [

    { "message": "Exception while fetching data (/cat) : CircuitBreaker 'CatCCB' is OPEN and does not permit further calls", "path": [ "cat" ] } ] }
  35. GraphQL For APIs consumed from the front-end. Resilience To prevent

    propagating errors downstream. Reactive For I/O heavy apps. Takeaways
  36. GraphQL Java There are already pure Kotlin libraries. Spring boot

    has a set of starters too. Coroutines Support in Spring. New utilities for Project reactor. Alternatives
  37. fun processQuery(query: String): Map<String, Any> @PostMapping( path = ["/graphql"], consumes

    = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.APPLICATION_JSON_VALUE] ) fun graphqlCoroutines( @RequestBody body: Map<String, Any> ): Mono<Map<String, Any>> = mono(Unconfined) { val query: String = body["query"] as String? ?: "{}" graphQLService.processQuery(query) } REACTOR WITH COROUTINES
  38. 1 2 3 Animal Facts GraphQL Gateway https://bitbucket.org/atlassianlabs/animal-facts-graphql-gateway-demo/src/ master/ graphql-java

    https://www.graphql-java.com/ Resilience4j https://github.com/resilience4j/resilience4j Resources / Code
  39. 1 2 3 Kotlin at Trello Trello tech blog:
 https://tech.trello.com/kotlin-at-trello/

    Engineering stateless, high-availability cloud services at Atlassian Atlassian Engineering Blog: https://www.atlassian.com/blog/technology/aws-scaling-multi-region-low-latency- service More about Micros Slightly outdated, but still relevant.
 https://www.youtube.com/watch?v=dc2nqzgqH24 Resources / Further information
  40. 1 Why not both? https://knowyourmeme.com/memes/why-not-both-why-dont-we-have-both Resources / Other 2 We

    are hiring! Jira Software is looking for engineers, Java and Kotlin http://go.atlassian.com/jsw-hiring