concurrency (fisher syer s2gx 2010)

65
Chicago, October 19 - 22, 2010 Concurrent Programming and Distributed Applications Mark Fisher, Dave Syer - SpringSource

Upload: dave-syer

Post on 15-Jan-2015

3.624 views

Category:

Technology


1 download

DESCRIPTION

Concurrent and Distributed Applications with Spring (from SpringOne2GX 2010)

TRANSCRIPT

Page 1: Concurrency (Fisher Syer S2GX 2010)

Chicago, October 19 - 22, 2010

Concurrent Programming and Distributed Applications

Mark Fisher, Dave Syer - SpringSource

Page 2: Concurrency (Fisher Syer S2GX 2010)

Goal

● Demystify concurrent and distributed programming.● Identify and help avoid the pitfalls● Show how Spring makes it easier to write multi-

threaded and multi-process applications

Page 3: Concurrency (Fisher Syer S2GX 2010)

Agenda

• Concurrency• Asynchronous execution• Tasks, schedules and triggers• Events, Messaging and intra-process communication• Distributed systems

Page 4: Concurrency (Fisher Syer S2GX 2010)

Concurrency

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Why? - Performance - Responsiveness - Scalability

Where? - Event-driven Architecture - Scheduling Tasks

Page 5: Concurrency (Fisher Syer S2GX 2010)

When threads were more esoteric,concurrency was an "advanced" topic;now, mainstream developers must beaware of thread-safety issues.

--Brian Goetz

Page 6: Concurrency (Fisher Syer S2GX 2010)

Thread Safe?

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public class Counter {

private boolean active = false;

public boolean isActive() { return active; }

public void setActive(boolean active) { this.active = active; }}

Page 7: Concurrency (Fisher Syer S2GX 2010)

Thread Safe?

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public class Counter {

private volatile int count = 0;

public int increment() { return count++; }}

Page 8: Concurrency (Fisher Syer S2GX 2010)

Thread Safe?

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public class Service {

private volatile Resource resource;

public void process(String data) { if (this.resource == null) { this.resource = new Resource(); } this.resource.process(data); }}

Page 9: Concurrency (Fisher Syer S2GX 2010)

Thread Safety

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

What?Properly managing concurrent access to mutable state.

How?a) avoid mutabilityb) don't sharec) synchronize

Page 10: Concurrency (Fisher Syer S2GX 2010)

Immutable?public class StringHolder {

private final int id; private final String value;

public StringHolder(int id, String value) { this.id = id; this.value = value; }

public void display() { Sysem.out.println(id + ": " + this.value); }}

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Page 11: Concurrency (Fisher Syer S2GX 2010)

Immutable?public class StringList {

private final int id; private final List<String> strings;

public StringList(int id, List<String> strings) { this.id = id; this.strings = strings; }

public void display() { Sysem.out.println(id + ": " + this.strings); }}

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Page 12: Concurrency (Fisher Syer S2GX 2010)

Immutable?public class MapHolder {

private final Map<Integer, StringValue> map;

public MapHolder(Map<Integer, StringValue> map) { this.map = new HashMap<Integer, StringValue>(map); }

public void display() { System.out.println(this.map); }}

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Page 13: Concurrency (Fisher Syer S2GX 2010)

Immutable?

private final Map<Integer, StringValue> map;

public MapHolder(Map<Integer, StringValue> map) { this.map = new HashMap<Integer, StringValue>(map); }

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public static class StringValue { private String string; public StringValue(String string) { this.string = string; }

public void setString(String string) { this.string = string; }

}

public static class StringValue { private String string; public StringValue(String string) { this.string = string; }

public void setString(String string) { this.string = string; }

}

Page 14: Concurrency (Fisher Syer S2GX 2010)

Immutable?

private final Map<Integer, StringValue> map;

public MapHolder(Map<Integer, StringValue> map) { this.map = new HashMap<Integer, StringValue>(map); }

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public static class StringValue { private final String string; public StringValue(String string) { this.string = string; }

}

public static class StringValue { private final String string; public StringValue(String string) { this.string = string; }

}

Page 15: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 16: Concurrency (Fisher Syer S2GX 2010)

Thread Safety: Confinement

When immutability is not an option, consider not sharing mutable state

• Stack Confinement– Method Parameters– Local Variables

• Thread Confinement– ThreadLocal– Custom Scopes in Spring

Page 17: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 18: Concurrency (Fisher Syer S2GX 2010)

Synchronization

• The 'synchronized' keyword• Locks• wait/notify• Atomic variables

Page 19: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 20: Concurrency (Fisher Syer S2GX 2010)

Lazy Initialization (recap)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public class Service {

private volatile Resource resource;

public void process(String data) { if (this.resource == null) { this.resource = new Resource(); } this.resource.process(data); }}

