Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving target h

From Leeroopedia
Knowledge Sources
Domains Model Serving, Core Framework
Last Updated 2026-02-13 00:00 GMT

Overview

Target and TargetBase define the abstract interface for modules that receive aspired-version callbacks from Sources in the TensorFlow Serving pipeline.

Description

The Target<T> class template is the abstract interface for any module that receives aspired-version instructions from a Source<T>. Its single method, GetAspiredVersionsCallback(), returns a callback that is thread-safe, valid forever (even after the Target is destroyed, becoming a no-op), and blocks until the target is fully set up.

TargetBase<T> is the recommended base class for all Target implementations. It handles the lifecycle management of the aspired-versions callback by using shared pointers to a mutex and an absl::Notification (the "detached" flag). This ensures the callback outlives the Target object safely. Subclasses implement SetAspiredVersions() to handle incoming aspired-version requests.

Critical requirement: Every leaf derived class must call Detach() at the top of its destructor. This stops any in-flight SetAspiredVersions() calls and prevents new ones, avoiding races during destruction.

The free function ConnectSourceToTarget() wires a Source to a Target by passing the Target's callback to the Source.

Usage

Use TargetBase as the base class for any module that needs to receive aspired-version notifications. Managers (like AspiredVersionsManager), SourceAdapters, and SourceRouters all extend TargetBase. Use ConnectSourceToTarget() to wire up the Source-Target relationship.

Code Reference

Source Location

Signature

template <typename T>
class Target {
 public:
  virtual ~Target() = default;
  virtual typename Source<T>::AspiredVersionsCallback
  GetAspiredVersionsCallback() = 0;
};

template <typename T>
class TargetBase : public Target<T> {
 public:
  ~TargetBase() override;
  typename Source<T>::AspiredVersionsCallback GetAspiredVersionsCallback() final;

 protected:
  TargetBase();
  virtual void SetAspiredVersions(const StringPiece servable_name,
                                  std::vector<ServableData<T>> versions) = 0;
  void Detach();
};

template <typename T>
void ConnectSourceToTarget(Source<T>* source, Target<T>* target);

Import

#include "tensorflow_serving/core/target.h"

I/O Contract

Inputs

Name Type Required Description
servable_name const StringPiece Yes The name of the servable stream being updated
versions std::vector<ServableData<T>> Yes The set of aspired versions for the named stream

Outputs

Name Type Description
GetAspiredVersionsCallback() Source<T>::AspiredVersionsCallback A thread-safe callback that forwards to SetAspiredVersions(); becomes a no-op after Detach()

Usage Examples

Connecting a Source to a Target

#include "tensorflow_serving/core/target.h"
#include "tensorflow_serving/core/source.h"

using namespace tensorflow::serving;

// Assume source and target are properly initialized
Source<StoragePath>* source = ...;
Target<StoragePath>* target = ...;

ConnectSourceToTarget(source, target);
// Now the source will forward aspired-version calls to the target

Implementing a Custom Target

#include "tensorflow_serving/core/target.h"

class MyTarget : public TargetBase<StoragePath> {
 public:
  ~MyTarget() override { Detach(); }  // REQUIRED in leaf class

 protected:
  void SetAspiredVersions(
      const StringPiece servable_name,
      std::vector<ServableData<StoragePath>> versions) override {
    // Handle incoming aspired versions
    for (auto& version : versions) {
      LOG(INFO) << "Aspired: " << version.id();
    }
  }
};

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment