private CompletableFuture < String > getId ( ) { String id = this . idRef . get ( ) ; if ( ! Strings . isNullOrEmpty ( id ) ) { return CompletableFuture . completedFuture ( id ) ; } else { // We will return empty string as id if the position does not exist. // This can happen if stream is being created and we access the cache. // Even if we access/load the cache against empty id, eventually cache will be populated against correct id // once it is created. return Futures . exceptionallyExpecting ( getStreamPosition ( ) . thenApply ( pos -> { String s = pos . toString ( ) ; this . idRef . compareAndSet ( null , s ) ; return s ; } ) , e -> Exceptions . unwrap ( e ) instanceof StoreException . DataNotFoundException , "" ) ; } }
Method to retrieve unique Id for the stream . We use streamPosition as the unique id for the stream . If the id had been previously retrieved then this method simply returns the previous value else it retrieves the stored stream position from zookeeper . The id of a stream is fixed for lifecycle of a stream and only changes when the stream is deleted and recreated . The id is used for caching entities and safeguarding against stream recreation .