summaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/documentdb/fileconfigmanager/fileconfigmanager_test.cpp
blob: 579e33250b311e9b5664c4ce68c5c5e1eb656e06 (plain) (blame)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "config-mycfg.h"
#include <vespa/config-attributes.h>
#include <vespa/config-imported-fields.h>
#include <vespa/config-indexschema.h>
#include <vespa/config-rank-profiles.h>
#include <vespa/config-summary.h>
#include <vespa/config-summarymap.h>
#include <vespa/config/helper/configgetter.hpp>
#include <vespa/searchcore/proton/common/schemautil.h>
#include <vespa/searchcore/proton/server/bootstrapconfig.h>
#include <vespa/searchcore/proton/server/fileconfigmanager.h>
#include <vespa/searchcore/proton/test/documentdb_config_builder.h>
#include <vespa/searchsummary/config/config-juniperrc.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/testkit/test_kit.h>

using namespace cloud::config::filedistribution;
using namespace config;
using namespace document;
using namespace proton;
using namespace search::index;
using namespace search;
using namespace vespa::config::search::core;
using namespace vespa::config::search;
using proton::matching::RankingConstants;

typedef DocumentDBConfigHelper DBCM;
typedef DocumentDBConfig::DocumenttypesConfigSP DocumenttypesConfigSP;
using vespalib::nbostream;

vespalib::string myId("myconfigid");

namespace
{

DocumentDBConfig::SP
getConfig(int64_t generation, const Schema::SP &schema)
{
    return test::DocumentDBConfigBuilder(generation, schema, "client", "test").build();
}

Schema::SP
getSchema(int step)
{
    Schema::SP schema(new Schema);
    schema->addIndexField(Schema::IndexField("foo1", schema::DataType::STRING));
    if (step < 2) {
        schema->addIndexField(Schema::IndexField("foo2", schema::DataType::STRING));
    }
    if (step < 1) {
        schema->addIndexField(Schema::IndexField("foo3", schema::DataType::STRING));
    }
    return schema;
}

        }

DocumentDBConfig::SP
makeBaseConfigSnapshot()
{
    config::DirSpec spec(TEST_PATH("cfg"));
    ConfigKeySet extraKeySet;
    extraKeySet.add<MycfgConfig>("");
    DBCM dbcm(spec, "test", extraKeySet);
    DocumenttypesConfigSP dtcfg(config::ConfigGetter<DocumenttypesConfig>::getConfig("", spec).release());
    BootstrapConfig::SP b(new BootstrapConfig(1,
                                              dtcfg,
                                              DocumentTypeRepo::SP(new DocumentTypeRepo(*dtcfg)),
                                              std::make_shared<ProtonConfig>(),
                                              std::make_shared<FiledistributorrpcConfig>(),
                                              std::make_shared<TuneFileDocumentDB>()));
    dbcm.forwardConfig(b);
    dbcm.nextGeneration(0);
    DocumentDBConfig::SP snap = dbcm.getConfig();
    snap->setConfigId(myId);
    ASSERT_TRUE(snap.get() != NULL);
    return snap;
}

Schema
makeHistorySchema()
{
    Schema hs;
    hs.addIndexField(Schema::IndexField("history", schema::DataType::STRING));
    return hs;
}

void
saveBaseConfigSnapshot(const DocumentDBConfig &snap, const Schema &history, SerialNum num)
{
    FileConfigManager cm("out", myId, snap.getDocTypeName());
    cm.saveConfig(snap, history, num);
}


DocumentDBConfig::SP
makeEmptyConfigSnapshot(void)
{
    return test::DocumentDBConfigBuilder(0, std::make_shared<Schema>(), "client", "test").build();
}

void incInt(int *i, const DocumentType&) { ++*i; }

void
assertEqualExtraConfigs(const DocumentDBConfig &expSnap, const DocumentDBConfig &actSnap)
{
    const ConfigSnapshot &exp = expSnap.getExtraConfigs();
    const ConfigSnapshot &act = actSnap.getExtraConfigs();
    EXPECT_EQUAL(1u, exp.size());
    EXPECT_EQUAL(1u, act.size());
    std::unique_ptr<MycfgConfig> expCfg = exp.getConfig<MycfgConfig>("");
    std::unique_ptr<MycfgConfig> actCfg = act.getConfig<MycfgConfig>("");
    EXPECT_EQUAL("foo", expCfg->myField);
    EXPECT_EQUAL("foo", actCfg->myField);
}

