Top Banner
Assignment 4 Solution Introduction to Databases DataLab CS, NTHU 1
30

Assignment 4 Solution - nthu-datalab.github.io

Mar 02, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Assignment 4 Solution - nthu-datalab.github.io

Assignment 4 Solution

Introduction to DatabasesDataLab

CS, NTHU

1

Page 2: Assignment 4 Solution - nthu-datalab.github.io

Outline

• Useful Java Classes for Concurrency• Lock Striping• Summary of File & Buffer Optimization

2

Page 3: Assignment 4 Solution - nthu-datalab.github.io

Outline

• Useful Java Classes for Concurrency• Lock Striping• Summary of File & Buffer Optimization

3

Page 4: Assignment 4 Solution - nthu-datalab.github.io

ReentrantLock

• An implementation of Lock– Provided in java.util.concurrent.locks

• A ReentrantLock has better performance than a synchronized block in multi-threading scenario

• See more here4

class X {private final ReentrantLock lock = new ReentrantLock();

public void m() {lock.lock(); // block until condition holdstry {

// do something} finally {

lock.unlock();}

}}

Page 5: Assignment 4 Solution - nthu-datalab.github.io

ReentrantReadWriteLock

• An implementation of ReadWriteLock– Provided in java.util.concurrent.locks

• In addition to all functions ReentrantLockprovide, ReentrantReadWriteLock also have ReadLock and WriteLock– A thread will be blocked during acquiring a ReadLock

only if there is another thread holds a WriteLock

• See more here5

Page 6: Assignment 4 Solution - nthu-datalab.github.io

ReentrantReadWriteLock

6

class Counter {// Locksprivate final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();private final Lock rLock = rwLock.readLock();private final Lock wLock = rwLock.writeLock();

// Valueprivate int value = 0;

public int get() {rLock.lock();try {

return value;} finally {

rLock.unlock();}

}

public void increment() {wLock.lock();try {

value += 1;} finally {

wLock.unlock();}

}}

Page 7: Assignment 4 Solution - nthu-datalab.github.io

ConcurrentHashMap

• A thread-safe HashMap– Provided in java.util.concurrent

• A ConcurrentHashMap works better than a synchronized HashMap which is just simply protected by synchronized blocks

• See more here

7

Page 8: Assignment 4 Solution - nthu-datalab.github.io

Outline

• Useful Java Classes for Concurrency• Lock Striping• Summary of File & Buffer Optimization

8

Page 9: Assignment 4 Solution - nthu-datalab.github.io

9

File “Cats” File “Dogs” File “Birds”

Thread 1 Thread 2

FileMgr

Page 10: Assignment 4 Solution - nthu-datalab.github.io

10

File “Cats” File “Dogs” File “Birds”

Thread 1 Thread 2

FileMgr

Synchronized on this

Waiting…

Page 11: Assignment 4 Solution - nthu-datalab.github.io

11

File “Cats” File “Dogs” File “Birds”

Thread 1 Thread 2

FileMgr

Synchronized on this

Waiting…

Page 12: Assignment 4 Solution - nthu-datalab.github.io

12

File “Cats” File “Dogs” File “Birds”

Thread 1 Thread 2

FileMgr

Sync. on “Cats” Sync. on “Dogs” Sync. on “Birds”

Page 13: Assignment 4 Solution - nthu-datalab.github.io

Global Synchronization

13

class ResourceMgr {

private Map<String, Resource> resourcePool =new HashMap<String, Resource>();

public synchronized void doSomething(String key) {Resource res = getResource(key);res.doAThing();

}

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

}

Page 14: Assignment 4 Solution - nthu-datalab.github.io

Synchronization on Each Resource Object

14

class ResourceMgr {

private Map<String, Resource> resourcePool =new HashMap<String, Resource>();

public void doSomething(String key) {Resource res = getResource(key);

synchronized (res) {res.doAThing();

}}

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

}

Lock on the required object

There is a problem here

Page 15: Assignment 4 Solution - nthu-datalab.github.io

Race Condition

15

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

Thread 1

Thread 2

key=“meow”

key=“meow”

res = NULL

res

Pool

= NULL

Page 16: Assignment 4 Solution - nthu-datalab.github.io

