Skip to content

Commit b091b95

Browse files
authored
Adding metrics. (#161)
1 parent 21b8b78 commit b091b95

File tree

10 files changed

+134
-61
lines changed

10 files changed

+134
-61
lines changed

src/Constants.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,16 @@ const char* INDEX_STATUS_READY = "ready";
6565
const char* INDEX_STATUS_BUILDING = "building";
6666
const char* INDEX_STATUS_ERROR = "error";
6767

68+
const char* MT_GUAGE_ACTIVE_CONNECTIONS = "dl_active_connections";
69+
const char* MT_GUAGE_ACTIVE_CURSORS = "dl_active_cursors";
70+
const char* MT_HIST_MESSAGE_SZ = "dl_message_size_bytes";
71+
const char* MT_TIME_QUERY_LATENCY_US = "dl_query_latency_useconds";
72+
const char* MT_HIST_KEYS_PER_DOCUMENT = "dl_keys_per_doc";
73+
const char* MT_HIST_DOCUMENT_SZ = "dl_doc_size_bytes";
74+
const char* MT_HIST_DOCS_PER_INSERT = "dl_docs_per_insert";
75+
const char* MT_TIME_INSERT_LATENCY_US = "dl_insert_latency_useconds";
76+
const char* MT_HIST_INSERT_SZ = "dl_insert_size_bytes";
77+
const char* MT_HIST_TR_PER_REQUEST = "dl_tr_per_request";
78+
const char* MT_RATE_IDX_REBUILD = "dl_index_rebuild_rate";
79+
6880
} // namespace DocLayerConstants

src/Constants.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ extern const char* INDEX_STATUS_READY;
7777
extern const char* INDEX_STATUS_BUILDING;
7878
extern const char* INDEX_STATUS_ERROR;
7979

80+
// Metrics
81+
extern const char* MT_GUAGE_ACTIVE_CONNECTIONS;
82+
extern const char* MT_GUAGE_ACTIVE_CURSORS;
83+
extern const char* MT_HIST_MESSAGE_SZ;
84+
extern const char* MT_TIME_QUERY_LATENCY_US;
85+
extern const char* MT_HIST_KEYS_PER_DOCUMENT;
86+
extern const char* MT_HIST_DOCUMENT_SZ;
87+
extern const char* MT_HIST_DOCS_PER_INSERT;
88+
extern const char* MT_TIME_INSERT_LATENCY_US;
89+
extern const char* MT_HIST_INSERT_SZ;
90+
extern const char* MT_HIST_TR_PER_REQUEST;
91+
extern const char* MT_RATE_IDX_REBUILD;
92+
8093
} // namespace DocLayerConstants
8194

8295
#endif // FDB_DOC_LAYER_CONSTANTS_H

src/Cursor.actor.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
#include "Cursor.h"
22+
#include "DocLayer.h"
2223
#include "Knobs.h"
2324