void
assertEqualSnapshot(const DocumentDBConfig &exp, const DocumentDBConfig &act)
{
    EXPECT_TRUE(exp.getRankProfilesConfig() == act.getRankProfilesConfig());
    EXPECT_TRUE(exp.getRankingConstants() == act.getRankingConstants());
    EXPECT_EQUAL(0u, exp.getRankingConstants().size());
    EXPECT_TRUE(exp.getIndexschemaConfig() == act.getIndexschemaConfig());
    EXPECT_TRUE(exp.getAttributesConfig() == act.getAttributesConfig());
    EXPECT_TRUE(exp.getSummaryConfig() == act.getSummaryConfig());
    EXPECT_TRUE(exp.getSummarymapConfig() == act.getSummarymapConfig());
    EXPECT_TRUE(exp.getJuniperrcConfig() == act.getJuniperrcConfig());
    EXPECT_TRUE(exp.getImportedFieldsConfig() == act.getImportedFieldsConfig());
    EXPECT_EQUAL(0u, exp.getImportedFieldsConfig().attribute.size());

    int expTypeCount = 0;
    int actTypeCount = 0;
    exp.getDocumentTypeRepoSP()->forEachDocumentType(
            *vespalib::makeClosure(incInt, &expTypeCount));
    act.getDocumentTypeRepoSP()->forEachDocumentType(
            *vespalib::makeClosure(incInt, &actTypeCount));
    EXPECT_EQUAL(expTypeCount, actTypeCount);
    EXPECT_TRUE(*exp.getSchemaSP() == *act.getSchemaSP());
    EXPECT_EQUAL(expTypeCount, actTypeCount);
    EXPECT_EQUAL(exp.getConfigId(), act.getConfigId());
    assertEqualExtraConfigs(exp, act);
}

DocumentDBConfig::SP
addConfigsThatAreNotSavedToDisk(const DocumentDBConfig &cfg)
{
    test::DocumentDBConfigBuilder builder(cfg);
    RankingConstants::Vector constants = {{"my_name", "my_type", "my_path"}};
    builder.rankingConstants(std::make_shared<RankingConstants>(constants));

    ImportedFieldsConfigBuilder importedFields;
    importedFields.attribute.resize(1);
    importedFields.attribute.back().name = "my_name";
    builder.importedFields(std::make_shared<ImportedFieldsConfig>(importedFields));
    return builder.build();
}

TEST_FF("requireThatConfigCanBeSavedAndLoaded", DocumentDBConfig::SP(makeBaseConfigSnapshot()),
                                                Schema(makeHistorySchema()))
{

    DocumentDBConfig::SP fullCfg = addConfigsThatAreNotSavedToDisk(*f1);
    saveBaseConfigSnapshot(*fullCfg, f2, 20);
    DocumentDBConfig::SP esnap(makeEmptyConfigSnapshot());
    Schema::SP ehs;
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.loadConfig(*esnap, 20, esnap, ehs);
    }
    assertEqualSnapshot(*f1, *esnap);
    EXPECT_TRUE(f2 == *ehs);
}

TEST_FF("requireThatConfigCanBeSerializedAndDeserialized", DocumentDBConfig::SP(makeBaseConfigSnapshot()),
                                                           Schema(makeHistorySchema()))
{
    saveBaseConfigSnapshot(*f1, f2, 30);
    nbostream stream;
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.serializeConfig(30, stream);
    }
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.deserializeConfig(40, stream);
    }
    DocumentDBConfig::SP fsnap(makeEmptyConfigSnapshot());
    Schema::SP fhs;
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.loadConfig(*fsnap, 40, fsnap, fhs);
    }
    assertEqualSnapshot(*f1, *fsnap);
    EXPECT_TRUE(f2 == *fhs);
    EXPECT_EQUAL("dummy", fsnap->getDocTypeName());
}

TEST_FF("requireThatWipeHistoryCanBeSaved", DocumentDBConfig::SP(makeBaseConfigSnapshot()),
                                            Schema(makeHistorySchema()))
{
    saveBaseConfigSnapshot(*f1, f2, 50);
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.saveWipeHistoryConfig(60, 0);
    }
    DocumentDBConfig::SP gsnap(makeEmptyConfigSnapshot());
    Schema::SP ghs;
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.loadConfig(*gsnap, 60, gsnap, ghs);
    }
    assertEqualSnapshot(*f1, *gsnap);
    EXPECT_TRUE(f2 != *ghs);
    EXPECT_TRUE(!f2.empty());
    EXPECT_TRUE(ghs->empty());
}


