1. Software Transactional Memory
Курс «Базы данных»
Цесько Вадим Александрович
http://incubos.org
@incubos
Computer Science Center
9 декабря 2013 г.
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
1 / 63
3. Введение
Мотивация
Мотивация
Задача
Множество изменяемых объектов в памяти
Атомарность наборов операций с объектами
Решение
Software Transactional Memorya :
Память как транзакционное хранилище
Универсальная альтернатива ручным блокировкам
ACI
a
http:
//en.wikipedia.org/wiki/Software_transactional_memory
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
3 / 63
4. Введение
Идея
Идея
(Очень) оптимистичные транзакции
Если прочитанные значения не менялись, то
commit
В противном случае — retry
Возможен abort в любой момент
Как следствие – max concurrency
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
4 / 63
5. Введение
Идея
Ручные блокировки
Нужно думать о перекрывающихся операциях
Нужно «держать в голове» весь код
Deadlocks, livelocks, progress, etc.
Очень трудно воспроизвести и отладить
Priority inversion1
1
http://en.wikipedia.org/wiki/Priority_inversion
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
5 / 63
6. Введение
Идея
Подход STM
Simple
Maintainable
Composable
Не нужно думать о deadlocks и livelocks
Priority inversion — abort низкоприоритетной
транзакции
Но (почти) недопустимы side effects (в т. ч. IO)
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
6 / 63
8. Введение
Реализации
Scala STM Expert Group
Akka
Stanford
Tel-Aviv University
EPFL
Cisco
etc.
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
8 / 63
9. Введение
ScalaSTM
С высоты птичьего полёта
Живёт между atomic-блоком и Heap
Перехватывает чтения и записи
Чтения и записи из разных потоков
перемешались — rollback записей и retry
Иначе — commit
Видны только закоммиченые изменения
Принимаем во внимание только Refы
Реализация в виде библиотеки
TSet и TMap
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
9 / 63
10. Введение
ScalaSTM
Достоинства
Say what you mean
(nested) atomic
Readers scale
CPU cache friendly
Exceptions automatically trigger cleanup
By default
Waiting for complex conditions is easy
retry, chaining
Simple
Just a library
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
10 / 63
11. Введение
ScalaSTM
Недостатки
Two extra characters per read or write
x: Ref
Read: x()
Write: x() = y
Single-thread overheads
Но полезен rollback при исключениях
Rollback doesn’t mix well with I/O
Но есть хуки
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
11 / 63
12. Примеры
API
Basic
1
import scala.concurrent.stm._
2
3
4
5
val x = Ref(0) // allocate a Ref[Int]
val y = Ref.make[String]() // type-specific default
val z = x.single // Ref.View[Int]
6
7
8
9
10
11
12
13
14
15
16
atomic { implicit txn =>
val i = x() // read
y() = "x was " + i // write
val eq = atomic { implicit txn => // nested atomic
// both Ref and Ref.View can be used inside atomic
x() == z()
}
assert(eq)
y.set(y.get + ", long-form access")
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
12 / 63
13. Примеры
API
Advanced
1
2
3
// only Ref.View can be used outside atomic
println("y was ’" + y.single() + "’")
println("z was " + z())
4
5
6
7
8
9
10
11
atomic { implicit txn =>
y() = y() + ", first alternative"
if (x getWith { _ > 0 }) // read via a function
retry // try alternatives or block
} orAtomic { implicit txn =>
y() = y() + ", second alternative"
}
12
13
14
15
16
17
val prev = z.swap(10) // atomic swap
val success = z.compareAndSet(10, 11) // atomic compare-and-set
z.transform { _ max 20 } // atomic transformation
val pre = y.single.getAndTransform { _.toUpperCase }
val post = y.single.transformAndGet { _.filterNot { _ == ’ ’ } }
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
13 / 63
14. Примеры
Doubly-linked list
Use Ref for shared variables
1
import scala.concurrent.stm._
2
3
4
5
6
7
8
9
10
11
class ConcurrentIntList {
private class Node(
val elem: Int,
prev0: Node,
next0: Node) {
val isHeader = prev0 == null
val prev = Ref(if (isHeader) this else prev0)
val next = Ref(if (isHeader) this else next0)
}
12
13
private val header = new Node(-1, null, null)
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
14 / 63
15. Примеры
Doubly-linked list
Wrap your code in atomic
1
2
3
4
5
6
7
8
def addLast(elem: Int) {
atomic { implicit txn =>
val p = header.prev()
val newNode = new Node(elem, p, header)
p.next() = newNode
header.prev() = newNode
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
15 / 63
16. Примеры
Doubly-linked list
Compose atomic operations
1
2
3
4
5
6
7
def addLast(e1: Int, e2: Int, elems: Int*) {
atomic { implicit txn =>
addLast(e1)
addLast(e2)
elems foreach { addLast(_) }
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
16 / 63
17. Примеры
Doubly-linked list
Optimize single-operation transactions
Ref.View:
Получение через Ref.single
Можно использовать вне atomic
Поддерживает транзакции из одной операции
swap, compareAndSet, transform, etc.
Пример:
1
2
3
4
5
/*
def isEmpty = atomic { implicit t =>
header.next() == header
}
*/
6
7
def isEmpty = header.next.single() == header
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
17 / 63
18. Примеры
Doubly-linked list
Wait for conditions to change
1
2
3
4
5
6
7
8
9
10
def removeFirst(): Int =
atomic { implicit txn =>
val n = header.next()
if (n == header)
retry
val nn = n.next()
header.next() = nn
nn.prev() = header
n.elem
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
18 / 63
19. Примеры
Doubly-linked list
Wait for multiple events
1
2
3
4
5
6
7
def maybeRemoveFirst(): Option[Int] = {
atomic { implicit txn =>
Some(removeFirst())
} orAtomic { implicit txn =>
None
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
19 / 63
20. Примеры
Doubly-linked list
Composition: select
1
2
3
4
5
6
7
8
9
10
11
12
object ConcurrentIntList {
def select(stacks: ConcurrentIntList*):
(ConcurrentIntList, Int) =
atomic { implicit txn =>
for (s <− stacks) {
s.maybeRemoveFirst() match {
case Some(e) => return (s -> e)
case None => _
}
}
retry
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
20 / 63
21. Примеры
Doubly-linked list
Be careful about rollback (1)
1
2
3
4
5
6
7
8
9
10
11
12
def badToString: String = {
val buf = new StringBuilder("ConcurrentIntList(")
atomic { implicit txn =>
var n = header.next()
while (n != header) {
buf ++= n.elem.toString
n = n.next()
if (n != header) buf ++= ","
}
}
buf ++= ")" toString
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
21 / 63
22. Примеры
Doubly-linked list
Be careful about rollback (2)
1
2
3
4
5
6
7
8
9
10
11
12
override def toString: String = {
atomic { implicit txn =>
val buf = new StringBuilder("ConcurrentIntList(")
var n = header.next()
while (n != header) {
buf ++= n.elem.toString
n = n.next()
if (n != header) buf ++= ","
}
buf ++= ")" toString
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
22 / 63
24. Примеры
Indexed Map
API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
scala> case class User(id: Int, name: String, likes: Set[String])
scala> val m = new IndexedMap[Int, User]
scala> m.put(10, User(10, "alice", Set("scala", "climbing")))
res0: Option[User] = None
scala> val byName = m.addIndex { (id,u) => Some(u.name) }
byName: (String) => Map[Int,User] = <function1>
scala> val byLike = m.addIndex { (id,u) => u.likes }
byLike: (String) => Map[Int,User] = <function1>
scala> m.put(11, User(11, "bob", Set("scala", "skiing")))
res1: Option[User] = None
scala> byName("alice")
res2: Map[Int,User] = Map((10,User(10,alice,Set(scala,
climbing))))
scala> byLike("scala").values map { _.name }
res3: Iterable[String] = List(alice, bob)
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
24 / 63
25. Примеры
Indexed Map
A high-level sketch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import scala.concurrent.stm._
class IndexedMap[A, B] {
private val contents = TMap.empty[A, B]
// TODO def addIndex(view: ?): ?
def get(key: A): Option[B] = contents.single.get(key)
def put(key: A, value: B): Option[B] =
atomic { implicit txn =>
val prev = contents.put(key, value)
// TODO: update indices
prev
}
def remove(key: A): Option[B] =
atomic { implicit txn =>
val prev = contents.remove(key)
// TODO: update indices
prev
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
25 / 63
26. Примеры
Indexed Map
Types for the view function and index
Помедитируем:
1
2
def addIndex(view: ((A, B) => Iterable[C])):
(C => Map[A, B]) = ...
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
26 / 63
27. Примеры
Indexed Map
Tracking and updating indices (1)
1
2
3
4
5
private class Index[C](view: (A, B) => Iterable[C])
extends (C => Map[A, B]) {
def += (kv: (A, B)) // TODO
def -= (kv: (A, B)) // TODO
}
6
7
private val indices = Ref(List.empty[Index[_]])
8
9
10
11
12
13
14
15
16
def addIndex[C](view: (A, B) => Iterable[C]):
(C => Map[A, B]) =
atomic { implicit txn =>
val index = new Index(view)
indices() = index :: indices()
contents foreach { index += _ }
index
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
27 / 63
28. Примеры
Indexed Map
Tracking and updating indices (2)
1
2
3
4
5
6
7
def put(key: A, value: B): Option[B] =
atomic { implicit txn =>
val prev = contents.put(key, value)
for (p <− prev; i <− indices()) i -= (key -> p)
for (i <− indices()) i += (key -> value)
prev
}
8
9
10
11
12
13
14
def remove(key: A): Option[B] =
atomic { implicit txn =>
val prev = contents.remove(key)
for (p <− prev; i <− indices()) i -= (key -> p)
prev
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
28 / 63
29. Примеры
Indexed Map
Index internals
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private class Index[C](view: (A, B) => Iterable[C])
extends (C => Map[A, B]) {
val mapping = TMap.empty[C, Map[A, B]]
def apply(derived: C) =
mapping.single.getOrElse(derived, Map.empty[A, B])
def += (kv: (A, B))(implicit txn: InTxn) {
for (c <− view(kv._1, kv._2))
mapping(c) = apply(c) + kv
}
def -= (kv: (A, B))(implicit txn: InTxn) {
for (c <− view(kv._1, kv._2)) {
val after = mapping(c) - kv._1
if (after.isEmpty)
mapping -= c
else
mapping(c) = after
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
29 / 63
31. Примеры
Dining Philosophers
STM solution
1
class Fork { val inUse = Ref(false) }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def meal(left: Fork, right: Fork) {
// thinking
atomic { implicit txn =>
if (left.inUse() || right.inUse())
retry // forks are not both ready, wait
left.inUse() = true
right.inUse() = true
}
// eating
atomic { implicit txn =>
left.inUse() = false
right.inUse() = false
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
31 / 63
32. Internals
Waiting
Waiting
retry в atomic ≈ wait() в synchronized, но retry:
Безопаснее
STM определяет модификации Ref, ведущие к
пробуждению (вместо notifyAll)
Невозможны «потерянные» пробуждения
Эффективнее
Нет «лишних» пробуждений
Можно ожидать на любых условиях, а не только на
предопределённых
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
32 / 63
33. Internals
Waiting
Search with backtracking (1)
Optimistic concurrency control as a search with
backtracking:
1
val (x, y) = (Ref(10), Ref(0))
2
3
4
5
6
7
def sum = atomic { implicit txn =>
val a = x()
val b = y()
a + b
}
8
9
10
11
12
13
14
def transfer(n: Int) {
atomic { implicit txn =>
x -= n
y += n
}
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
33 / 63
34. Internals
Waiting
Search with backtracking (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// sum
atomic
| begin txn
| | read x
| |
:
| |
| |
:
| |
| | read y
| roll back
| begin txn
| | read x
| | read y
| commit
+-> 10
attempt
-> 10
-> x read is invalid
// transfer(2)
atomic
| begin txn attempt
| | read x -> 10
| | write x <− 8
| | read y -> 0
| | write y <− 2
| commit
+-> ()
attempt
-> 8
-> 2
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
34 / 63
35. Internals
Waiting
Retry. Семантика
Вызов retry — сигнал о dead end, даже если все
чтения и записи консистентны
STM откатится и попробует снова
Если некоторые из прочитанных Refов
изменились, то atomic блок может пойти по
другому пути и избежать retry
Условие ожидания retry
Неявно задано потоком управления в atomic-блоке
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
35 / 63
36. Internals
Waiting
Retry. Эффективность
Примеры:
if (x() <= 10) retry
if (x() == 0 && y() == 0 && z() == 0)
retry
Реализация:
STM отслеживает, к каким Refам обращались
Под капотом — блокирующиеся конструкции
retry: Nothing
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
36 / 63
38. Internals
Waiting
Alternatives. Пример
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
val msg = atomic { implicit txn =>
if (x() == 0)
retry
x -= 1
"took one from x"
} orAtomic { implicit txn =>
if (y() == 0)
retry
y -= 1
"took one from y"
} orAtomic { implicit txn =>
if (z() == 0)
retry
z -= 1
"took one from z"
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
38 / 63
39. Internals
Waiting
Timeouts
Зачем timeout при retry:
Error logging/handling
No work — waiting thread shutdown
Timeouts in spec of higher-level interface
Способы ограничения retry:
Модифицированный TxnExecutor
(InterruptedException)
retryFor()
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
39 / 63
40. Internals
Waiting
Timeouts. TxnExecutor
1
2
3
atomic.withRetryTimeout(1000) { implicit txn =>
// any retries in this atomic block will wait for at most
1000 milliseconds
}
4
5
6
7
8
9
val myAtomic = atomic.withRetryTimeout(1, TimeUnit.SECONDS)
myAtomic { implicit txn =>
// this atomic block has a timeout of 1 seconds
}
myAtomic { ... }
10
11
12
13
14
TxnExecutor.transformDefault( _.withRetryTimeout(1000) )
atomic { implicit txn =>
// all atomic blocks now default to a 1 second timeout
}
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
40 / 63
43. Internals
Maps + Sets
Maps + Sets
Консистентные итераторы у TMap.View и
TSet.View
Быстрые слепки за O(1)
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
43 / 63
44. Internals
Maps + Sets
Consistent iteration
TMap.View extends mutable.MapLike
get()/put() outside atomic should be atomic
TMap.View.iterator/TSet.View.iterator для
атомарного слепка
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
44 / 63
45. Internals
Maps + Sets
Inconsistent iteration
1
val m = TMap("one" -> 1).single
2
3
4
5
6
7
8
(new Thread { override def run {
atomic { implicit txn =>
m -= "one"
m += ("ONE" -> 1)
}
} }).start
9
10
for ((k, v) <− m; if v == 1) println(k)
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
45 / 63
46. Internals
Maps + Sets
Manual snapshots
<TMap|TSet>[.View].snapshot() возвращает
immutable.Map/immutable.Set
<TMap|TSet>.clone()
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
46 / 63
47. Internals
Maps + Sets
How does it work?
Mutable hash tries from Refs with generation
numbers that control copy-on-write
N. G. Bronson, J. Casper, H. Chafi and K. Olukotun. A
Practical Concurrent Binary Search Tree. 2010.
N. G. Bronson, J. Casper, H. Chafi and K. Olukotun.
Transactional Predication: High-Performance
Concurrent Sets and Maps for STM. In PODC’10:
Proceedings of the 29th Annual ACM Conference on
Principles of Distributed Computing, 2010.
N. G. Bronson. Composable Operations on
High-Performance Concurrent Collections. Ph.D.
Dissertation, Stanford University, 2011.
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
47 / 63
51. Заключение
Benchmarking
STMBench7
R. Guerraoui, M. Kapalka and J. Vitek. STMBench7:
A Benchmark for Software Transactional Memory.
2007.
A. Dragojevic, R. Guerraoui and M. Kapalka. Dividing
Transactional Memories by Zero. 2008.
Comparison to coarse- and medium-grained locking
See the details4
4
http://nbronson.github.io/scala-stm/benchmark.html
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
51 / 63
57. Заключение
Bibliography
Bibliography
F. T. Schneider, V. Menon, T. Shpeisman, and
A.-R. Adl-Tabatabai. Dynamic Optimization for
Efficient Strong Atomicity. 2008.
N. G. Bronson, C. Kozyrakis and K. Olukotun.
Feedback-Directed Barrier Optimization in a Strongly
Isolated STM. 2009
V. Menon, S. Balensieger, T. Shpeisman,
A.-R. Adl-Tabatabai, R. L. Hudson, B. Saha and
A. Welc. Practical Weak-Atomicity Semantics for
Java STM. 2008.
K. F. Moore and D. Grossman. High-Level Small-Step
Operational Semantics for transactions. 2008.
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
57 / 63
58. Заключение
Bibliography continued
Bibliography continued
T. Harris, S. Marlow, S. Peyton-Jones and M. Herlihy.
Composable Memory Transactions. 2005.
R. Hickey. The Clojure Programming Language. 2008
D. Dice, O. Shalev and N. Shavit. Transactional
Locking II. 2006.
T. Riegel, P. Felber and C. Fetzer. A Lazy Snapshot
Algorithm with Eager Validation. 2006.
R. Guerraoui and M. Kapalka. On the Correctness of
Transactional Memory. 2008.
A. Dragojevic, R. Guerraoui and M. Kapalka.
Stretching Transactional Memory. 2009.
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
58 / 63
59. Заключение
Куда двигаться дальше
Куда двигаться дальше
Herb Sutter. The Free Lunch Is Over. 2009.
Jonas Bon´r. State: You’re Doing It Wrong —
e
Alternative Concurrency Paradigms For The JVM5 .
JavaOne 2009.
ScalaDays 2013. Concurrency — The good, the bad,
the ugly6
Chris Okasaki. Purely Functional Data Structures.
1999
JCIP 2nd edition + JMM
5
http://www.slideshare.net/jboner/
state-youre-doing-it-wrong-javaone-2009
6
http://www.parleys.com/play/51c0bc58e4b0ed877035680a/
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
59 / 63
61. Домашнее задание
Могут получить зачёт
Могут получить зачёт
Бакрадзе Л.
Грязнов С.
Егоров Д.
Королев Д.
Хомутов В.
Шашкова Е.
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
61 / 63
62. Домашнее задание
Last Feature Requests
Last Feature Requests
2013-11-18
2013-11-25
2013-12-02
Batches
2013-12-09
Processing
7
FR8: Redis Hashes7
FR9: Compression
FR10: Multithreaded Node + Atomic
FR11: Server Side Filtering and
http://redis.io/commands#hash
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
62 / 63
63. Вопросы?
Вопросы?
http://incubos.org/contacts/
Общие вопросы — в Twitter: @incubos
Вопросы по лекциям — в комментариях:
http://incubos.org/blog/
Частные вопросы — в почту
vadim.tsesko@gmail.com
Цесько В. А. (CompSciCenter)
STM
9 декабря 2013 г.
63 / 63