summaryrefslogtreecommitdiff
path: root/src/mongo/db/views/view_graph_test.cpp
blob: fa8c865efd6dc668b888d222916f622fe8534c56 (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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 *    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.
 */

#include "mongo/platform/basic.h"

#include <memory>

#include "mongo/bson/bsonmisc.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/query/collation/collator_factory_interface.h"
#include "mongo/db/query/query_test_service_context.h"
#include "mongo/db/views/view.h"
#include "mongo/db/views/view_graph.h"
#include "mongo/idl/server_parameter_test_util.h"
#include "mongo/unittest/unittest.h"

namespace mongo {
namespace {
constexpr auto kEmptyPipelineSize = 0;
const auto kTestDb = DatabaseName::createDatabaseName_forTest(boost::none, "test");
constexpr auto kFooName = "foo"_sd;
constexpr auto kBarName = "bar"_sd;
constexpr auto kQuxName = "qux"_sd;
const auto kFooNamespace = NamespaceString::createNamespaceString_forTest(kTestDb, kFooName);
const auto kBarNamespace = NamespaceString::createNamespaceString_forTest(kTestDb, kBarName);
const auto kQuxNamespace = NamespaceString::createNamespaceString_forTest(kTestDb, kQuxName);
const auto kEmptyPipeline = BSONArray();
const auto kBinaryCollation = BSONObj();
const auto kFilipinoCollation = BSON("locale"
                                     << "fil");

class ViewGraphFixture : public unittest::Test {
public:
    ViewGraphFixture()
        : _queryServiceContext(), _opCtx(_queryServiceContext.makeOperationContext()) {}

    const OperationContext* opCtx() const {
        return _opCtx.get();
    }

    ViewGraph* viewGraph() {
        return &_viewGraph;
    }

    ViewDefinition makeViewDefinition(const DatabaseName& dbName,
                                      StringData view,
                                      StringData viewOn,
                                      BSONArray pipeline,
                                      BSONObj collatorSpec) const {
        auto collator = std::unique_ptr<CollatorInterface>(nullptr);
        if (!collatorSpec.isEmpty()) {
            auto factoryCollator = CollatorFactoryInterface::get(_opCtx->getServiceContext())
                                       ->makeFromBSON(collatorSpec);
            ASSERT_OK(factoryCollator.getStatus());
            collator = std::move(factoryCollator.getValue());
        }

        return {dbName, view, viewOn, pipeline, std::move(collator)};
    }

private:
    QueryTestServiceContext _queryServiceContext;
    ServiceContext::UniqueOperationContext _opCtx;
    ViewGraph _viewGraph;
};

TEST_F(ViewGraphFixture, CanInsertViewsWithMatchingBinaryCollations) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 3UL);
}

TEST_F(ViewGraphFixture, CanInsertViewsWithMatchingNonTrivialCollations) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 3UL);
}

TEST_F(ViewGraphFixture, CannotInsertViewsWithNonMatchingCollations) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kBinaryCollation);
    ASSERT_EQ(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize),
              ErrorCodes::OptionNotSupportedOnView);
    ASSERT_EQ(viewGraph()->size(), 2UL);
}

TEST_F(ViewGraphFixture, CannotRecreateViewWithDifferentCollationIfDependedOnByOtherViews) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 3UL);

    viewGraph()->remove(kBarNamespace);
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barViewBinaryCollation =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kBinaryCollation);
    ASSERT_EQ(
        viewGraph()->insertAndValidate(barViewBinaryCollation, {kQuxNamespace}, kEmptyPipelineSize),
        ErrorCodes::OptionNotSupportedOnView);
    ASSERT_EQ(viewGraph()->size(), 2UL);
}

// Tests that an insertion that would create a view cycle is rejected, but will later be accepted if
// the cycle is broken by removing another existing view.
TEST_F(ViewGraphFixture, CanCreateViewThatReferencesDroppedView) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 3UL);

    const auto quxView =
        makeViewDefinition(kTestDb, kQuxName, kBarName, kEmptyPipeline, kBinaryCollation);

    // Inserting qux should fail, as it cycles with bar.
    ASSERT_EQ(viewGraph()->insertAndValidate(quxView, {kBarNamespace}, kEmptyPipelineSize),
              ErrorCodes::GraphContainsCycle);
    ASSERT_EQ(viewGraph()->size(), 3UL);

    viewGraph()->remove(kBarNamespace);
    ASSERT_EQ(viewGraph()->size(), 2UL);

    // With bar removed, we expect qux to be inserted successfully.
    ASSERT_OK(viewGraph()->insertAndValidate(quxView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 3UL);
}

