Configuration.cpp 11 KB
Newer Older
1
/*
2
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2015-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
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
 */

#include "Configuration.hpp"
#include "common/exception/Errnum.hpp"

#include <algorithm>
#include <fstream>
#include <errno.h>


//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
cta::common::Configuration::Configuration(std::string fileName)
   : m_fileName(fileName),
  m_lastUpdateTime(0) {
  // create internal r/w lock
  int rc = pthread_rwlock_init(&m_lock, NULL);
  if (0 != rc) {
    cta::exception::Errnum e(rc);
    e.getMessage() << "CastorConfiguration constructor Failed"
      ": Failed to create internal r/w lock";
    throw e;
  }
}

//------------------------------------------------------------------------------
// copy constructor
//------------------------------------------------------------------------------
cta::common::Configuration::Configuration(
  const Configuration & other) :
  m_fileName(other.m_fileName), m_lastUpdateTime(other.m_lastUpdateTime),
  m_config(other.m_config) {
  // create a new internal r/w lock
  int rc = pthread_rwlock_init(&m_lock, NULL);
  if (0 != rc) {
    cta::exception::Errnum e(rc);
    e.getMessage() << "CastorConfiguration copy constructor failed"
      ": Failed to create a new internal r/w lock";
    throw e;
  }
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
cta::common::Configuration::~Configuration() {
  // relase read write lock
  pthread_rwlock_destroy(&m_lock);
}

//------------------------------------------------------------------------------
// assignment operator
//------------------------------------------------------------------------------
cta::common::Configuration &
  cta::common::Configuration::operator=(
    const cta::common::Configuration & other)
   {
  m_fileName = other.m_fileName;
  m_lastUpdateTime = other.m_lastUpdateTime;
  m_config = other.m_config;
  // create a new internal r/w lock
  int rc = pthread_rwlock_init(&m_lock, NULL);
  if (0 != rc) {
    cta::exception::Errnum e(rc);
    e.getMessage() << "Assignment operator of CastorConfiguration object failed"
      ": Failed to create a new internal r/w lock";
    throw e;
  }
  return *this;
}

//------------------------------------------------------------------------------
// getConfEntString
//------------------------------------------------------------------------------
const std::string& cta::common::Configuration::getConfEntString(
  const std::string &category, const std::string &key,
Victor Kotlyar's avatar
Victor Kotlyar committed
93
  const std::string &defaultValue, cta::log::Logger *const log) {
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  try {
    if (isStale()) {
      tryToRenewConfig();
    }
    // get read lock
    int rc = pthread_rwlock_rdlock(&m_lock);
    if (0 != rc) {
      cta::exception::Errnum e(rc);
      e.getMessage() << "Failed to get configuration entry " << category << ":"
                     << key << ": Failed to get read lock";
      throw e;
    }
    // get the entry
    std::map<std::string, ConfCategory>::const_iterator catIt = m_config.find(category);
    if (m_config.end() != catIt) {
      // get the entry
      ConfCategory::const_iterator entIt = catIt->second.find(key);
      if (catIt->second.end() != entIt) {
        // release the lock
        pthread_rwlock_unlock(&m_lock);
        if(NULL != log) {           
Victor Kotlyar's avatar
Victor Kotlyar committed
115
116
117
118
119
          std::list<cta::log::Param> params = {   
            cta::log::Param("category", category),
            cta::log::Param("key", key),
            cta::log::Param("value", entIt->second),
            cta::log::Param("source", m_fileName)};
120
          (*log)(log::INFO, "Configuration entry", params);
121
122
123
124
125
126
        }
        return entIt->second;
      }
    }
    // no entry found
    if(NULL != log) {
Victor Kotlyar's avatar
Victor Kotlyar committed
127
128
129
130
131
      std::list<cta::log::Param> params = {
        cta::log::Param("category", category),
        cta::log::Param("key", key),
        cta::log::Param("value", defaultValue),
        cta::log::Param("source", "DEFAULT")};
132
      (*log)(log::INFO, "Configuration entry", params);
133
134
135
    }
    // Unlock and return default
    pthread_rwlock_unlock(&m_lock);
136
  } catch (cta::exception::Exception &) {
137
138
139
140
    // exception caught : Unlock and return default
    pthread_rwlock_unlock(&m_lock);
    // log the exception
    if(NULL != log) {
Victor Kotlyar's avatar
Victor Kotlyar committed
141
142
143
144
145
      std::list<cta::log::Param> params = {
        cta::log::Param("category", category),
        cta::log::Param("key", key),
        cta::log::Param("value", defaultValue),
        cta::log::Param("source", "DEFAULT")};
146
      (*log)(log::INFO, "Configuration entry", params);
147
148
149
150
151
152
153
154
155
156
157
158
159
    }
  } catch (...) {
    // release the lock
    pthread_rwlock_unlock(&m_lock);
    throw;
  }
  return defaultValue;
}

//------------------------------------------------------------------------------
// getConfEntString
//------------------------------------------------------------------------------
const std::string& cta::common::Configuration::getConfEntString(
Victor Kotlyar's avatar
Victor Kotlyar committed
160
  const std::string &category, const std::string &key, cta::log::Logger *const log) {
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
  // check whether we need to reload the configuration
  if (isStale()) {
    tryToRenewConfig();
  }
  // get read lock
  int rc = pthread_rwlock_rdlock(&m_lock);
  if (0 != rc) {
    cta::exception::Errnum e(rc);
    e.getMessage() << "Failed to get configuration entry " << category << ":"
      << key << ": Failed to get read lock";
    throw e;
  }
  // get the entry
  try {
    std::map<std::string, ConfCategory>::const_iterator catIt =
      m_config.find(category);
    if (m_config.end() == catIt) {
      NoEntry e;
      e.getMessage() << "Failed to get configuration entry " << category << ":"
        << key << ": Failed to find " << category << " category";
      throw e;
    }
    // get the entry
    ConfCategory::const_iterator entIt = catIt->second.find(key);
    if (catIt->second.end() == entIt) {
      NoEntry e;
      e.getMessage() << "Failed to get configuration entry " << category << ":"
        << key << ": Failed to find " << key << " key";
      throw e;
    }

    if(NULL != log) {
Victor Kotlyar's avatar
Victor Kotlyar committed
193
194
195
196
197
      std::list<cta::log::Param> params = {
        cta::log::Param("category", category),
        cta::log::Param("key", key),
        cta::log::Param("value", entIt->second),
        cta::log::Param("source", m_fileName)};
198
      (*log)(log::INFO, "Configuration entry", params);
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    }

    // release the lock
    pthread_rwlock_unlock(&m_lock);
    return entIt->second;
  } catch (...) {
    // release the lock
    pthread_rwlock_unlock(&m_lock);
    throw;
  }
}

//------------------------------------------------------------------------------
// isStale
//------------------------------------------------------------------------------
bool cta::common::Configuration::isStale()
   {
  // get read lock
  int rc = pthread_rwlock_rdlock(&m_lock);
  if (0 != rc) {
    cta::exception::Errnum e(rc);
    e.getMessage() << "Failed to determine if CASTOR configuration is stale"
      ": Failed to get read lock";
    throw e;
  }
  try {
    // get the timeout
    int timeout = getTimeoutNolock();
    // release the lock
    pthread_rwlock_unlock(&m_lock);
    // return whether we should renew  
    return time(0) > m_lastUpdateTime + timeout;
  } catch (...) {
    // release the lock
    pthread_rwlock_unlock(&m_lock);
    throw;
  }   
}

//------------------------------------------------------------------------------
// tryToRenewConfig
//------------------------------------------------------------------------------
void cta::common::Configuration::tryToRenewConfig()
   {
  // we should probably renew. First take the write lock.
  int rc = pthread_rwlock_wrlock(&m_lock);
  if (0 != rc) {
    cta::exception::Errnum e(rc);
    e.getMessage() << "Failed to renew configuration cache"
      ": Failed to take write lock";
    throw e;
  }
  // now check that we should really renew, because someone may have done it
  // while we waited for the lock
  try {
    if (time(0) > m_lastUpdateTime + getTimeoutNolock()) {
      // now we should really renew
      renewConfigNolock();
    }
  } catch (...) {
    // release the lock
    pthread_rwlock_unlock(&m_lock);
    throw;
  }
  // release the lock
  pthread_rwlock_unlock(&m_lock);
  return;
}

//------------------------------------------------------------------------------
// getTimeoutNolock
//------------------------------------------------------------------------------
int cta::common::Configuration::getTimeoutNolock()
   {
  // start with the default (300s = 5mn)
  int timeout = 300;
  // get value from config
  std::map<std::string, ConfCategory>::const_iterator catIt =
    m_config.find("Config");
  if (m_config.end() != catIt) {
    ConfCategory::const_iterator entIt = catIt->second.find("ExpirationDelay");
    if (catIt->second.end() != entIt) {
      // parse the timeout into an integer
      timeout = atoi(entIt->second.c_str());
    }
  }
  // return timeout
  return timeout;
}

//------------------------------------------------------------------------------
// renewConfigNolock
//------------------------------------------------------------------------------
void cta::common::Configuration::renewConfigNolock()
   {
  // reset the config
  m_config.clear();

  // try to open the configuration file, throwing an exception if there is a
  // failure
  std::ifstream file(m_fileName.c_str());
  if(file.fail()) {
    cta::exception::Errnum ex(EIO);
    ex.getMessage() << __FUNCTION__ << " failed"
      ": Failed to open file"
      ": m_fileName=" << m_fileName;
    throw ex;
  }

  std::string line;
  while(std::getline(file, line)) {
    // get rid of potential tabs
    std::replace(line.begin(),line.end(),'\t',' ');
    // get the category
    std::istringstream sline(line);
    std::string category;
    if (!(sline >> category)) continue; // empty line
    if (category[0] == '#') continue;   // comment
    // get the key
    std::string key;
    if (!(sline >> key)) continue;      // no key on line
    if (key[0] == '#') continue;        // key commented
    // get and store value
    while (sline.get() == ' '){}; sline.unget(); // skip spaces
    std::string value;
    std::getline(sline, value, '#');
    value.erase(value.find_last_not_of(" \n\r\t")+1); // right trim
    m_config[category][key] = value;
  }
  m_lastUpdateTime = time(0);
}