1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
import collection._
import collection.concurrent.TrieMap
import Test.Spec
object SnapshotSpec extends Spec {
def test() {
"support snapshots" in {
val ctn = new TrieMap
ctn.snapshot()
ctn.readOnlySnapshot()
val ct = new TrieMap[Int, Int]
for (i <- 0 until 100) ct.put(i, i)
ct.snapshot()
ct.readOnlySnapshot()
}
"empty 2 quiescent snapshots in isolation" in {
val sz = 4000
class Worker(trie: TrieMap[Wrap, Int]) extends Thread {
override def run() {
for (i <- 0 until sz) {
assert(trie.remove(new Wrap(i)) == Some(i))
for (j <- 0 until sz)
if (j <= i) assert(trie.get(new Wrap(j)) == None)
else assert(trie.get(new Wrap(j)) == Some(j))
}
}
}
val ct = new TrieMap[Wrap, Int]
for (i <- 0 until sz) ct.put(new Wrap(i), i)
val snapt = ct.snapshot()
val original = new Worker(ct)
val snapshot = new Worker(snapt)
original.start()
snapshot.start()
original.join()
snapshot.join()
for (i <- 0 until sz) {
assert(ct.get(new Wrap(i)) == None)
assert(snapt.get(new Wrap(i)) == None)
}
}
def consistentReadOnly(name: String, readonly: Map[Wrap, Int], sz: Int, N: Int) {
@volatile var e: Exception = null
// reads possible entries once and stores them
// then reads all these N more times to check if the
// state stayed the same
class Reader(trie: Map[Wrap, Int]) extends Thread {
setName("Reader " + name)
override def run() =
try check()
catch {
case ex: Exception => e = ex
}
def check() {
val initial = mutable.Map[Wrap, Int]()
for (i <- 0 until sz) trie.get(new Wrap(i)) match {
case Some(i) => initial.put(new Wrap(i), i)
case None => // do nothing
}
for (k <- 0 until N) {
for (i <- 0 until sz) {
val tres = trie.get(new Wrap(i))
val ires = initial.get(new Wrap(i))
if (tres != ires) println(i, "initially: " + ires, "traversal %d: %s".format(k, tres))
assert(tres == ires)
}
}
}
}
val reader = new Reader(readonly)
reader.start()
reader.join()
if (e ne null) {
e.printStackTrace()
throw e
}
}
// traverses the trie `rep` times and modifies each entry
class Modifier(trie: TrieMap[Wrap, Int], index: Int, rep: Int, sz: Int) extends Thread {
setName("Modifier %d".format(index))
override def run() {
for (k <- 0 until rep) {
for (i <- 0 until sz) trie.putIfAbsent(new Wrap(i), i) match {
case Some(_) => trie.remove(new Wrap(i))
case None => // do nothing
}
}
}
}
// removes all the elements from the trie
class Remover(trie: TrieMap[Wrap, Int], index: Int, totremovers: Int, sz: Int) extends Thread {
setName("Remover %d".format(index))
override def run() {
for (i <- 0 until sz) trie.remove(new Wrap((i + sz / totremovers * index) % sz))
}
}
"have a consistent quiescent read-only snapshot" in {
val sz = 10000
val N = 100
val W = 10
val ct = new TrieMap[Wrap, Int]
for (i <- 0 until sz) ct(new Wrap(i)) = i
val readonly = ct.readOnlySnapshot()
val threads = for (i <- 0 until W) yield new Modifier(ct, i, N, sz)
threads.foreach(_.start())
consistentReadOnly("qm", readonly, sz, N)
threads.foreach(_.join())
}
// now, we check non-quiescent snapshots, as these permit situations
// where a thread is caught in the middle of the update when a snapshot is taken
"have a consistent non-quiescent read-only snapshot, concurrent with removes only" in {
val sz = 1250
val W = 100
val S = 5000
val ct = new TrieMap[Wrap, Int]
for (i <- 0 until sz) ct(new Wrap(i)) = i
val threads = for (i <- 0 until W) yield new Remover(ct, i, W, sz)
threads.foreach(_.start())
for (i <- 0 until S) consistentReadOnly("non-qr", ct.readOnlySnapshot(), sz, 5)
threads.foreach(_.join())
}
"have a consistent non-quiescent read-only snapshot, concurrent with modifications" in {
val sz = 1000
val N = 7000
val W = 10
val S = 7000
val ct = new TrieMap[Wrap, Int]
for (i <- 0 until sz) ct(new Wrap(i)) = i
val threads = for (i <- 0 until W) yield new Modifier(ct, i, N, sz)
threads.foreach(_.start())
for (i <- 0 until S) consistentReadOnly("non-qm", ct.readOnlySnapshot(), sz, 5)
threads.foreach(_.join())
}
def consistentNonReadOnly(name: String, trie: TrieMap[Wrap, Int], sz: Int, N: Int) {
@volatile var e: Exception = null
// reads possible entries once and stores them
// then reads all these N more times to check if the
// state stayed the same
class Worker extends Thread {
setName("Worker " + name)
override def run() =
try check()
catch {
case ex: Exception => e = ex
}
def check() {
val initial = mutable.Map[Wrap, Int]()
for (i <- 0 until sz) trie.get(new Wrap(i)) match {
case Some(i) => initial.put(new Wrap(i), i)
case None => // do nothing
}
for (k <- 0 until N) {
// modify
for ((key, value) <- initial) {
val oldv = if (k % 2 == 0) value else -value
val newv = -oldv
trie.replace(key, oldv, newv)
}
// check
for (i <- 0 until sz) if (initial.contains(new Wrap(i))) {
val expected = if (k % 2 == 0) -i else i
//println(trie.get(new Wrap(i)))
assert(trie.get(new Wrap(i)) == Some(expected))
} else {
assert(trie.get(new Wrap(i)) == None)
}
}
}
}
val worker = new Worker
worker.start()
worker.join()
if (e ne null) {
e.printStackTrace()
throw e
}
}
"have a consistent non-quiescent snapshot, concurrent with modifications" in {
val sz = 9000
val N = 1000
val W = 10
val S = 400
val ct = new TrieMap[Wrap, Int]
for (i <- 0 until sz) ct(new Wrap(i)) = i
val threads = for (i <- 0 until W) yield new Modifier(ct, i, N, sz)
threads.foreach(_.start())
for (i <- 0 until S) {
consistentReadOnly("non-qm", ct.snapshot(), sz, 5)
consistentNonReadOnly("non-qsnap", ct.snapshot(), sz, 5)
}
threads.foreach(_.join())
}
"work when many concurrent snapshots are taken, concurrent with modifications" in {
val sz = 12000
val W = 10
val S = 10
val modifytimes = 1200
val snaptimes = 600
val ct = new TrieMap[Wrap, Int]
for (i <- 0 until sz) ct(new Wrap(i)) = i
class Snapshooter extends Thread {
setName("Snapshooter")
override def run() {
for (k <- 0 until snaptimes) {
val snap = ct.snapshot()
for (i <- 0 until sz) snap.remove(new Wrap(i))
for (i <- 0 until sz) assert(!snap.contains(new Wrap(i)))
}
}
}
val mods = for (i <- 0 until W) yield new Modifier(ct, i, modifytimes, sz)
val shooters = for (i <- 0 until S) yield new Snapshooter
val threads = mods ++ shooters
threads.foreach(_.start())
threads.foreach(_.join())
}
}
}
|