summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/qpid/linearstore/jrnl/EmptyFilePool.cpp
blob: 8d9cf8ce43bdc729b717b30a35040baa071fc020 (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "EmptyFilePool.h"

#include <cctype>
#include <fstream>
#include "qpid/linearstore/jrnl/jcfg.h"
#include "qpid/linearstore/jrnl/jdir.h"
#include "qpid/linearstore/jrnl/JournalFile.h"
#include "qpid/linearstore/jrnl/slock.h"
#include "qpid/linearstore/jrnl/utils/file_hdr.h"
#include <sys/stat.h>
#include <uuid/uuid.h>
#include <vector>

#include <iostream> // DEBUG

namespace qpid {
namespace qls_jrnl {

EmptyFilePool::EmptyFilePool(const std::string& efpDirectory_,
                             const EmptyFilePoolPartition* partitionPtr_) :
                efpDirectory(efpDirectory_),
                efpDataSize_kib(fileSizeKbFromDirName(efpDirectory_, partitionPtr_->partitionNumber())),
                partitionPtr(partitionPtr_)
{}

EmptyFilePool::~EmptyFilePool() {}

void
EmptyFilePool::initialize() {
    //std::cout << "Reading " << efpDirectory << std::endl; // DEBUG
    std::vector<std::string> dirList;
    jdir::read_dir(efpDirectory, dirList, false, true, false, false);
    for (std::vector<std::string>::iterator i = dirList.begin(); i != dirList.end(); ++i) {
        size_t dotPos = i->rfind(".");
        if (dotPos != std::string::npos) {
            if (i->substr(dotPos).compare(".jrnl") == 0 && i->length() == 41) {
                std::string emptyFile(efpDirectory + "/" + (*i));
                if (validateEmptyFile(emptyFile)) {
                    pushEmptyFile(emptyFile);
                }
            }
        }
    }
    //std::cout << "Found " << emptyFileList.size() << " files" << std::endl; // DEBUG
}

efpDataSize_kib_t
EmptyFilePool::dataSize_kib() const {
    return efpDataSize_kib;
}

efpFileSize_kib_t
EmptyFilePool::fileSize_kib() const {
    return efpDataSize_kib + (QLS_JRNL_FHDR_RES_SIZE_SBLKS * JRNL_SBLK_SIZE_KIB);
}

efpDataSize_sblks_t
EmptyFilePool::dataSize_sblks() const {
    return efpDataSize_kib / JRNL_SBLK_SIZE_KIB;
}

efpFileSize_sblks_t
EmptyFilePool::fileSize_sblks() const {
    return (efpDataSize_kib / JRNL_SBLK_SIZE_KIB) + QLS_JRNL_FHDR_RES_SIZE_SBLKS;
}

efpFileCount_t
EmptyFilePool::numEmptyFiles() const {
    slock l(emptyFileListMutex);
    return efpFileCount_t(emptyFileList.size());
}

efpDataSize_kib_t
EmptyFilePool::cumFileSize_kib() const {
    slock l(emptyFileListMutex);
    return efpDataSize_kib_t(emptyFileList.size()) * efpDataSize_kib;
}

efpPartitionNumber_t
EmptyFilePool::getPartitionNumber() const {
    return partitionPtr->partitionNumber();
}

const EmptyFilePoolPartition*
EmptyFilePool::getPartition() const {
    return partitionPtr;
}

const efpIdentity_t
EmptyFilePool::getIdentity() const {
    return efpIdentity_t(partitionPtr->partitionNumber(), efpDataSize_kib);
}

std::string
EmptyFilePool::takeEmptyFile(const std::string& destDirectory) {
    std::string emptyFileName = popEmptyFile();
    std::string newFileName = destDirectory + emptyFileName.substr(emptyFileName.rfind('/')); // NOTE: substr() includes leading '/'
    if (::rename(emptyFileName.c_str(), newFileName.c_str())) {
        pushEmptyFile(emptyFileName);
        std::ostringstream oss;
        oss << "file=\"" << emptyFileName << "\" dest=\"" <<  newFileName << "\"" << FORMAT_SYSERR(errno);
        throw jexception(jerrno::JERR_JDIR_FMOVE, oss.str(), "EmptyFilePool", "takeEmptyFile");
    }
    return newFileName;
}

bool
EmptyFilePool::returnEmptyFile(const JournalFile* srcFile) {
    std::string emptyFileName(efpDirectory + srcFile->getFileName());
    // TODO: reset file here
    if (::rename(srcFile->getFqFileName().c_str(), emptyFileName.c_str())) {
        std::ostringstream oss;
        oss << "file=\"" << srcFile << "\" dest=\"" <<  emptyFileName << "\"" << FORMAT_SYSERR(errno);
        throw jexception(jerrno::JERR_JDIR_FMOVE, oss.str(), "EmptyFilePool", "returnEmptyFile");
    }
    pushEmptyFile(emptyFileName);
    return true;
}

// protected

void
EmptyFilePool::pushEmptyFile(const std::string fqFileName_) {
    slock l(emptyFileListMutex);
    emptyFileList.push_back(fqFileName_);
}

std::string
EmptyFilePool::popEmptyFile() {
    std::string emptyFileName;
    bool isEmpty = false;
    {
        slock l(emptyFileListMutex);
        isEmpty = emptyFileList.empty();
    }
    if (isEmpty) {
        createEmptyFile();
    }
    {
        slock l(emptyFileListMutex);
        emptyFileName = emptyFileList.front();
        emptyFileList.pop_front();
    }
    return emptyFileName;
}

void
EmptyFilePool::createEmptyFile() {
    ::file_hdr_t fh;
    ::file_hdr_create(&fh, QLS_FILE_MAGIC, QLS_JRNL_VERSION, QLS_JRNL_FHDR_RES_SIZE_SBLKS, partitionPtr->partitionNumber(), efpDataSize_kib);
    std::string efpfn = getEfpFileName();
    std::ofstream ofs(efpfn.c_str(), std::ofstream::out | std::ofstream::binary);
    if (ofs.good()) {
        ofs.write((char*)&fh, sizeof(::file_hdr_t));
        uint64_t rem = ((efpDataSize_kib + (QLS_JRNL_FHDR_RES_SIZE_SBLKS * JRNL_SBLK_SIZE_KIB)) * 1024) - sizeof(::file_hdr_t);
        while (rem--)
            ofs.put('\0');
        ofs.close();
        pushEmptyFile(efpfn);
        std::cout << "WARNING: EFP " << efpDirectory << " is empty - created new journal file " <<
                     efpfn.substr(efpfn.rfind('/') + 1) << " on the fly" << std::endl;
    } else {
        std::cerr << "ERROR: Unable to open file \"" << efpfn << "\"" << std::endl; // DEBUG
    }
}

bool
EmptyFilePool::validateEmptyFile(const std::string& emptyFileName_) const {
    struct stat s;
    if (::stat(emptyFileName_.c_str(), &s))
    {
        std::ostringstream oss;
        oss << "stat: file=\"" << emptyFileName_ << "\"" << FORMAT_SYSERR(errno);
        throw jexception(jerrno::JERR_JDIR_STAT, oss.str(), "EmptyFilePool", "validateEmptyFile");
    }
    efpDataSize_kib_t expectedSize = (JRNL_SBLK_SIZE_KIB + efpDataSize_kib) * 1024;
    if ((efpDataSize_kib_t)s.st_size != expectedSize) {
        //std::cout << "ERROR: File " << emptyFileName << ": Incorrect size: Expected=" << expectedSize << "; actual=" << s.st_size << std::endl; // DEBUG
        return false;
    }

    std::ifstream ifs(emptyFileName_.c_str(), std::ifstream::in | std::ifstream::binary);
    if (!ifs) {
        //std::cout << "ERROR: File " << emptyFileName << ": Unable to open for reading" << std::endl;
        return false;
    }

    const uint8_t fhFileNameBuffLen = 50;
    char fhFileNameBuff[fhFileNameBuffLen];
    ::file_hdr_t fh;
    ifs.read((char*)&fh, sizeof(::file_hdr_t));
    uint16_t fhFileNameLen = fh._queue_name_len > fhFileNameBuffLen ? fhFileNameBuffLen : fh._queue_name_len;
    ifs.read(fhFileNameBuff, fhFileNameLen);
    std::string fhFileName(fhFileNameBuff, fhFileNameLen);
    ifs.close();

    if (fh._rhdr._magic != QLS_FILE_MAGIC ||
        fh._rhdr._version != QLS_JRNL_VERSION ||
        fh._efp_partition != partitionPtr->partitionNumber() ||
        fh._file_size_kib != efpDataSize_kib ||
        !::is_file_hdr_reset(&fh))
    {
        //std::cout << "ERROR: File " << emptyFileName << ": Invalid file header" << std::endl;
        return false;
    }

    return true;
}

std::string
EmptyFilePool::getEfpFileName() {
    uuid_t uuid;
    ::uuid_generate(uuid); // NOTE: NOT THREAD SAFE
    char uuid_str[37]; // 36 char uuid + trailing \0
    ::uuid_unparse(uuid, uuid_str);
    std::ostringstream oss;
    oss << efpDirectory << "/" << uuid_str << QLS_JRNL_FILE_EXTENSION;
    return oss.str();
}

// protected
// static
efpDataSize_kib_t
EmptyFilePool::fileSizeKbFromDirName(const std::string& dirName_,
                                     const efpPartitionNumber_t partitionNumber_) {
    // Check for dirName format 'NNNk', where NNN is a number, convert NNN into an integer. NNN cannot be 0.
    std::string n(dirName_.substr(dirName_.rfind('/')+1));
    bool valid = true;
    for (uint16_t charNum = 0; charNum < n.length(); ++charNum) {
        if (charNum < n.length()-1) {
            if (!::isdigit((int)n[charNum])) {
                valid = false;
                break;
            }
        } else {
            valid = n[charNum] == 'k';
        }
    }
    efpDataSize_kib_t s = ::atol(n.c_str());
    if (!valid || s == 0 || s % JRNL_SBLK_SIZE_KIB != 0) {
        std::ostringstream oss;
        oss << "Partition: " << partitionNumber_ << "; EFP directory: \'" << n << "\'";
        throw jexception(jerrno::JERR_EFP_BADEFPDIRNAME, oss.str(), "EmptyFilePool", "fileSizeKbFromDirName");
    }
    return s;
}

}} // namespace qpid::qls_jrnl