25
Threading in Firebird and the Future Ann W. Harrison James A. Starkey

Threading through InterBase, Firebird, and beyond

Embed Size (px)

DESCRIPTION

"Threading through InterBase, Firebird, and beyond", by Ann Harrison and Jim Starkey

Citation preview

Page 1: Threading through InterBase, Firebird, and beyond

Threading in Firebird and the Future

Ann W. HarrisonJames A. Starkey

Page 2: Threading through InterBase, Firebird, and beyond

A Word of Thanks to our Sponsors

Page 3: Threading through InterBase, Firebird, and beyond

Why Threads?

Improve multi-user performanceUtilize multi-processor systems

Databases are too easy to multi-thread

Page 4: Threading through InterBase, Firebird, and beyond

What are Atomic Instructions?

Most machine instructions can be interrupted, allowing the world to change.

Atomic instructions (e.g. CAS) run to completion.Essential for multi-thread performance.

Page 5: Threading through InterBase, Firebird, and beyond

Wasn’t Firebird Always Threaded?

Earliest versions of shared server ran query for one user until it stalled before responding to next user.

Not friendly.Multi-threaded server runs to the next wait or for

a fixed period plus the time to make the database state consistent.

Threads never run concurrently.

Page 6: Threading through InterBase, Firebird, and beyond

Firebird Classic

Designed for VAX ClustersMultiple independent computersShared intelligent disk controllerCluster-wide lock manager

Page 7: Threading through InterBase, Firebird, and beyond

Firebird Classic Multi-Processor

Single Machine, Multi-ProcessorO/S schedules Firebird clients on processorsClients share

DiskLock manager

Clients do not sharePage CacheMetadata

Page 8: Threading through InterBase, Firebird, and beyond

Non-shared cache

Firebird classic, super classic

Client B wantspage 123

Client A changed

page 123

Yes, that is really a disk write

Page 9: Threading through InterBase, Firebird, and beyond

Shared cache - Superserver

Client A changed

page 123

Client B wantspage 123

Client A releases lock on

page 123

Client B lockspage 123

1

23

Page 10: Threading through InterBase, Firebird, and beyond

Threading, 101

ThreadPC: Instruction stream of controlDedicated Stack (1 mb+)Thread specific dataAll threads share process memoryExpensive to create, cheap to use(If you don’t thrash)

Page 11: Threading through InterBase, Firebird, and beyond

Threading 101

Interlocked Instruction: Atomic compare and swapCompares given value to value at given

addressIf equal, store new value at given addressIf not, fails and does nothing

Interlocked instructions are the mortar of multi-threading

Page 12: Threading through InterBase, Firebird, and beyond

Threading 101

Non-interlocked data structuresData structures managed only by interlocked

instructionsCompletely non-blockingThe fastest – and hardest – form of multi-

programming

Page 13: Threading through InterBase, Firebird, and beyond

Threading 101

RW-lock, aka SyncObjectCan be locked for read/sharedCan be locked for write/exclusiveBlocks until access can be grantedMonitor semantics: Thread doesn’t lock

against itselfImplemented with interlocked CAS

Page 14: Threading through InterBase, Firebird, and beyond

Threading 101

Coarse grain multi-threadingSingle mutex controls an entire subsystemIndividual data structures are not interlocked

Fine grain multi-threadingIndividual RW-lock per data structureAllows many threads to share a subsystem

Page 15: Threading through InterBase, Firebird, and beyond

Threading 101

Dedicated ThreadThread assigned specific taskGarbage collector, network listener, etc.

Client threadThread executing user request

Worker ThreadThread idle or executing user request

Thread poolManages worker threads

Page 16: Threading through InterBase, Firebird, and beyond

Threading Models

Thread per connectionWorker thread assigned at connection timeWorker thread == Client threadIdle client threads consume resourcesMany connections => high contention

Page 17: Threading through InterBase, Firebird, and beyond

Threading Models

Limited worker threadsLimit active worker threads to approx. number

of processorsUser requests queued until work thread

becomes availableIf worker thread stalls (page read), thread

pool can release next user requestUtilizes processors without unnecessary

contention

Page 18: Threading through InterBase, Firebird, and beyond

Threading Models

Limited Worker Threads:Dedicated listener thread waits for readable

socketConnection object (on listener thread) does

socket readWhen packet is complete, connection object

queue to thread poolWhen worker thread becomes available,

connection object is executed

Page 19: Threading through InterBase, Firebird, and beyond

Threading Model

Thread per connection is first stepLimited worker threads is essential for scalability

Page 20: Threading through InterBase, Firebird, and beyond

Interbase Threads: The Beginning

The concept of threads was known at the birth of Interbase, but no implementations existed on small machines.

SMP didn’t exist in the mini or workstation worldThe initial version of Interbase used signals for

communicationUser requests executed with “looper”; when a

request stalled, another request could run

Page 21: Threading through InterBase, Firebird, and beyond

Interbase Theads: The V3 Disaster

Apollo was the first workstation vendor with threads

I implemented a VMS threading packageSun’s first attempt at threads didn’t even

compileInterbase V3 was going to be mixed signals +

threadsThen disaster: Apollo Domain had unfixable

architectural flaw mixing threads and signalsA long slip ensued

Page 22: Threading through InterBase, Firebird, and beyond

Interbase Threads: V3 Reborn

The engine was either threaded or signal basedDedicated threads for lock manager, event

manager, etc.Server was thread per clientEngine continued with coarse grain multi-

threading

Page 23: Threading through InterBase, Firebird, and beyond

Firebird Threading: Vulcan

Vulcan, now deceased, introduced limited fine grain multi-threading

Threads synchronized with SyncObject: User mode read/write locks with monitor semantics

SMP had arrived, followed shortly by processor based threads

Page 24: Threading through InterBase, Firebird, and beyond

Some Performance Lessons

The goal is to saturate CPU, network, memory, and disk bandwidth simultaneously.

There is no reason to run more worker threads than cores (and many reasons not to), but

A stalled thread is an efficient way to maintain request state (death to “looper”!)

Page 25: Threading through InterBase, Firebird, and beyond

A Winning Architecture

A single dedicated thread waiting for readable sockets

Request starts are posted to thread manager for available worker thread

When active worker threads drops below threshold, a pending request is assigned a worker thread

A stalling thread checks in with thread manager to drop the number of active worker threads

An unstalled request bumps the number of a.w.t.