3030import static org .openqa .selenium .remote .http .Contents .string ;
3131import static org .openqa .selenium .remote .http .HttpMethod .DELETE ;
3232
33+ import com .github .benmanes .caffeine .cache .Cache ;
34+ import com .github .benmanes .caffeine .cache .Caffeine ;
35+ import com .github .benmanes .caffeine .cache .RemovalCause ;
36+ import com .github .benmanes .caffeine .cache .Ticker ;
3337import com .google .common .annotations .VisibleForTesting ;
34- import com .google .common .base .Ticker ;
35- import com .google .common .cache .Cache ;
36- import com .google .common .cache .CacheBuilder ;
37- import com .google .common .cache .RemovalCause ;
38- import com .google .common .cache .RemovalListener ;
39- import com .google .common .cache .RemovalNotification ;
4038import com .google .common .collect .ImmutableList ;
4139import com .google .common .collect .ImmutableMap ;
4240import java .io .Closeable ;
5654import java .util .Optional ;
5755import java .util .Set ;
5856import java .util .UUID ;
59- import java .util .concurrent .ExecutionException ;
6057import java .util .concurrent .Executors ;
6158import java .util .concurrent .ScheduledExecutorService ;
6259import java .util .concurrent .TimeUnit ;
@@ -200,35 +197,35 @@ protected LocalNode(
200197 // Do not clear this cache automatically using a timer.
201198 // It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
202199 this .uploadsTempFileSystem =
203- CacheBuilder .newBuilder ()
200+ Caffeine .newBuilder ()
204201 .removalListener (
205- (RemovalListener < SessionId , TemporaryFilesystem >)
206- notification ->
207- Optional . ofNullable ( notification . getValue ())
208- . ifPresent (
209- tempFS -> {
210- tempFS . deleteTemporaryFiles ();
211- tempFS . deleteBaseDir ( );
212- }) )
202+ (SessionId key , TemporaryFilesystem tempFS , RemovalCause cause ) -> {
203+ Optional . ofNullable ( tempFS )
204+ . ifPresent (
205+ fs -> {
206+ fs . deleteTemporaryFiles ();
207+ fs . deleteBaseDir ();
208+ } );
209+ } )
213210 .build ();
214211
215212 // Do not clear this cache automatically using a timer.
216213 // It will be explicitly cleaned up, as and when "currentSessions" is auto cleaned.
217214 this .downloadsTempFileSystem =
218- CacheBuilder .newBuilder ()
215+ Caffeine .newBuilder ()
219216 .removalListener (
220- (RemovalListener < SessionId , TemporaryFilesystem >)
221- notification ->
222- Optional . ofNullable ( notification . getValue ())
223- . ifPresent (
224- fs -> {
225- fs .deleteTemporaryFiles ();
226- fs . deleteBaseDir ( );
227- }) )
217+ (SessionId key , TemporaryFilesystem tempFS , RemovalCause cause ) -> {
218+ Optional . ofNullable ( tempFS )
219+ . ifPresent (
220+ fs -> {
221+ fs . deleteTemporaryFiles ();
222+ fs .deleteBaseDir ();
223+ } );
224+ } )
228225 .build ();
229226
230227 this .currentSessions =
231- CacheBuilder .newBuilder ()
228+ Caffeine .newBuilder ()
232229 .expireAfterAccess (sessionTimeout )
233230 .ticker (ticker )
234231 .removalListener (this ::stopTimedOutSession )
@@ -313,19 +310,19 @@ public void close() {
313310 shutdown .run ();
314311 }
315312
316- private void stopTimedOutSession (RemovalNotification < SessionId , SessionSlot > notification ) {
313+ private void stopTimedOutSession (SessionId key , SessionSlot value , RemovalCause cause ) {
317314 try (Span span = tracer .getCurrentContext ().createSpan ("node.stop_session" )) {
318315 AttributeMap attributeMap = tracer .createAttributeMap ();
319316 attributeMap .put (AttributeKey .LOGGER_CLASS .getKey (), getClass ().getName ());
320- if (notification . getKey () != null && notification . getValue () != null ) {
321- SessionSlot slot = notification . getValue () ;
322- SessionId id = notification . getKey () ;
317+ if (key != null && value != null ) {
318+ SessionSlot slot = value ;
319+ SessionId id = key ;
323320 attributeMap .put ("node.id" , getId ().toString ());
324321 attributeMap .put ("session.slotId" , slot .getId ().toString ());
325322 attributeMap .put ("session.id" , id .toString ());
326323 attributeMap .put ("session.timeout_in_seconds" , getSessionTimeout ().toSeconds ());
327- attributeMap .put ("session.remove.cause" , notification . getCause () .name ());
328- if (notification . wasEvicted () && notification . getCause () == RemovalCause .EXPIRED ) {
324+ attributeMap .put ("session.remove.cause" , cause .name ());
325+ if (cause == RemovalCause .EXPIRED ) {
329326 // Session is timing out, stopping it by sending a DELETE
330327 LOG .log (Level .INFO , () -> String .format ("Session id %s timed out, stopping..." , id ));
331328 span .setStatus (Status .CANCELLED );
@@ -334,7 +331,7 @@ private void stopTimedOutSession(RemovalNotification<SessionId, SessionSlot> not
334331 LOG .log (Level .INFO , () -> String .format ("Session id %s is stopping on demand..." , id ));
335332 span .addEvent (String .format ("Stopping the session %s on demand" , id ), attributeMap );
336333 }
337- if (notification . wasEvicted () ) {
334+ if (cause == RemovalCause . EXPIRED ) {
338335 try {
339336 slot .execute (new HttpRequest (DELETE , "/session/" + id ));
340337 } catch (Exception e ) {
@@ -686,15 +683,11 @@ public Session getSession(SessionId id) throws NoSuchSessionException {
686683
687684 @ Override
688685 public TemporaryFilesystem getUploadsFilesystem (SessionId id ) throws IOException {
689- try {
690- return uploadsTempFileSystem .get (
691- id ,
692- () ->
693- TemporaryFilesystem .getTmpFsBasedOn (
694- TemporaryFilesystem .getDefaultTmpFS ().createTempDir ("session" , id .toString ())));
695- } catch (ExecutionException e ) {
696- throw new IOException (e );
697- }
686+ return uploadsTempFileSystem .get (
687+ id ,
688+ key ->
689+ TemporaryFilesystem .getTmpFsBasedOn (
690+ TemporaryFilesystem .getDefaultTmpFS ().createTempDir ("session" , id .toString ())));
698691 }
699692
700693 @ Override
@@ -862,7 +855,7 @@ public void stop(SessionId id) throws NoSuchSessionException {
862855 }
863856
864857 private void stopAllSessions () {
865- if (currentSessions .size () > 0 ) {
858+ if (currentSessions .estimatedSize () > 0 ) {
866859 LOG .info ("Trying to stop all running sessions before shutting down..." );
867860 currentSessions .invalidateAll ();
868861 }
0 commit comments