The Lease interface provides methods to grant, revoke, and keepalive leases.
- The function build leaseGrantRequest with ttl value (unit seconds).
- It call respond gRPC interface with leaseGrantRequest to build a new lease in etcd.
- The function build leaseRevokeRequest with lease id.
- It call respond gRPC interface with leaseRevokeRequest to revoke respond lease.
- The function will start a background scheduler
keepAliveSchedule
to send keep alive request for lease registered to Lease Client. - It will open a StreamObserver to etcd, send request to this StreamObserver can keep the lease alive.
- It will new a ScheduledExecutorService to run
keepAliveExecutor
anddeadLineExecutor
periodically.
- This function will end the background scheduler.
- It will close the StreamObserver to etcd.
- This function set LeaseHandler for the lease.
- The LeaseHandler interface:
- onKeepAliveRespond
- It will be called when the receive lease response from etcd.
- onLeaseExpired
- It will be called when the lease expire and removed from etcd.
- onError
- It will be called when some exception occurred.
- onKeepAliveRespond
- It will create a keepAlive Object, and keep the lease alive.
- It will add the created keepAlive object to the keepAlives map.
- It will set handler for the keepAlive.
- The background scheduler will send keep alive requests for the added keepAlive when it approaches to the nextKeepAliveTime.
- This function is called periodically by
keepAliveSchedule
. - The lease may expire as:
- The etcd respond overtime.
- The client fails to send request in time.
- It will scan the keepAlives map and find the leases that requires keepAlive requests to send based on its nextKeepAliveTime.
- Send request to the StreamObserver for these leases.
- This function is called periodically by
keepAliveSchedule
. - It will scan the keepAlives map and find the leases that requires removed from the map based on its DeadLine.
- call the on onLeaseExpired method for these leases.
- It will get the keepAlive instance based on the leaseID.
- if ttl < 0, it remove respond keepAlive from keepAlives map and call
onLeaseExpired
. - else it reset the keepAlive's nextKeepAliveTime and deadLine value.
- The StreamObserver is the stream etcd uses to send responses.
- The gRPC client calls onNext when etcd server delivers a response.
- The onNext function will call
processKeepAliveRespond
.