2。hbase CRUD--Lease in hbase
1.What is Lease
in general,between the communication of client and Region Server,there are RPCs in there.Lease is a mechanism to hold a reference object updated each client rpc. so Lease is similar to Hearbeat for server to know that client is alive always!(of course that is in a certain period in fact)
2.Why use Lease
at a first communication ,e.g. a scan operation,the client will get by batch to limit to size returned,then the client issue the second request to retrieve later data.during these requests,region Server will know to these are a scan operaction in fact,so it is needless to renew or create some new objects/caches/handles to locate the nexxt batch data.that is using a Lease will decrease some resourcces overhead!
so all theese cases will provide more stable,avilable.
u will say:why not use hearbeat instead of it?yes this is a good question.but if take a look carefully,the advantages are distingguished :
-Lease has NOT additional connection overhead,but Hearbeat does.in server,there are resouces created once per 'session',and ONLY 'update-on-requesting'
-easy to implement.the server can use some mechanism like lockid to archive Lease.and the client will only keep a lockid in mind for each request.also,the server will have a timeout strategy to cancel this Lease if over a time range that client has not requested something.
and if some exceptions occurs in server,the client will get them and use some utility methods(eg. scan#close() or htable#unlock() to release the lease immediately)
3.How to use Lease
as mentioned above ,there are two clients interacted with Lease:user client and server-side listener.
that is a 'double check ' for the lease in fact!
4.Compare:Mutation VS Get VS Scan
OP | use lease | through by | time to get | time to release | feature |
Mutation | yes | row lock id |
client:lockRow() server:internalObtainRowLock() if client not locked row before |
client: htable#unlock server: RowLockListener#leaseExpired |
client: manually to generate or remove lease server: automatically to generate or remove if timeout |
Scan | yes | scannerId |
client:openScanner (transparent to client) |
client: scanner#close server: ScannerListener#leaseExpired |
same as above |
Gets | no | ||||
Questions:
while lock id is unique BUT scanner id ?this will causes some odd issues when multi clients use same scanner id.
lock id generateion:(unique)
while (true) { Integer lockId = lockIdGenerator.incrementAndGet(); HashedBytes existingRowKey = lockIds.putIfAbsent(lockId, rowKey); if (existingRowKey == null) { return lockId; } else { // lockId already in use, jump generator to a new spot lockIdGenerator.set(rand.nextInt()); } }
scanner id :
protected long addScanner(RegionScanner s) throws LeaseStillHeldException { long scannerId = -1L; scannerId = rand.nextLong(); String scannerName = String.valueOf(scannerId); scanners.put(scannerName, s); //直接put不考虑存在问题? TBD this.leases.createLease(scannerName, new ScannerListener(scannerName)); return scannerId; }
any clues for me is appreciated!
Note
* invoke a scanner's close method will only remove the lease on region server,but NOT for connection to it.instead of Htable's close method does close all connections established to all servers!
Ref: