Class ReferenceCountedLivenessNode

All Implemented Interfaces:
LogOutputAppendable, LivenessManager, LivenessNode, LivenessReferent
Direct Known Subclasses:
BarrageSnapshotImpl, BarrageSubscriptionImpl, LivenessArtifact, LivenessScope, StreamPublisherBase, TableLocationSubscriptionBuffer

public abstract class ReferenceCountedLivenessNode extends ReferenceCountedLivenessReferent implements LivenessNode
LivenessNode implementation that relies on reference counting to determine its liveness.
  • Constructor Details

    • ReferenceCountedLivenessNode

      protected ReferenceCountedLivenessNode(boolean enforceStrongReachability)
      Parameters:
      enforceStrongReachability - Whether this LivenessManager should maintain strong references to its referents
  • Method Details

    • initializeTransientFieldsForLiveness

      @VisibleForTesting public final void initializeTransientFieldsForLiveness()
      Package-private for Serializable sub-classes to use in readObject only. Public to allow unit tests in another package to work around mock issues where the constructor is never invoked.
    • getWeakReference

      public WeakReference<? extends LivenessReferent> getWeakReference()
      Description copied from interface: LivenessReferent
      Get a WeakReference to this referent. This may be cached, or newly created.
      Specified by:
      getWeakReference in interface LivenessReferent
      Overrides:
      getWeakReference in class ReferenceCountedLivenessReferent
      Returns:
      A new or cached reference to this referent
    • ensureCleanupOnGC

      public final void ensureCleanupOnGC()
      Ensure that this node's RetainedReferenceTracker is kept alive and will fire cleanup even if this node is garbage-collected without an explicit call to onReferenceCountAtZero().

      Registers a phantom reference to this node via CleanupReferenceProcessorInstance.LIVENESS whose action captures the tracker. This keeps the tracker strongly reachable (via the registration set) until after this node is collected, guaranteeing that the tracker's CleanupReference.cleanup() will be invoked.

      Note, each call registers a new cleanup action. If you call this method multiple times, you may have multiple cleanup actions registered, and thus multiple calls to CleanupReference.cleanup() when this node is collected.

    • tryManage

      public final boolean tryManage(@NotNull @NotNull LivenessReferent referent)
      Description copied from interface: LivenessManager
      Attempt to add referent to this manager. Will succeed if referent is live and if this manager is not a LivenessReferent or is live.
      Specified by:
      tryManage in interface LivenessManager
      Parameters:
      referent - The referent to add
      Returns:
      Whether the referent was in fact added
    • tryUnmanage

      public final boolean tryUnmanage(@NotNull @NotNull LivenessReferent referent)
      Description copied from interface: LivenessManager
      If this manager manages referent one or more times, drop one such reference. If this manager is also a LivenessReferent, then this method is a no-op if this is not live.

      Strongly prefer using LivenessManager.tryUnmanage(Stream)} when multiple referents should be unmanaged.

      Specified by:
      tryUnmanage in interface LivenessManager
      Parameters:
      referent - The referent to drop
      Returns:
      If this node is also a LivenessReferent, whether this node was live and thus in fact tried to drop a reference. Else always returns true if dropping a reference via this method is supported by the implementation.
    • tryUnmanage

      public final boolean tryUnmanage(@NotNull @NotNull Stream<? extends LivenessReferent> referents)
      Description copied from interface: LivenessManager
      For each referent in referents, if this manager manages referent one or more times, drop one such reference. If this manager is also a LivenessReferent, then this method is a no-op if this is not live.
      Specified by:
      tryUnmanage in interface LivenessManager
      Parameters:
      referents - The referents to drop
      Returns:
      If this node is also a LivenessReferent, whether this node was live and thus in fact tried to drop the references. Else always returns true if dropping a reference via this method is supported by the implementation.
    • onReferenceCountAtZero

      public final void onReferenceCountAtZero()
      Description copied from class: ReferenceCounted
      Callback method that will be invoked when the reference count returns to zero.
      Overrides:
      onReferenceCountAtZero in class ReferenceCountedLivenessReferent
    • findAnyManagedReferent

      protected Optional<? extends LivenessReferent> findAnyManagedReferent(Predicate<LivenessReferent> referentPredicate)
      Find a managed reference that matches the given predicate.
      Parameters:
      referentPredicate - a predicate to test against our managed items
      Returns:
      an Optional of a LivenessReferent that matches the given predicate; or empty if no such reference exists
    • forEachManagedReference

      protected void forEachManagedReference(Consumer<LivenessReferent> consumer)
      Apply consumer to each managed reference.