From 64fe5c2e4cc6934f12e62e5c355062dd1c164dc2 Mon Sep 17 00:00:00 2001 From: Alberto Gomez Date: Mon, 20 Sep 2021 19:24:53 +0200 Subject: [PATCH] Have a global and thread local observer in QueryObserverHolder --- .../query/internal/QueryObserverHolder.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/geode-core/src/main/java/org/apache/geode/cache/query/internal/QueryObserverHolder.java b/geode-core/src/main/java/org/apache/geode/cache/query/internal/QueryObserverHolder.java index aeb881fa0203..2d0640a39642 100644 --- a/geode-core/src/main/java/org/apache/geode/cache/query/internal/QueryObserverHolder.java +++ b/geode-core/src/main/java/org/apache/geode/cache/query/internal/QueryObserverHolder.java @@ -14,6 +14,7 @@ */ package org.apache.geode.cache.query.internal; + import org.apache.geode.annotations.Immutable; import org.apache.geode.annotations.internal.MakeNotStatic; @@ -49,25 +50,39 @@ public class QueryObserverHolder { * The current observer which will be notified of all query events. */ @MakeNotStatic - private static ThreadLocal _instance = ThreadLocal.withInitial(() -> NO_OBSERVER); + private static final ThreadLocal _instance = new ThreadLocal<>(); + + private static volatile QueryObserver _globalInstance = NO_OBSERVER; /** * Set the given observer to be notified of query events. Returns the current observer. */ - public static QueryObserver setInstance(QueryObserver observer) { + public static synchronized QueryObserver setInstance(QueryObserver observer) { Support.assertArg(observer != null, "setInstance expects a non-null argument!"); - QueryObserver oldObserver = _instance.get(); + QueryObserver oldObserver; + if (_instance.get() != null) { + oldObserver = _instance.get(); + } else { + oldObserver = _globalInstance; + } _instance.set(observer); + _globalInstance = observer; return oldObserver; } - public static boolean hasObserver() { - return _instance.get() != NO_OBSERVER; + public static synchronized boolean hasObserver() { + if (_instance.get() != null) { + return _instance.get() != NO_OBSERVER; + } + return _globalInstance != NO_OBSERVER; } /** Return the current QueryObserver instance */ public static QueryObserver getInstance() { - return _instance.get(); + if (_instance.get() != null) { + return _instance.get(); + } + return _globalInstance; } /** @@ -75,5 +90,6 @@ public static QueryObserver getInstance() { */ public static void reset() { _instance.set(NO_OBSERVER); + _globalInstance = NO_OBSERVER; } }