http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsSystem.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsSystem.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsSystem.cs new file mode 100644 index 0000000..cfd6532 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsSystem.cs @@ -0,0 +1,77 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + [Unstable("0.16", "Contract may change.")] + public interface IMetricsSystem : IObservable<IMetricsRecord> + { + /// <summary> + /// (Re)Start the whole Metrics system. This along with <see cref="Stop"/> can be called + /// multiple times to start and stop the metrics system. Exact logic of what to do if + /// function is called multiple times before Stop is called is left to the underlying + /// implementation. + /// </summary> + void Start(); + + /// <summary> + /// Stop the metrics system. The system can be started and stopped again. Exact logic of what to do if + /// function is called multiple times before Start is called is left to the underlying + /// implementation. + /// </summary> + void Stop(); + + /// <summary> + /// Register the metrics source. + /// </summary> + /// <param name="name">Name of the source. Must be unique.</param> + /// <param name="desc">Description of the source.</param> + /// <param name="source">Metrics source to register.</param> + /// <returns>Metrics source</returns> + IMetricsSource RegisterSource(string name, string desc, IMetricsSource source); + + /// <summary> + /// Unregister the metrics source. + /// </summary> + /// <param name="name">Name of the source.</param> + void UnRegisterSource(string name); + + /// <summary> + /// Gets the metrics source. + /// </summary> + /// <param name="name">Name of the metrics source.</param> + /// <returns>Metrics source.</returns> + IMetricsSource GetSource(string name); + + /// <summary> + /// Requests an immediate publish of all metrics from sources to sinks. + /// Best effort will be done to push metrics from source to sink synchronously + /// before returning. However, if it cannot be done in reasonable time + /// it is ok to return to the caller before everything is done. + /// </summary> + void PublishMetricsNow(); + + /// <summary> + /// Completely shuts down the metrics system. + /// </summary> + /// <returns>True if proper shutdown happened.</returns> + bool ShutDown(); + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsVisitor.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsVisitor.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsVisitor.cs new file mode 100644 index 0000000..83289cb --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/IMetricsVisitor.cs @@ -0,0 +1,56 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Metrics visitor interface. The visitor is used to extract metric + /// specific information from the immutable metrics where specfic information + /// is lost. For example, <see cref="IImmutableMetric"/> loses any information about the derived class + /// but provides a function that takes IMetricsVisitor as input. The specific + /// implementations can then call the appropriate call back functions below + /// that then allows visitor to take appropriate action. This interface can for + /// example be used by observers of <see cref="IMetricsRecord"/> to get specific information about metrics + /// when it receives the record. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface IMetricsVisitor + { + /// <summary> + /// Callback for long value gauges + /// </summary> + /// <param name="info">Meta-data of the metric.</param> + /// <param name="value">Long value of the gauge.</param> + void Gauge(IMetricsInfo info, long value); + + /// <summary> + /// Callback for double value gauges + /// </summary> + /// <param name="info">Meta-data of the metric.</param> + /// <param name="value">Double value of the gauge.</param> + void Gauge(IMetricsInfo info, double value); + + /// <summary> + /// Callback for long value counter + /// </summary> + /// <param name="info">Meta-data of the metric.</param> + /// <param name="value">Long value of the counter.</param> + void Counter(IMetricsInfo info, long value); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/ImmutableMetricImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/ImmutableMetricImpl.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/ImmutableMetricImpl.cs new file mode 100644 index 0000000..4d57850 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/ImmutableMetricImpl.cs @@ -0,0 +1,153 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Base implementation of <see cref="IImmutableMetric"/>. + /// </summary> + internal class ImmutableMetricsImpl : IImmutableMetric + { + private readonly IMetricsInfo _info; + private readonly Action<IMetricsVisitor> _onVisit; + + /// <summary> + /// Constructor. Called by metrics of integral types. + /// </summary> + /// <param name="info">Meta-data for the metric</param> + /// <param name="value">The integral value of the metric. Integral types can be + /// directly type casted to ulong.</param> + /// <param name="typeOfMetric">Type of metric - counter or gauge</param> + /// <param name="onVisit">Action to take on receiving <see cref="IMetricsVisitor"/></param> + public ImmutableMetricsImpl(IMetricsInfo info, + long value, + MetricType typeOfMetric, + Action<IMetricsVisitor> onVisit) + { + LongValue = value; + NumericValue = null; + _info = info; + TypeOfMetric = typeOfMetric; + _onVisit = onVisit; + } + + /// <summary> + /// Constructor. Called by metrics of numerical types. + /// </summary> + /// <param name="info">Meta-data for the metric</param> + /// <param name="value">The numerical value of the metric. Numerical types can be + /// directly type casted to double.</param> + /// <param name="typeOfMetric">Type of metric - counter or gauge</param> + /// <param name="onVisit">Action to take on receiving <see cref="IMetricsVisitor"/></param> + public ImmutableMetricsImpl(IMetricsInfo info, + double value, + MetricType typeOfMetric, + Action<IMetricsVisitor> onVisit) + { + NumericValue = value; + LongValue = null; + TypeOfMetric = typeOfMetric; + _info = info; + _onVisit = onVisit; + } + + /// <summary> + /// Meta-data of the metric. + /// </summary> + public IMetricsInfo Info + { + get { return _info; } + } + + /// <summary> + /// Long Value of the metric. Immutable metrics of + /// type integrals, byte, bool are all type casted to long before storing + /// them as immutable metrics. For a given instance of this interface, either this property + /// or <see cref="NumericValue"/> returns a valid value. + /// </summary> + public long? LongValue { get; private set; } + + /// <summary> + /// Numeric Value of the metric. Immutable metrics of + /// non integral numerical types are all type casted to double before storing + /// them as immutable metrics. For a given instance of this interface, either this property + /// or <see cref="LongValue"/> returns a valid value. + /// </summary> + public double? NumericValue { get; private set; } + + /// <summary> + /// Type of metric - counter or gauge. Filled in by exact + /// metric type. + /// </summary> + public MetricType TypeOfMetric { get; private set; } + + /// <summary> + /// Accepts a visitor interface + /// </summary> + /// <param name="visitor">Metrics visitor interface.</param> + public void Visit(IMetricsVisitor visitor) + { + _onVisit(visitor); + } + + /// <summary> + /// String representation of a metric for display. + /// </summary> + /// <returns>The string representation of the metric.</returns> + public override string ToString() + { + return string.Format("Metric Type: {0}, Metric Information: {1}, Metric Value: {2}", + TypeOfMetric, + _info, + LongValue ?? NumericValue); + } + + /// <summary> + /// Checks whether two metrics are equal. Relies on Equals + /// function of <see cref="IMetricsInfo"/> implementations. + /// </summary> + /// <param name="obj">Object to compare against.</param> + /// <returns>True if both represent the same metric.</returns> + public override bool Equals(object obj) + { + var otherMetric = obj as IImmutableMetric; + if (otherMetric != null) + { + if (otherMetric.Info.Equals(_info) && otherMetric.TypeOfMetric.Equals(TypeOfMetric) && + otherMetric.LongValue == LongValue) + { + return LongValue != null + ? LongValue.Value == otherMetric.LongValue.Value + : NumericValue.Value == otherMetric.NumericValue.Value; + } + } + return false; + } + + /// <summary> + /// Return hash code of the metric object. Simply uses the hash of ToString() method. + /// </summary> + /// <returns>Hash code.</returns> + public override int GetHashCode() + { + var hashCode = ToString().GetHashCode(); + return hashCode; + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricType.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricType.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricType.cs new file mode 100644 index 0000000..c37a538 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricType.cs @@ -0,0 +1,39 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Types of metrics we support in MetricsSystem. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public enum MetricType + { + /// <summary> + /// A monotonically increasing metric. For example to record amount of data read, + /// iterations etc. + /// </summary> + Counter, + + /// <summary> + /// An arbitrary varying metric, For example loss function, trnasfer rates etc. + /// </summary> + Gauge, + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsException.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsException.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsException.cs new file mode 100644 index 0000000..3f72ed0 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsException.cs @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Exception class used to wrap any exception in metrics. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public class MetricsException : Exception + { + // Static message to pre-append to any user message. + private static readonly string MessagePrefix = "Error in Metrics."; + + /// <summary> + /// Empty constructor. + /// </summary> + public MetricsException() : base(MessagePrefix) + { + } + + /// <summary> + /// Construct exception with the user message. + /// </summary> + /// <param name="message">User defined message.</param> + public MetricsException(string message) + : base(string.Format("{0} {1}", MessagePrefix, message)) + { + } + + /// <summary> + /// Construct exception with the user message and inner exception. + /// </summary> + /// <param name="message">User defined message.</param> + /// <param name="innerException">Inner exception.</param> + public MetricsException(string message, Exception innerException) + : base(string.Format("{0} {1}", MessagePrefix, message), innerException) + { + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsInfoImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsInfoImpl.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsInfoImpl.cs new file mode 100644 index 0000000..be2776f --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsInfoImpl.cs @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Default implementation of the <see cref="IMetricsInfo"/> + /// </summary> + [Unstable("0.16", "Contract may change.")] + public sealed class MetricsInfoImpl : IMetricsInfo + { + /// <summary> + /// Constructor. + /// </summary> + /// <param name="name">Name of the metric.</param> + /// <param name="desc">Description of the metric.</param> + public MetricsInfoImpl(string name, string desc) + { + Name = name; + Description = desc; + } + + /// <summary> + /// Name of the metric. + /// </summary> + public string Name { get; private set; } + + /// <summary> + /// Description of the metric. + /// </summary> + public string Description { get; private set; } + + /// <summary> + /// Overrides base ToString method. + /// </summary> + /// <returns>string representation of the class.</returns> + public override string ToString() + { + return string.Format("Name: {0}, Description: {1}", Name, Description); + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsTag.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsTag.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsTag.cs new file mode 100644 index 0000000..8d6d392 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/MetricsTag.cs @@ -0,0 +1,114 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Metrics tag class. Tags can be used to group records together at source, sink or other + /// appropriate places. They can also be used in MetricsFilter to filter records at higher level. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public sealed class MetricsTag + { + private readonly IMetricsInfo _info; + private readonly string _value; + + /// <summary> + /// Constructor for tags. + /// </summary> + /// <param name="info">Meta data for tags.</param> + /// <param name="value">Value of the tag.</param> + internal MetricsTag(IMetricsInfo info, string value) + { + _info = info; + _value = value; + } + + /// <summary> + /// Name of the tag. + /// </summary> + public string Name + { + get { return _info.Name; } + } + + /// <summary> + /// Description of the tag. + /// </summary> + public string Description + { + get { return _info.Description; } + } + + /// <summary> + /// Info object of the tag. + /// </summary> + public IMetricsInfo Info + { + get { return _info; } + } + + /// <summary> + /// Value of the tag. + /// </summary> + public string Value + { + get { return _value; } + } + + /// <summary> + /// String representation of a tag for display. + /// </summary> + /// <returns>The string representation of the tag.</returns> + public override string ToString() + { + return string.Format("Tag Information: {0}, Tag Value: {1}", _info, _value); + } + + /// <summary> + /// Checks whether two tags are equal. Relies on Equals + /// function of <see cref="IMetricsInfo"/> implementations. + /// </summary> + /// <param name="obj">Object to compare against.</param> + /// <returns>True if both represent the same tag.</returns> + public override bool Equals(object obj) + { + var metricsTag = obj as MetricsTag; + if (metricsTag != null) + { + if (metricsTag.Info.Equals(_info) && metricsTag.Value.Equals(_value)) + { + return true; + } + } + + return false; + } + + /// <summary> + /// Return hash code of the Tag object. Simply uses the hash of ToString() method. + /// </summary> + /// <returns>Hash code.</returns> + public override int GetHashCode() + { + var hashCode = ToString().GetHashCode(); + return hashCode; + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/Api/SnapshotRequest.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/Api/SnapshotRequest.cs b/lang/cs/Org.Apache.REEF.Common/metrics/Api/SnapshotRequest.cs new file mode 100644 index 0000000..0240f42 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/Api/SnapshotRequest.cs @@ -0,0 +1,59 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.Api +{ + /// <summary> + /// Class representing the Snapshot request. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public sealed class SnapshotRequest + { + /// <summary> + /// Constructor. + /// </summary> + /// <param name="builder">Metrics record builder to be used to add metrics to record.</param> + /// <param name="fullSnapshot">If true, record even uncanged metrics.</param> + internal SnapshotRequest(IMetricsRecordBuilder builder, bool fullSnapshot) + { + Builder = builder; + FullSnapshot = fullSnapshot; + } + + /// <summary> + /// Constructor. Sets <see cref="FullSnapshot"/> to false. + /// </summary> + /// <param name="builder">Metrics record builder to be used to add metrics to record.</param> + internal SnapshotRequest(IMetricsRecordBuilder builder) + { + Builder = builder; + FullSnapshot = false; + } + + /// <summary> + /// Builder to add metrics to the record. + /// </summary> + public IMetricsRecordBuilder Builder { get; private set; } + + /// <summary> + /// Determines whether to take snapshot of unchangesmetrics (true) or not (false). + /// </summary> + public bool FullSnapshot { get; private set; } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsFactoryImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsFactoryImpl.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsFactoryImpl.cs new file mode 100644 index 0000000..f32d6aa --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsFactoryImpl.cs @@ -0,0 +1,163 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Tang.Annotations; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Default implementation of <see cref="IMetricsFactory"/>. + /// </summary> + internal sealed class DefaultMetricsFactoryImpl : IMetricsFactory + { + [Inject] + private DefaultMetricsFactoryImpl() + { + } + + /// <summary> + /// Creates new tag + /// </summary> + /// <param name="info">Meta-data of the tag.</param> + /// <param name="value">Value of the tag.</param> + /// <returns>The new tag.</returns> + public MetricsTag CreateTag(IMetricsInfo info, string value) + { + return new MetricsTag(info, value); + } + + /// <summary> + /// Creates a counter by name. Description is assumed to be + /// same as name. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the counter.</param> + /// <returns>Newly created counter.</returns> + public ICounter CreateCounter(string name) + { + return new MutableCounter(name); + } + + /// <summary> + /// Creates a counter by name and description. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the counter.</param> + /// <param name="desc">Description of the counter</param> + /// <param name="initValue">Initial value of the counter</param> + /// <returns>Newly created counter.</returns> + public ICounter CreateCounter(string name, string desc, long initValue = 0) + { + return new MutableCounter(new MetricsInfoImpl(name, desc), initValue); + } + + /// <summary> + /// Creates a long gauge by name. Description is assumed to be + /// same as name. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <returns>Newly created gauge.</returns> + public ILongGauge CreateLongGauge(string name) + { + return new MutableLongGauge(name); + } + + /// <summary> + /// Creates a long gauge by name and description. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <param name="desc">Description of the gauge</param> + /// <param name="initValue">Initial value of the gauge</param> + /// <returns>Newly created gauge.</returns> + public ILongGauge CreateLongGauge(string name, string desc, long initValue = 0) + { + return new MutableLongGauge(new MetricsInfoImpl(name, desc), initValue); + } + + /// <summary> + /// Creates a double gauge by name. Description is assumed to be + /// same as name. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <returns>Newly created gauge.</returns> + public IDoubleGauge CreateDoubleGauge(string name) + { + return new MutableDoubleGauge(name); + } + + /// <summary> + /// Creates a double gauge by name and description. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <param name="desc">Description of the gauge</param> + /// <param name="initValue">Initial value of the gauge</param> + /// <returns>Newly created gauge.</returns> + public IDoubleGauge CreateDoubleGauge(string name, string desc, double initValue = 0) + { + return new MutableDoubleGauge(new MetricsInfoImpl(name, desc), initValue); + } + + /// <summary> + /// Creates the rate metric by name and description. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <param name="desc">Description of the rate.</param> + /// <param name="extendedMetrics">if true, stdev, min, max are also generated. Otherwise + /// only mean is computed.</param> + /// <returns>Newly created rate</returns> + public IRate CreateRateMetric(string name, string desc, bool extendedMetrics = true) + { + return new MutableRate(new MetricsInfoImpl(name, desc), extendedMetrics); + } + + /// <summary> + /// Creates the rate metric by name. Description is assumed to be + /// same as name. All metrics - mean, stdev, min , max are generated. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <returns>Newly created rate</returns> + public IRate CreateRateMetric(string name) + { + return new MutableRate(name); + } + + /// <summary> + /// Creates stats metric by name. Description is assumed to be + /// same as name. All metrics - mean, stdev, min , max are generated. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <param name="valueName">Value that which this metric represents (for example, Time, Latency etc.</param> + /// <returns>Newly created stat.</returns> + public IStat CreateStatMetric(string name, string valueName) + { + return new MutableStat(name, valueName); + } + + /// <summary> + /// Creates the stat metric by name and description. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <param name="desc">Description of the rate.</param> + /// <param name="valueName">Value that which this metric represents (for example, Time, Latency etc.</param> + /// <param name="extendedMetrics">if true, stdev, min, max are also generated. Otherwise + /// only mean is computed.</param> + /// <returns>Newly created stat.</returns> + public IStat CreateStatMetric(string name, string desc, string valueName, bool extendedMetrics = true) + { + return new MutableStat(new MetricsInfoImpl(name, desc), valueName, extendedMetrics); + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceConfiguration.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceConfiguration.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceConfiguration.cs new file mode 100644 index 0000000..611abd5 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceConfiguration.cs @@ -0,0 +1,62 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Tang.Formats; +using Org.Apache.REEF.Tang.Util; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// This configuration module defines configuration for <see cref="DefaultMetricsSourceImpl"/>. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public sealed class DefaultMetricsSourceConfiguration : ConfigurationModuleBuilder + { + /// <summary> + /// Id of task or context the source is part of. + /// </summary> + public static readonly RequiredParameter<string> TaskOrContextId = new RequiredParameter<string>(); + + /// <summary> + /// Id of the evaluator. + /// </summary> + public static readonly RequiredParameter<string> EvaluatorId = new RequiredParameter<string>(); + + /// <summary> + /// Context of the source (for example VM, ML etc.). + /// </summary> + public static readonly RequiredParameter<string> SourceContext = new RequiredParameter<string>(); + + /// <summary> + /// Id of the records generated by source. + /// </summary> + public static readonly RequiredParameter<string> RecordId = new RequiredParameter<string>(); + + /// <summary> + /// This configuration module set see<see cref="IMetricsSource"/> as <see cref="DefaultMetricsSourceImpl"/> + /// </summary> + public static ConfigurationModule ConfigurationModule = new DefaultMetricsSourceConfiguration() + .BindImplementation(GenericType<IMetricsSource>.Class, GenericType<DefaultMetricsSourceImpl>.Class) + .BindNamedParameter(GenericType<DefaultMetricsSourceParameters.ContextOrTaskName>.Class, TaskOrContextId) + .BindNamedParameter(GenericType<DefaultMetricsSourceParameters.EvaluatorId>.Class, EvaluatorId) + .BindNamedParameter(GenericType<DefaultMetricsSourceParameters.SourceContext>.Class, SourceContext) + .BindNamedParameter(GenericType<DefaultMetricsSourceParameters.RecordName>.Class, RecordId) + .Build(); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceImpl.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceImpl.cs new file mode 100644 index 0000000..bb39259 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceImpl.cs @@ -0,0 +1,220 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using System.Collections.Generic; +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Default implementation of <see cref="IMetricsSource"/>. Contains user friendly aceess to + /// add and update metrics. Can be extended or totally ignored by the user. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public class DefaultMetricsSourceImpl : IMetricsSource, IDisposable + { + private readonly IMetricContainer<ICounter> _counters; + private readonly IMetricContainer<ILongGauge> _longGauges; + private readonly IMetricContainer<IDoubleGauge> _doubleGauges; + private readonly IMetricContainer<IRate> _rates; + private readonly Dictionary<string, MetricsTag> _tags = new Dictionary<string, MetricsTag>(); + + private readonly IList<IObserver<SnapshotRequest>> _observers = new List<IObserver<SnapshotRequest>>(); + private readonly object _lock = new object(); + private readonly string _contextOrTaskName; + private readonly string _evaluatorId; + private readonly string _sourceContext; + private readonly string _recordName; + private readonly IMetricsFactory _metricsFactory; + + [Inject] + private DefaultMetricsSourceImpl( + [Parameter(typeof(DefaultMetricsSourceParameters.ContextOrTaskName))] string contextOrTaskName, + [Parameter(typeof(DefaultMetricsSourceParameters.EvaluatorId))] string evaluatorId, + [Parameter(typeof(DefaultMetricsSourceParameters.SourceContext))] string sourceContext, + [Parameter(typeof(DefaultMetricsSourceParameters.RecordName))] string recordName, + IMetricsFactory metricsFactory) + { + _contextOrTaskName = contextOrTaskName; + _evaluatorId = evaluatorId; + _sourceContext = sourceContext; + _recordName = recordName; + _metricsFactory = metricsFactory; + + _counters = new MutableMetricContainer<ICounter>( + (name, desc) => metricsFactory.CreateCounter(name, desc), + this); + _longGauges = new MutableMetricContainer<ILongGauge>( + (name, desc) => metricsFactory.CreateLongGauge(name, desc), + this); + _doubleGauges = new MutableMetricContainer<IDoubleGauge>( + (name, desc) => metricsFactory.CreateDoubleGauge(name, desc), + this); + _rates = new MutableMetricContainer<IRate>( + (name, desc) => metricsFactory.CreateRateMetric(name, desc), + this); + } + + /// <summary> + /// Returns indexable counters container. + /// </summary> + public IMetricContainer<ICounter> Counters + { + get { return _counters; } + } + + /// <summary> + /// Returns indexable long guage container. + /// </summary> + public IMetricContainer<ILongGauge> LongGauges + { + get { return _longGauges; } + } + + /// <summary> + /// Returns indexable double gauge container. + /// </summary> + public IMetricContainer<IDoubleGauge> DoubleGauges + { + get { return _doubleGauges; } + } + + /// <summary> + /// Returns indexable double gauge container. + /// </summary> + public IMetricContainer<IRate> Rates + { + get { return _rates; } + } + + /// <summary> + /// Adds a tag to the source. Replaces old one if it exists. + /// </summary> + /// <param name="name">Name of the tag.</param> + /// <param name="desc">Description of the tag.</param> + /// <param name="value">Value of the tag.</param> + public void AddTag(string name, string desc, string value) + { + lock (_lock) + { + _tags[name] = _metricsFactory.CreateTag(new MetricsInfoImpl(name, desc), value); + } + } + + /// <summary> + /// Returns tag by name. + /// </summary> + /// <param name="name">Name of the tag.</param> + /// <returns>Tag if it exists, null otherwise.</returns> + public MetricsTag GetTag(string name) + { + lock (_lock) + { + MetricsTag tag; + _tags.TryGetValue(name, out tag); + return tag; + } + } + + /// <summary> + /// Subscribes the <see cref="MutableMetricBase"/> whose snapshot + /// will be taken. This function will be used if user has its own metric types except + /// from the ones used in this class. For example, a ML algorithm can subscribe itself + /// as observer, and directly add metric vlaues like iterations, loss function etc. in the + /// record. + /// </summary> + /// <param name="observer">Observer that takes snapshot of the metric.</param> + /// <returns>A disposable handler used to unsubscribe the observer.</returns> + public IDisposable Subscribe(IObserver<SnapshotRequest> observer) + { + lock (_lock) + { + if (!_observers.Contains(observer)) + { + _observers.Add(observer); + } + return new Unsubscriber(_observers, observer, _lock); + } + } + + /// <summary> + /// Gets metrics from the source. + /// </summary> + /// <param name="collector">Collector that stores the resulting metrics snapshot as records.</param> + /// <param name="all">If true, gets metric values even if they are unchanged.</param> + public void GetMetrics(IMetricsCollector collector, bool all) + { + lock (_lock) + { + var rb = collector.CreateRecord(_recordName) + .SetContext(_sourceContext) + .AddTag("TaskOrContextName", _contextOrTaskName) + .AddTag("EvaluatorId", _evaluatorId) + .AddTag("SourceType", "DefaultSource"); + var request = new SnapshotRequest(rb, all); + foreach (var entry in _observers) + { + entry.OnNext(request); + } + + foreach (var entry in _tags) + { + rb.Add(entry.Value); + } + } + } + + /// <summary> + /// Diposes the <see cref="DefaultMetricsSourceImpl"/>. + /// Removes all the observers. + /// </summary> + public void Dispose() + { + foreach (var observer in _observers) + { + observer.OnCompleted(); + } + } + + private class Unsubscriber : IDisposable + { + private readonly IList<IObserver<SnapshotRequest>> _observers; + private readonly IObserver<SnapshotRequest> _observer; + private readonly object _lock; + public Unsubscriber(IList<IObserver<SnapshotRequest>> observers, IObserver<SnapshotRequest> observer, object lockObject) + { + _observers = observers; + _observer = observer; + _lock = lockObject; + } + + public void Dispose() + { + lock (_lock) + { + if (_observer != null && _observers.Contains(_observer)) + { + _observers.Remove(_observer); + } + } + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceParameters.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceParameters.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceParameters.cs new file mode 100644 index 0000000..3d39256 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/DefaultMetricsSourceParameters.cs @@ -0,0 +1,46 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + [Unstable("0.16", "Contract may change.")] + public sealed class DefaultMetricsSourceParameters + { + [NamedParameter("Name of the task or context IMetricsSource is part of.", "sourceid")] + public class ContextOrTaskName : Name<string> + { + } + + [NamedParameter("Id of evaluator IMetricsSource is part of.", "evalid")] + public class EvaluatorId : Name<string> + { + } + + [NamedParameter("Context of the metrics source. For example: ML, VM etc.", "context")] + public class SourceContext : Name<string> + { + } + + [NamedParameter("Name of the records generated by source during snapshot", "record")] + public class RecordName : Name<string> + { + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ICounter.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ICounter.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ICounter.cs new file mode 100644 index 0000000..348c608 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ICounter.cs @@ -0,0 +1,40 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Interface to implement the counter. A user will only work against this interface. + /// Implementations will be internal. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface ICounter : IMutableMetric + { + /// <summary> + /// Increments the counter by 1. + /// </summary> + void Increment(); + + /// <summary> + /// Increments the counter by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + void Increment(long delta); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IDoubleGauge.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IDoubleGauge.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IDoubleGauge.cs new file mode 100644 index 0000000..c3a2d78 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IDoubleGauge.cs @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Interface to implement the double gauge. A user will only work against this interface. + /// Implementations will be internal. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface IDoubleGauge : IMutableMetric + { + /// <summary> + /// Increments the gauge by 1. + /// </summary> + void Increment(); + + /// <summary> + /// Increments the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + void Increment(double delta); + + /// <summary> + /// Decrements the gauge by 1. + /// </summary> + void Decrement(); + + /// <summary> + /// Decrements the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + void Decrement(double delta); + + /// <summary> + /// Resets the gauge. + /// </summary> + /// <param name="value">Value to which the gauge should be reset.</param> + void Reset(double value); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IExtendedMutableMetric.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IExtendedMutableMetric.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IExtendedMutableMetric.cs new file mode 100644 index 0000000..aca61d5 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IExtendedMutableMetric.cs @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Extends the <see cref="IMutableMetric"/> with methods to be used internally. These + /// functions will not be visible to the users. + /// </summary> + internal interface IExtendedMutableMetric : IMutableMetric + { + /// <summary> + /// Sets the changed flag. Called in derived classes when metric values are changed. + /// </summary> + void SetChanged(); + + /// <summary> + /// Clears the changed flag. Called by snapshot operations after recording latest values. + /// </summary> + void ClearChanged(); + + /// <summary> + /// True if metric value changed after taking a snapshot, false otherwise. + /// </summary> + bool Changed { get; } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ILongGauge.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ILongGauge.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ILongGauge.cs new file mode 100644 index 0000000..ebe7d9c --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/ILongGauge.cs @@ -0,0 +1,57 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Interface to implement the long gauge. A user will only work against this interface. + /// Implementations will be internal. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface ILongGauge : IMutableMetric + { + /// <summary> + /// Increments the gauge by 1. + /// </summary> + void Increment(); + + /// <summary> + /// Increments the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + void Increment(long delta); + + /// <summary> + /// Decrements the gauge by 1. + /// </summary> + void Decrement(); + + /// <summary> + /// Decrements the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + void Decrement(long delta); + + /// <summary> + /// Resets the gauge. + /// </summary> + /// <param name="value">Value to which the gauge should be reset.</param> + void Reset(long value); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricContainer.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricContainer.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricContainer.cs new file mode 100644 index 0000000..73a6dcd --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricContainer.cs @@ -0,0 +1,54 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// A container interface visible to the user to create and access metric of particular type. + /// </summary> + /// <typeparam name="T">Type of mutable metric.</typeparam> + [Unstable("0.16", "Contract may change.")] + public interface IMetricContainer<out T> where T : IMutableMetric + { + /// <summary> + /// Creates metric of type T from name and description. + /// <exception cref="MetricsException">thrown if metric with the name already exists.</exception> + /// </summary> + /// <param name="name">Name of the metric.</param> + /// <param name="desc">Description of the metric.</param> + /// <returns>Newly created Metric.</returns> + T Create(string name, string desc); + + /// <summary> + /// Indexer to access metric by name. + /// <exception cref="MetricsException">thrown if metric with that name does not exist.</exception> + /// </summary> + /// <param name="name">Name of the metric.</param> + /// <returns>Metric with given name.</returns> + T this[string name] { get; } + + /// <summary> + /// Deletes a metric with given name and description. + /// </summary> + /// <param name="name">Name of the metric.</param> + /// <returns>True if the metric is deleted, false if it does not exist</returns> + bool Delete(string name); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricsFactory.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricsFactory.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricsFactory.cs new file mode 100644 index 0000000..b17c8bc --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMetricsFactory.cs @@ -0,0 +1,128 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Tang.Annotations; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Factory interface to help users and <see cref="IMetricsSource"/>create different types of inbuilt metrics. + /// </summary> + [DefaultImplementation(typeof(DefaultMetricsFactoryImpl))] + [Unstable("0.16", "Contract may change.")] + public interface IMetricsFactory + { + /// <summary> + /// Creates new tag + /// </summary> + /// <param name="info">Meta-data of the tag.</param> + /// <param name="value">Value of the tag.</param> + /// <returns>The new tag.</returns> + MetricsTag CreateTag(IMetricsInfo info, string value); + + /// <summary> + /// Creates a counter by name. Description is assumed to be + /// same as name. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the counter.</param> + /// <returns>Newly created counter.</returns> + ICounter CreateCounter(string name); + + /// <summary> + /// Creates a counter by name and description. + /// </summary> + /// <param name="name">Name of the counter.</param> + /// <param name="desc">Description of the counter</param> + /// <param name="initValue">Initial value of the counter</param> + /// <returns>Newly created counter.</returns> + ICounter CreateCounter(string name, string desc, long initValue = 0); + + /// <summary> + /// Creates a long gauge by name. Description is assumed to be + /// same as name. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <returns>Newly created gauge.</returns> + ILongGauge CreateLongGauge(string name); + + /// <summary> + /// Creates a long gauge by name and description. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <param name="desc">Description of the gauge</param> + /// <param name="initValue">Initial value of the gauge</param> + /// <returns>Newly created gauge.</returns> + ILongGauge CreateLongGauge(string name, string desc, long initValue = 0); + + /// <summary> + /// Creates a double gauge by name. Description is assumed to be + /// same as name. Initial value is assumed to be zero. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <returns>Newly created gauge.</returns> + IDoubleGauge CreateDoubleGauge(string name); + + /// <summary> + /// Creates a double gauge by name and description. + /// </summary> + /// <param name="name">Name of the gauge.</param> + /// <param name="desc">Description of the gauge</param> + /// <param name="initValue">Initial value of the gauge</param> + /// <returns>Newly created gauge.</returns> + IDoubleGauge CreateDoubleGauge(string name, string desc, double initValue = 0); + + /// <summary> + /// Creates the rate metric by name and description. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <param name="desc">Description of the rate.</param> + /// <param name="extendedMetrics">if true, stdev, min, max are also generated. Otherwise + /// only mean is computed.</param> + /// <returns>Newly created rate</returns> + IRate CreateRateMetric(string name, string desc, bool extendedMetrics = true); + + /// <summary> + /// Creates the rate metric by name. Description is assumed to be + /// same as name. All metrics - mean, stdev, min , max are generated. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <returns>Newly created rate</returns> + IRate CreateRateMetric(string name); + + /// <summary> + /// Creates stats metric by name. Description is assumed to be + /// same as name. All metrics - mean, stdev, min , max are generated. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <param name="valueName">Value that which this metric represents (for example, Time, Latency etc.</param> + /// <returns>Newly created stat.</returns> + IStat CreateStatMetric(string name, string valueName); + + /// <summary> + /// Creates the stat metric by name and description. + /// </summary> + /// <param name="name">Name of the rate</param> + /// <param name="desc">Description of the rate.</param> + /// <param name="valueName">Value that which this metric represents (for example, Time, Latency etc.</param> + /// <param name="extendedMetrics">if true, stdev, min, max are also generated. Otherwise + /// only mean is computed.</param> + /// <returns>Newly created stat.</returns> + IStat CreateStatMetric(string name, string desc, string valueName, bool extendedMetrics = true); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMutableMetric.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMutableMetric.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMutableMetric.cs new file mode 100644 index 0000000..2a3f20b --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IMutableMetric.cs @@ -0,0 +1,41 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Base mutable metric interface from which all other metrics derive. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface IMutableMetric : IObserver<SnapshotRequest> + { + /// <summary> + /// Meta-data of the metric. + /// </summary> + IMetricsInfo Info { get; } + + /// <summary> + /// This function should be used to subscribe metric tot he observable, + /// typically <see cref="IMetricsSource"/> + /// </summary> + void Subscribe(IObservable<SnapshotRequest> requestor); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IRate.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IRate.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IRate.cs new file mode 100644 index 0000000..4408ca8 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IRate.cs @@ -0,0 +1,30 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Interface to implement the rate. A user will only work against this interface. + /// Implementations will be internal. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface IRate : IStat + { + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IStat.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IStat.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IStat.cs new file mode 100644 index 0000000..9eefd14 --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/IStat.cs @@ -0,0 +1,35 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using Org.Apache.REEF.Utilities.Attributes; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Interface to implement the stat. A user will only work against this interface. + /// Implementations will be internal. + /// </summary> + [Unstable("0.16", "Contract may change.")] + public interface IStat : IMutableMetric + { + /// <summary> + /// Adds a sample to the stat. + /// </summary> + /// <param name="value">Value of the sample.</param> + void Sample(double value); + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableCounter.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableCounter.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableCounter.cs new file mode 100644 index 0000000..5889d7e --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableCounter.cs @@ -0,0 +1,103 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using System.Reactive; +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Mutable Counter metric class. + /// </summary> + internal sealed class MutableCounter : MutableMetricBase, ICounter + { + private long _value = 0; + private readonly object _lock = new object(); + private static readonly Logger Logger = Logger.GetLogger(typeof(MutableCounter)); + + /// <summary> + /// Counter Constructor + /// </summary> + /// <param name="info">Meta-data info. of the metric.</param> + /// <param name="initValue">Initial counter value.</param> + public MutableCounter(IMetricsInfo info, long initValue) + : base(info) + { + RegisterSnapshotRequestObserver(Observer.Create<SnapshotRequest>(this.GiveSnapshot, + this.SnapshotError, + UnSubscribe)); + _value = initValue; + } + + /// <summary> + /// Counter Constructor. Initializes counter to zero. + /// </summary> + /// <param name="info">Meta-data info. of the metric.</param> + public MutableCounter(IMetricsInfo info) : this(info, 0) + { + } + + /// <summary> + /// Counter Constructor. Initializes counter to zero. + /// </summary> + /// <param name="name">Name of the counter.</param> + public MutableCounter(string name) + : this(new MetricsInfoImpl(name, name), 0) + { + } + + /// <summary> + /// Increments the counter by 1. + /// </summary> + public void Increment() + { + Increment(1); + } + + /// <summary> + /// Increments the counter by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + public void Increment(long delta) + { + lock (_lock) + { + _value += delta; + SetChanged(); + } + } + + private void GiveSnapshot(SnapshotRequest request) + { + lock (_lock) + { + if (request.FullSnapshot || Changed) + { + request.Builder.AddCounter(Info, _value); + ClearChanged(); + } + } + } + + private void SnapshotError(Exception e) + { + Logger.Log(Level.Error, "Exception happened while trying to take the snapshot"); + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableDoubleGauge.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableDoubleGauge.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableDoubleGauge.cs new file mode 100644 index 0000000..1defa3b --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableDoubleGauge.cs @@ -0,0 +1,137 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using System.Reactive; +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Mutable double gauge metrics class. + /// </summary> + internal sealed class MutableDoubleGauge : MutableMetricBase, IDoubleGauge + { + private double _value = 0; + private readonly object _lock = new object(); + private static readonly Logger Logger = Logger.GetLogger(typeof(MutableDoubleGauge)); + + /// <summary> + /// Gauge Constructor + /// </summary> + /// <param name="info">Meta-data info. of the metric.</param> + /// <param name="initValue">Initial gauge value.</param> + public MutableDoubleGauge(IMetricsInfo info, double initValue) + : base(info) + { + RegisterSnapshotRequestObserver(Observer.Create<SnapshotRequest>(this.GiveSnapshot, + this.SnapshotError, + UnSubscribe)); + _value = initValue; + } + + /// <summary> + /// Gauge Constructor. Initializes gauge to zero. + /// </summary> + /// <param name="info">Meta-data info. of the metric.</param> + public MutableDoubleGauge(IMetricsInfo info) + : this(info, 0) + { + } + + /// <summary> + /// Gauge Constructor. Initializes gauge to zero. + /// </summary> + /// <param name="name">Name of the gauge.</param> + public MutableDoubleGauge(string name) + : this(new MetricsInfoImpl(name, name), 0) + { + } + + /// <summary> + /// Decrements the gauge by 1. + /// </summary> + public void Decrement() + { + Decrement(1); + } + + /// <summary> + /// Decrements the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to decrement.</param> + public void Decrement(double delta) + { + lock (_lock) + { + _value -= delta; + SetChanged(); + } + } + + /// <summary> + /// Resets the gauge. + /// </summary> + /// <param name="value">Value to which the gauge should be reset.</param> + public void Reset(double value) + { + lock (_lock) + { + _value = value; + } + } + + /// <summary> + /// Increments the gauge by 1. + /// </summary> + public void Increment() + { + Increment(1); + } + + /// <summary> + /// Increments the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + public void Increment(double delta) + { + lock (_lock) + { + _value += delta; + SetChanged(); + } + } + + private void GiveSnapshot(SnapshotRequest request) + { + lock (_lock) + { + if (request.FullSnapshot || Changed) + { + request.Builder.AddGauge(Info, _value); + ClearChanged(); + } + } + } + + private void SnapshotError(Exception e) + { + Logger.Log(Level.Error, "Exception happened while trying to take the snapshot"); + } + } +} http://git-wip-us.apache.org/repos/asf/reef/blob/0b8da4cf/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableLongGauge.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableLongGauge.cs b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableLongGauge.cs new file mode 100644 index 0000000..bb41cfa --- /dev/null +++ b/lang/cs/Org.Apache.REEF.Common/metrics/MutableMetricsLayer/MutableLongGauge.cs @@ -0,0 +1,136 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +using System; +using System.Reactive; +using Org.Apache.REEF.Common.Metrics.Api; +using Org.Apache.REEF.Utilities.Logging; + +namespace Org.Apache.REEF.Common.Metrics.MutableMetricsLayer +{ + /// <summary> + /// Mutable long gauge metrics class. + /// </summary> + internal sealed class MutableLongGauge : MutableMetricBase, ILongGauge + { + private long _value = 0; + private readonly object _lock = new object(); + private static readonly Logger Logger = Logger.GetLogger(typeof(MutableLongGauge)); + + /// <summary> + /// Gauge Constructor + /// </summary> + /// <param name="info">Meta-data info. of the metric.</param> + /// <param name="initValue">Initial gauge value.</param> + public MutableLongGauge(IMetricsInfo info, long initValue) + : base(info) + { + RegisterSnapshotRequestObserver(Observer.Create<SnapshotRequest>(this.GiveSnapshot, + this.SnapshotError, + UnSubscribe)); + _value = initValue; + } + + /// <summary> + /// Gauge Constructor. Initializes gauge to zero. + /// </summary> + /// <param name="info">Meta-data info. of the metric.</param> + public MutableLongGauge(IMetricsInfo info) : this(info, 0) + { + } + + /// <summary> + /// Gauge Constructor. Initializes gauge to zero. + /// </summary> + /// <param name="name">Name of the gauge.</param> + public MutableLongGauge(string name) + : this(new MetricsInfoImpl(name, name), 0) + { + } + + /// <summary> + /// Decrements the gauge by 1. + /// </summary> + public void Decrement() + { + Decrement(1); + } + + /// <summary> + /// Decrements the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to decrement.</param> + public void Decrement(long delta) + { + lock (_lock) + { + _value -= delta; + SetChanged(); + } + } + + /// <summary> + /// Resets the gauge. + /// </summary> + /// <param name="value">Value to which the gauge should be reset.</param> + public void Reset(long value) + { + lock (_lock) + { + _value = value; + } + } + + /// <summary> + /// Increments the gauge by 1. + /// </summary> + public void Increment() + { + Increment(1); + } + + /// <summary> + /// Increments the gauge by delta. + /// </summary> + /// <param name="delta">Value with which to increment.</param> + public void Increment(long delta) + { + lock (_lock) + { + _value += delta; + SetChanged(); + } + } + + private void GiveSnapshot(SnapshotRequest request) + { + lock (_lock) + { + if (request.FullSnapshot || Changed) + { + request.Builder.AddGauge(Info, _value); + ClearChanged(); + } + } + } + + private void SnapshotError(Exception e) + { + Logger.Log(Level.Error, "Exception happened while trying to take the snapshot"); + } + } +}
