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

nstream.reflect.MetaPart Maven / Gradle / Ivy

The 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.MetaPartAgent;
import nstream.reflect.model.DataStats;
import nstream.reflect.model.HostStats;
import nstream.reflect.model.LinkStats;
import nstream.reflect.model.LogEntry;
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.collections.HashTrieMap;
import swim.system.HostAddress;
import swim.system.HostBinding;
import swim.system.LinkBinding;
import swim.system.NodeBinding;
import swim.system.PartBinding;
import swim.system.PartProxy;
import swim.system.WarpBinding;
import swim.uri.Uri;
import swim.uri.UriPath;
import swim.util.Builder;

public class MetaPart extends PartProxy implements MetaRouter {
  final MetaMesh parent;
  final MetaPartAgent agent;

  public MetaPart(MetaMesh parent, PartBinding partBinding) {
    super(partBinding);
    this.parent = parent;
    this.agent = new MetaPartAgent(this);
  }

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

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

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

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

  @Override
  public StoreStats storeStats() {
    final StoreStats stats = new StoreStats();
    //stats.dataSize = getDataSize();
    //stats.storeSize = getStoreSize();
    //stats.treeCount = getTreeCount();
    //stats.leafCount = getLeafCount();
    return stats;
  }

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

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

  public void cueSystemStats() {
    this.agent.cueSystemStats();
    for (HostBinding host : hosts().values()) {
      final MetaHost metaHost = host.hostWrapper().unwrapHost(MetaHost.class);
      if (metaHost != null) {
        metaHost.cueSystemStats();
      }
    }
  }

  public PartStats partStats() {
    final HashTrieMap hosts = hosts();
    final Builder> hostStats = FingerTrieSeq.builder();
    for (HostBinding host : hosts.values()) {
      final MetaHost metaHost = host.hostWrapper().unwrapHost(MetaHost.class);
      if (metaHost != null) {
        hostStats.add(metaHost.hostStats());
      }
    }
    return new PartStats(partKey(), linkStats(), hostStats.bind());
  }

  @Override
  public HostBinding injectHost(HostAddress hostAddress, HostBinding host) {
    host = super.injectHost(hostAddress, host);
    if (host.isRemote() && !meshUri().isDefined()) {
      // TODO: host = new MetaHostRemote(this, host);
    } else {
      host = new MetaHost(this, host);
    }
    return host;
  }

  @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 resolveMetaPart(UriPath nodePath) {
    if (nodePath.isEmpty()) {
      return this.agent;
    } 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 resolveMetaHost(UriPath nodePath) {
    if (nodePath.isAbsolute()) {
      return null;
    }
    final Uri hostUri = nodePath.isEmpty() ? Uri.empty() : Uri.parse(nodePath.head());
    final HostBinding hostBinding = getHost(hostUri);
    if (hostBinding != null) {
      final MetaHost metaHost = hostBinding.hostWrapper().unwrapHost(MetaHost.class);
      if (metaHost != null) {
        if (!nodePath.isEmpty()) {
          nodePath = nodePath.tail(); // drop hostUri
          if (!nodePath.isEmpty()) {
            nodePath = nodePath.tail(); // drop /
          }
        }
        return metaHost.resolveMetaHost(nodePath);
      }
    }
    return null;
  }

  public NodeBinding resolveMetaNode(UriPath nodePath) {
    final HostBinding hostBinding = master();
    if (hostBinding != null) {
      final MetaHost metaHost = hostBinding.hostWrapper().unwrapHost(MetaHost.class);
      if (metaHost != null) {
        return metaHost.resolveMetaNode(nodePath);
      }
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy