server and threads - national tsing hua university
TRANSCRIPT
![Page 1: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/1.jpg)
Server and Threads
Shan Hung Wu & DataLab
CS, NTHU
![Page 2: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/2.jpg)
Sql/UtilMetadataConcurrency
Remote.JDBC (Client/Server)
Algebra
Record
Buffer
Recovery
Log
File
Query Interface
Storage Interface
VanillaCore
Parse
Server
Planner
Index
Tx
JDBC Interface (at Client Side)
Where are we?
2
![Page 3: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/3.jpg)
Before Diving into Engines…
• How does the an RDBMS run?
– How many processes?
– How many threads?
– Thread-local or thread-safe components?
– Difference between running embedded clients and remote clients?
• Answers may influence the software architecture as well as performance
3
![Page 4: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/4.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
4
![Page 5: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/5.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
5
![Page 6: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/6.jpg)
What’s difference between a process and a thread?
6
![Page 7: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/7.jpg)
Process vs. Thread (1/2)
• Thread = a unit of CPU execution + local resources
– E.g., program counter, registers, function call stack, etc.
• Process = threads (at least one) + global resources
– E.g., memory space/heap, opened files, etc.
7
![Page 8: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/8.jpg)
Process vs. Thread (2/2)
8
![Page 9: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/9.jpg)
What’s difference between a kernel thread and a user thread?
9
![Page 10: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/10.jpg)
Kernel Threads
• Scheduled by OS
– On signel-core machines:
– On multi-core machines:
– Examples: POSIX Pthreads (UNIX), Win32 threads
10
![Page 11: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/11.jpg)
User Threads
• Scheduled by user applications (in user space above the kernel)
– Lightweight -> faster to create/destroy
– Examples: POSIX Pthreads (UNIX), Java threads
• Eventually mapped to kernel threads
– How?
11
![Page 12: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/12.jpg)
Many-to-One
• Pros: – Simple– Efficient thread mgr.
• Cons: – One blocking system call
makes all threads halt– Cannot run across multiple
CPU cores (each kernel thread runs on only one core)
• Examples: – Green threads in Solaris,
seldom used in modern OS
12
![Page 13: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/13.jpg)
One-to-One
• Pros:
– Avoid the blocking problem
• Cons:
– Slower thread mgr.
• Most OSs limit the number of kernel threads to be mapped for a process
• Examples: Linux and Windows (from 95)
13
![Page 14: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/14.jpg)
Many-to-Many
• Combining the best features of the one-to-one and many-to-one
• Allowing more kernel threads for a heavy user thread
• Examples: IRIX, HP-UX, ru64, and Solaris (prior to 9)– Downgradable to one-to-
one
14
![Page 15: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/15.jpg)
How about Java threads?
15
![Page 16: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/16.jpg)
Java Threads
• Scheduled by JVM
• Mapping depends on the JVM implementation
– But normally one-to-one mapped to Pthreads/Win32 threads on UNIX/Windows
• Pros over POSIX (one2one) threads:
– System independent (if there’s a JVM)
16
![Page 17: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/17.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
17
![Page 18: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/18.jpg)
Why does an RDBMS support concurrent statements/txs?
18
![Page 19: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/19.jpg)
Serialized or interleaved operations?
19
![Page 20: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/20.jpg)
Throughput via Pipelining
• Interleaving ops increases throughput by pipelining CPU and I/O
20
Tx1 Tx2
R(A)
CPU R(A)
CPU
R(A)
W(A) CPU
W(B)
Tx1 Tx2
R(A)
CPU
R(A)
CPU
W(B)
R(A)
CPU
W(A)
=>idle
![Page 21: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/21.jpg)
Statements run by processes or threads?
21
![Page 22: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/22.jpg)
Processes vs. Threads
• DBMS is about resource management
• If statements are run by process, then we need inter-process communications– When, e.g., two statements access the same table (file)
– System dependent
• Threads allows global resources to be shared directly– E.g., through argument passing or static variables
22
![Page 23: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/23.jpg)
What Resources to Share?
• Opened files
• Buffers (to cache pages)
• Logs
• Locks of objects (incl. files/blocks/record locks)
• Metadata
• Example: VanillaCore
23
![Page 24: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/24.jpg)
Sql/UtilMetadataConcurrency
Remote.JDBC (Client/Server)
Algebra
Record
Buffer
Recovery
Log
File
Query Interface
Storage Interface
VanillaCore
Parse
Server
Planner
Index
Tx
JDBC Interface (at Client Side)
Architecture of VanillaCore
24
![Page 25: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/25.jpg)
VanillaDb (1/2)
• Provides access to global resources:– FileMgr, BufferMgr, LogMgr, CatalogMgr
• Creates the new objects that access global resources:– Planner and Transaction
25
VanillaDb
+ init(dirName : String)
+ init(dirName : String, bufferMgrType : BufferMgrType)
+ isInited() : boolean
+ initFileMgr(dirname : String)
+ initFileAndLogMgr(dirname : String)
+ initFileLogAndBufferMgr(dirname : String, bufferMgrType : BufferMgrType)
+ initTaskMgr()
+ initTxMgr()
+ initCatalogMgr(isnew : boolean, tx : Transaction)
+ initStatMgr(tx : Transaction)
+ initSPFactory()
+ initCheckpointingTask()
+ fileMgr() : FileMgr
+ bufferMgr() : BufferMgr
+ logMgr() : LogMgr
+ catalogMgr() : CatalogMgr
+ statMgr() : StatMgr
+ taskMgr() : TaskMgr
+ txMgr() : TransactionMgr
+ spFactory() : StoredProcedureFactory
+ newPlanner() : Planner
+ initAndStartProfiler()
+ stopProfilerAndReport()
![Page 26: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/26.jpg)
VanillaDb (2/2)
• Before using the VanillaCore, the VanillaDb.init(name) must be called
– Initialize file, log, buffer, metadata, and tx mgrs
– Create or recover the specified database
26
![Page 27: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/27.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
27
![Page 28: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/28.jpg)
Embedded Clients
• Running on the same machine as RDBMS
• Usually single-threaded– E.g., sensor nodes, dictionaries, phone apps, etc.
• If you need high throughput, manage threads yourself– Identify causal relationship between statements
– Run each group of causal statements in a thread
– No causal relationship between the results outputted by different groups
28
![Page 29: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/29.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
29
![Page 30: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/30.jpg)
Remote Clients
• Server (thread) creates worker threads
• One worker thread per request• Each client can be multi-threaded
– E.g., a web/application server
30
server/dispatcher thread
worker threadsclient threads
![Page 31: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/31.jpg)
What is a request?
• An I/O operation?
• A statement?
• A transaction?
• A connection?
31
![Page 32: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/32.jpg)
Request = Connection
• In VanillaDB, a worker thread handles all statements issued by the same user
• Rationale:– Statements issued by a user are usually in a causal
order ensure casualty in a session – A user may re-examine the data he/shed accessed
easier caching
• Implications:– All statements issued in a JDBC connection is run by a
single thread at server– #connections = #threads
32
![Page 33: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/33.jpg)
Thread Pooling
• Creating/destroying a thread each time upon connection/disconnection leads to large overhead
• To reduce this overhead, a worker thread pool is commonly used– Threads are allocated from the pool as needed, and
returned to the pool when no longer needed– When no threads are available in the pool, the client
may have to wait until one becomes available
• Other benefit? • Graceful performance degradation by limiting the
pool size33
![Page 34: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/34.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
34
![Page 35: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/35.jpg)
Sql/UtilMetadataConcurrency
Remote.JDBC (Client/Server)
Algebra
Record
Buffer
Recovery
Log
File
Query Interface
Storage Interface
VanillaCore
Parse
Server
Planner
Index
Tx
JDBC Interface (at Client Side)
Architecture of VanillaCore
35
![Page 36: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/36.jpg)
JDBC Programming
1. Connect to the server
2. Execute the desired query
3. Loop through the result set (for SELECT only)
4. Close the connection• A result set ties up valuable resources on the server,
such as buffers and locks
• Client should close its connection as soon as the database is no longer needed
36
![Page 37: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/37.jpg)
java.sql (1/2)
• Makes connections to the server
37
<<interface>>
Driver
+ connect(url : String, info : Properties) : Connection
<<interface>>
Connection
+ createStatement() : Statement
+ close()
+ setAutoCommit(autoCommit : boolean)
+ setReadOnly(readOnly : boolean)
+ setTransactionIsolation(level : int)
+ getAutoCommit() : boolean
+ getTransactionIsolation() : int
+ commit()
+ rollback()
![Page 38: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/38.jpg)
java.sql (2/2)
38
• An iterator of output records
<<interface>>
Statement
+ executeQuery(gry : String) : ResultSet
+ executeUpdate(cmd : String) : int
...
<<interface>>
ResultSet
+ next() : boolean
+ getInt(fldname : String) : int
+ getString(fldname : String) : String
+ getLong(fldname : String) : Long
+ getDouble(fldname : String) : Double
+ getMetaData() : ResultSetMetaData
+ beforeFirst()
+ close()
...
<<interface>>
ResultSetMetaData
+ getColumnCount() : int
+ getColumnName(column : int) : String
+ getColumnType(column : int) : int
+ getColumnDisplaySize(column : int) : int
...
![Page 39: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/39.jpg)
Implementing JDBC in VanillaCore
• JDBC API is defined at client side
• Needs both client- and server-side implementations– In org.vanilladb.core.remote.jdbc package
– JdbcXxx are client-side classes
– RemoteXxx are server-side classes
• Based on Java RMI– Handles server threading: dispatcher thread, worker
threads, and thread pool
– But no control to pool size
– Synchronizes a client thread with a worker thread • Blocking method calls at clients
39
![Page 40: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/40.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
40
![Page 41: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/41.jpg)
Java RMI
• Java RMI allows methods of an object at server VM to be invoked remotely at a client VM
– We call this object a remote object
• How?
41
![Page 42: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/42.jpg)
The Stub and Skeleton
1. The skeleton (run by a server thread) binds the interface of the remote object
2. A client thread looks up and obtain a stub of the skeleton3. When a client thread invokes a method, it is blocked and the call is
first forwarded to the stub4. The stub marshals the parameters and sends the call to the
skeleton through the network5. The skeleton receives the call, unmarshals the parameters,
allocates from pool a worker thread that runs the remote object’s method on behalf of the client
6. When the method returns, the worker thread returns the result to skeleton and returns to pool
7. The skeleton marshals the results and send it to stub8. The stub unmarshals the results and continues the client thread
42
Stu
b
RMI Client RMI Server
sk
ele
ton
return
call
![Page 43: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/43.jpg)
RMI registry
• The server must first bind the remote obj’sinterface to the registry with a name– The interface must
extend the java.rml.Remote
interface
• The client lookup the name in the registry to obtain a stub
RMI Server
skeleton
stub
RMI Client
Registry
bind
lookupreturn call
Cilent-side Machine
Server-side Machine
43
![Page 44: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/44.jpg)
Things to Note
• A client thread and a worker thread is synchronized• The same remote object is run by multiple worker
threads (each per client)– Remote objects bound to registry must be thread-safe
• If the return of a remote method is another remote object, the stub of that object is created automatically and sent back to the client – That object can be either thread-local or thread-safe,
depending on whether it is created or reused during each method call
• A remote object will not be garbage collected if there’s a client holding its stub– Destroy stub (e.g., closing connection) at client side ASAP
44
![Page 45: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/45.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
45
![Page 46: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/46.jpg)
Server-Side JDBC Impl.
• RemoteXxx classes that mirror their corresponding JDBC interfaces at client-side– Implement the most essential JDBC methods only
• Interfaces: RemoteDriver, RemoteConnection, RemoteStatement, RemoteResultSet and RemoteMetaData– To be bound to registry
– Extend java.rml.Remote
– Throw RemoteException instead of SQLException
46
![Page 47: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/47.jpg)
RemoteDriver
• Corresponds to the JDBC Driver interface
47
<<interface>>
RemoteDriver
+ connect() : RemoteConnection
RemoteDriverImpl
+ RemoteDriverImpl()
+ connect() : RemoteConnection
![Page 48: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/48.jpg)
RemoteConnection
• Corresponds to JDBC Connection interface
48
<<interface>>
RemoteConnection
+ createStatement() : RemoteStatement
+ close()
+ setAutoCommit(autoCommit : boolean)
+ setReadOnly(readOnly : boolean)
+ setTransactionIsolation(level : int)
+ getAutoCommit() : boolean
+ isReadOnly() : boolean
+ getTransactionIsolation() : int
+ commit()
+ rollback()
RemoteConnectionImpl
~ RemoteConnectionImpl()
+ createStatement() : RemoteStatement
+ close()
+ setAutoCommit(autoCommit : boolean)
+ setReadOnly(readOnly : boolean)
+ setTransactionIsolation(level : int)
+ getAutoCommit() : boolean
+ isReadOnly() : boolean
+ getTransactionIsolation() : int
+ commit()
+ rollback()
~ getTransaction() : Transaction
~ endStatement()
![Page 49: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/49.jpg)
RemoteStatement
• Corresponds to JDBC Statement interface
49
<<interface>>
RemoteStatement
+ executeQuery(qry : String) :
RemoteResultSet
+ executeUpdate(cmd : String) : int
RemoteStatementImpl
+ RemoteStatementImpl(rconn :
RemoteConnectionImpl)
+ executeQuery(qry : String) :
RemoteResultSet
+ executeUpdate(cmd : String) : int
![Page 50: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/50.jpg)
RemoteResultSet
• Corresponds to JDBC ResultSet interface
50
RemoteResultSetImpl
+ RemoteResultSetImpl(plan : Plan, rconn :
RemoteConnectionImpl)
+ next() : boolean
+ getInt(fldname : String) : int
+ getLong(fldname : String) : long
+ getDouble(fldname : String) : double
+ getString(fldname : String) : String
+ getMetaData() : RemoteMetaData
+ beforeFirst()
+ close()
<<interface>>
RemoteResultSet
+ next() : boolean
+ getInt(fldname : String) : int
+ getLong(fldname : String) : long
+ getDouble(fldname : String) : double
+ getString(fldname : String) : String
+ getMetaData() : RemoteMetaData
+ beforeFirst()
+ close()
![Page 51: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/51.jpg)
RemoteMetaData
• Corresponds to JDBC ResultSetMetaDatainterface
51
<<interface>>
RemoteMetaData
+ getColumnCount() : int
+ getColumnName(column : int) : String
+ getColumnType(column : int) : int
+ getColumnDisplaySize(column : int) : int
RemoteMetaDataImpl
+ RemoteMetaDataImpl(sch : Schema)
+ getColumnCount() : int
+ getColumnName(column : int) : String
+ getColumnType(column : int) : int
+ getColumnDisplaySize(column : int) : int
![Page 52: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/52.jpg)
Registering Remote Objects
• Only the RemoteDriver need to be bound to registry– Stubs of others can be obtained by method returns
• Done by JdbcStartUp:
/* create a registry specific for the server onthe default port 1099 */
Registry reg = LocateRegistry.createRegistry(1099);
// post the server entry in itRemoteDriver d = new RemoteDriverImpl();
/* create a stub for the remote implementation object d,save it in the RMI registry */
reg.rebind("vanilladb-jdbc", d);52
![Page 53: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/53.jpg)
Obtaining Stubs
• To obtain the stubs at client-side:
• Directly through registry or indirectly through method returns
53
// url = "jdbc:vanilladb://xxx.xxx.xxx.xxx:1099"String host = url.replace("jdbc:vanilladb://", "");Registry reg = LocateRegistry.getRegistry(host);RemoteDriver rdvr = (RemoteDriver)
reg.lookup("vanilladb-jdbc");// creates connectionRemoteConnection rconn = rdvr.connect();// creates statementRemoteStatement rstmt = rconn.createStatement();
![Page 54: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/54.jpg)
JDBC Client-Side Impl.
• Implement java.sql interfaces using the client-side wrappers of stubs
– E.g., JdbcDriver wraps the stub of RemoteDriver
54
<<interface>>
java.sql.Driver
+ connect(url : String, info : Properties) :
Connection
+ acceptsURL(url : String) : boolean
+ getMajorVersion() : int
+ getMinorVersion() : int
+ getPropertyInfo(url : String, info :
Properties) : DriverPropertyInfo[]
+ jdbcCompliant() : boolean
<<abstract>>
DriverAdapter
// throws exceptions for
unimplemented methods
JdbcDriver
+ connect(url : String, prop : Properties) :
Connection
![Page 55: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/55.jpg)
DriverAdapter and JdbcDriver
55
• DriverAdapter
• Dummy impl. of the Driver interface (by throwing exceptions)
• JdbcDriver:public class JdbcDriver extends DriverAdapter {
public Connection connect(String url, Properties prop) throws SQLException{
try {// assumes no port specifiedString host = url.replace("jdbc:vanilladb://", "");Registry reg = LocateRegistry.getRegistry(host);RemoteDriver rdvr = (RemoteDriver) reg.lookup("vanilladb-jdbc");RemoteConnection rconn = rdvr.connect();
return new JdbcConnection(rconn);} catch (Exception e) {
throw new SQLException(e);}
}}
![Page 56: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/56.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
56
![Page 57: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/57.jpg)
Remote Class Implementation in RMI Layers
TCPRemote Reference Layer
Transport Layer
Java Virtual Machine
Client Object
Remote Reference Layer
Transport Layer
Java Virtual Machine
Stub
Remote Object
Skeleton
57
![Page 58: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/58.jpg)
RemoteDriverImpl
• RemoteDriverImpl is the entry point into the server
• Each time its connect method is called (via the stub), it creates a new RemoteConnectionImpl on the server– RMI creates the corresponding stub and returns back it to
the client
• Run by multiple threads, must be thread-safe
58
<<interface>>
RemoteDriver
+ connect() : RemoteConnection
RemoteDriverImpl
+ RemoteDriverImpl()
+ connect() : RemoteConnection
![Page 59: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/59.jpg)
RemoteConnectionImpl
• Manages client connections on the server– Associated with a tx– commit() commits the current tx and starts a new one
immediately
• Thread local
59
<<interface>>
RemoteConnection
+ createStatement() : RemoteStatement
+ close()
+ setAutoCommit(autoCommit : boolean)
+ setReadOnly(readOnly : boolean)
+ setTransactionIsolation(level : int)
+ getAutoCommit() : boolean
+ isReadOnly() : boolean
+ getTransactionIsolation() : int
+ commit()
+ rollback()
RemoteConnectionImpl
~ RemoteConnectionImpl()
+ createStatement() : RemoteStatement
+ close()
+ setAutoCommit(autoCommit : boolean)
+ setReadOnly(readOnly : boolean)
+ setTransactionIsolation(level : int)
+ getAutoCommit() : boolean
+ isReadOnly() : boolean
+ getTransactionIsolation() : int
+ commit()
+ rollback()
~ getTransaction() : Transaction
~ endStatement()
![Page 60: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/60.jpg)
RemoteStatementImpl
• Executes SQL statements– Creates a planner that finds the best plan tree
• If the connection is set to be auto commit, the executeUpdate() method will call connection.commit() in the end
• Thread local
60
<<interface>>
RemoteStatement
+ executeQuery(qry : String) :
RemoteResultSet
+ executeUpdate(cmd : String) : int
RemoteStatementImpl
+ RemoteStatementImpl(rconn :
RemoteConnectionImpl)
+ executeQuery(qry : String) :
RemoteResultSet
+ executeUpdate(cmd : String) : int
![Page 61: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/61.jpg)
RemoteResultSetImpl
• Provides methods for iterating the output records– The scan opened from the best plan tree
• Tx spans through the iteration– Avoid doing heavy jobs during the iteration
• Thread local
61
RemoteResultSetImpl
+ RemoteResultSetImpl(plan : Plan, rconn :
RemoteConnectionImpl)
+ next() : boolean
+ getInt(fldname : String) : int
+ getLong(fldname : String) : long
+ getDouble(fldname : String) : double
+ getString(fldname : String) : String
+ getMetaData() : RemoteMetaData
+ beforeFirst()
+ close()
<<interface>>
RemoteResultSet
+ next() : boolean
+ getInt(fldname : String) : int
+ getLong(fldname : String) : long
+ getDouble(fldname : String) : double
+ getString(fldname : String) : String
+ getMetaData() : RemoteMetaData
+ beforeFirst()
+ close()
![Page 62: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/62.jpg)
RemoteMetaDataImpl
• Provides the schema information about the query results– Contains the Schema object of the output table
• Thread local
62
<<interface>>
RemoteMetaData
+ getColumnCount() : int
+ getColumnName(column : int) : String
+ getColumnType(column : int) : int
+ getColumnDisplaySize(column : int) : int
RemoteMetaDataImpl
+ RemoteMetaDataImpl(sch : Schema)
+ getColumnCount() : int
+ getColumnName(column : int) : String
+ getColumnType(column : int) : int
+ getColumnDisplaySize(column : int) : int
![Page 63: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/63.jpg)
Outline
• Processes, threads, and resource management– Processes and threads
– Supporting concurrent clients
– Embedded clients
– Remote clients
• Implementing JDBC– RMI
– Remote Interfaces and client-side wrappers
– Remote Implementations
– StartUp
63
![Page 64: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/64.jpg)
Staring Up
• StartUp provides main() that runs VanillaCore as a JDBC server
– Calls VanillaDB.init()
• Sharing global resources through static variables
– Binds RemoteDriver to RMI registry
• One thread per connection
64
![Page 65: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/65.jpg)
Threading in Engines
• Generally,
• Classes in the query engine are thread-local
• Classes in the storage engine are thread-safe
65
![Page 66: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/66.jpg)
Assignment Reading
• The following packages in VanillaCore
– org.vanilladb.core.server
– org.vanilladb.core.remote.jdbc
66
![Page 67: Server and Threads - National Tsing Hua University](https://reader030.vdocuments.net/reader030/viewer/2022040620/624bd5795221ef245e057342/html5/thumbnails/67.jpg)
References
• Java Threads and Concurrency
• Java RMI
67