Race Condition

16

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

Thread 1

Thread 2

key=“meow”

key=“meow”

res

res

Pool

Resource 1

= NULL

= NULL

Page 17: Assignment 4 Solution - nthu-datalab.github.io

Race Condition

17

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

Thread 1

Thread 2

key=“meow”

key=“meow”

res

res

meow

Pool

Resource 1

= NULL

Page 18: Assignment 4 Solution - nthu-datalab.github.io

Race Condition

18

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

Thread 1

Thread 2

key=“meow”

key=“meow”

res

res

meow

Pool

Resource 1

Resource 2

= NULL

Page 19: Assignment 4 Solution - nthu-datalab.github.io

Race Condition

19

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

Thread 1

Thread 2

key=“meow”

key=“meow”

res

res

meow

Pool

Resource 1

Resource 2

Page 20: Assignment 4 Solution - nthu-datalab.github.io

Race Condition

20

private Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

Thread 1

Thread 2

key=“meow”

key=“meow”

res

res

meow

Pool

Resource 1

Resource 2

There are two resourcewith the same key !!And only 1 can be found

Page 21: Assignment 4 Solution - nthu-datalab.github.io

Solution

21

private synchronized Resource getResource(String key) {Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

The problem solved, but not good enough

Page 22: Assignment 4 Solution - nthu-datalab.github.io

22

Can We Do Even Better?

Page 23: Assignment 4 Solution - nthu-datalab.github.io

Lock Striping

23

• Lock striping basically uses a fixed-size, shared collection of locks to reduce the contention on the same object

Page 24: Assignment 4 Solution - nthu-datalab.github.io

24

Thread 2

Object1

Object2

Object3

Object4

Object5

Object6

Object7

Object8

Thread 1

key=“meow”(142890132)

key=“meow”(142890132)

142890132 % 8 = 4

142890132 % 8 = 4

Lock Striping

Anchors

Synchronized on object(4)

Synchronized on object(4)Blocked

Page 25: Assignment 4 Solution - nthu-datalab.github.io

25

Thread 2

Object1

Object2

Object3

Object4

Object5

Object6

Object7

Object8

Thread 1

key=“meow”(142890132)

key=“kerker”(453621342)

142890132 % 8 = 4

453621342 % 8 = 6

Lock Striping

Anchors

Synchronized on object(4)

Synchronized on object(6)

Pass

Page 26: Assignment 4 Solution - nthu-datalab.github.io

Final Solution

26

private Object[] anchors = new Object[100];

private Object getAnchor(String key) {return anchors[key.hashCode() % anchors.length];

}

private Resource getResource(String key) {synchronized (getAnchor(key)) {

Resource res = resourcePool.get(key);

if (res == null) {res = new Resource();resourcePool.put(key, res);

}

return res;}

}

Don’t forget to use ConcurrentHashMap for resource pool

Page 27: Assignment 4 Solution - nthu-datalab.github.io

Outline

• Useful Java Classes for Concurrency• Lock Striping• Summary of File & Buffer Optimization

27

Page 28: Assignment 4 Solution - nthu-datalab.github.io

File Optimization

• Read Write Lock– We use RentreenReadWriteLock in each IoChannel, use ReadLock for reading and WriteLock for modifications

• Lock Striping– Use lock-striping in getFileChannel()

• Caching– Cache the hashcode of BlockId

28

Page 29: Assignment 4 Solution - nthu-datalab.github.io

Buffer Optimization

• Reduce the size of critical section as small as possible– e.g. BufferMgr.pin() and pinNew()

• Read Write Lock– For each Buffer

• Lock Striping– In BufferPoolMgr.pin() and pinNew()

• Improved Clock Strategy

29

Page 30: Assignment 4 Solution - nthu-datalab.github.io

Some Research on pin()

• According to a research [1], txsusually take more time in buffer manager than in other modules

• Some researchers of HP lab found pin()is a big bottleneck when traversing B-tree indexes [2]• They purposed a new way to

optimize buffer manager for B-tree indexes

30

[1] “OLTP Through the Looking Glass, and What We Found There.” in SIGMOD’08[2] “In-Memory Performance for Big Data” in VLDB’14