summaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcorespi/index/indexwriteutilities.cpp
blob: 54d2cf6e1f59b4c775d78d069864935ddfb4e14a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "indexwriteutilities.h"
#include "indexdisklayout.h"
#include "indexreadutilities.h"
#include <vespa/searchlib/common/serialnumfileheadercontext.h>
#include <vespa/searchlib/index/schemautil.h>
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/vespalib/io/fileutil.h>
#include <vespa/vespalib/util/exceptions.h>
#include <filesystem>
#include <sstream>
#include <system_error>
#include <unistd.h>

#include <vespa/log/log.h>
LOG_SETUP(".searchcorespi.index.indexwriteutilities");


using search::FixedSourceSelector;
using search::TuneFileAttributes;
using search::common::FileHeaderContext;
using search::common::SerialNumFileHeaderContext;
using search::index::Schema;
using search::index::SchemaUtil;
using search::SerialNum;
using vespalib::IllegalStateException;
using vespalib::FileHeader;

namespace searchcorespi::index {

namespace {

SerialNum noSerialNumHigh = std::numeric_limits<SerialNum>::max();

}

void
IndexWriteUtilities::writeSerialNum(SerialNum serialNum,
                                    const vespalib::string &dir,
                                    const FileHeaderContext &fileHeaderContext)
{
    const vespalib::string fileName =
        IndexDiskLayout::getSerialNumFileName(dir);
    const vespalib::string tmpFileName = fileName + ".tmp";

    SerialNumFileHeaderContext snFileHeaderContext(fileHeaderContext, serialNum);
    Fast_BufferedFile file;
    file.WriteOpen(tmpFileName.c_str());
    FileHeader fileHeader;
    snFileHeaderContext.addTags(fileHeader, fileName);
    fileHeader.putTag(FileHeader::Tag(IndexDiskLayout::SerialNumTag, serialNum));
    bool ok = (fileHeader.writeFile(file) >= fileHeader.getSize());
    if ( ! ok) {
        LOG(error, "Unable to write file header '%s'", tmpFileName.c_str());
    }
    if ( ! file.Sync()) {
        ok = false;
        LOG(error, "Unable to fsync '%s'", tmpFileName.c_str());
    }
    if ( ! file.Close()) {
        ok = false;
        LOG(error, "Unable to close '%s'", tmpFileName.c_str());
    }
    vespalib::File::sync(dir);

    if (ok) {
        FastOS_File renameFile(tmpFileName.c_str());
        ok &= renameFile.Rename(fileName.c_str());
    }
    if (!ok) {
        std::ostringstream msg;
        msg << "Unable to write serial number to '" << dir << "'.";
        throw IllegalStateException(msg.str());
    }
    vespalib::File::sync(dir);
}

bool
IndexWriteUtilities::copySerialNumFile(const vespalib::string &sourceDir,
                                       const vespalib::string &destDir)
{
    vespalib::string source = IndexDiskLayout::getSerialNumFileName(sourceDir);
    vespalib::string dest = IndexDiskLayout::getSerialNumFileName(destDir);
    vespalib::string tmpDest = dest + ".tmp";
    std::error_code ec;

    std::filesystem::copy_file(std::filesystem::path(source), std::filesystem::path(tmpDest), ec);
    if (ec) {
        LOG(error, "Unable to copy file '%s'", source.c_str());
        return false;
    }
    FastOS_File file(tmpDest.c_str());
    if (!file.OpenReadWrite()) {
        LOG(error, "Unable to open '%s' for fsync", tmpDest.c_str());
        return false;
    }
    if (!file.Sync()) {
        LOG(error, "Unable to fsync '%s'", tmpDest.c_str());
        return false;
    }
    if (!file.Close()) {
        LOG(error, "Unable to close '%s'", tmpDest.c_str());
        return false;
    }
    vespalib::File::sync(destDir);
    if (!file.Rename(dest.c_str())) {
        LOG(error, "Unable to rename file '%s' to '%s'", tmpDest.c_str(), dest.c_str());
        return false;
    }
    vespalib::File::sync(destDir);
    return true;
}

void
IndexWriteUtilities::writeSourceSelector(FixedSourceSelector::SaveInfo &
                                         saveInfo,
                                         uint32_t sourceId,
                                         const TuneFileAttributes &
                                         tuneFileAttributes,
                                         const FileHeaderContext &
                                         fileHeaderContext,
                                         SerialNum serialNum)
{
    SerialNumFileHeaderContext snFileHeaderContext(fileHeaderContext,
                                                   serialNum);
    if (!saveInfo.save(tuneFileAttributes, snFileHeaderContext)) {
        std::ostringstream msg;
        msg << "Flush of sourceselector failed. Source id = " << sourceId;
        throw IllegalStateException(msg.str());
    }
}

void
IndexWriteUtilities::updateDiskIndexSchema(const vespalib::string &indexDir,
                                           const Schema &schema,
                                           SerialNum serialNum)
{
    vespalib::string schemaName = IndexDiskLayout::getSchemaFileName(indexDir);
    Schema oldSchema;
    if (!oldSchema.loadFromFile(schemaName)) {
        LOG(error, "Could not open schema '%s'",
            schemaName.c_str());
        return;
    }
    if (!SchemaUtil::validateSchema(oldSchema)) {
        LOG(error, "Could not validate schema loaded from '%s'",
            schemaName.c_str());
        return;
    }
    Schema::UP newSchema = Schema::intersect(oldSchema, schema);
    if (*newSchema == oldSchema) {
        return;
    }
    if (serialNum != noSerialNumHigh) {
        SerialNum oldSerial = IndexReadUtilities::readSerialNum(indexDir);
        if (oldSerial >= serialNum) {
            return;
        }
    }
    vespalib::string schemaTmpName = schemaName + ".tmp";
    vespalib::string schemaOrigName = schemaName + ".orig";
    vespalib::unlink(schemaTmpName);
    if (!newSchema->saveToFile(schemaTmpName)) {
        LOG(error, "Could not save schema to '%s'",
            schemaTmpName.c_str());
    }
    // XXX: FastOS layer violation
    FastOS_StatInfo statInfo;
    bool statres;
    statres = FastOS_File::Stat(schemaOrigName.c_str(), &statInfo);
    if (!statres) {
        if (statInfo._error != FastOS_StatInfo::FileNotFound) {
            LOG(error, "Failed to stat orig schema '%s': %s",
                schemaOrigName.c_str(),
                FastOS_File::getLastErrorString().c_str());
        }
        int linkres = ::link(schemaName.c_str(), schemaOrigName.c_str());
        if (linkres != 0) {
            LOG(error, "Could not link '%s' to '%s': %s",
                schemaOrigName.c_str(),
                schemaName.c_str(),
                FastOS_File::getLastErrorString().c_str());
        }
        vespalib::File::sync(indexDir);
    }
    // XXX: FastOS layer violation
    int renameres = ::rename(schemaTmpName.c_str(), schemaName.c_str());
    if (renameres != 0) {
        int error = errno;
        std::string errString = FastOS_File::getErrorString(error);
        LOG(error, "Could not rename '%s' to '%s': %s",
            schemaTmpName.c_str(),
            schemaName.c_str(),
            errString.c_str());
    }
    vespalib::File::sync(indexDir);
}

}