nstream.reflect.model.LaneStats 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.AtomicLongFieldUpdater;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Value;
import swim.uri.Uri;
@SuppressWarnings("checkstyle:VisibilityModifier")
public class LaneStats {
public Uri laneUri;
public volatile long laneCount;
public final LinkStats linkStats;
public LaneStats(Uri laneUri, long laneCount, LinkStats linkStats) {
this.laneUri = laneUri;
this.laneCount = laneCount;
this.linkStats = linkStats;
}
public LaneStats(Uri laneUri, LinkStats linkStats) {
this(laneUri, 0L, linkStats);
}
public LaneStats(Uri laneUri) {
this(laneUri, 0L, new LinkStats());
}
public void didOpenLane() {
LANE_COUNT.incrementAndGet(this);
}
public void didCloseLane() {
LANE_COUNT.decrementAndGet(this);
}
public void accumulate(LaneStats lane) {
LANE_COUNT.addAndGet(this, lane.laneCount);
this.linkStats.accumulate(lane.linkStats);
}
public void supersede(LaneStats total, LaneStats delta) {
final long newLaneCount = this.laneCount;
final long oldLaneCount = LANE_COUNT.getAndSet(total, newLaneCount);
LANE_COUNT.addAndGet(delta, newLaneCount - oldLaneCount);
this.linkStats.supersede(total.linkStats, total.linkStats);
}
public LaneStats getAndReset() {
final long laneCount = LANE_COUNT.getAndSet(this, 0L);
return new LaneStats(this.laneUri, laneCount, this.linkStats.getAndReset());
}
public LaneStats get() {
return new LaneStats(this.laneUri, this.laneCount, this.linkStats.get());
}
public Value toValue() {
return form().mold(this).toValue();
}
static final AtomicLongFieldUpdater LANE_COUNT =
AtomicLongFieldUpdater.newUpdater(LaneStats.class, "laneCount");
private static Form form;
@Kind
public static Form form() {
if (form == null) {
form = new LaneStatsForm();
}
return form;
}
}
final class LaneStatsForm extends Form {
@Override
public String tag() {
return "lane";
}
@Override
public Class> type() {
return LaneStats.class;
}
@Override
public Item mold(LaneStats stats) {
if (stats != null) {
final Record record = Record.create();
record.attr(tag(), stats.laneUri.toString());
if (stats.laneCount > 0L) {
record.slot("laneCount", stats.laneCount);
}
record.addAll((Record) stats.linkStats.toValue());
return record;
} else {
return Item.extant();
}
}
@Override
public LaneStats cast(Item item) {
final Value value = item.toValue();
final Value header = value.getAttr(tag());
if (header.isDefined()) {
final Uri laneUri = header.coerce(Uri.form());
final long laneCount = value.get("laneCount").longValue(0L);
final LinkStats linkStats = LinkStats.form().cast(value);
return new LaneStats(laneUri, laneCount, linkStats);
}
return null;
}
}