@ Nullable @ Override public T startIfOwner ( String name , Duration waitDuration ) { long timeoutAt = System . currentTimeMillis ( ) + waitDuration . toMillis ( ) ; LeaderService leaderService = _leaderMap . getUnchecked ( name ) . orNull ( ) ; if ( leaderService == null || ! awaitRunning ( leaderService , timeoutAt ) ) { return null ; } Service service ; for ( ; ; ) { Optional < Service > opt = leaderService . getCurrentDelegateService ( ) ; if ( opt . isPresent ( ) ) { service = opt . get ( ) ; break ; } if ( System . currentTimeMillis ( ) >= timeoutAt ) { return null ; } try { Thread . sleep ( 10 ) ; } catch ( InterruptedException e ) { throw Throwables . propagate ( e ) ; } } if ( ! awaitRunning ( service , timeoutAt ) ) { return null ; } //noinspection unchecked return ( T ) service ; }
Returns the specified managed service if this server is responsible for the specified object and has won a ZooKeeper - managed leader election .