1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /*****************************************************************************
19  * $Id: blockresolutionmanager.cpp 1910 2013-06-18 15:19:15Z rdempsey $
20  *
21  ****************************************************************************/
22 
23 #include <iostream>
24 #include <sys/types.h>
25 #include <vector>
26 #ifdef __linux__
27 #include <values.h>
28 #endif
29 #include <limits>
30 #include <sys/stat.h>
31 
32 #include "brmtypes.h"
33 #include "rwlock.h"
34 #include "mastersegmenttable.h"
35 #include "extentmap.h"
36 #include "copylocks.h"
37 #include "vss.h"
38 #include "vbbm.h"
39 #include "exceptclasses.h"
40 #include "slavecomm.h"
41 #define BLOCKRESOLUTIONMANAGER_DLLEXPORT
42 #include "blockresolutionmanager.h"
43 #undef BLOCKRESOLUTIONMANAGER_DLLEXPORT
44 #include "IDBDataFile.h"
45 #include "IDBPolicy.h"
46 
47 using namespace idbdatafile;
48 using namespace logging;
49 using namespace std;
50 
51 namespace BRM
52 {
53 
BlockResolutionManager(bool ronly)54 BlockResolutionManager::BlockResolutionManager(bool ronly) throw()
55 {
56     if (ronly)
57     {
58         em.setReadOnly();
59         vss.setReadOnly();
60         vbbm.setReadOnly();
61         copylocks.setReadOnly();
62     }
63 }
64 
BlockResolutionManager(const BlockResolutionManager & brm)65 BlockResolutionManager::BlockResolutionManager(const BlockResolutionManager& brm)
66 {
67     throw logic_error("BRM: Don't use the copy constructor.");
68 }
69 
~BlockResolutionManager()70 BlockResolutionManager::~BlockResolutionManager() throw()
71 {
72 }
73 
operator =(const BlockResolutionManager & brm)74 BlockResolutionManager& BlockResolutionManager::operator=(const BlockResolutionManager& brm)
75 {
76     throw logic_error("BRM: Don't use the = operator.");
77 }
78 
loadExtentMap(const string & filename,bool fixFL)79 int BlockResolutionManager::loadExtentMap(const string& filename, bool fixFL)
80 {
81     em.load(filename, fixFL);
82     return 0;
83 }
84 
saveExtentMap(const string & filename)85 int BlockResolutionManager::saveExtentMap(const string& filename)
86 {
87     em.save(filename);
88     return 0;
89 }
90 
saveState(string filename)91 int BlockResolutionManager::saveState(string filename) throw()
92 {
93     string emFilename = filename + "_em";
94     string vssFilename = filename + "_vss";
95     string vbbmFilename = filename + "_vbbm";
96     string journalFilename = filename + "_journal";
97 
98     bool locked[2] = { false, false };
99 
100     try
101     {
102         vbbm.lock(VBBM::READ);
103         locked[0] = true;
104         vss.lock(VSS::READ);
105         locked[1] = true;
106 
107         saveExtentMap(emFilename);
108 
109         // truncate teh file if already exists since no truncate in HDFS.
110         const char* filename = journalFilename.c_str();
111 
112         IDBDataFile* journal = IDBDataFile::open(
113                                    IDBPolicy::getType(filename, IDBPolicy::WRITEENG), filename, "wb", 0);
114         delete journal;
115 
116         vbbm.save(vbbmFilename);
117         vss.save(vssFilename);
118 
119         vss.release(VSS::READ);
120         locked[1] = false;
121         vbbm.release(VBBM::READ);
122         locked[0] = false;
123     }
124     catch (exception& e)
125     {
126         if (locked[1])
127             vss.release(VSS::READ);
128 
129         if (locked[0])
130             vbbm.release(VBBM::READ);
131 
132         cout << e.what() << endl;
133         return -1;
134     }
135 
136     return 0;
137 }
138 
loadState(string filename,bool fixFL)139 int BlockResolutionManager::loadState(string filename, bool fixFL) throw()
140 {
141     string emFilename = filename + "_em";
142     string vssFilename = filename + "_vss";
143     string vbbmFilename = filename + "_vbbm";
144     bool locked[2] = { false, false};
145 
146     try
147     {
148         vbbm.lock(VBBM::WRITE);
149         locked[0] = true;
150         vss.lock(VSS::WRITE);
151         locked[1] = true;
152 
153         loadExtentMap(emFilename, fixFL);
154         vbbm.load(vbbmFilename);
155         vss.load(vssFilename);
156 
157         vss.release(VSS::WRITE);
158         locked[1] = false;
159         vbbm.release(VBBM::WRITE);
160         locked[0] = false;
161     }
162     catch (exception& e)
163     {
164         if (locked[1])
165             vss.release(VSS::WRITE);
166 
167         if (locked[0])
168             vbbm.release(VBBM::WRITE);
169 
170         cout << e.what() << endl;
171         return -1;
172     }
173 
174     return 0;
175 }
176 
replayJournal(string prefix)177 int BlockResolutionManager::replayJournal(string prefix) throw()
178 {
179     SlaveComm sc;
180     int err = -1;
181 
182     try
183     {
184         err = sc.replayJournal(prefix);
185     }
186     catch (exception& e)
187     {
188         cout << e.what();
189     }
190 
191     return err;
192 }
193 
194 
195 }   //namespace
196 
197