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

nstream.reflect.MetaMesh 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;

import nstream.reflect.agent.MetaMeshAgent;
import nstream.reflect.model.DataStats;
import nstream.reflect.model.LinkStats;
import nstream.reflect.model.LogEntry;
import nstream.reflect.model.MeshStats;
import nstream.reflect.model.PartStats;
import nstream.reflect.model.ProcessStats;
import nstream.reflect.model.StoreStats;
import nstream.reflect.model.SystemStats;
import swim.api.Downlink;
import swim.collections.FingerTrieSeq;
import swim.recon.Recon;
import swim.structure.Value;
import swim.system.LinkBinding;
import swim.system.MeshBinding;
import swim.system.MeshProxy;
import swim.system.NodeBinding;
import swim.system.PartAddress;
import swim.system.PartBinding;
import swim.system.WarpBinding;
import swim.uri.UriPath;
import swim.util.Builder;

public class MetaMesh extends MeshProxy implements MetaRouter {
  final MetaEdge parent;
  final MetaMeshAgent agent;

  public MetaMesh(MetaEdge parent, MeshBinding meshBinding) {
    super(meshBinding);
    this.parent = parent;
    this.agent = new MetaMeshAgent(this);
  }

  @Override
  public MetaEdge metaParent() {
    return this.parent;
  }

  @Override
  public MetaMeshAgent metaAgent() {
    return this.agent;
  }

  @Override
  public LinkStats linkStats() {
    return this.agent.linkStats();
  }

  @Override
  public DataStats dataStats() {
    return this.agent.dataStats();
  }

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

  @Override
  public ProcessStats processStats() {
    return this.parent.processStats();
  }

  @Override
  public SystemStats systemStats() {
    return this.parent.systemStats();
  }

  public void cueSystemStats() {
    this.agent.cueSystemStats();
    for (PartBinding part : parts()) {
      final MetaPart metaPart = part.partWrapper().unwrapPart(MetaPart.class);
      if (metaPart != null) {
        metaPart.cueSystemStats();
      }
    }
  }

  public MeshStats meshStats() {
    final FingerTrieSeq parts = parts();
    final Builder> partStats = FingerTrieSeq.builder();
    for (PartBinding part : parts) {
      final MetaPart metaPart = part.partWrapper().unwrapPart(MetaPart.class);
      if (metaPart != null) {
        partStats.add(metaPart.partStats());
      }
    }
    return new MeshStats(meshUri(), linkStats(), partStats.bind());
  }

  @Override
  public PartBinding injectPart(PartAddress partAddress, PartBinding part) {
    part = super.injectPart(partAddress, part);
    if (!"swim".equals(partAddress.partKey().stringValue(null))) { // Don't introspect introspection
      part = new MetaPart(this, part);
    }
    return part;
  }

  @Override
  public LinkBinding bindDownlink(Downlink downlink) {
    final LinkBinding link = super.bindDownlink(downlink);
    if (link instanceof WarpBinding) {
      final MetaWarpDownlink metaLink = link.linkWrapper().unwrapLink(MetaWarpDownlink.class);
      if (metaLink != null) {
        metaLink.parent = this;
      }
    }
    return link;
  }

  @Override
  public void openDownlink(LinkBinding link) {
    if (link instanceof WarpBinding) {
      if (link.linkWrapper().unwrapLink(MetaWarpDownlink.class) == null) {
        link = new MetaWarpDownlink(this, (WarpBinding) link);
      }
    }
    super.openDownlink(link);
  }

  @Override
  public void trace(Object message) {
    this.agent.didLogTrace(LogEntry.trace(message));
  }

  @Override
  public void debug(Object message) {
    this.agent.didLogDebug(LogEntry.debug(message));
  }

  @Override
  public void info(Object message) {
    this.agent.didLogInfo(LogEntry.info(message));
  }

  @Override
  public void warn(Object message) {
    this.agent.didLogWarn(LogEntry.warn(message));
  }

  @Override
  public void error(Object message) {
    this.agent.didLogError(LogEntry.error(message));
  }

  @Override
  public void didFail(Throwable error) {
    try {
      this.agent.didLogFail(LogEntry.fail(error));
    } finally {
      super.didFail(error);
    }
  }

  @Override
  public void willLoad() {
    super.willLoad();
    this.agent.load();
  }

  @Override
  public void didClose() {
    this.agent.close();
    super.didClose();
  }

  public NodeBinding resolveMetaMesh(UriPath nodePath) {
    if (nodePath.isEmpty()) {
      return this.agent;
    } else if ("part".equals(nodePath.head())) {
      nodePath = nodePath.tail(); // drop part
      if (!nodePath.isEmpty()) {
        nodePath = nodePath.tail(); // drop /
      }
      return resolveMetaPart(nodePath);
    } else if ("host".equals(nodePath.head())) {
      nodePath = nodePath.tail(); // drop host
      if (!nodePath.isEmpty()) {
        nodePath = nodePath.tail(); // drop /
      }
      return resolveMetaHost(nodePath);
    } else if ("node".equals(nodePath.head())) {
      nodePath = nodePath.tail(); // drop node
      if (!nodePath.isEmpty()) {
        nodePath = nodePath.tail(); // drop /
      }
      return resolveMetaNode(nodePath);
    }
    return null;
  }

  public NodeBinding resolveMetaPart(UriPath nodePath) {
    if (nodePath.isAbsolute()) {
      return null;
    }
    final Value partKey = nodePath.isEmpty() ? Value.extant() : Recon.parse(nodePath.head());
    final PartBinding partBinding = getPart(partKey);
    if (partBinding != null) {
      final MetaPart metaPart = partBinding.partWrapper().unwrapPart(MetaPart.class);
      if (metaPart != null) {
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop partKey
          if (!nodePath.isEmpty()) {
            nodePath = nodePath.tail(); // drop /
          }
        }
        return metaPart.resolveMetaPart(nodePath);
      }
    }
    return null;
  }

  public NodeBinding resolveMetaHost(UriPath nodePath) {
    final PartBinding partBinding = gateway();
    if (partBinding != null) {
      final MetaPart metaPart = partBinding.partWrapper().unwrapPart(MetaPart.class);
      if (metaPart != null) {
        return metaPart.resolveMetaHost(nodePath);
      }
    }
    return null;
  }

  public NodeBinding resolveMetaNode(UriPath nodePath) {
    final PartBinding partBinding = gateway();
    if (partBinding != null) {
      final MetaPart metaPart = partBinding.partWrapper().unwrapPart(MetaPart.class);
      if (metaPart != null) {
        return metaPart.resolveMetaNode(nodePath);
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy