25
Replay your productions bugs JavaZone 2013 Mads Enevoldsen, Developer, Jayway

Deja vu JavaZone 2013

Embed Size (px)

DESCRIPTION

Bugs in production systems are typically communicated in stack traces. But why don't we communicate in unit tests? This presentation discuss a concrete open source Java framework that allows production systems to communicate bugs as test cases. It will produce deterministic, sand-boxable test cases having identical execution paths as the original run. Using the framework correctly will even make multi-threaded scenarios re-playable.

Citation preview

Page 1: Deja vu  JavaZone 2013

Replay your productions bugsJavaZone 2013

Mads Enevoldsen, Developer, Jayway

Page 2: Deja vu  JavaZone 2013

Overview

• Problem Definition

• Tracing

• Reproducablilty

• Threads

• Example Architecture

Page 3: Deja vu  JavaZone 2013

Part I

Problem Definition

Page 4: Deja vu  JavaZone 2013

Spot of bother

• Why are errors communicated in stack traces?

• Why are production bugs hunted down by inspecting log files?

• Why can’t we communicate in running code?

Page 5: Deja vu  JavaZone 2013

Unit test

• Proves how code works

• Deterministic

• Environment agnostic

Page 6: Deja vu  JavaZone 2013

Execution path

1 public class Example {

2

3 public void foo() {

4 bar();

5 // exception occurs

6 }

7

8 private void bar() {

9 //...

10 }

11 }

Page 7: Deja vu  JavaZone 2013

Computable?

• Reasoning about code

• Source code vs. running code

Page 8: Deja vu  JavaZone 2013

Part II

Tracing

Page 9: Deja vu  JavaZone 2013

Deja Vu

• Open source

• Define trace points in your code

• Serialize traces to unit tests

Page 10: Deja vu  JavaZone 2013

Example

1 public class Example {

2

3 @Traced

4 public void foo() {

5 bar();

6 //...

7 }

8

9 private void bar() {

10 //...

11 }

12 }

Page 11: Deja vu  JavaZone 2013

@Traced

• Method annotation

• Define ”lumps” of code to be traced

• Store arguments and method ”pointer”

• No state in objects

Page 12: Deja vu  JavaZone 2013

Setup

• Install callback

• Trace-mode for tracing @Traced

• Replay-mode for re-running generated traces

Page 13: Deja vu  JavaZone 2013

Replay

1 public class ExampleTest{

2

3 @Test

4 public void exampletest() throws Throwable {

5 TraceBuilder builder = TraceBuilder.build().

6 setMethod(Example.class);

7

8 builder.run();

9 }

10 }

Page 14: Deja vu  JavaZone 2013

Part III

Reproducability

Page 15: Deja vu  JavaZone 2013

@Impure

• Randomized/Non-deterministic

• Environment dependent

• Threading

Page 16: Deja vu  JavaZone 2013

Rules of the game

• Immutable

• Must be serializable++

Page 17: Deja vu  JavaZone 2013

Lucky number bug

1 public class AlmostWorking {

2

3 @Traced

4 public void getLucky() {

5 Long value = timeStamp();

6 Long luckyNumber = 2304432 / ( value % 1001 );

7 System.out.println( "My lucky number is: "+luckyNumber);

8 }

9

10 @Impure

11 private Long timeStamp() {

12 return System.nanoTime();

13 }

14 }

Page 18: Deja vu  JavaZone 2013

Overhead

• Execution overhead in intercepting calls

• Memory overhead in storing ”impure” values

• Immutable overhead

Page 19: Deja vu  JavaZone 2013

Part IV

Threads

Page 20: Deja vu  JavaZone 2013

Threads

• Non-deterministic!

• For pure parts it doesn’t matter

Page 21: Deja vu  JavaZone 2013

@AttachThread

• Includes a thread in a trace

• Trace is not done until trace method is executed...

• ...and all attached threads are done executing

• Impure parts are replayed in same order

Page 22: Deja vu  JavaZone 2013

Example

1 public class WithThreads {

2

3 private ExecutorService executorService;

4

5 public void initialize() {

6 executorService = Executors.newCachedThreadPool();

7 }

8

9 @Traced

10 public void begin( Integer threads) {

11 initialize();

12 for ( int i=0; i<threads; i++ ) {

13 runInThreadPool(new Runner());

14 }

15 }

16

17 @AttachThread

18 private void runInThreadPool( Runnable runnable ) {

19 executorService.submit(runnable);

20 }

21 }

Page 23: Deja vu  JavaZone 2013

Part V

Example Architecture

Page 24: Deja vu  JavaZone 2013

Usecases

• Trace on each ’usecase’

• Usecase becomes a programming concept

Page 25: Deja vu  JavaZone 2013

Thank you

• https://github.com/madsenevoldsen/dejavu

• http://www.jayway.com/2013/01/09/deja-vu/