TEST("require that wipe history clears only portions of history")
{
    FileConfigManager cm("out2", myId, "dummy");
    Schema::SP schema(getSchema(0));
    Schema::SP history(new Schema);
    DocumentDBConfig::SP config(getConfig(5, schema));
    cm.saveConfig(*config, *history, 5);
    Schema::SP oldSchema(schema);
    schema = getSchema(1);
    config = getConfig(6, schema);
    history = SchemaUtil::makeHistorySchema(*schema, *oldSchema, *history,
                                            100);
    cm.saveConfig(*config, *history, 10);
    oldSchema = schema;
    schema = getSchema(2);
    config = getConfig(7, schema);
    history = SchemaUtil::makeHistorySchema(*schema, *oldSchema, *history,
                                            200);
    cm.saveConfig(*config, *history, 15);
    cm.saveWipeHistoryConfig(20, 50);
    cm.saveWipeHistoryConfig(25, 100);
    cm.saveWipeHistoryConfig(30, 150);
    cm.saveWipeHistoryConfig(35, 200);
    cm.saveWipeHistoryConfig(40, 250);
    DocumentDBConfig::SP oldconfig(config);
    cm.loadConfig(*oldconfig, 20, config, history);
    EXPECT_EQUAL(2u, history->getNumIndexFields());
    oldconfig = config;
    cm.loadConfig(*oldconfig, 25, config, history);
    EXPECT_EQUAL(2u, history->getNumIndexFields());
    oldconfig = config;
    cm.loadConfig(*oldconfig, 30, config, history);
    EXPECT_EQUAL(1u, history->getNumIndexFields());
    oldconfig = config;
    cm.loadConfig(*oldconfig, 35, config, history);
    EXPECT_EQUAL(1u, history->getNumIndexFields());
    oldconfig = config;
    cm.loadConfig(*oldconfig, 40, config, history);
    EXPECT_EQUAL(0u, history->getNumIndexFields());
}

TEST_FF("requireThatConfigCanBeLoadedWithoutExtraConfigsDataFile", DocumentDBConfig::SP(makeBaseConfigSnapshot()),
                                                                   Schema(makeHistorySchema()))
{
    saveBaseConfigSnapshot(*f1, f2, 70);
    EXPECT_TRUE(vespalib::unlink("out/config-70/extraconfigs.dat"));
    DocumentDBConfig::SP esnap(makeEmptyConfigSnapshot());
    Schema::SP ehs;
    {
        FileConfigManager cm("out", myId, "dummy");
        cm.loadConfig(*esnap, 70, esnap, ehs);
    }
    EXPECT_EQUAL(0u, esnap->getExtraConfigs().size());
}


TEST_FF("requireThatVisibilityDelayIsPropagated",
        DocumentDBConfig::SP(makeBaseConfigSnapshot()),
        Schema(makeHistorySchema()))
{
    saveBaseConfigSnapshot(*f1, f2, 80);
    DocumentDBConfig::SP esnap(makeEmptyConfigSnapshot());
    Schema::SP ehs;
    {
        ProtonConfigBuilder protonConfigBuilder;
        ProtonConfigBuilder::Documentdb ddb;
        ddb.inputdoctypename = "dummy";
        ddb.visibilitydelay = 61.0;
        protonConfigBuilder.documentdb.push_back(ddb);
        protonConfigBuilder.maxvisibilitydelay = 100.0;
        FileConfigManager cm("out", myId, "dummy");
        using ProtonConfigSP = BootstrapConfig::ProtonConfigSP;
        cm.setProtonConfig(
                ProtonConfigSP(new ProtonConfig(protonConfigBuilder)));
        cm.loadConfig(*esnap, 70, esnap, ehs);
    }
    EXPECT_EQUAL(0u, esnap->getExtraConfigs().size());
    EXPECT_EQUAL(61.0, esnap->getMaintenanceConfigSP()->getVisibilityDelay().sec());
}



TEST_MAIN() { TEST_RUN_ALL(); }