Page 21: Concurrency (Fisher Syer S2GX 2010)

Dependency Injection and Singletons

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

public class Service {

private volatile Resource resource;

public void setResource(Resources resource) { this.resource = resource; }

public void process(String data) { this.resource.process(data); }}

Page 22: Concurrency (Fisher Syer S2GX 2010)

Concurrency

• Background processing and performance optimization• Java Memory Model: final, volatile, synchronized• Locks: wait, notify, utility wrappers• Concurrent collections, immutable wrappers• Queue, BlockingQueue, DelayQueue• Deadlock, livelock, starvation• Immutability, stateless components, "hidden" state, thread

safety• Typical Spring application concerns• Stateful components in Spring

Page 23: Concurrency (Fisher Syer S2GX 2010)

Executor and ExecutorService

JDK Functionality• Configurable thread pools• Execute Runnables asynchronously• Submit Callable<V> that returns Future<V>

Page 24: Concurrency (Fisher Syer S2GX 2010)

Async Execution Considerations

• Blocking and timeouts• Cancellation• Interruption• Thread Pool Rejection Policies• Excessive thread creation• Context-switching overhead

Page 25: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 26: Concurrency (Fisher Syer S2GX 2010)

Asynchronous Execution

• Futures, executors, completion service and thread pools• @Async• Lifecycle and SmartLifecycle, cf. InitializingBean• Gateways with Futures• Timeouts

Page 27: Concurrency (Fisher Syer S2GX 2010)

Spring Support for Task Management

• TaskExecutor• TaskScheduler and Trigger• @Async• @Scheduled• File-Polling adapter in Spring Integration• Message listener containers

– JMS– AMQP– Spring Integration– GemFire

• Spring Batch

Page 28: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 29: Concurrency (Fisher Syer S2GX 2010)

Events and Intraprocess Communication

• The observer pattern: application and framework design• ApplicationEvent and ApplicationListener• Messaging• SEDA• Ordering• Stateful Messaging patterns

Page 30: Concurrency (Fisher Syer S2GX 2010)

Observer Pattern

Publisher

invoke(“input”)

confirmation

Multicaster

fire(“input”)

observe(“input”)

Observer

Event

Page 31: Concurrency (Fisher Syer S2GX 2010)

ApplicationListener

// This is a listener

public class TransferListener implements

ApplicationListener<TransferEvent> {

public onApplicationEvent(TransferEvent event) {

this.auditLogger.log(event.getTransfer());

}

}

ApplicationEvent

java.util.Event

Page 32: Concurrency (Fisher Syer S2GX 2010)

ApplicationEventPublisher

public class TransferService implements

