ApacheDerbyInternals 1 1
ApacheDerbyInternals 1 1
ApacheDerbyInternals 1 1
Dat ab ase(s)
on d i sk
Em b ed d ed i n Ap p l i cat i on
Ser ver
Java Vi r t u al Mach i n e
Ap p Ser ver
Ap p Ap
Appp Ap p Web - cl i en t s
It ’s j u st an em b ed d ed u se of
JDBC
Der b y w h er e t h e ap p l i cat i on
ser ver i s t h e ap p l i cat i on
Der b y en g i n e
Dat ab ase(s)
on d i sk
Sm al l Foot p r i n t
En g i n e j ar f i l e i s ar ou n d 2Mb
➢ Op t i on al Jar f i l es
● Net w or kser ver ~ 150k
● Tool s ~ 200k
Ru n t i m e m em or y u se
➢ Dep en d en t on ap p l icat ion , d at a cach i n g ,
et c.
➢ Can r u n w h en Java h eap m em or y
r est r i ct ed t o 4Mb
➢ Have r u n i n m ach i n es w i t h on l y 16Mb
p h ysi cal m em or y
St an d ar d s
SQL
➢ SQL92, SQL99, SQL2003, SQL/ XML,
…
Java
➢ J2SE 1.3, 1.4
➢ JDBC 2.0 & 3.0
➢ J2EE – cer t i f i ed as a JDBC d r i ver f or
J2EE 1.4 & 1.3
➢ J2ME/ OSGi
DRDA
Com p l et e Rel at i on al En g i n e
Mu l t i - u ser , m u l t i - t h r ead ed , t r an sact ion s,
r ow l ock i n g , isol at i on level s, l ock d ead l ock
d et ect i on s, cr ash r ecover y, b ack u p & r est or e
SQL
➢ Tab l es, in d exes, vi ew s, t r i g g er s,
p r oced u r es, f u n ct i on s, t em p t ab l es
➢ For ei g n k ey an d ch eck con st r ai n t s
➢ Joi n s, cost b ased op t i m i zer
Dat a cach i n g , st at em en t cach i n g , w r i t e ah ead
l og , g r ou p com m i t
Mu l t i p l e d at ab ases p er syst em
Ag en d a
Der b y Over vi ew
> > Syst em i n t er n al s
Lan g u ag e i n t er n al s
St or e i n t er n al s
Or i g i n al Cl ou d scap e
In t en t i on s
Dat ab ase en g i n e f or h i g h l y p er vasi ve
m ar k et
Sm al l f oot p r i n t f or PDAs
Wr i t t en i n Java f or
➢ Pl at f or m i n d ep en d en ce
➢ Dat a i n d ep en d en ce
Su p p or t m u l t i p l e APIs
➢ Possi b l e l ow l evel st or ag e API
➢ Possi b l e execu t e on l y en g i n e
Wh at Was Bu i l t
JDBC d r i ver t h at i s a d at ab ase en g i n e
➢ Tr yi n g t o i sol at e JDBC f r om r est of eng i n e i n cr eased
f oot p r i n t , n eed l ess con ver si ons
Foot p r i n t t oo b i g f or PDAs
➢ No PDAs r u n ni n g Java b ack i n 1997
➢ Ran on Psi on 5m x w i t h 16Mb , b u t b oot t i m e 40
secon d s
➢ Ear l y cu st om er s w an t ed al l t h e t yp i cal SQL f eat u r es
No l ow - l evel API, b u t m u ch cod e
i n f r ast r u ct u r e t o su p p or t i t
➢ Ad d s cod e, m u l t i p l e con n ect i on an d t r an sact i on st at e
ob j ect s
Typ i cal JDBC Cod e
PreparedStatement ps = conn.prepareStatement(
“SELECT ORDERID, COST FROM ORDERS
WHERE CID = ? AND COST > ?”);
ps.setInt(1, customer);
ps.setBigDecimail(2, threshold);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int orderId = rs.getInt(1);
BigDecimal cost = rs.getBigDecimal(2);
// process order
}
rs.close();
ps.close();
Typ i cal f or w eb - ap p l i cat i on s, cl i en t ap p l i cat i on s w i l l r e- u se
Pr ep ar ed St at em en t m u l t i p l e t i m es.
Typ i cal JDBC Cod e
PreparedStatement ps = conn.prepareStatement(
“SELECT ORDERID, COST FROM ORDERS
WHERE CID = ? AND COST > ?”);
ps.setInt(1, customer);
ps.setBigDecimal(2, threshold);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
int orderId = rs.getInt(1);
BigDecimal cost = rs.getBigDecimal(2);
// process order
}
rs.close();
ps.close();
Ever y m et h od cal l i n r ed i s a cal l i n t o t h e JDBC d r i ver
JDBC Th r ead i n g Mod el
(T1) conn.prepareStatement(
“SELECT ORDERID, COST FROM ORDERS
WHERE CID = ? AND COST > ?”)
(T4) ps.executeQuery()
(T7) rs.next()
(T1) rs.getInt(1)
(T9) rs.getBigDecimal(2)
(T2) rs.close()
(T3) ps.close()
Dr i ver h as t o assu m e ever y cal l cou l d b e f r om a d i f f er en t
t h r ead
99.99% of t h e t i m e, i t w on 't b e
Ti m i n g
(T1) conn.prepareStatement(
“SELECT ORDERID, COST FROM ORDERS
WHERE CID = ? AND COST > ?”)
// could be significant time between prepare and execute
(T4) ps.executeQuery()
// could spend significant time in executeQuery
(T7) rs.next()
(T1) rs.getInt(1)
(T9) rs.getBigDecimal(2)
(T2) rs.close()
(T3) ps.close()
Tw o ch an ces f or u n d er l yi n g t ab l e t o b e m od i f i ed , b et w een
p r ep ar e an d execu t e, an d i n execu t e w hi l e w ai t i n g f or i n t en t
l ock s on t ab l e
Log i cal Ar ch i t ect u r e
Embedded JDBC Layer Monitor
Services
Virtual Table Interface Store
Mod u l e Ar ch i t ect u r e
Pr i m ar y
Mon i t or
Ser v i ce Mod u l es
Syst em Mod u l es Ser vi ce
Pr i m ar y
Ser v i ce Mod u l es
ser v i ce.p r op er t i es
Per si st en t Ser v i ce
Mod u l e
Set of u sab l e f u n ct i on al i t y
Wel l d ef i n ed API – “ p r ot ocol ”
Pr ot ocol sep ar at ed f r om i m p lem en t at i on
Typ i cal l y d eclar ed as set of Java
i n t er f aces
Id en t i f i ed b y sin g le Java in t er f ace,
“ f act or y cl ass” .
➢ e.g .
or g .ap ach e.d er b y.i ap i .ser vi ces.l ock s.Lock Fact or
y
May r ef er en ce ot h er m od u l e p r ot ocol s
Zer o or m or e i m p lem en t at i on s i n a
r u n n i n g syst em
Im p l em en t at i on can i m p l em en t con t r ol
i n t er f aces t o:
➢ Def i n e ad d i t i on al b oot & sh ut d ow n act i on s
➢
Ser vi ce
Col l ect ion of co- op er at in g m od u l es
p r ovi d i n g a com p l et e set of f u n ct i on al i t y.
Si n g l e p r i m ar y m od u l e d ef i n i n g ext er n al
API
Per si st en t
➢ Boot - u p st at e i n si n g l e ser vi ce.p r op er t i es f i l e,
i n cl u d i n g t h e r eq u i r ed p r i m ar y m od u l e
i d en t i f i cat i on (as t h e p r ot ocol , n ot t h e
i m p l em en t at i on )
Non - p er si st en t
➢ Pu r el y r u n - t i m e d ef i ni t i on .
Mod u l es al w ays b oot ed t h r ou g h m on i t or
Mon i t or
Man ag es Der b y syst em
Boot s & sh u t d ow n Ser vi ces
Fi n d s ser vi ce.p r op er t i es f i l e b ased u p on
ser vice b oot
Map s r eq u est s f or a m od u l e p r ot ocol t o
an i m p lem en t at i on .
Based u p on :
➢ Vi r t u al m ach i n e en vi r onm en t (J2ME/ J2SE
1.3/ 1.4)
➢ Avai l ab l e cl asses (e.g . JCE en cr yp t i on
cl asses)
➢ Su i t ab i l i t y f or cu r r en t ser vi ce
En su r es syst em i s n ot g ar b ag e
col l ect ed aw ay
Dat ab ase En g i n e Syst em
ser v i ce.p r op er t i es
Dat ab ase - / u sr / d j d / sal esd b
Wh y a Com p l ex Mon i t or ?
Pr ot ocol sep ar at i on f r om im p l em en t at i on
➢ Good p r og r am m i n g p r act i ce
➢ Al l ow ed ear l y r ap i d d evel op m en t
➢ Al l ow s sp eci f i c u n i t l evel m od u l e t est i n g
Si n g l e j ar f i l e su p p or t s m u l t i p le Java
en vi r on m en t s w i t h n o set t in g s f r om
ap p l i cat i on or u ser (ease of u se)
➢ JDK 1.1, J2ME, J2SE 1.2, 1.3, 1.4
Su p p or t s d i f f er en t i m p l em en t at i on s f r om
si n g l e sou r ce t r ee
➢ Dat ab ase, Cl ou d syn c ser ver , Cl ou d syn c
t ar g et
Su p p or t s d i f f er en t st or e i m p l em en t at i on s
w i t h n o ch an g e t o l an g u ag e
➢ Di sk , r ead - onl y, d at ab ase i n a j ar , d at ab ase
on HTTP ser ver
Issu es w i t h Mon i t or
Sel ect i on b et w een i m p l em en t at i on s
t h at sat i sf y r eq u i r em en t s i s n ot
d ef i n ed .
Im p l em en t at i on l i st i n si n g l e r esou r ce
(m od u l es.p r op er t i es), n o ab i l i t y t o ad d
ad d i t i on al i m p l em en t at i on s i n
sep ar at e j ar
Al l ow i n g d yn am i c i m p l em en t at i on
sel ect i on cou l d com p r i se secu r i t y.
Sessi on St at e & Er r or Cl ean u p
Desi g n g oal s
➢ Avoi d b u g s i n er r or h an d l i n g cod e
➢ Con si st en t r esou r ce cl ean u p
➢ Mod u l ar st at e ob j ect s
Con si st en t Excep t i on Cl ean
Up
Si n g l e excep t i on m od el
(St an d ar d Excep t i on )
SQLExcep t i on t h r ow n t h r ou g h JDBC
➢ Hen ce con ver si on al w ays n eed ed
Fi ve p ossi b l e act i on s f or an excep t i on
➢ St at em en t r ol l ed b ack
➢ Tr an sact i on r ol led b ack
➢ Sessi on (con n ect i on ) cl osed
➢ Dat ab ase cl ose
➢ Syst em sh u t d ow n
Excep t i on Mod el
Or i g i n al l y f i ve su b - cl asses, e.g .
Tr an sact i on Excep t i on
➢ Com p l i cat es sever i t y ch eck in g , ad d s cod e
Now al l act i on d r i ven b y i n t eg er
sever i t y l evel
Excep t i on cl ean u p f or r eg u l ar p at h as
w el l . Con n ect i on .cl ose() t h r ow s a cl ose
sessi on sever i t y excep t i on
En su r es cl ean u p cod e i s cor r ect , n o
h i d d en b u g s d u e t o i n f r eq u en t l y
execu t ed cl ean u p
Con t ext Cl ean u p
Each sessi on h as a st ack of con t ext
ob j ect s
Each con t ext ob j ect m ai n t ai n s sessi on
st at e an d h an d l es cl ean u pEach con t ext i s p assed t h
excep t i on & p er f or m s
Dat ab ase i t s ow n cl ean u p , i n cl u d i n
JDBC Con n ect i on p ossi b l y p op p i n g i t sel f o
t h e st ack
Lan g u ag eCon n ect i on In d i cat es i f i t i s t h e l ast
Tr an sact i on con t ext f or t h at sever i t y
St at em en t
St at em en t
Com p i l e
Th r ead t o Con t ext Map p i n g
Der b y & JDBC d o n ot h ar d l i n k Th r ead
t o Con n ect i on
Ap p l i cat i on m ay u se an y Th r ead t o
execu t e m et h od s on an y Con n ect i on or
JDBC ob j ect
Der b y l i n k s ap p l i cat i on Th r ead t o
Sessi on / Con n ect i on f or t h e l i f et i m e of
t h e JDBC m et h od cal l
➢ Al l w or k p er f or m ed u si n g ap p li cat ion 's
t h r ead , n ot Der b y w or k er t h r ead s
Si n g l e Act i ve Th r ead p er
Sessi on
Th r ead BTh r ead C
on
Ap p l i cat i
Th r ead A
C1 C2
A Bl ock s B
y
Der b
Session C1 l i n k ed Sessi on C2 l i n k ed
t o Th r ead A t o Th r ead C
Gen er at ed cod e m ai n g l u e cod e
Exp r essi on s ar e g en er at ed as m et h od s
i n g en er at ed cl ass
St at em en t Resu l t s
JDBC PreparedStatement Up d at e Coun t
PS (i nt ) f or non - q u er i es
Hol d er
UC
(Language) ResultSet Tree Act i v at i on
Gen er at ed b y act i vat i on
execu t i on , p er f or m s u p d at es,
f et ch es r ow s, et c.
e.g . Pr oj ect Rest r i ct Resu l t Set
on t op of In d exScan Resu l t Set LRS RS
LRS
LRS
Ben ef i t s of Gen er at ed Cod e
No n eed t o h ave Der b y sp eci f i c
i n t er p r et er w r i t t en i n Java, j u st u se
JVM
Gen er at ed cod e w i l l g et JIT'ed an d
t h u s g ai n t h e p er f or m an ce b en ef i t s
Ti g h t i n t eg r at i on w i t h Java cal l s f r om
SQL, e.g . SQL f u n ct i on s w r i t t en i n Java.
No u se of (sl ow ) r ef l ect i on , j u st
com p i l e m et h od cal l i n t o g en er at ed
cl ass
Issu es w i t h Gen er at ed Cod e
(1)
Fi r st r u n on a n ew Java Vi r t u al
Mach i n e p r et t y m u ch g u ar an t eed t o
b r eak , f i n d VM b u g s
➢ Der b y g en er at es cod e t o JVM
sp eci f i cat ion
➢ VMs t en d ed t o on l y exp ect cod e as
g en er at ed b y Java com p i ler s
➢ Mu ch b et t er in r ecen t year s
➢ Su n an d IBM u se Cl ou d scap e d at ab ase
t est s i n t h ei r n i g h t l y VM t est i n g
Issu es w i t h Gen er at ed Cod e
(2)
Deb u g g i n g – h ar d , n o sou r ce, n o b yt e
cod e f i l e, n o l i n e n u m b er s, n o m ap p i n g
t o el em en t s i n SQL st at em en t
➢ Deb u g op t i on s t o:
● dum p b yt e cod e f il e
● ad d “ l i n e n u m b er s” t h at m ap t o b yt e cod e
of f set s
In syst em w i t h m u l t i p l e act i ve
st at em en t s can b e h ar d t o f i g u r e ou t
w h i ch g en er at ed cl ass m ap s t o w h i ch
SQL st at em en t
Gen er at or Hi st or y
Gen er at or m od u l e i n i t i al l y u sed Java
sou r ce cod e & j avac f or sp eed of
i m p l em en t at i on
Th en d i r ect b yt e cod e i m p l em en t at i on ,
u si n g sam e APIs (i n t er f aces)
Byt e cod e u sed f or p r od u ct , j ava
sou r ce f or d eb u g g i n g
Com p i l at i on w as sl ow , si g n i f i can t t i m e
sp en t i n g en er at i on
Byt eCod e Gen er at or Issu es
Too m an y ob j ect s cr eat ed – ob j ect p er
b yt e cod e i n st r u ct i on
Too m an y cl asses – f oot p r i n t i ssu es
Tr ee of ob j ect s r ep r esen t i n g
g en er at ed cl ass m i m i ced st r u ct u r e of
cl ass f or m at g en er at or
API w as n ot n eu t r al , t r an sl at ed
n at u r al l y i n t o Java sou r ce, n ot i n t o
b yt e cod e
Byt eCod e Im p r ovem en t s
Ch an g ed API t o m at ch f ast b yt e cod e
g en er at i on – d r op p ed Jav a sou r ce i m p l em en t at i on
Qu er yNod es g en er at e al m ost at t h e
b yt e cod e l evel , d ef i n i t e k n ow l ed g e of
st ack b ased VM
Nu m b er of cl asses r ed u ced t o 16
(4,12) f r om 65 (14,51)
Cl ose i n t eg r at i on w i t h cl ass f or m at
b u i l d er
Met h od cod e ar r ays b u i l t as- you - g o
Gen er at or Op t i m i zat i on s
Met h od s ad d ed t o act i vat i on i n t er f ace
f or 10 exp r essi on s, al l ow s d i r ect
execu t i on r at h er t h an t h r ou g h
r ef l ect i on
➢ St i l l su p p or t ar b i t ar y n u m b er of
exp r essi on s, ot h er s w i l l b e cal l ed t h r ou g h
r ef l ect i on
Gen er at ed secon d cl ass t o act as an
Act i vat i on f act or y, avoi d s u se of
r ef l ect i on t o cr eat e i n st an ce of
g en er at ed cl ass
➢ Not u sed as m ost JVMs n ow op t i m i ze
Ar t i f act of Com p i l e Mod el
Each SQL st at em en t i n vol ves cl ass
g en er at i on an d l oad i n g , p er f or m an ce
h u r t b y m u l t i p l e st at em en t s l i k e
➢ INSERT INTO T VALUES(1, 'f r ed ');
➢ INSERT INTO T VALUES(2, 'f r ed ');
➢ INSERT INTO T VALUES(3, 'n an cy');
Cor r ect ap p r oach i s
Pr ep ar ed St at em en t s
➢ INSERT INTO T VALUES(?, ?);
➢ St an d ar d r ecom m en d ed JDBC p r act i ce, w i l l p er f or m b et t er
on al l d at ab ases, b u t ear l y JDBC p r og r am m er s st i l l use
sep ar at e st at em en t s.
Ag en d a
Der b y Over vi ew
Syst em i n t er n al s
Lan g u ag e i n t er n al s
> > St or e i n t er n al s
St or e Ar ch i t ect u r e
St or e Access In t er f ace
Access
(Row s)
Heap BTr ee RTr ee? ...
St or e Raw In t er f ace
Raw Dat a Log Tr an sact ion s
(Pag es)
Con f i g u r ab l e en cr yp t i on u si n g JCE
St or ag e Fi l e
Jar HTTP Mem or y An yt h i n g
Syst em
St or e Not es
St or e u n aw ar e of SQL sem an t i cs
In d ex t o h eap l i n k ag e con t r ol l ed b y
l an g u ag e l ayer , n ot st or e
Al l l an g u ag e access t h r ou g h r ow s i n a
con t ai n er , n o con cep t of p ag es
Fi xed r ow m od el – en ab l es r ow l evel
l ock i n g
Fl exi b l e St or e
Desi g n ed t o st or e ser i al i zed Java
ob j ect s
Var i ab l e l en g t h w i t h n o m axi m u m
d ef i n ed
Hen ce n o l i m i t s on col u m n si ze or r ow
si ze
Si d e ef f ect of ease of u se
➢ Row al w ays f i t s, r eg ar d l ess of p ag e si ze
Al so con si d er ed :
➢ Java cl ass p r e- p r ocessi n g
Fl exi b i l i t y Dow n si d e
Col u m n f i el d l en g t h s st or ed i n each
val u e
No op t i m i zat i on f or f i xed l en g t h f i el d s
➢ E.g . SMALLINT val u e i s p r ep en d ed b y t w o
st at u s b yt es
● Fi el d st at e (NULL b i t )
● Fi el d l en g t h (al w ays 2) - “ com p r essed
i n t eg er ”
th
No f ast access t o N f i el d , n eed t o
w al k t h r ou g h p r evi ou s f i el d s
Tr an sact i on Log g i n g
Ar i es l og g i n g syst em f or r ol l b ack an d
r ecover y
Or d er of a ch an g e – w r i t e ah ead
l og g i n g
➢ Wr i t e l og r ecor d
➢ Mod i f y d at a p ag e
➢ On com m i t f l u sh l og u p t o p oi n t
con t ai n i n g com m i t r ecor d
➢ On d at a p ag e f l u sh , en su r e l og r ecor d s
f or al l m od i f i cat i on s ar e f l u sh ed t o d isk
Log Recor d
On r ecover y i n i t i al l y t h e 'd o' act i on of
a l og r ecor d i s m ad e
Su b seq u en t l y t h e 'u n d o' act i on m ay
occu r i f t r an sact i on w as n ot
com m i t t ed b ef or e cr ash
Log Recor d s ar e ob j ect s i n Der b y
'd o' m et h od u sed f or r u n t i m e
ap p l i cat i on as w el l as r ecover y
'u n d o' m et h od u sed f or r u n t i m e
r ol l b ack an d r ecover y
Resou r ces
Ap ach e si t e – w w w .ap ach e.or g
Der b y si t e –
i n cu b at or .ap ach e.or g / d er b y
IBM Cl ou d scap e –
w w w .i b m .com / d evel op er w or k s/ cl ou d s
cap e
JDBC – j ava.su n .com / j d b c
Dan Deb r u n n er
➢ d eb r u n n e@u s.i b m .com (IBM)
➢ d j d @d eb r u n n er s.com (Ap ach e Der b y)