1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <boost/filesystem/path.hpp>
#include <vector>
#include "mongo/base/status.h"
#include "mongo/base/status_with.h"
#include "mongo/db/ftdc/decompressor.h"
#include "mongo/db/jsobj.h"
namespace mongo {
/**
* Utilities for inflating and deflating BSON documents and metric arrays
*/
namespace FTDCBSONUtil {
/**
* Type of FTDC document.
*
* NOTE: Persisted to disk via BSON Objects.
*/
enum class FTDCType : std::int32_t {
/**
* A metadata document is composed of a header + an array of bson documents
*
* See createBSONMetadataChunkDocument
*/
kMetadata = 0,
/**
* A metrics chunk is composed of a header + a compressed metric chunk.
*
* See createBSONMetricChunkDocument
*/
kMetricChunk = 1,
};
/**
* Extract an array of numbers from a pair of documents. Verifies the pair of documents have same
* structure.
*
* Types considered numbers for the purpose of metrics:
* - double - encoded as an integer, loses fractional components via truncation
* - 32-bit integer
* - 64-integer
* - bool
* - date
* - timestamp
* Note: Timestamp is encoded as two integers, the timestamp value followed by the increment.
*
* Two documents are considered the same if satisfy the following criteria:
*
* Criteria: During a Depth First traversal of the document:
* 1. Each element has the same name regardless of its type.
* 2. The same number of elements exist in each document.
* 3. The types of each element are the same.
* Note: Double, Int, and Long are treated as equivalent for this purpose.
*
* @param referenceDoc A reference document to use the as the definition of the correct schema.
* @param doc A second document to compare against the reference document and extract metrics
* from
* @param metrics A vector of metrics that were extracted from the doc
*
* \return false if the documents differ in terms of metrics
*/
StatusWith<bool> extractMetricsFromDocument(const BSONObj& referenceDoc,
const BSONObj& doc,
std::vector<std::uint64_t>* metrics);
/**
* Construct a document from a reference document and array of metrics.
*
* @param referenceDoc A reference document to use the as the definition of the correct schema.
* @param builder A BSON builder to construct a single document into. Each document will be a
*copy
* of the reference document with the numerical fields replaced with values from metrics array.
* @param metrics A vector of metrics for all documents
* @param pos A position into the array of metrics to start iterating from.
*
* \return Status if the decompression of the buffer was successful or failed. Decompression may
* fail if the buffer is not valid.
*/
Status constructDocumentFromMetrics(const BSONObj& referenceDoc,
BSONObjBuilder& builder,
const std::vector<std::uint64_t>& metrics,
size_t* pos);
/**
* Construct a document from a reference document and array of metrics. See documentation above.
*/
StatusWith<BSONObj> constructDocumentFromMetrics(const BSONObj& ref,
const std::vector<std::uint64_t>& metrics);
/**
* Create BSON metadata document for storage. The passed in document is embedded as the doc
* field in the example above. For the _id field, the specified date is used.
*
* Example:
* {
* "_id" : Date_t
* "type" : 0
* "doc" : { ... }
* }
*/
BSONObj createBSONMetadataDocument(const BSONObj& metadata, Date_t date);
/**
* Create a BSON metric chunk document for storage. The passed in document is embedded as the
* data field in the example above. For the _id field, the date is specified by the caller
* since the metric chunk usually composed of multiple samples gathered over a period of time.
*
* Example:
* {
* "_id" : Date_t
* "type" : 1
* "data" : BinData(...)
* }
*/
BSONObj createBSONMetricChunkDocument(ConstDataRange buf, Date_t now);
/**
* Get the _id field of a BSON document
*/
StatusWith<Date_t> getBSONDocumentId(const BSONObj& obj);
/**
* Get the type of a BSON document
*/
StatusWith<FTDCType> getBSONDocumentType(const BSONObj& obj);
/**
* Extract the metadata field from a BSON document
*/
StatusWith<BSONObj> getBSONDocumentFromMetadataDoc(const BSONObj& obj);
/**
* Get the set of metric documents from the compressed chunk of a metric document
*/
StatusWith<std::vector<BSONObj>> getMetricsFromMetricDoc(const BSONObj& obj,
FTDCDecompressor* decompressor);
/**
* Is this a type that FTDC find's interesting? I.e. is this a numeric or container type?
*/
bool isFTDCType(BSONType type);
} // namespace FTDCBSONUtil
/**
* Miscellaneous utilties for FTDC.
*/
namespace FTDCUtil {
/**
* Construct the full path to the interim file
*/
boost::filesystem::path getInterimFile(const boost::filesystem::path& file);
/**
* Construct the full path to the interim temp file before it is renamed.
*/
boost::filesystem::path getInterimTempFile(const boost::filesystem::path& file);
/**
* Round the specified time_point to the next multiple of period after the specified time_point
*/
Date_t roundTime(Date_t now, Milliseconds period);
/**
* Get the storage path for MongoS from the log file path.
*/
boost::filesystem::path getMongoSPath(const boost::filesystem::path& logFile);
} // namespace FTDCUtil
} // namespace mongo
|