|
| 1 | +// Copyright 2018, OpenCensus Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef OPENCENSUS_STATS_INTERNAL_DELTA_PRODUCER_H_ |
| 16 | +#define OPENCENSUS_STATS_INTERNAL_DELTA_PRODUCER_H_ |
| 17 | + |
| 18 | +#include <cstdint> |
| 19 | +#include <memory> |
| 20 | +#include <thread> |
| 21 | +#include <unordered_map> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "absl/synchronization/mutex.h" |
| 25 | +#include "absl/time/time.h" |
| 26 | +#include "opencensus/stats/bucket_boundaries.h" |
| 27 | +#include "opencensus/stats/distribution.h" |
| 28 | +#include "opencensus/stats/internal/measure_data.h" |
| 29 | +#include "opencensus/stats/measure.h" |
| 30 | +#include "opencensus/stats/tag_set.h" |
| 31 | + |
| 32 | +namespace opencensus { |
| 33 | +namespace stats { |
| 34 | + |
| 35 | +// Delta is thread-compatible. |
| 36 | +class Delta final { |
| 37 | + public: |
| 38 | + void Record(std::initializer_list<Measurement> measurements, TagSet tags); |
| 39 | + |
| 40 | + // Swaps registered_boundaries_ and delta_ with *other, clears delta_, and |
| 41 | + // updates registered_boundaries_. |
| 42 | + void SwapAndReset( |
| 43 | + std::vector<std::vector<BucketBoundaries>>& registered_boundaries, |
| 44 | + Delta* other); |
| 45 | + |
| 46 | + // Clears registered_boundaries_ and delta_. |
| 47 | + void clear(); |
| 48 | + |
| 49 | + const std::unordered_map<TagSet, std::vector<MeasureData>, TagSet::Hash>& |
| 50 | + delta() const { |
| 51 | + return delta_; |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + // A copy of registered_boundaries_ in the DeltaProducer as of when the |
| 56 | + // delta was started. |
| 57 | + std::vector<std::vector<BucketBoundaries>> registered_boundaries_; |
| 58 | + |
| 59 | + // The actual data. Each MeasureData[] contains one element for each |
| 60 | + // registered measure. |
| 61 | + std::unordered_map<TagSet, std::vector<MeasureData>, TagSet::Hash> delta_; |
| 62 | +}; |
| 63 | + |
| 64 | +// DeltaProducer is thread-safe. |
| 65 | +class DeltaProducer final { |
| 66 | + public: |
| 67 | + // Returns a pointer to the singleton DeltaProducer. |
| 68 | + static DeltaProducer* Get(); |
| 69 | + |
| 70 | + // Adds a new Measure. |
| 71 | + void AddMeasure(); |
| 72 | + |
| 73 | + // Adds a new BucketBoundaries for the measure 'index' if it does not already |
| 74 | + // exist. |
| 75 | + void AddBoundaries(uint64_t index, const BucketBoundaries& boundaries); |
| 76 | + |
| 77 | + void Record(std::initializer_list<Measurement> measurements, TagSet tags) |
| 78 | + LOCKS_EXCLUDED(delta_mu_); |
| 79 | + |
| 80 | + // Flushes the active delta and blocks until it is harvested. |
| 81 | + void Flush() LOCKS_EXCLUDED(delta_mu_, harvester_mu_); |
| 82 | + |
| 83 | + private: |
| 84 | + DeltaProducer(); |
| 85 | + |
| 86 | + // Flushing has two stages: swapping active_delta_ to last_delta_ and |
| 87 | + // consuming last_delta_. Callers should release delta_mu_ before calling |
| 88 | + // ConsumeLastDelta so that Record() is blocked for as little time as |
| 89 | + // possible. SwapDeltas should never be called without then calling |
| 90 | + // ConsumeLastDelta--otherwise the delta will be lost. |
| 91 | + void SwapDeltas() EXCLUSIVE_LOCKS_REQUIRED(delta_mu_, harvester_mu_); |
| 92 | + void ConsumeLastDelta() EXCLUSIVE_LOCKS_REQUIRED(harvester_mu_) |
| 93 | + LOCKS_EXCLUDED(delta_mu_); |
| 94 | + |
| 95 | + // Loops flushing the active delta (calling SwapDeltas and ConsumeLastDelta()) |
| 96 | + // every harvest_interval_. |
| 97 | + void RunHarvesterLoop(); |
| 98 | + |
| 99 | + const absl::Duration harvest_interval_ = absl::Seconds(5); |
| 100 | + |
| 101 | + // Guards the active delta and its configuration. Anything that changes the |
| 102 | + // delta configuration (e.g. adding a measure or BucketBoundaries) must |
| 103 | + // acquire delta_mu_, update configuration, and call SwapDeltas() before |
| 104 | + // releasing delta_mu_ to prevent Record() from accessing the delta with |
| 105 | + // mismatched configuration. |
| 106 | + mutable absl::Mutex delta_mu_; |
| 107 | + |
| 108 | + // The BucketBoundaries of each registered view with Distribution aggregation, |
| 109 | + // by measure. Array indices in the outer array correspond to measure indices. |
| 110 | + std::vector<std::vector<BucketBoundaries>> registered_boundaries_ |
| 111 | + GUARDED_BY(delta_mu_); |
| 112 | + Delta active_delta_ GUARDED_BY(delta_mu_); |
| 113 | + |
| 114 | + // Guards the last_delta_; acquired by the main thread when triggering a |
| 115 | + // flush. |
| 116 | + mutable absl::Mutex harvester_mu_ ACQUIRED_AFTER(delta_mu_); |
| 117 | + // TODO: consider making this a lockless queue to avoid blocking the main |
| 118 | + // thread when calling a flush during harvesting. |
| 119 | + Delta last_delta_ GUARDED_BY(harvester_mu_); |
| 120 | + std::thread harvester_thread_ GUARDED_BY(harvester_mu_); |
| 121 | +}; |
| 122 | + |
| 123 | +// TODO: Replace Record with this when ready. |
| 124 | +void ExperimentalDeltaProducerRecord( |
| 125 | + std::initializer_list<Measurement> measurements, TagSet tags = TagSet({})); |
| 126 | + |
| 127 | +} // namespace stats |
| 128 | +} // namespace opencensus |
| 129 | + |
| 130 | +#endif // OPENCENSUS_STATS_INTERNAL_DELTA_PRODUCER_H_ |
0 commit comments