2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* MetricLogger.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed 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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-08-11 06:18:24 +08:00
|
|
|
#include <cmath>
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
#include "flow/TDMetric.actor.h"
|
|
|
|
#include "fdbclient/DatabaseContext.h"
|
|
|
|
#include "fdbclient/ReadYourWrites.h"
|
|
|
|
#include "fdbclient/KeyBackedTypes.h"
|
2021-01-26 08:09:32 +08:00
|
|
|
#include "fdbserver/MetricLogger.actor.h"
|
2021-03-11 02:06:03 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
struct MetricsRule {
|
2021-03-11 02:06:03 +08:00
|
|
|
MetricsRule(bool enabled = false, int minLevel = 0, StringRef const& name = StringRef())
|
2021-07-25 02:20:51 +08:00
|
|
|
: namePattern(name), enabled(enabled), minLevel(minLevel) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
Standalone<StringRef> typePattern;
|
|
|
|
Standalone<StringRef> namePattern;
|
|
|
|
Standalone<StringRef> addressPattern;
|
|
|
|
Standalone<StringRef> idPattern;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
bool enabled;
|
|
|
|
int minLevel;
|
|
|
|
|
|
|
|
Tuple pack() const {
|
2021-03-11 02:06:03 +08:00
|
|
|
return Tuple()
|
|
|
|
.append(namePattern)
|
|
|
|
.append(typePattern)
|
|
|
|
.append(addressPattern)
|
|
|
|
.append(idPattern)
|
|
|
|
.append(enabled ? 1 : 0)
|
|
|
|
.append(minLevel);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
static inline MetricsRule unpack(Tuple const& t) {
|
2017-05-26 04:48:44 +08:00
|
|
|
MetricsRule r;
|
|
|
|
int i = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (i < t.size())
|
2017-05-26 04:48:44 +08:00
|
|
|
r.namePattern = t.getString(i++);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (i < t.size())
|
2017-05-26 04:48:44 +08:00
|
|
|
r.typePattern = t.getString(i++);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (i < t.size())
|
2017-05-26 04:48:44 +08:00
|
|
|
r.addressPattern = t.getString(i++);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (i < t.size())
|
2017-05-26 04:48:44 +08:00
|
|
|
r.idPattern = t.getString(i++);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (i < t.size())
|
2017-05-26 04:48:44 +08:00
|
|
|
r.enabled = t.getInt(i++) != 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (i < t.size())
|
2017-05-26 04:48:44 +08:00
|
|
|
r.minLevel = (int)t.getInt(i++);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For now this just returns true if pat is in subject. Returns true if pat is empty.
|
|
|
|
// TODO: Support more complex patterns?
|
2021-03-11 02:06:03 +08:00
|
|
|
static inline bool patternMatch(StringRef const& pat, StringRef const& subject) {
|
|
|
|
if (pat.size() == 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
return true;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0, iend = subject.size() - pat.size() + 1; i < iend; ++i)
|
|
|
|
if (subject.substr(i, pat.size()) == pat)
|
2017-05-26 04:48:44 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
|
|
|
|
bool applyTo(BaseMetric* m, StringRef const& address) const {
|
|
|
|
if (!patternMatch(addressPattern, address))
|
2017-05-26 04:48:44 +08:00
|
|
|
return false;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!patternMatch(namePattern, m->metricName.name))
|
2017-05-26 04:48:44 +08:00
|
|
|
return false;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!patternMatch(typePattern, m->metricName.type))
|
2017-05-26 04:48:44 +08:00
|
|
|
return false;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!patternMatch(idPattern, m->metricName.id))
|
2017-05-26 04:48:44 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
m->setConfig(enabled, minLevel);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MetricsConfig {
|
|
|
|
MetricsConfig(Key prefix = KeyRef())
|
2021-03-11 02:06:03 +08:00
|
|
|
: space(prefix), ruleMap(space.get(LiteralStringRef("Rules")).key()),
|
|
|
|
addressMap(space.get(LiteralStringRef("Enum")).get(LiteralStringRef("Address")).key()),
|
|
|
|
nameAndTypeMap(space.get(LiteralStringRef("Enum")).get(LiteralStringRef("NameType")).key()),
|
|
|
|
ruleChangeKey(space.get(LiteralStringRef("RulesChanged")).key()),
|
|
|
|
enumsChangeKey(space.get(LiteralStringRef("EnumsChanged")).key()),
|
|
|
|
fieldChangeKey(space.get(LiteralStringRef("FieldsChanged")).key()) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
Subspace space;
|
|
|
|
|
|
|
|
typedef KeyBackedMap<int64_t, MetricsRule> RuleMapT;
|
|
|
|
RuleMapT ruleMap;
|
|
|
|
RuleMapT::PairsType rules;
|
|
|
|
|
|
|
|
KeyBackedMap<Key, int64_t> addressMap;
|
|
|
|
KeyBackedMap<std::pair<Key, Key>, int64_t> nameAndTypeMap;
|
|
|
|
|
|
|
|
Key ruleChangeKey;
|
|
|
|
Key enumsChangeKey;
|
|
|
|
Key fieldChangeKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Rule Updater:
|
|
|
|
|
|
|
|
For now:
|
|
|
|
Read and store all rules locally.
|
|
|
|
For each metric
|
|
|
|
Disable the metric
|
|
|
|
For each rule in reverse order, apply the rule to the metric and stop if it returns true
|
|
|
|
Wait for rule change, repeat.
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
If this gets too slow, yields can be added but at the cost of potentially missing a few data points
|
|
|
|
because a metric was disabled and not yet re-enabled before it was logged.
|
2021-03-11 02:06:03 +08:00
|
|
|
|
|
|
|
Or, rules and metrics can be stored for more efficient matching and rule updates can be applied
|
2017-05-26 04:48:44 +08:00
|
|
|
differentially.
|
|
|
|
Read all rules, store latest version
|
|
|
|
Clear all configs for each registered metric
|
|
|
|
For each rule in order,
|
|
|
|
Efficiently select matching metrics and set config
|
|
|
|
Loop
|
|
|
|
Wait for rule change
|
|
|
|
If rule version skipped, go back to start of rule updater
|
|
|
|
Read only new rules.
|
|
|
|
For each new rule, in order
|
|
|
|
Remove any old rules that new rule completely covers
|
|
|
|
Efficiently select matching metrics and set config
|
|
|
|
Go back to wait for rule change
|
|
|
|
*/
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> metricRuleUpdater(Database cx, MetricsConfig* config, TDMetricCollection* collection) {
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
state Reference<ReadYourWritesTransaction> tr(new ReadYourWritesTransaction(cx));
|
|
|
|
|
|
|
|
loop {
|
|
|
|
state Future<Void> newMetric = collection->metricAdded.onTrigger();
|
|
|
|
try {
|
|
|
|
tr->setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS);
|
|
|
|
MetricsConfig::RuleMapT::PairsType rules = wait(config->ruleMap.getRange(tr, 0, {}, 1e6));
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto& it : collection->metricMap) {
|
2017-05-26 04:48:44 +08:00
|
|
|
it.value->setConfig(false);
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto i = rules.rbegin(); !(i == rules.rend()); ++i)
|
|
|
|
if (i->second.applyTo(it.value.getPtr(), collection->address))
|
2017-05-26 04:48:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
config->rules = std::move(rules);
|
|
|
|
|
|
|
|
state Future<Void> rulesChanged = tr->watch(config->ruleChangeKey);
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(tr->commit());
|
|
|
|
wait(rulesChanged || newMetric);
|
2017-05-26 04:48:44 +08:00
|
|
|
tr->reset();
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(tr->onError(e));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implementation of IMetricDB
|
|
|
|
class MetricDB : public IMetricDB {
|
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
MetricDB(ReadYourWritesTransaction* tr = nullptr) : tr(tr) {}
|
2021-01-26 09:55:43 +08:00
|
|
|
~MetricDB() override {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
// levelKey is the prefix for the entire level, no timestamp at the end
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Optional<Standalone<StringRef>>> getLastBlock_impl(ReadYourWritesTransaction* tr,
|
|
|
|
Standalone<StringRef> levelKey) {
|
2021-07-17 15:11:40 +08:00
|
|
|
RangeResult results = wait(tr->getRange(normalKeys.withPrefix(levelKey), 1, Snapshot::True, Reverse::True));
|
2021-03-11 02:06:03 +08:00
|
|
|
if (results.size() == 1)
|
2017-05-26 04:48:44 +08:00
|
|
|
return results[0].value;
|
|
|
|
return Optional<Standalone<StringRef>>();
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:55:43 +08:00
|
|
|
Future<Optional<Standalone<StringRef>>> getLastBlock(Standalone<StringRef> key) override {
|
2017-05-26 04:48:44 +08:00
|
|
|
return getLastBlock_impl(tr, key);
|
|
|
|
}
|
2021-01-26 09:55:43 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ReadYourWritesTransaction* tr;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> dumpMetrics(Database cx, MetricsConfig* config, TDMetricCollection* collection) {
|
2017-05-26 04:48:44 +08:00
|
|
|
state MetricUpdateBatch batch;
|
|
|
|
state Standalone<MetricKeyRef> mk;
|
|
|
|
ASSERT(collection != nullptr);
|
|
|
|
mk.prefix = StringRef(mk.arena(), config->space.key());
|
|
|
|
mk.address = StringRef(mk.arena(), collection->address);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
batch.clear();
|
|
|
|
uint64_t rollTime = std::numeric_limits<uint64_t>::max();
|
2021-03-11 02:06:03 +08:00
|
|
|
if (collection->rollTimes.size()) {
|
2017-05-26 04:48:44 +08:00
|
|
|
rollTime = collection->rollTimes.front();
|
|
|
|
collection->rollTimes.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Are any metrics enabled?
|
|
|
|
state bool enabled = false;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// Flush data for each metric, track if any are enabled.
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto& it : collection->metricMap) {
|
2017-05-26 04:48:44 +08:00
|
|
|
// If this metric was ever enabled at all then flush it
|
2021-03-11 02:06:03 +08:00
|
|
|
if (it.value->pCollection != nullptr) {
|
2017-05-26 04:48:44 +08:00
|
|
|
mk.name = it.value->metricName;
|
|
|
|
it.value->flushData(mk, rollTime, batch);
|
|
|
|
}
|
|
|
|
enabled = enabled || it.value->enabled;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (rollTime == std::numeric_limits<uint64_t>::max()) {
|
2017-05-26 04:48:44 +08:00
|
|
|
collection->currentTimeBytes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
state ReadYourWritesTransaction cbtr(cx);
|
|
|
|
state MetricDB mdb(&cbtr);
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
state std::map<int, Future<Void>> results;
|
|
|
|
// Call all of the callbacks, map each index to its resulting future
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0, iend = batch.callbacks.size(); i < iend; ++i)
|
2017-05-26 04:48:44 +08:00
|
|
|
results[i] = batch.callbacks[i](&mdb, &batch);
|
|
|
|
|
|
|
|
loop {
|
|
|
|
state std::map<int, Future<Void>>::iterator cb = results.begin();
|
|
|
|
// Wait for each future, return the ones that succeeded
|
|
|
|
state Error lastError;
|
2021-03-11 02:06:03 +08:00
|
|
|
while (cb != results.end()) {
|
2017-05-26 04:48:44 +08:00
|
|
|
try {
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(cb->second);
|
2017-05-26 04:48:44 +08:00
|
|
|
cb = results.erase(cb);
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
2017-05-26 04:48:44 +08:00
|
|
|
++cb;
|
|
|
|
lastError = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the callbacks completed then we're done.
|
2021-03-11 02:06:03 +08:00
|
|
|
if (results.empty())
|
2017-05-26 04:48:44 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Otherwise, wait to retry
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(cbtr.onError(lastError));
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto& cb : results)
|
2017-05-26 04:48:44 +08:00
|
|
|
cb.second = batch.callbacks[cb.first](&mdb, &batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are more rolltimes then next dump is now, otherwise if no metrics are enabled then it is
|
|
|
|
// whenever the next metric is enabled but if there are metrics enabled then it is in 1 second.
|
|
|
|
state Future<Void> nextDump;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (collection->rollTimes.size() > 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
nextDump = Void();
|
|
|
|
else {
|
|
|
|
nextDump = collection->metricEnabled.onTrigger();
|
2021-03-11 02:06:03 +08:00
|
|
|
if (enabled)
|
2017-05-26 04:48:44 +08:00
|
|
|
nextDump = nextDump || delay(1.0);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
state Transaction tr(cx);
|
2017-05-26 04:48:44 +08:00
|
|
|
loop {
|
|
|
|
tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS);
|
|
|
|
try {
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto& i : batch.inserts) {
|
|
|
|
// fprintf(stderr, "%s: dump insert: %s\n", collection->address.toString().c_str(),
|
|
|
|
// printable(allInsertions[i].key).c_str());
|
|
|
|
tr.set(i.key, i.value());
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
|
|
|
|
for (auto& a : batch.appends) {
|
|
|
|
// fprintf(stderr, "%s: dump append: %s\n", collection->address.toString().c_str(),
|
|
|
|
// printable(allAppends[i].key).c_str());
|
2017-05-26 04:48:44 +08:00
|
|
|
tr.atomicOp(a.key, a.value(), MutationRef::AppendIfFits);
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
|
|
|
|
for (auto& u : batch.updates) {
|
|
|
|
// fprintf(stderr, "%s: dump update: %s\n", collection->address.toString().c_str(),
|
|
|
|
// printable(allUpdates[i].first).c_str());
|
2017-05-26 04:48:44 +08:00
|
|
|
tr.set(u.first, u.second);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(tr.commit());
|
2017-05-26 04:48:44 +08:00
|
|
|
break;
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(nextDump);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push metric field registrations to database.
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> updateMetricRegistration(Database cx, MetricsConfig* config, TDMetricCollection* collection) {
|
2017-05-26 04:48:44 +08:00
|
|
|
state Standalone<MetricKeyRef> mk;
|
|
|
|
mk.prefix = StringRef(mk.arena(), config->space.key());
|
|
|
|
mk.address = StringRef(mk.arena(), collection->address);
|
|
|
|
|
|
|
|
state bool addressRegistered = false;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
state Future<Void> registrationChange = collection->metricRegistrationChanged.onTrigger();
|
|
|
|
state Future<Void> newMetric = collection->metricAdded.onTrigger();
|
2021-09-17 08:42:34 +08:00
|
|
|
state std::vector<Standalone<StringRef>> keys;
|
2017-05-26 04:48:44 +08:00
|
|
|
state bool fieldsChanged = false;
|
|
|
|
state bool enumsChanged = false;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// Register each metric that isn't already registered
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto& it : collection->metricMap) {
|
|
|
|
if (!it.value->registered) {
|
2017-05-26 04:48:44 +08:00
|
|
|
// Register metric so it can create its field keys
|
|
|
|
mk.name = it.value->metricName;
|
|
|
|
it.value->registerFields(mk, keys);
|
|
|
|
|
|
|
|
// Also set keys for the metric's (name,type) pair in the type-and-name map
|
2021-03-11 02:06:03 +08:00
|
|
|
keys.push_back(
|
|
|
|
config->nameAndTypeMap.getProperty({ it.value->metricName.name, it.value->metricName.type }).key);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
it.value->registered = true;
|
|
|
|
fieldsChanged = true;
|
|
|
|
enumsChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a key for this collection's address in the address map if it hasn't been done.
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!addressRegistered) {
|
2017-05-26 04:48:44 +08:00
|
|
|
keys.push_back(config->addressMap.getProperty(collection->address).key);
|
|
|
|
addressRegistered = true;
|
|
|
|
enumsChanged = true;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (enumsChanged)
|
2017-05-26 04:48:44 +08:00
|
|
|
keys.push_back(config->enumsChangeKey);
|
2021-03-11 02:06:03 +08:00
|
|
|
if (fieldsChanged)
|
2017-05-26 04:48:44 +08:00
|
|
|
keys.push_back(config->fieldChangeKey);
|
|
|
|
|
|
|
|
// Write keys collected to database
|
|
|
|
state Transaction tr(cx);
|
|
|
|
loop {
|
|
|
|
tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS);
|
|
|
|
try {
|
2021-03-11 02:06:03 +08:00
|
|
|
Value timestamp =
|
|
|
|
BinaryWriter::toValue(CompressedInt<int64_t>(now()), AssumeVersion(g_network->protocolVersion()));
|
|
|
|
for (auto& key : keys) {
|
|
|
|
// fprintf(stderr, "%s: register: %s\n", collection->address.toString().c_str(),
|
|
|
|
// printable(key).c_str());
|
2017-05-26 04:48:44 +08:00
|
|
|
tr.set(key, timestamp);
|
|
|
|
}
|
|
|
|
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(tr.commit());
|
2017-05-26 04:48:44 +08:00
|
|
|
break;
|
2021-03-11 02:06:03 +08:00
|
|
|
} catch (Error& e) {
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(tr.onError(e));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for a metric to require registration or a new metric to be added
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(registrationChange || newMetric);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> runMetrics(Future<Database> fcx, Key prefix) {
|
2017-05-26 04:48:44 +08:00
|
|
|
// Never log to an empty prefix, it's pretty much always a bad idea.
|
2021-03-11 02:06:03 +08:00
|
|
|
if (prefix.size() == 0) {
|
2021-07-27 10:55:10 +08:00
|
|
|
TraceEvent(SevWarnAlways, "TDMetricsRefusingEmptyPrefix").log();
|
2017-05-26 04:48:44 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until the collection has been created and initialized.
|
2021-03-11 02:06:03 +08:00
|
|
|
state TDMetricCollection* metrics = nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
loop {
|
|
|
|
metrics = TDMetricCollection::getTDMetrics();
|
2021-03-11 02:06:03 +08:00
|
|
|
if (metrics != nullptr)
|
|
|
|
if (metrics->init())
|
2017-05-26 04:48:44 +08:00
|
|
|
break;
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(delay(1.0));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
state MetricsConfig config(prefix);
|
|
|
|
|
|
|
|
try {
|
2021-03-11 02:06:03 +08:00
|
|
|
Database cx = wait(fcx);
|
2017-05-26 04:48:44 +08:00
|
|
|
Future<Void> conf = metricRuleUpdater(cx, &config, metrics);
|
|
|
|
Future<Void> dump = dumpMetrics(cx, &config, metrics);
|
|
|
|
Future<Void> reg = updateMetricRegistration(cx, &config, metrics);
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(conf || dump || reg);
|
|
|
|
} catch (Error& e) {
|
|
|
|
if (e.code() != error_code_actor_cancelled) {
|
2017-05-26 04:48:44 +08:00
|
|
|
// Disable all metrics
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto& it : metrics->metricMap)
|
2017-05-26 04:48:44 +08:00
|
|
|
it.value->setConfig(false);
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
TraceEvent(SevWarnAlways, "TDMetricsStopped").error(e);
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2018-10-06 13:09:58 +08:00
|
|
|
TEST_CASE("/fdbserver/metrics/TraceEvents") {
|
2021-03-11 02:06:03 +08:00
|
|
|
auto getenv2 = [](const char* s) -> const char* {
|
|
|
|
s = getenv(s);
|
|
|
|
return s ? s : "";
|
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
std::string metricsConnFile = getenv2("METRICS_CONNFILE");
|
|
|
|
std::string metricsPrefix = getenv2("METRICS_PREFIX");
|
2021-03-11 02:06:03 +08:00
|
|
|
if (metricsConnFile == "") {
|
2017-05-26 04:48:44 +08:00
|
|
|
fprintf(stdout, "Metrics cluster file must be specified in environment variable METRICS_CONNFILE\n");
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
fprintf(stdout, "Using environment variables METRICS_CONNFILE and METRICS_PREFIX.\n");
|
|
|
|
|
2018-09-22 06:58:14 +08:00
|
|
|
state Database metricsDb = Database::createDatabase(metricsConnFile, Database::API_VERSION_LATEST);
|
2017-05-26 04:48:44 +08:00
|
|
|
TDMetricCollection::getTDMetrics()->address = LiteralStringRef("0.0.0.0:0");
|
2018-09-22 06:58:14 +08:00
|
|
|
state Future<Void> metrics = runMetrics(metricsDb, KeyRef(metricsPrefix));
|
2017-05-26 04:48:44 +08:00
|
|
|
state int64_t x = 0;
|
|
|
|
|
|
|
|
state double w = 0.5;
|
|
|
|
state int chunk = 4000;
|
|
|
|
state int total = 200000;
|
|
|
|
|
|
|
|
fprintf(stdout, "Writing trace event named Dummy with fields a, b, c, d, j, k, s, x, y, z.\n");
|
|
|
|
fprintf(stdout, " There is a %f second pause every %d events\n", w, chunk);
|
|
|
|
fprintf(stdout, " %d events will be logged.\n", total);
|
|
|
|
fprintf(stdout, " a is always present. It starts with = 0 and increments by 1 with each event.\n");
|
|
|
|
fprintf(stdout, " b, if present, is always a*2.\n");
|
|
|
|
fprintf(stdout, " c, if present, is always a*3.\n");
|
|
|
|
fprintf(stdout, " b and c are never present in the same event.\n");
|
|
|
|
fprintf(stdout, " x, y, and z, if present, are doubles and equal to 1.5 * a, b, and c, respectively\n");
|
|
|
|
fprintf(stdout, " d is always present, is a string, and rotates through the values 'one', 'two', and ''.\n");
|
|
|
|
fprintf(stdout, " Plotting j on the x axis and k on the y axis should look like x=sin(2t), y=sin(3t)\n");
|
|
|
|
|
|
|
|
state Int64MetricHandle intMetric = Int64MetricHandle(LiteralStringRef("DummyInt"));
|
|
|
|
state BoolMetricHandle boolMetric = BoolMetricHandle(LiteralStringRef("DummyBool"));
|
|
|
|
state StringMetricHandle stringMetric = StringMetricHandle(LiteralStringRef("DummyString"));
|
2021-03-11 02:06:03 +08:00
|
|
|
|
|
|
|
static const char* dStrings[] = { "one", "two", "" };
|
|
|
|
state const char** d = dStrings;
|
2017-05-26 04:48:44 +08:00
|
|
|
state Arena arena;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
loop {
|
2019-02-18 06:55:47 +08:00
|
|
|
{
|
|
|
|
double sstart = x;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < chunk; ++i, ++x) {
|
2019-02-18 06:55:47 +08:00
|
|
|
intMetric = x;
|
|
|
|
boolMetric = (x % 2) > 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
const char* s = d[x % 3];
|
2019-02-18 06:55:47 +08:00
|
|
|
// s doesn't actually require an arena
|
2021-03-11 02:06:03 +08:00
|
|
|
stringMetric = Standalone<StringRef>(StringRef((uint8_t*)s, strlen(s)), arena);
|
2019-02-18 06:55:47 +08:00
|
|
|
|
|
|
|
TraceEvent("Dummy")
|
2021-03-11 02:06:03 +08:00
|
|
|
.detail("A", x)
|
|
|
|
.detail("X", 1.5 * x)
|
|
|
|
.detail("D", s)
|
|
|
|
.detail("J", sin(2.0 * x))
|
|
|
|
.detail("K", sin(3.0 * x))
|
|
|
|
.detail("S", sstart + (double)chunk * sin(10.0 * i / chunk));
|
2019-02-18 06:55:47 +08:00
|
|
|
}
|
|
|
|
wait(delay(w));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2019-02-18 06:55:47 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
double sstart = x;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < chunk; ++i, ++x) {
|
2019-02-18 06:55:47 +08:00
|
|
|
intMetric = x;
|
|
|
|
boolMetric = x % 2 > 0;
|
|
|
|
TraceEvent("Dummy")
|
2021-03-11 02:06:03 +08:00
|
|
|
.detail("A", x)
|
|
|
|
.detail("X", 1.5 * x)
|
|
|
|
.detail("B", x * 2)
|
|
|
|
.detail("Y", 3.0 * x)
|
|
|
|
.detail("D", d[x % 3])
|
|
|
|
.detail("J", sin(2.0 * x))
|
|
|
|
.detail("K", sin(3.0 * x))
|
|
|
|
.detail("S", sstart + (double)chunk * sin(40.0 * i / chunk));
|
2019-02-18 06:55:47 +08:00
|
|
|
}
|
|
|
|
wait(delay(w));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 06:55:47 +08:00
|
|
|
{
|
|
|
|
double sstart = x;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < chunk; ++i, ++x) {
|
2019-02-18 06:55:47 +08:00
|
|
|
intMetric = x;
|
|
|
|
boolMetric = x % 2 > 0;
|
|
|
|
TraceEvent("Dummy")
|
2021-03-11 02:06:03 +08:00
|
|
|
.detail("A", x)
|
|
|
|
.detail("X", 1.5 * x)
|
|
|
|
.detail("C", x * 3)
|
|
|
|
.detail("Z", 4.5 * x)
|
|
|
|
.detail("D", d[x % 3])
|
|
|
|
.detail("J", sin(2.0 * x))
|
|
|
|
.detail("K", sin(3.0 * x))
|
|
|
|
.detail("S", sstart + (double)chunk * sin(160.0 * i / chunk));
|
2019-02-18 06:55:47 +08:00
|
|
|
}
|
|
|
|
wait(delay(w));
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (x >= total)
|
2019-02-18 06:55:47 +08:00
|
|
|
return Void();
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|