Permalink
Browse files

Replace CopyOnWriteMap with ConcurrentHashMap in MetricsRepository

CopyOnWriteMap which makes a copy of the whole map every time "put" operation is called is extremely expensive when loading lots of metrics at one time. Change it to ConcurrentHashMap instead.
  • Loading branch information...
SidW committed Nov 22, 2017
1 parent ebb44b3 commit 106aa362940acd9e146e59c855692857e83e9c94
@@ -3,9 +3,9 @@
* 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.
@@ -15,10 +15,10 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import io.tehuti.Metric;
import io.tehuti.utils.CopyOnWriteMap;
import io.tehuti.utils.SystemTime;
import io.tehuti.utils.Time;
import io.tehuti.utils.Utils;
@@ -32,14 +32,14 @@
* that are recorded by the sensor.
* <p>
* Usage looks something like this:
*
*
* <pre>
* // set up metrics:
* MetricsRepository metrics = new MetricsRepository(); // this is the global repository of metrics and sensors
* Sensor sensor = metrics.sensor(&quot;message-sizes&quot;);
* sensor.add(&quot;message-sizes.avg&quot;, new Avg());
* sensor.add(&quot;message-sizes.max&quot;, new Max());
*
*
* // as messages are sent we record the sizes
* sensor.record(messageSize);
* </pre>
@@ -83,8 +83,8 @@ public MetricsRepository(MetricConfig defaultConfig) {
*/
public MetricsRepository(MetricConfig defaultConfig, List<MetricsReporter> reporters, Time time) {
this.config = defaultConfig;
this.sensors = new CopyOnWriteMap<String, Sensor>();
this.metrics = new CopyOnWriteMap<String, TehutiMetric>();
this.sensors = new ConcurrentHashMap<>();
this.metrics = new ConcurrentHashMap<>();
this.reporters = Utils.notNull(reporters);
this.time = time;
for (MetricsReporter reporter : reporters)

This file was deleted.

Oops, something went wrong.

0 comments on commit 106aa36

Please sign in to comment.