nstream.reflect.model.RouterStats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-reflect Show documentation
Show all versions of nstream-reflect Show documentation
Web Agent introspection runtime
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.model;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import swim.collections.HashTrieMap;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Value;
public class RouterStats {
volatile HashTrieMap nodeStats;
public RouterStats(HashTrieMap nodeStats) {
this.nodeStats = nodeStats;
}
public RouterStats() {
this(HashTrieMap.empty());
}
public NodeStats getOrCreateNodeStats(Value nodeKey) {
NodeStats service;
do {
final HashTrieMap oldNodeStats = this.nodeStats;
service = oldNodeStats.get(nodeKey);
if (service == null) {
service = new NodeStats(nodeKey);
final HashTrieMap newNodeStats = oldNodeStats.updated(nodeKey, service);
if (NODE_STATS.compareAndSet(this, oldNodeStats, newNodeStats)) {
break;
}
} else {
break;
}
} while (true);
return service;
}
public void didOpenNode(Value nodeKey) {
final NodeStats service = getOrCreateNodeStats(nodeKey);
service.didOpenNode();
}
public void didCloseNode(Value nodeKey) {
final NodeStats service = getOrCreateNodeStats(nodeKey);
service.didCloseNode();
}
public void accumulate(RouterStats router) {
for (NodeStats service : router.nodeStats.values()) {
accumulate(service);
}
}
public void accumulate(NodeStats stats) {
final NodeStats service = getOrCreateNodeStats(stats.nodeKey);
service.accumulate(stats);
}
public void accumulate(Value nodeKey, LaneStats lane) {
final NodeStats service = getOrCreateNodeStats(nodeKey);
service.accumulate(lane);
}
public void supersede(RouterStats total, RouterStats delta) {
for (NodeStats service : this.nodeStats.values()) {
final NodeStats serviceTotal = total.getOrCreateNodeStats(service.nodeKey);
final NodeStats serviceDelta = delta.getOrCreateNodeStats(service.nodeKey);
service.supersede(serviceTotal, serviceDelta);
}
}
public RouterStats getAndReset() {
final HashTrieMap oldNodeStats = this.nodeStats;
HashTrieMap newNodeStats = HashTrieMap.empty();
for (NodeStats service : oldNodeStats.values()) {
newNodeStats = newNodeStats.updated(service.nodeKey, service.getAndReset());
}
return new RouterStats(newNodeStats);
}
public RouterStats get() {
final HashTrieMap oldNodeStats = this.nodeStats;
HashTrieMap newNodeStats = HashTrieMap.empty();
for (NodeStats service : oldNodeStats.values()) {
newNodeStats = newNodeStats.updated(service.nodeKey, service.get());
}
return new RouterStats(newNodeStats);
}
public Value toValue() {
return form().mold(this).toValue();
}
@SuppressWarnings("unchecked")
static final AtomicReferenceFieldUpdater> NODE_STATS =
AtomicReferenceFieldUpdater.newUpdater(RouterStats.class, (Class>) (Class>) HashTrieMap.class, "nodeStats");
private static Form form;
@Kind
public static Form form() {
if (form == null) {
form = new RouterStatsForm();
}
return form;
}
}
final class RouterStatsForm extends Form {
@Override
public Class> type() {
return RouterStats.class;
}
@Override
public Item mold(RouterStats stats) {
if (stats != null) {
final Record record = Record.create();
for (NodeStats service : RouterStats.NODE_STATS.get(stats).values()) {
record.item(service.toValue());
}
return record;
} else {
return Item.extant();
}
}
@Override
public RouterStats cast(Item item) {
final Value value = item.toValue();
HashTrieMap nodeStats = HashTrieMap.empty();
for (Item member : value) {
final NodeStats node = NodeStats.form().cast(member.toValue());
if (node != null) {
nodeStats = nodeStats.updated(node.nodeKey, node);
}
}
return new RouterStats(nodeStats);
}
}