summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/linearstore/journal/RecoveryManager.h
blob: e19f92e305768404b5e2b325ff85c6fdfb9468e8 (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
/*
 *
 * 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.
 *
 */

#ifndef QPID_LINEARSTORE_JOURNAL_RECOVERYSTATE_H_
#define QPID_LINEARSTORE_JOURNAL_RECOVERYSTATE_H_

#include <fstream>
#include <map>
#include "qpid/linearstore/journal/LinearFileController.h"
#include <stdint.h>
#include <vector>

struct file_hdr_t;
struct rec_hdr_t;

namespace qpid {
namespace linearstore {
namespace journal {

class data_tok;
class enq_map;
class EmptyFilePool;
class EmptyFilePoolManager;
class JournalLog;
class jrec;
class txn_map;

struct RecoveredRecordData_t {
    uint64_t recordId_;
    uint64_t fileId_;
    std::streampos fileOffset_;
    bool pendingTransaction_;
    RecoveredRecordData_t(const uint64_t rid, const uint64_t fid, const std::streampos foffs, bool ptxn);
};

struct RecoveredFileData_t {
    JournalFile* journalFilePtr_;
    uint32_t completedDblkCount_;
    RecoveredFileData_t(JournalFile* journalFilePtr, const uint32_t completedDblkCount);
};

bool recordIdListCompare(RecoveredRecordData_t a, RecoveredRecordData_t b);

class RecoveryManager
{
protected:
    // Types
    typedef std::vector<std::string> stringList_t;
    typedef stringList_t::const_iterator stringListConstItr_t;
    typedef std::map<uint64_t, RecoveredFileData_t*> fileNumberMap_t;
    typedef fileNumberMap_t::iterator fileNumberMapItr_t;
    typedef fileNumberMap_t::const_iterator fileNumberMapConstItr_t;
    typedef std::vector<RecoveredRecordData_t> recordIdList_t;
    typedef recordIdList_t::const_iterator recordIdListConstItr_t;

    // Location and identity
    const std::string journalDirectory_;
    const std::string queueName_;
    enq_map& enqueueMapRef_;
    txn_map& transactionMapRef_;
    JournalLog& journalLogRef_;

    // Initial journal analysis data
    fileNumberMap_t fileNumberMap_;             ///< File number - JournalFilePtr map
    stringList_t notNeededFilesList_;           ///< Files not needed and to be returned to EFP
    stringList_t uninitFileList_;               ///< File name of uninitialized journal files found during header analysis
    bool journalEmptyFlag_;                     ///< Journal data files empty
    std::streamoff firstRecordOffset_;          ///< First record offset in ffid
    std::streamoff endOffset_;                  ///< End offset (first byte past last record)
    uint64_t highestRecordId_;                  ///< Highest rid found
    uint64_t highestFileNumber_;                ///< Highest file number found
    bool lastFileFullFlag_;                     ///< Last file is full
    uint64_t initial_fid_;                      ///< File id where initial write after recovery will occur

    // State for recovery of individual enqueued records
    uint64_t currentSerial_;
    uint32_t efpFileSize_kib_;
    fileNumberMapConstItr_t currentJournalFileItr_;
    std::string currentFileName_;
    std::ifstream inFileStream_;
    recordIdList_t recordIdList_;
    recordIdListConstItr_t recordIdListConstItr_;

public:
    RecoveryManager(const std::string& journalDirectory,
                    const std::string& queuename,
                    enq_map& enqueueMapRef,
                    txn_map& transactionMapRef,
                    JournalLog& journalLogRef);
    virtual ~RecoveryManager();

    void analyzeJournals(const std::vector<std::string>* preparedTransactionListPtr,
                         EmptyFilePoolManager* emptyFilePoolManager,
                         EmptyFilePool** emptyFilePoolPtrPtr);
    std::streamoff getEndOffset() const;
    uint64_t getHighestFileNumber() const;
    uint64_t getHighestRecordId() const;
    bool isLastFileFull() const;
    bool readNextRemainingRecord(void** const dataPtrPtr,
                                 std::size_t& dataSize,
                                 void** const xidPtrPtr,
                                 std::size_t& xidSize,
                                 bool& transient,
                                 bool& external,
                                 data_tok* const dtokp,
                                 bool ignore_pending_txns);
    void setLinearFileControllerJournals(lfcAddJournalFileFn fnPtr,
                                         LinearFileController* lfcPtr);
    std::string toString(const std::string& jid);
    std::string toLog(const std::string& jid, const int indent);
protected:
    void analyzeJournalFileHeaders(efpIdentity_t& efpIdentity);
    void checkFileStreamOk(bool checkEof);
    void checkJournalAlignment(const uint64_t start_fid, const std::streampos recordPosition);
    bool decodeRecord(jrec& record,
                      std::size_t& cumulativeSizeRead,
                      ::rec_hdr_t& recordHeader,
                      const uint64_t start_fid,
                      const std::streampos recordOffset);
    std::string getCurrentFileName() const;
    uint64_t getCurrentFileNumber() const;
    bool getFile(const uint64_t fileNumber, bool jumpToFirstRecordOffsetFlag);
    bool getNextFile(bool jumpToFirstRecordOffsetFlag);
    bool getNextRecordHeader();
    void lastRecord(const uint64_t file_id, const std::streamoff endOffset);
    bool needNextFile();
    void prepareRecordList();
    bool readFileHeader();
    void readJournalData(char* target, const std::streamsize size);
    void removeEmptyFiles(EmptyFilePool* emptyFilePoolPtr);

    static void readJournalFileHeader(const std::string& journalFileName,
                                      ::file_hdr_t& fileHeaderRef,
                                      std::string& queueName);
};

}}}

#endif // QPID_LINEARSTORE_JOURNAL_RECOVERYSTATE_H_