// Tests that an insertion that would create mismatching collators is rejected, but will later be
// accepted if the existing view with the conflicting collator is removed.
TEST_F(ViewGraphFixture, CanCreateViewWithDifferentCollationThanDroppedView) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kBinaryCollation);

    // Inserting bar should fail, as foo depends on bar and has a different collation.
    ASSERT_EQ(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize),
              ErrorCodes::OptionNotSupportedOnView);
    ASSERT_EQ(viewGraph()->size(), 2UL);

    viewGraph()->remove(kFooNamespace);
    ASSERT_EQ(viewGraph()->size(), 0UL);

    // Now bar should be inserted successfully, as there are no existing views in the graph that
    // depend on it.
    ASSERT_OK(viewGraph()->insertAndValidate(barView, {kFooNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);
}

// Tests that a node in the graph is properly converted from a "view" node to a "non-view" node when
// a view with that namespace is removed.
TEST_F(ViewGraphFixture, DroppingViewPreservesNodeInGraphIfDependedOnByOtherViews) {
    const auto fooView =
        makeViewDefinition(kTestDb, kFooName, kBarName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    const auto barView =
        makeViewDefinition(kTestDb, kBarName, kQuxName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(barView, {kQuxNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 3UL);

    // Inserts baz into the graph so that qux has another namespace that depends on it. This way,
    // the node for qux won't be destroyed when baz is removed.
    const auto bazView =
        makeViewDefinition(kTestDb, "baz"_sd, kQuxName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(bazView, {kQuxNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 4UL);

    // Inserting a view that depends on bar but has a different collation should fail.
    const auto viewWithDifferentCollation = makeViewDefinition(
        kTestDb, "badCollation"_sd, kBarName, kEmptyPipeline, kFilipinoCollation);
    ASSERT_EQ(viewGraph()->insertAndValidate(
                  viewWithDifferentCollation, {kBarNamespace}, kEmptyPipelineSize),
              ErrorCodes::OptionNotSupportedOnView);
    ASSERT_EQ(viewGraph()->size(), 4UL);

    // Removes bar from the graph. The graph's size should remain at 4, since bar is still depended
    // on by qux.
    viewGraph()->remove(kBarNamespace);
    ASSERT_EQ(viewGraph()->size(), 4UL);

    // Inserting the viewWithDifferentCollation from above should now succeed, since bar is no
    // longer a view.
    ASSERT_OK(viewGraph()->insertAndValidate(
        viewWithDifferentCollation, {kBarNamespace}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 5UL);
}

TEST_F(ViewGraphFixture, DifferentTenantsCanCreateViewWithConflictingNamespaces) {
    RAIIServerParameterControllerForTest multitenancyController("multitenancySupport", true);

    DatabaseName db1 = DatabaseName::createDatabaseName_forTest(TenantId(OID::gen()), "test");
    DatabaseName db2 = DatabaseName::createDatabaseName_forTest(TenantId(OID::gen()), "test");

    NamespaceString viewOn1 = NamespaceString::createNamespaceString_forTest(db1, kBarName);
    NamespaceString viewOn2 = NamespaceString::createNamespaceString_forTest(db2, kBarName);

    // Create a view "foo" on tenant1's collection "test.bar".
    const auto fooView1 =
        makeViewDefinition(db1, kFooName, kBarName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView1, {viewOn1}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 2UL);

    // Create a view "foo" on tenant2's collection "test.bar".
    const auto fooView2 =
        makeViewDefinition(db2, kFooName, kBarName, kEmptyPipeline, kBinaryCollation);
    ASSERT_OK(viewGraph()->insertAndValidate(fooView2, {viewOn2}, kEmptyPipelineSize));
    ASSERT_EQ(viewGraph()->size(), 4UL);

    // Remove tenant1's view "foo".
    NamespaceString viewToRemove = NamespaceString::createNamespaceString_forTest(db1, kFooName);
    viewGraph()->remove(viewToRemove);
    ASSERT_EQ(viewGraph()->size(), 2UL);
}
}  // namespace
}  // namespace mongo