2425
int32_t Cursor::prune(std::map<int64_t, Reference<Cursor>>& cursors) {
@@ -33,7 +34,7 @@ int32_t Cursor::prune(std::map<int64_t, Reference<Cursor>>& cursors) {
3334
++it;
3435
}
3536

36-
for (auto i : to_be_pruned) {
37+
for (const auto& i : to_be_pruned) {
3738
(void)pluck(i);
3839
pruned++;
3940
}
@@ -45,13 +46,14 @@ void Cursor::pluck(Reference<Cursor> cursor) {
4546
if (cursor) {
4647
cursor->siblings->erase(cursor->id);
4748
cursor->checkpoint->stop();
49+
DocumentLayer::metricReporter->captureGauge(DocLayerConstants::MT_GUAGE_ACTIVE_CURSORS,
50+
cursor->siblings->size());
4851
}
4952
}
5053

5154
Reference<Cursor> Cursor::add(std::map<int64_t, Reference<Cursor>>& siblings, Reference<Cursor> cursor) {
5255
cursor->siblings = &siblings;
53-
54-
// FIXME: limit the number of allowed cursors?
55-
56-
return siblings[cursor->id] = cursor;
56+
siblings[cursor->id] = cursor;
57+
DocumentLayer::metricReporter->captureGauge(DocLayerConstants::MT_GUAGE_ACTIVE_CURSORS, siblings.size());
58+
return cursor;
5759
}

src/DocLayer.actor.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ ACTOR Future<Void> extServerConnection(Reference<DocumentLayer> docLayer,
182182
state PromiseStream<std::pair<int, Future<Void>>> msg_size_inuse;
183183
state Future<Void> onError = ec->bc->onClosed() || popDisposedMessages(bc, msg_size_inuse.getFuture());
184184

185-
DocumentLayer::metricReporter->captureGauge("activeConnections", ++docLayer->nrConnections);
185+
DocumentLayer::metricReporter->captureGauge(DocLayerConstants::MT_GUAGE_ACTIVE_CONNECTIONS,
186+
++docLayer->nrConnections);
186187
try {
187188
ec->startHousekeeping();
188189

@@ -205,8 +206,8 @@ ACTOR Future<Void> extServerConnection(Reference<DocumentLayer> docLayer,
205206
Void _ = wait(ec->bc->onBytesAvailable(header->messageLength));
206207
auto messageBytes = ec->bc->peekExact(header->messageLength);
207208

208-
DocumentLayer::metricReporter->captureHistogram("messageLength", header->messageLength);
209-
DocumentLayer::metricReporter->captureMeter("messageRate", 1);
209+
DocumentLayer::metricReporter->captureHistogram(DocLayerConstants::MT_HIST_MESSAGE_SZ,
210+
header->messageLength);
210211

211212
/* We don't use hdr in this call because the second peek may
212213
have triggered a copy that the first did not, but it's nice
@@ -222,7 +223,8 @@ ACTOR Future<Void> extServerConnection(Reference<DocumentLayer> docLayer,
222223
}
223224
}
224225
} catch (Error& e) {
225-
DocumentLayer::metricReporter->captureGauge("activeConnections", --docLayer->nrConnections);
226+
DocumentLayer::metricReporter->captureGauge(DocLayerConstants::MT_GUAGE_ACTIVE_CONNECTIONS,
227+
--docLayer->nrConnections);
226228
return Void();
227229
}
228230
}

src/ExtMsg.actor.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,6 @@ ACTOR static Future<Void> doRun(Reference<ExtMsgQuery> query, Reference<ExtConne
490490
state Future<Void> x;
491491
state uint64_t startTime = timer_int();
492492

493-
DocumentLayer::metricReporter->captureMeter("queryRate", 1);
494493
if (query->isCmd) {
495494
// It's a command
496495
x = runCommand(ec, query, replyStream);
@@ -513,7 +512,8 @@ ACTOR static Future<Void> doRun(Reference<ExtMsgQuery> query, Reference<ExtConne
513512
}
514513
}
515514

516-
DocumentLayer::metricReporter->captureTime("queryLatency_us", (timer_int() - startTime) / 1000);
515+
DocumentLayer::metricReporter->captureTime(DocLayerConstants::MT_TIME_QUERY_LATENCY_US,
516+
(timer_int() - startTime) / 1000);
517517

518518
return Void();
519519
}
@@ -647,10 +647,13 @@ ACTOR Future<Reference<IReadWriteContext>> insertDocument(Reference<CollectionCo
647647

648648
dcx->set(LiteralStringRef(""), DataValue::subObject().encode_value());
649649

650+
int nrFDBKeys = 0;
650651
for (auto i = d.begin(); i.more();) {
651652
auto e = i.next();
652-
insertElementRecursive(e, dcx);
653+
nrFDBKeys += insertElementRecursive(e, dcx);
653654
}
655+
DocumentLayer::metricReporter->captureHistogram(DocLayerConstants::MT_HIST_KEYS_PER_DOCUMENT, nrFDBKeys);
656+
DocumentLayer::metricReporter->captureHistogram(DocLayerConstants::MT_HIST_DOCUMENT_SZ, d.objsize());
654657

655658
if (idObj.present())
656659
insertElementRecursive(DocLayerConstants::ID_FIELD, idObj.get(), dcx);
@@ -835,6 +838,7 @@ ACTOR Future<WriteCmdResult> doInsertCmd(Namespace ns,
835838
std::list<bson::BSONObj>* documents,
836839
Reference<ExtConnection> ec) {
837840
state Reference<DocTransaction> tr = ec->getOperationTransaction();
841+
state uint64_t startTime = timer_int();
838842

839843
if (ns.second == DocLayerConstants::SYSTEM_INDEXES) {
840844
if (verboseLogging)
@@ -847,11 +851,15 @@ ACTOR Future<WriteCmdResult> doInsertCmd(Namespace ns,
847851
const char* collnsStr = firstDoc.getField(DocLayerConstants::NS_FIELD).String().c_str();
848852
const auto collns = getDBCollectionPair(collnsStr, std::make_pair("msg", "Bad coll name in index insert"));
849853
WriteCmdResult result = wait(attemptIndexInsertion(firstDoc.getOwned(), ec, tr, collns));
854+
855+
DocumentLayer::metricReporter->captureTime(DocLayerConstants::MT_TIME_INSERT_LATENCY_US,
856+
(timer_int() - startTime) / 1000);
850857
return result;
851858
}
852859

853860
std::vector<Reference<IInsertOp>> inserts;
854861
std::set<std::string> ids;
862+
int insertSize = 0;
855863
for (const auto& d : *documents) {
856864
const bson::BSONObj& obj = d;
857865
Optional<IdInfo> encodedIds = extractEncodedIds(obj);
@@ -861,10 +869,16 @@ ACTOR Future<WriteCmdResult> doInsertCmd(Namespace ns,
861869
}
862870
}
863871
inserts.push_back(Reference<IInsertOp>(new ExtInsert(obj, encodedIds)));
872+
insertSize += obj.objsize();
864873
}
874+
DocumentLayer::metricReporter->captureHistogram(DocLayerConstants::MT_HIST_INSERT_SZ, insertSize);
875+
DocumentLayer::metricReporter->captureHistogram(DocLayerConstants::MT_HIST_DOCS_PER_INSERT, documents->size());
865876

866877
Reference<Plan> plan = ec->isolatedWrapOperationPlan(ref(new InsertPlan(inserts, ec->mm, ns)));
867878
int64_t i = wait(executeUntilCompletionTransactionally(plan, tr));
879+
880+
DocumentLayer::metricReporter->captureTime(DocLayerConstants::MT_TIME_INSERT_LATENCY_US,
881+
(timer_int() - startTime) / 1000);
868882
return WriteCmdResult(i);
869883
}
870884

@@ -1273,9 +1287,10 @@ std::string ExtMsgKillCursors::toString() {
12731287

12741288
Future<Void> doKillCursorsRun(Reference<ExtMsgKillCursors> msg, Reference<ExtConnection> ec) {
12751289
int64_t* ptr = msg->cursorIDs;
1276-
int32_t numberOfCursorIDs =
1277-
msg->numberOfCursorIDs; // FIXME: I'm not quite sure what the contract around the memory owned by
1278-
// BufferedConnection is. So do this copy for now to be conservative.
1290+
1291+
// FIXME: I'm not quite sure what the contract around the memory owned by
1292+
// BufferedConnection is. So do this copy for now to be conservative.
1293+
int32_t numberOfCursorIDs = msg->numberOfCursorIDs;
12791294

12801295
while (numberOfCursorIDs--) {
12811296
Cursor::pluck(ec->cursors[*ptr++]);

src/ExtUtil.actor.cpp

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,86 +73,106 @@ std::string encodeMaybeDotted(std::string fieldname) {
7373
return path;
7474
}
7575

76-
void insertElementRecursive(std::string fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx) {
76+
int insertElementRecursive(std::string fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx) {
7777
if (fn[0] == '$')
7878
throw fieldname_with_dollar();
7979
std::string kp = encodeMaybeDotted(fn);
80+
81+
int nrFDBKeys = 1;
8082
cx->set(kp, DataValue::subObject().encode_value());
83+
8184
auto scx = cx->getSubContext(kp);
8285
for (auto i = obj.begin(); i.more();) {
8386
auto e = i.next();
84-
insertElementRecursive(e, scx);
87+
nrFDBKeys += insertElementRecursive(e, scx);
8588
}
89+
90+
return nrFDBKeys;
8691
}
8792

88-
void insertElementRecursive(std::string fn, bson::BSONArray const& arr, Reference<IReadWriteContext> cx) {
93+
int insertElementRecursive(std::string fn, bson::BSONArray const& arr, Reference<IReadWriteContext> cx) {
8994
if (fn[0] == '$')
9095
throw fieldname_with_dollar();
96+
9197
std::string kp = encodeMaybeDotted(fn);
98+
int nrFDBKeys = 1;
99+
92100
cx->set(kp, DataValue::arrayOfLength(arr.nFields()).encode_value());
101+
93102
auto scx = cx->getSubContext(kp);
94103
for (auto i = arr.begin(); i.more();) {
95104
bson::BSONElement e = i.next();
96-
insertElementRecursive(e, scx);
105+
nrFDBKeys += insertElementRecursive(e, scx);
97106
}
107+
108+
return nrFDBKeys;
98109
}
99110

100-
void insertElementRecursive(std::string fn, bson::BSONElement const& elem, Reference<IReadWriteContext> cx) {
111+
int insertElementRecursive(std::string fn, bson::BSONElement const& elem, Reference<IReadWriteContext> cx) {
101112
if (fn[0] == '$')
102113
throw fieldname_with_dollar();
114+
103115
std::string kp = encodeMaybeDotted(fn);
104116
if (!elem.isABSONObj()) {
105117
cx->set(kp, DataValue(elem).encode_value());
106-
} else {
107-
if (elem.type() == bson::BSONType::Array) {
108-
insertElementRecursive(fn, bson::BSONArray(elem.Obj()), cx);
109-
} else {
110-
insertElementRecursive(fn, elem.Obj(), cx);
111-
}
118+
return 1;
112119
}
120+
121+
if (elem.type() == bson::BSONType::Array)
122+
return insertElementRecursive(fn, bson::BSONArray(elem.Obj()), cx);
123+
124+
return insertElementRecursive(fn, elem.Obj(), cx);
113125
}
114126

115-
void insertElementRecursive(bson::BSONElement const& elem, Reference<IReadWriteContext> cx) {
127+
int insertElementRecursive(bson::BSONElement const& elem, Reference<IReadWriteContext> cx) {
116128
std::string fn = elem.fieldName();
117129
if (std::all_of(fn.begin(), fn.end(), ::isdigit)) {
118130
const char* c_fn = fn.c_str();
119-
insertElementRecursive(atoi(c_fn), elem, cx);
120-
} else {
121-
insertElementRecursive(fn, elem, cx);
131+
return insertElementRecursive(atoi(c_fn), elem, cx);
122132
}
133+
134+
return insertElementRecursive(fn, elem, cx);
123135
}
124136

125-
void insertElementRecursive(int fn, bson::BSONElement const& elem, Reference<IReadWriteContext> cx) {
137+
int insertElementRecursive(int fn, bson::BSONElement const& elem, Reference<IReadWriteContext> cx) {
126138
std::string kp = DataValue(fn).encode_key_part();
127139
if (!elem.isABSONObj()) {
128140
cx->set(kp, DataValue(elem).encode_value());
129-
} else {
130-
if (elem.type() == bson::BSONType::Array) {
131-
insertElementRecursive(fn, bson::BSONArray(elem.Obj()), cx);
132-
} else {
133-
insertElementRecursive(fn, elem.Obj(), cx);
134-
}
141+
return 1;
135142
}
143+
144+
if (elem.type() == bson::BSONType::Array)
145+
return insertElementRecursive(fn, bson::BSONArray(elem.Obj()), cx);
146+
147+
return insertElementRecursive(fn, elem.Obj(), cx);
136148
}
137149

138-
void insertElementRecursive(int fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx) {
150+
int insertElementRecursive(int fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx) {
139151
std::string kp = DataValue(fn).encode_key_part();
152+
int nrFDBKeys = 1;
153+
140154
cx->set(kp, DataValue::subObject().encode_value());
155+
141156
auto scx = cx->getSubContext(kp);
142157
for (auto i = obj.begin(); i.more();) {
143158
auto e = i.next();
144-
insertElementRecursive(e, scx);
159+
nrFDBKeys += insertElementRecursive(e, scx);
145160
}
161+
return nrFDBKeys;
146162
}
147163

148-
void insertElementRecursive(int fn, bson::BSONArray const& arr, Reference<IReadWriteContext> cx) {
164+
int insertElementRecursive(int fn, bson::BSONArray const& arr, Reference<IReadWriteContext> cx) {
149165
std::string kp = DataValue(fn).encode_key_part();
166+
int nrFDBKeys = 1;
167+
150168
cx->set(kp, DataValue::arrayOfLength(arr.nFields()).encode_value());
169+
151170
auto scx = cx->getSubContext(kp);
152171
for (auto i = arr.begin(); i.more();) {
153172
bson::BSONElement e = i.next();
154-
insertElementRecursive(e, scx);
173+
nrFDBKeys += insertElementRecursive(e, scx);
155174
}
175+
return nrFDBKeys;
156176
}
157177

158178
ACTOR Future<Void> ensureValidObject(Reference<IReadWriteContext> cx,

src/ExtUtil.actor.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ std::string encodeMaybeDotted(std::string fieldname);
8080
/**
8181
* The usual way of inserting an element
8282
*/
83-
void insertElementRecursive(const bson::BSONElement& elem, Reference<IReadWriteContext> cx);
83+
int insertElementRecursive(const bson::BSONElement& elem, Reference<IReadWriteContext> cx);
8484

8585
/**
8686
* An overload that is used only in bizarre cases involving upserting things with compound ids.
8787
*/
88-
void insertElementRecursive(std::string fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx);
88+
int insertElementRecursive(std::string fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx);
8989

9090
/**
9191
* Utility overloads used by some of the array update operators
9292
*/
93-
void insertElementRecursive(int fn, bson::BSONElement const& elem, Reference<IReadWriteContext> cx);
94-
void insertElementRecursive(int fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx);
95-
void insertElementRecursive(int fn, bson::BSONArray const& arr, Reference<IReadWriteContext> cx);
93+
int insertElementRecursive(int fn, bson::BSONElement const& elem, Reference<IReadWriteContext> cx);
94+
int insertElementRecursive(int fn, bson::BSONObj const& obj, Reference<IReadWriteContext> cx);
95+
int insertElementRecursive(int fn, bson::BSONArray const& arr, Reference<IReadWriteContext> cx);
9696

9797
Future<Void> ensureValidObject(const Reference<IReadWriteContext>& cx,
9898
const std::string& objectRoot,

src/QLExpression.actor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ ACTOR Future<Optional<std::pair<Standalone<StringRef>, DataValue>>> getArrayAnce
3535
futures.push_back(cx->get(dk.bytes()));
3636
}
3737

38-
if (futures.size()) {
38+
if (!futures.empty()) {
3939
std::vector<Optional<DataValue>> results = wait(getAll(futures));
4040

4141
for (int i = 0; i < (checkLast ? results.size() - 1 : results.size()); ++i) {

0 commit comments

Comments
 (0)