ApplicationEventPublisherAware {

public setApplicationEventPublisher(

ApplicationEventPublisher publisher) {

this.publisher = publisher;

}

public void transfer(Transfer transfer) {

...

this.publisher.publish(new TransferEvent(transfer));

}

Page 33: Concurrency (Fisher Syer S2GX 2010)

The Observer Pattern

Publisher

Multicaster

ObserverObserverObserverObserverApplicationListener

ApplicationContext

ApplicationEventPublisher

Page 34: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 35: Concurrency (Fisher Syer S2GX 2010)

Messaging

• Decouple Producers and Consumers

• Observer Pattern but often with extra semantics– header and payload– publish-subscribe or point-to-point– acks and nacks and other protocol details– persistence and quality of service– once-only or at-least-once delivery

widgets

Page 36: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 37: Concurrency (Fisher Syer S2GX 2010)

Staged Event Driven Architecture (SEDA)

Page 38: Concurrency (Fisher Syer S2GX 2010)

Staged Event Driven Architecture (SEDA)

Page 39: Concurrency (Fisher Syer S2GX 2010)

Staged Event Driven Architecture (SEDA)

Page 40: Concurrency (Fisher Syer S2GX 2010)

Staged Event Driven Architecture (SEDA)

Dispatcher

Queue

Page 41: Concurrency (Fisher Syer S2GX 2010)

Ordering

• Multi-threaded applications in general do not preserve order

• E.g. Messaging– Producer sends two widgets 1, 2– Consumer receives 2, 1

• To preserve order expect some overhead– Storage (stateful patterns)– Processing time (wait for out of order messages)

Page 42: Concurrency (Fisher Syer S2GX 2010)

Stateful Messaging Patterns

• Scatter-Gather• Composed Message Processor• Claim Check

• Publish-Subscribe Channel• Recipient List Router• Splitter• Multi-valued Router

• Aggregator• Resequencer

Page 43: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 44: Concurrency (Fisher Syer S2GX 2010)

Quality of Service, Transactions

Asynchronous hand off = potential lost messages

Page 45: Concurrency (Fisher Syer S2GX 2010)

Quality of Service, Transactions

Transactional (receive, send) = no lost messages

Page 46: Concurrency (Fisher Syer S2GX 2010)

Distributed Applications

• RPC, Spring Remoting• Messaging• Middleware: HTTP, JDBC, JMS, AMQP• Latency• QoS, transactions, durability and guaranteed delivery• Distributed data: partitioning and eventual consistency• Gemfire• Spring Integration adapters

Page 47: Concurrency (Fisher Syer S2GX 2010)

Distributed Application or System

• Application:– Multiple JVM process nodes– Same binaries– Same release schedule

• System:– Multiple JVM process nodes– Different binaries– Different release schedule per node

• Blurred boundary if Application has to have a rolling upgrade with no downtime

Page 48: Concurrency (Fisher Syer S2GX 2010)

Patterns of Distributed Transactions

• Full XA with 2PC• XA with the 1PC Optimisation• XA and the Last Resource Gambit• Shared Transaction Resource• Best Efforts 1PC• Non-transactional Access• Wing and a Prayer (anti-pattern)

http://www.javaworld.com/javaworld/jw-01-2009/jw-01-spring-transactions.html

JTA + Spring

Spring Data + Oracle

Spring JMS

Page 49: Concurrency (Fisher Syer S2GX 2010)

Remote Procedure Call (RPC)

• RPC is synchronous inter-process communication• Only evil if the system is not an Application• Transports:

– RMI– HTTP– JMS– JDBC– ...

• Spring Remoting helps strategise and configure: defers important architectural choices

• Spring Integration provides additional options

Page 50: Concurrency (Fisher Syer S2GX 2010)

Messaging

• Messaging is asynchronous inter-process communication• Can use Messaging to implement RPC• Transports (some point-to-point only):

– RMI– HTTP– JMS– JDBC– AMQP– ...

• Spring Integration: helps strategise and configure: defers important architectural choices

Page 51: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 52: Concurrency (Fisher Syer S2GX 2010)

Latency

• All distributed systems and applications suffer from latency– EU-US = 6000km, min network latency 6e6/3e8 = 20ms– Add marshal/unmarshal overhead (variable)– Transaction (min 50ms)

• Often much worse, e.g. >100ms even on local network• Request-reply patterns double the problem• Over-modularization: each inter-process hop is expensive• Abstractions (e.g. Spring) are dangerous• Measure and analyse

Page 53: Concurrency (Fisher Syer S2GX 2010)

Distributed Application and Data

Page 54: Concurrency (Fisher Syer S2GX 2010)

Nosql & Spring Data

• Key value stores– redis, gemfire– coherence– riak, voldemort, memcached

• Document stores– couchdb, mongodb

• Sparse table or column stores– cassandra, bigtable

• Graph or object stores– neo4j

• Distributed filesystem– hdf

Page 55: Concurrency (Fisher Syer S2GX 2010)

SpringSource Gemfire

• Distributed data cache, datastore and compute grid• Java (so embeddable)• Low-level API is java.util.Map• Many high-level abstractions

– Transactions– Functions and node affinity– Events, continuous queries– Replication

• Spring Gemfire:http://git.springsource.org/spring-gemfire

Page 56: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Demo

Page 57: Concurrency (Fisher Syer S2GX 2010)

SpringOne 2GX 2010. All rights reserved. Do not distribute without permission.

Q&A

Source code for demos:

http://git.springsource.org/s2gx-2010/concurrent-programming-distributed-applications

Page 58: Concurrency (Fisher Syer S2GX 2010)

Distributed Transaction: Sunny Day

1. Start messaging transaction2. Receive message3. Start database transaction4. Update database5. Commit database transaction6. Commit messaging transaction

Page 59: Concurrency (Fisher Syer S2GX 2010)

Distributed Transaction: Rollback

1. Start messaging transaction2. Receive message3. Start database transaction4. Update database, fail!5. Rollback database transaction6. Rollback messaging transaction

Page 60: Concurrency (Fisher Syer S2GX 2010)

Distributed Transaction: Partial Failure

1. Start messaging transaction2. Receive message3. Start database transaction4. Update database5. Commit database transaction6. Commit messaging transaction, fail!

Page 61: Concurrency (Fisher Syer S2GX 2010)

Distributed Data: Shared Database

Application

Cache

Page 62: Concurrency (Fisher Syer S2GX 2010)

Distributed Data: Cache Overflow

Cache

Page 63: Concurrency (Fisher Syer S2GX 2010)

Distributed Cache: Database Meltdown

Page 64: Concurrency (Fisher Syer S2GX 2010)

Distributed Data

Page 65: Concurrency (Fisher Syer S2GX 2010)

Distributed Data