Skip to content
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

Fix thread race condition in SynchronousMetricStorage #540

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class SynchronousMetricStorage: SynchronousMetricStorageProtocol {
var aggregatorHandles = [[String: AttributeValue]: AggregatorHandle]()
let attributeProcessor: AttributeProcessor
var aggregatorHandlePool = [AggregatorHandle]()
private let aggregatorHandlesQueue = DispatchQueue(label: "org.opentelemetry.SynchronousMetricStorage.aggregatorHandlesQueue", attributes: .concurrent)


static func empty() -> SynchronousMetricStorageProtocol {
return EmptyMetricStorage.instance
Expand Down Expand Up @@ -48,22 +50,24 @@ public class SynchronousMetricStorage: SynchronousMetricStorageProtocol {
}

private func getAggregatorHandle(attributes: [String: AttributeValue]) throws -> AggregatorHandle {
let processedAttributes = attributeProcessor.process(incoming: attributes)
if let handle = aggregatorHandles[processedAttributes] {
return handle
}
if aggregatorHandles.count >= MetricStorageConstants.MAX_CARDINALITY {
// error
throw MetricStoreError.maxCardinality
}

let newHandle = aggregatorHandlePool.isEmpty ? aggregator.createHandle() : aggregatorHandlePool.remove(at: 0)
if let existingHandle = aggregatorHandles[processedAttributes] {
return existingHandle
} else {
var aggregatorHandle: AggregatorHandle!
try aggregatorHandlesQueue.sync {
let processedAttributes = attributeProcessor.process(incoming: attributes)
if let handle = aggregatorHandles[processedAttributes] {
aggregatorHandle = handle
return
}
guard aggregatorHandles.count < MetricStorageConstants.MAX_CARDINALITY else {
throw MetricStoreError.maxCardinality
}

let newHandle = aggregatorHandlePool.isEmpty ? aggregator.createHandle() : aggregatorHandlePool.remove(at: 0)
aggregatorHandles[processedAttributes] = newHandle
return newHandle
aggregatorHandle = newHandle
}
return aggregatorHandle

}

public func collect(resource: Resource, scope: InstrumentationScopeInfo, startEpochNanos: UInt64, epochNanos: UInt64) -> StableMetricData {
Expand All @@ -72,13 +76,15 @@ public class SynchronousMetricStorage: SynchronousMetricStorageProtocol {

var points = [PointData]()

aggregatorHandles.forEach { key, value in
let point = value.aggregateThenMaybeReset(startEpochNano: start, endEpochNano: epochNanos, attributes: key, reset: reset)
if reset {
aggregatorHandles.removeValue(forKey: key)
aggregatorHandlePool.append(value)
aggregatorHandlesQueue.sync(flags: .barrier) {
aggregatorHandles.forEach { key, value in
let point = value.aggregateThenMaybeReset(startEpochNano: start, endEpochNano: epochNanos, attributes: key, reset: reset)
if reset {
aggregatorHandles.removeValue(forKey: key)
aggregatorHandlePool.append(value)
}
points.append(point)
}
points.append(point)
}

if points.isEmpty {
Expand Down
Loading