All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nstream.reflect.agent.MetaRouterAgent Maven / Gradle / Ivy

There is a newer version: 4.13.21
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package nstream.reflect.agent;

import nstream.reflect.MetaRouter;
import nstream.reflect.model.ProcessStats;
import nstream.reflect.model.RouterStats;
import nstream.reflect.model.StoreStats;
import nstream.reflect.model.SystemStats;
import swim.api.lane.DemandLane;
import swim.api.lane.function.OnCue;
import swim.api.warp.WarpUplink;
import swim.uri.Uri;

public abstract class MetaRouterAgent extends MetaCellAgent {
  protected final RouterStats routerTotal = new RouterStats();
  protected final RouterStats routerDelta = new RouterStats();
  protected final StoreStats storeTotal = new StoreStats();
  protected final StoreStats storeDelta = new StoreStats();

  volatile DemandLane routerStats;
  volatile DemandLane storeStats;
  volatile DemandLane processStats;
  volatile DemandLane systemStats;

  public MetaRouterAgent() {
    // nop
  }

  @Override
  public abstract MetaRouter meta();

  @Override
  protected void openLanes() {
    super.openLanes();

    final DemandLane routerStats = demandLane()
        .valueForm(RouterStats.form())
        .observe(new MetaRouterStatsController(this));
    openLane(ROUTER_STATS_URI, routerStats);
    this.routerStats = routerStats;

    final DemandLane storeStats = demandLane()
        .valueForm(StoreStats.form())
        .observe(new MetaStoreStatsController(this));
    openLane(STORE_STATS_URI, storeStats);
    this.storeStats = storeStats;

    final DemandLane processStats = demandLane()
        .valueForm(ProcessStats.form())
        .observe(new MetaProcessStatsController(this));
    openLane(PROCESS_STATS_URI, processStats);
    this.processStats = processStats;

    final DemandLane systemStats = demandLane()
        .valueForm(SystemStats.form())
        .observe(new MetaSystemStatsController(this));
    openLane(SYSTEM_STATS_URI, systemStats);
    this.systemStats = systemStats;
  }

  public RouterStats routerStats() {
    return this.routerTotal.get();
  }

  public void setRouterStats(RouterStats stats) {
    stats.supersede(this.routerTotal, this.routerDelta);
    didUpdateStats();
  }

  public void accumulateRouterStats(RouterStats stats) {
    this.routerTotal.accumulate(stats);
    this.routerDelta.accumulate(stats);
    didUpdateStats();
  }

  protected void bubbleRouterStats() {
    final MetaRouter metaParent = meta().metaParent();
    if (metaParent != null) {
      final MetaRouterAgent parentAgent = metaParent.metaAgent();
      parentAgent.accumulateRouterStats(this.routerDelta.getAndReset());
    }
  }

  protected void cueRouterStats() {
    final DemandLane routerStats = this.routerStats;
    if (routerStats != null) {
      routerStats.cue();
    }
  }

  public StoreStats storeStats() {
    return this.storeTotal.get();
  }

  public void storeDidCommit(long commitSize) {
    this.storeTotal.didCommit(commitSize);
    this.storeDelta.didCommit(commitSize);
    didUpdateStats();
  }

  public void storeDidCompact() {
    this.storeTotal.didCompact();
    this.storeDelta.didCompact();
    didUpdateStats();
  }

  public void setStoreStats(StoreStats stats) {
    stats.supersede(this.storeTotal, this.storeDelta);
    didUpdateStats();
  }

  public void accumulateStoreStats(StoreStats stats) {
    this.storeTotal.accumulate(stats);
    this.storeDelta.accumulate(stats);
    didUpdateStats();
  }

  protected void bubbleStoreStats() {
    final MetaRouter metaParent = meta().metaParent();
    if (metaParent != null) {
      final MetaRouterAgent parentAgent = metaParent.metaAgent();
      parentAgent.accumulateStoreStats(this.storeDelta.getAndReset());
    }
  }

  protected void cueStoreStats() {
    final DemandLane storeStats = this.storeStats;
    if (storeStats != null) {
      storeStats.cue();
    }
  }

  @Override
  public void bubbleStats() {
    super.bubbleStats();
    bubbleStoreStats();
    bubbleRouterStats();
  }

  @Override
  public void cueStats() {
    super.cueStats();
    cueStoreStats();
    cueRouterStats();
  }

  public ProcessStats processStats() {
    return meta().processStats();
  }

  public SystemStats systemStats() {
    return meta().systemStats();
  }

  public void cueSystemStats() {
    final DemandLane processStats = this.processStats;
    if (processStats != null) {
      processStats.cue();
    }

    final DemandLane systemStats = this.systemStats;
    if (systemStats != null) {
      systemStats.cue();
    }
  }

  static final Uri ROUTER_STATS_URI = Uri.parse("routerStats");
  static final Uri STORE_STATS_URI = Uri.parse("storeStats");
  static final Uri PROCESS_STATS_URI = Uri.parse("processStats");
  static final Uri SYSTEM_STATS_URI = Uri.parse("systemStats");
}

final class MetaRouterStatsController implements OnCue {
  final MetaRouterAgent agent;

  MetaRouterStatsController(MetaRouterAgent agent) {
    this.agent = agent;
  }

  @Override
  public RouterStats onCue(WarpUplink uplink) {
    return this.agent.routerStats();
  }
}

final class MetaStoreStatsController implements OnCue {
  final MetaRouterAgent agent;

  MetaStoreStatsController(MetaRouterAgent agent) {
    this.agent = agent;
  }

  @Override
  public StoreStats onCue(WarpUplink uplink) {
    return this.agent.storeStats();
  }
}

final class MetaProcessStatsController implements OnCue {
  final MetaRouterAgent agent;

  MetaProcessStatsController(MetaRouterAgent agent) {
    this.agent = agent;
  }

  @Override
  public ProcessStats onCue(WarpUplink uplink) {
    return this.agent.processStats();
  }
}

final class MetaSystemStatsController implements OnCue {
  final MetaRouterAgent agent;

  MetaSystemStatsController(MetaRouterAgent agent) {
    this.agent = agent;
  }

  @Override
  public SystemStats onCue(WarpUplink uplink) {
    return this.agent.systemStats();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy