summaryrefslogtreecommitdiff
path: root/src/mongo/db/timeseries/timeseries_commands_conversion_helper.cpp
blob: 961344afd0e066280011be45b92dfc4c2d14947c (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 *    Copyright (C) 2021-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.
 */

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kStorage

#include "mongo/db/timeseries/timeseries_commands_conversion_helper.h"

#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/index_names.h"
#include "mongo/db/pipeline/document_source_internal_unpack_bucket.h"
#include "mongo/db/query/collation/collator_factory_interface.h"
#include "mongo/db/query/collation/collator_interface.h"
#include "mongo/db/storage/storage_parameters_gen.h"
#include "mongo/db/timeseries/timeseries_constants.h"
#include "mongo/db/timeseries/timeseries_index_schema_conversion_functions.h"
#include "mongo/logv2/log.h"
#include "mongo/logv2/redaction.h"

namespace mongo::timeseries {

namespace {
NamespaceString makeTimeseriesBucketsNamespace(const NamespaceString& nss) {
    return nss.isTimeseriesBucketsCollection() ? nss : nss.makeTimeseriesBucketsNamespace();
}
}  // namespace


BSONObj makeTimeseriesCommand(const BSONObj& origCmd,
                              const NamespaceString& ns,
                              const StringData nsFieldName,
                              boost::optional<StringData> appendTimeSeriesFlag) {
    // Translate time-series collection view namespace to bucket namespace.
    const auto bucketNs = ns.makeTimeseriesBucketsNamespace();
    BSONObjBuilder builder;
    for (const auto& entry : origCmd) {
        if (entry.fieldNameStringData() == nsFieldName) {
            builder.append(nsFieldName, bucketNs.coll());
        } else {
            builder.append(entry);
        }
    }

    if (appendTimeSeriesFlag) {
        builder.append(*appendTimeSeriesFlag, true);
    }
    return builder.obj();
}

CreateIndexesCommand makeTimeseriesCreateIndexesCommand(OperationContext* opCtx,
                                                        const CreateIndexesCommand& origCmd,
                                                        const TimeseriesOptions& options) {
    const auto& origNs = origCmd.getNamespace();
    const auto& origIndexes = origCmd.getIndexes();

    std::vector<mongo::BSONObj> indexes;
    for (const auto& origIndex : origIndexes) {
        BSONObjBuilder builder;
        bool isBucketsIndexSpecCompatibleForDowngrade = true;
        for (const auto& elem : origIndex) {
            if (elem.fieldNameStringData() == IndexDescriptor::kPartialFilterExprFieldName) {
                if (feature_flags::gTimeseriesMetricIndexes.isEnabledAndIgnoreFCV() &&
                    serverGlobalParams.featureCompatibility.isFCVUpgradingToOrAlreadyLatest()) {
                    isBucketsIndexSpecCompatibleForDowngrade = false;
                } else {
                    uasserted(ErrorCodes::InvalidOptions,
                              "Partial indexes are not supported on time-series collections");
                }

                uassert(ErrorCodes::CannotCreateIndex,
                        "Partial indexes on time-series collections require FCV 5.3",
                        feature_flags::gTimeseriesMetricIndexes.isEnabled(
                            serverGlobalParams.featureCompatibility));
                BSONObj pred = elem.Obj();

                // If the createIndexes command specifies a collation for this index, then that
                // collation affects how we should interpret expressions in the partial filter
                // ($gt, $lt, etc).
                if (auto collatorSpec = origIndex[NewIndexSpec::kCollationFieldName]) {
                    uasserted(
                        ErrorCodes::IndexOptionsConflict,
                        std::string{"On a time-series collection, partialFilterExpression and "} +
                            NewIndexSpec::kCollationFieldName + " arguments are incompatible"_sd);
                }
                // Since no collation was specified in the command, we know the index collation will
                // match the collection's collation.
                auto collationMatchesDefault = ExpressionContext::CollationMatchesDefault::kYes;

                // Even though the index collation will match the collection's collation, we don't
                // know whether or not that collation is simple. However, I think we can correctly
                // rewrite the filter expression without knowing this... Looking up the correct
                // value would require handling mongos and mongod separately.
                std::unique_ptr<CollatorInterface> collator{nullptr};

                auto expCtx = make_intrusive<ExpressionContext>(opCtx, std::move(collator), origNs);
                expCtx->collationMatchesDefault = collationMatchesDefault;

                // partialFilterExpression is evaluated against a collection, so there are no
                // computed fields.
                bool haveComputedMetaField = false;

                // As part of building the index, we verify that the collection does not contain
                // any mixed-schema buckets. So by the time the index is visible to the query
                // planner, this will be true.
                bool assumeNoMixedSchemaData = true;

                BSONObj bucketPred =
                    BucketSpec::pushdownPredicate(expCtx,
                                                  options,
                                                  collationMatchesDefault,
                                                  pred,
                                                  haveComputedMetaField,
                                                  assumeNoMixedSchemaData,
                                                  BucketSpec::IneligiblePredicatePolicy::kError);
                builder.append(IndexDescriptor::kPartialFilterExprFieldName, bucketPred);
                continue;
            }

            if (elem.fieldNameStringData() == IndexDescriptor::kSparseFieldName) {
                // Sparse indexes are only allowed on the time and meta fields.
                auto timeField = options.getTimeField();
                auto metaField = options.getMetaField();

                BSONObj keyPattern = origIndex.getField(NewIndexSpec::kKeyFieldName).Obj();
                for (const auto& keyElem : keyPattern) {
                    if (keyElem.fieldNameStringData() == timeField) {
                        continue;
                    }

                    if (metaField &&
                        (keyElem.fieldNameStringData() == *metaField ||
                         keyElem.fieldNameStringData().startsWith(*metaField + "."))) {
                        continue;
                    }

                    uasserted(ErrorCodes::InvalidOptions,
                              "Sparse indexes are not supported on time-series measurements");
                }
            }

            if (elem.fieldNameStringData() == IndexDescriptor::kExpireAfterSecondsFieldName) {
                uasserted(ErrorCodes::InvalidOptions,
                          "TTL indexes are not supported on time-series collections");
            }


            if (elem.fieldNameStringData() == IndexDescriptor::kUniqueFieldName) {
                uassert(ErrorCodes::InvalidOptions,
                        "Unique indexes are not supported on time-series collections",
                        !elem.trueValue());
            }

            if (elem.fieldNameStringData() == NewIndexSpec::kKeyFieldName) {
                auto pluginName = IndexNames::findPluginName(elem.Obj());
                uassert(ErrorCodes::InvalidOptions,
                        "Text indexes are not supported on time-series collections",
                        pluginName != IndexNames::TEXT);

                auto bucketsIndexSpecWithStatus =
                    timeseries::createBucketsIndexSpecFromTimeseriesIndexSpec(options, elem.Obj());
                uassert(ErrorCodes::CannotCreateIndex,
                        str::stream() << bucketsIndexSpecWithStatus.getStatus().toString()
                                      << " Command request: " << redact(origCmd.toBSON({})),
                        bucketsIndexSpecWithStatus.isOK());

                if (!timeseries::isBucketsIndexSpecCompatibleForDowngrade(
                        options,
                        BSON(NewIndexSpec::kKeyFieldName
                             << bucketsIndexSpecWithStatus.getValue()))) {
                    isBucketsIndexSpecCompatibleForDowngrade = false;
                }

                builder.append(NewIndexSpec::kKeyFieldName,
                               std::move(bucketsIndexSpecWithStatus.getValue()));
                continue;
            }

            // Any index option that's not explicitly banned, and not handled specially, we pass
            // through unchanged.
            builder.append(elem);
        }

        if (feature_flags::gTimeseriesMetricIndexes.isEnabledAndIgnoreFCV() &&
            !isBucketsIndexSpecCompatibleForDowngrade) {
            // Store the original user index definition on the transformed index definition for the
            // time-series buckets collection if this is a newly supported index type on time-series
            // collections. This is to avoid any additional downgrade steps for index types already
            // supported in 5.0.
            builder.appendObject(IndexDescriptor::kOriginalSpecFieldName, origIndex.objdata());
        }

        indexes.push_back(builder.obj());
    }

    auto ns = makeTimeseriesBucketsNamespace(origNs);
    auto cmd = CreateIndexesCommand(ns, std::move(indexes));
    cmd.setV(origCmd.getV());
    cmd.setIgnoreUnknownIndexOptions(origCmd.getIgnoreUnknownIndexOptions());
    cmd.setCommitQuorum(origCmd.getCommitQuorum());

    return cmd;
}

DropIndexes makeTimeseriesDropIndexesCommand(OperationContext* opCtx,
                                             const DropIndexes& origCmd,
                                             const TimeseriesOptions& options) {
    const auto& origNs = origCmd.getNamespace();
    auto ns = makeTimeseriesBucketsNamespace(origNs);

    const auto& origIndex = origCmd.getIndex();
    if (auto keyPtr = stdx::get_if<BSONObj>(&origIndex)) {
        auto bucketsIndexSpecWithStatus =
            timeseries::createBucketsIndexSpecFromTimeseriesIndexSpec(options, *keyPtr);

        uassert(ErrorCodes::IndexNotFound,
                str::stream() << bucketsIndexSpecWithStatus.getStatus().toString()
                              << " Command request: " << redact(origCmd.toBSON({})),
                bucketsIndexSpecWithStatus.isOK());

        return DropIndexes(ns, std::move(bucketsIndexSpecWithStatus.getValue()));
    }

    return DropIndexes(ns, origIndex);
}

}  // namespace mongo::timeseries