-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add distribution metric to semantic-core and ffwd-reporter #79
Changes from 5 commits
58883b5
6785e73
dab6897
912e50a
4ae53fe
b3cb919
89b365d
c2e676a
b07eca4
055de7e
339a19f
c000a1f
b0ece44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2016 Spotify AB. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.spotify.metrics.core; | ||
|
||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.spotify.metrics.core.codahale.metrics.ext.Distribution; | ||
import com.tdunning.math.stats.TDigest; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
|
||
public class DistributionImpl implements Distribution { | ||
|
||
private static final int COMPRESSION_DEFAULT_LEVEL = 100; | ||
private final AtomicReference<TDigest> distRef; | ||
|
||
protected DistributionImpl() { | ||
this.distRef = new AtomicReference<>(TDigest.createDigest(COMPRESSION_DEFAULT_LEVEL)); | ||
} | ||
|
||
@Override | ||
public void record(double val) { | ||
distRef.get().add(val); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TDigest.add() does not appear to be threadsafe There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I am looking into this. I checked couple of Dropwizard metrics there are threadsafe so I will make this thread safe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could either
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check the implementation I added yesterday. I thought about adding threadLocal then change mind. ThreadLocal may introduce memory leak if getValueAndFlush isn't called often. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that should work now I think (but it's hard to know if there will be much contention, some multithreaded JMH benchmarks might be useful). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, sure will do. Please take a look I need a +1 |
||
} | ||
|
||
@Override | ||
public java.nio.ByteBuffer getValueAndFlush() { | ||
ao2017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TDigest curVal = distRef.getAndSet(create()); // reset tdigest | ||
ByteBuffer byteBuffer = ByteBuffer.allocate(curVal.smallByteSize()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. re:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are ok with the performance. Data storage cost is more of a concern. |
||
curVal.asSmallBytes(byteBuffer); | ||
return byteBuffer; | ||
} | ||
|
||
/** | ||
* Returns the current count. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not terribly useful :) Either remove or explain what's being counted, cheers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, something is better than guessing :) . I will update. |
||
* | ||
* @return the current count | ||
*/ | ||
@Override | ||
public long getCount() { | ||
return this.tDigest().size(); | ||
} | ||
|
||
private TDigest create() { | ||
return TDigest.createDigest(COMPRESSION_DEFAULT_LEVEL); | ||
} | ||
|
||
|
||
@VisibleForTesting | ||
TDigest tDigest() { | ||
return distRef.get(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the rationale behind using an
AtomicReference
? With AtomicRefernce, the only guarantee is that this reference will be updated/set in a thread-safe manner and AFAICT we don't update it...?Also, AFAICT, multiple threads could still overwrite
this.distRef
here, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's ok, As long as each thread has its own copy we are good.