1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-08-08 14:22:09 +03:00

MCOL-892 - changed from using the flock command

This commit is contained in:
david hill
2017-08-28 13:40:19 -05:00
parent 6f66b0e0eb
commit 7e15f1c296
4 changed files with 291 additions and 186 deletions

View File

@@ -1,4 +1,4 @@
COLUMNSTORE_VERSION_MAJOR=1 COLUMNSTORE_VERSION_MAJOR=1
COLUMNSTORE_VERSION_MINOR=0 COLUMNSTORE_VERSION_MINOR=0
COLUMNSTORE_VERSION_PATCH=11 COLUMNSTORE_VERSION_PATCH=11
COLUMNSTORE_VERSION_RELEASE=1 COLUMNSTORE_VERSION_RELEASE=2

View File

@@ -2523,6 +2523,9 @@ namespace oam
string Section = "AlarmConfig"; string Section = "AlarmConfig";
int returnValue; int returnValue;
struct flock fl;
int fd;
// validate Alarm ID // validate Alarm ID
if( alarmid > MAX_ALARM_ID ) if( alarmid > MAX_ALARM_ID )
@@ -2544,11 +2547,24 @@ namespace oam
string fileName = AlarmConfigFile; string fileName = AlarmConfigFile;
int fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0644); memset(&fl, 0, sizeof(fl));
fl.l_type = F_RDLCK; // read lock
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; //lock whole file
// Aquire an exclusive lock // open config file
if (flock(fd,LOCK_EX) == -1) { if ((fd = open(fileName.c_str(), O_RDWR)) >= 0)
throw runtime_error ("Lock file error: " + fileName); { // lock file
if (fcntl(fd, F_SETLKW, &fl) != 0)
{
ostringstream oss;
oss << "Oam::setAlarmConfig: error locking file " <<
fileName <<
": " <<
strerror(errno) <<
", proceding anyway.";
cerr << oss.str() << endl;
} }
// write parameter to disk // write parameter to disk
@@ -2561,24 +2577,25 @@ namespace oam
alaConfig->write(); alaConfig->write();
} }
catch(...) catch(...)
{ {}
// Release lock
if (flock(fd,LOCK_UN)==-1)
{
throw runtime_error ("Release lock file error: " + fileName);
}
exceptionControl("setAlarmConfig", API_FAILURE); fl.l_type = F_UNLCK; //unlock
} fcntl(fd, F_SETLK, &fl);
// Release lock
if (flock(fd,LOCK_UN)==-1)
{
throw runtime_error ("Release lock file error: " + fileName);
}
close(fd); close(fd);
} }
else
{
ostringstream oss;
oss << "Oam::setAlarmConfig: error opening file " <<
fileName <<
": " <<
strerror(errno);
throw runtime_error(oss.str());
}
return;
}
/******************************************************************** /********************************************************************
* *

View File

@@ -97,6 +97,9 @@ ALARMManager::~ALARMManager()
*****************************************************************************************/ *****************************************************************************************/
void rewriteActiveLog (const AlarmList& alarmList) void rewriteActiveLog (const AlarmList& alarmList)
{ {
struct flock fl;
int fd;
if (ALARM_DEBUG) { if (ALARM_DEBUG) {
LoggingID lid(11); LoggingID lid(11);
MessageLog ml(lid); MessageLog ml(lid);
@@ -110,12 +113,24 @@ void rewriteActiveLog (const AlarmList& alarmList)
// delete the old file // delete the old file
unlink (ACTIVE_ALARM_FILE.c_str()); unlink (ACTIVE_ALARM_FILE.c_str());
// create new file memset(&fl, 0, sizeof(fl));
int fd = open(ACTIVE_ALARM_FILE.c_str(), O_RDWR|O_CREAT, 0644); fl.l_type = F_RDLCK; // read lock
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; //lock whole file
// Aquire an exclusive lock // create new file
if (flock(fd,LOCK_EX) == -1) { if ((fd = open(ACTIVE_ALARM_FILE.c_str(), O_RDWR|O_CREAT, 0664)) >= 0)
throw runtime_error ("Lock active alarm log file error"); { // lock file
if (fcntl(fd, F_SETLKW, &fl) != 0)
{
ostringstream oss;
oss << "rewriteActiveLog: error locking file " <<
ACTIVE_ALARM_FILE <<
": " <<
strerror(errno) <<
", proceding anyway.";
cerr << oss.str() << endl;
} }
ofstream activeAlarmFile (ACTIVE_ALARM_FILE.c_str()); ofstream activeAlarmFile (ACTIVE_ALARM_FILE.c_str());
@@ -128,13 +143,23 @@ void rewriteActiveLog (const AlarmList& alarmList)
activeAlarmFile.close(); activeAlarmFile.close();
// Release lock fl.l_type = F_UNLCK; //unlock
if (flock(fd,LOCK_UN)==-1) fcntl(fd, F_SETLK, &fl);
{
throw runtime_error ("Release lock active alarm log file error");
}
close(fd); close(fd);
} }
else
{
ostringstream oss;
oss << "rewriteActiveLog: error opening file " <<
ACTIVE_ALARM_FILE <<
": " <<
strerror(errno);
throw runtime_error(oss.str());
}
return;
}
/***************************************************************************************** /*****************************************************************************************
* @brief logAlarm * @brief logAlarm
@@ -144,6 +169,9 @@ void rewriteActiveLog (const AlarmList& alarmList)
*****************************************************************************************/ *****************************************************************************************/
void logAlarm (const Alarm& calAlarm, const string& fileName) void logAlarm (const Alarm& calAlarm, const string& fileName)
{ {
struct flock fl;
int fd;
if (ALARM_DEBUG) { if (ALARM_DEBUG) {
LoggingID lid(11); LoggingID lid(11);
MessageLog ml(lid); MessageLog ml(lid);
@@ -154,25 +182,49 @@ void logAlarm (const Alarm& calAlarm, const string& fileName)
ml.logDebugMessage(msg); ml.logDebugMessage(msg);
} }
int fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0644); memset(&fl, 0, sizeof(fl));
ofstream AlarmFile (fileName.c_str(), ios::app); fl.l_type = F_RDLCK; // read lock
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; //lock whole file
// Aquire an exclusive lock // create new file
if (flock(fd,LOCK_EX) == -1) { if ((fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0664)) >= 0)
throw runtime_error ("Lock file error: " + fileName); { // lock file
if (fcntl(fd, F_SETLKW, &fl) != 0)
{
ostringstream oss;
oss << "logAlarm: error locking file " <<
fileName <<
": " <<
strerror(errno) <<
", proceding anyway.";
cerr << oss.str() << endl;
} }
ofstream AlarmFile (fileName.c_str(), ios::app);
AlarmFile << calAlarm; AlarmFile << calAlarm;
AlarmFile.close(); AlarmFile.close();
// Release lock fl.l_type = F_UNLCK; //unlock
if (flock(fd,LOCK_UN)==-1) fcntl(fd, F_SETLK, &fl);
{
throw runtime_error ("Release lock file error: " + fileName);
}
close(fd); close(fd);
} }
else
{
ostringstream oss;
oss << "logAlarm: error opening file " <<
fileName <<
": " <<
strerror(errno);
throw runtime_error(oss.str());
}
return;
}
/***************************************************************************************** /*****************************************************************************************
* @brief processAlarm * @brief processAlarm
@@ -323,7 +375,6 @@ void configAlarm (Alarm& calAlarm)
oam.setAlarmConfig (alarmID, "LastIssueTime", now); oam.setAlarmConfig (alarmID, "LastIssueTime", now);
oam.setAlarmConfig (alarmID, "Occurrences", 1); oam.setAlarmConfig (alarmID, "Occurrences", 1);
} }
else else
{ {
// increment counter and check the ctnThreshold // increment counter and check the ctnThreshold
@@ -475,30 +526,35 @@ void ALARMManager::sendAlarmReport (const char* componentID, int alarmID, int st
*****************************************************************************************/ *****************************************************************************************/
void ALARMManager::getActiveAlarm(AlarmList& alarmList) const void ALARMManager::getActiveAlarm(AlarmList& alarmList) const
{ {
struct flock fl;
int fd;
//add-on to fileName with mount name if on non Parent Module //add-on to fileName with mount name if on non Parent Module
Oam oam; Oam oam;
string fileName = ACTIVE_ALARM_FILE; string fileName = ACTIVE_ALARM_FILE;
int fd = open(fileName.c_str(),O_RDONLY); memset(&fl, 0, sizeof(fl));
fl.l_type = F_RDLCK; // read lock
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; //lock whole file
if (fd == -1) { // create new file
// file may being deleted temporarily by trapHandler if (fd = open(fileName.c_str(),O_RDONLY) >= 0)
sleep (1); { // lock file
fd = open(fileName.c_str(),O_RDONLY); if (fcntl(fd, F_SETLKW, &fl) != 0)
if (fd == -1) { {
// no active alarms, return ostringstream oss;
return; oss << "getActiveAlarm: error locking file " <<
} fileName <<
": " <<
strerror(errno) <<
", proceding anyway.";
cerr << oss.str() << endl;
} }
ifstream activeAlarm (fileName.c_str(), ios::in); ifstream activeAlarm (fileName.c_str(), ios::in);
// acquire read lock
if (flock(fd,LOCK_SH) == -1)
{
throw runtime_error ("Lock active alarm log file error");
}
Alarm alarm; Alarm alarm;
while (!activeAlarm.eof()) while (!activeAlarm.eof())
@@ -511,11 +567,8 @@ void ALARMManager::getActiveAlarm(AlarmList& alarmList) const
} }
activeAlarm.close(); activeAlarm.close();
// release lock fl.l_type = F_UNLCK; //unlock
if (flock(fd,LOCK_UN) == -1) fcntl(fd, F_SETLK, &fl);
{
throw runtime_error ("Release lock active alarm log file error");
}
close(fd); close(fd);
@@ -527,6 +580,8 @@ void ALARMManager::getActiveAlarm(AlarmList& alarmList) const
cout << i->second << endl; cout << i->second << endl;
} }
} }
}
return; return;
} }
@@ -540,6 +595,9 @@ void ALARMManager::getActiveAlarm(AlarmList& alarmList) const
*****************************************************************************************/ *****************************************************************************************/
void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const
{ {
struct flock fl;
int fd;
string alarmFile = "/tmp/alarms"; string alarmFile = "/tmp/alarms";
//make 1 alarm log file made up of archive and current alarm.log //make 1 alarm log file made up of archive and current alarm.log
@@ -568,20 +626,28 @@ void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const
cmd = "cat " + ALARM_FILE + " >> /tmp/alarms"; cmd = "cat " + ALARM_FILE + " >> /tmp/alarms";
(void)system(cmd.c_str()); (void)system(cmd.c_str());
int fd = open(alarmFile.c_str(),O_RDONLY); memset(&fl, 0, sizeof(fl));
fl.l_type = F_RDLCK; // read lock
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; //lock whole file
if (fd == -1) // create new file
// doesn't exist yet, return if (fd = open(alarmFile.c_str(),O_RDONLY) >= 0)
return; { // lock file
if (fcntl(fd, F_SETLKW, &fl) != 0)
{
ostringstream oss;
oss << "getAlarm: error locking file " <<
alarmFile <<
": " <<
strerror(errno) <<
", proceding anyway.";
cerr << oss.str() << endl;
}
ifstream hisAlarm (alarmFile.c_str(), ios::in); ifstream hisAlarm (alarmFile.c_str(), ios::in);
// acquire read lock
if (flock(fd,LOCK_SH) == -1)
{
throw runtime_error ("Lock alarm log file error");
}
//get mm / dd / yy from incoming date //get mm / dd / yy from incoming date
string mm = date.substr(0,2); string mm = date.substr(0,2);
string dd = date.substr(3,2); string dd = date.substr(3,2);
@@ -616,11 +682,9 @@ void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const
hisAlarm.close(); hisAlarm.close();
unlink (alarmFile.c_str()); unlink (alarmFile.c_str());
// release lock fl.l_type = F_UNLCK; //unlock
if (flock(fd,LOCK_UN) == -1) fcntl(fd, F_SETLK, &fl);
{
throw runtime_error ("Release lock active alarm log file error");
}
if (ALARM_DEBUG) if (ALARM_DEBUG)
{ {
@@ -630,6 +694,11 @@ void ALARMManager::getAlarm(std::string date, AlarmList& alarmList) const
cout << i->second << endl; cout << i->second << endl;
} }
} }
close(fd);
}
return;
} }
} //namespace alarmmanager } //namespace alarmmanager

View File

@@ -2840,6 +2840,8 @@ int ProcessMonitor::updateLog(std::string action, std::string level)
{ {
MonitorLog log; MonitorLog log;
Oam oam; Oam oam;
struct flock fl;
int fd;
string fileName; string fileName;
oam.getSystemConfig("SystemLogConfigFile", fileName); oam.getSystemConfig("SystemLogConfigFile", fileName);
@@ -3080,30 +3082,47 @@ int ProcessMonitor::updateLog(std::string action, std::string level)
unlink (fileName.c_str()); unlink (fileName.c_str());
ofstream newFile (fileName.c_str()); ofstream newFile (fileName.c_str());
// create new file memset(&fl, 0, sizeof(fl));
int fd = open(fileName.c_str(),O_RDWR|O_CREAT, 0644); fl.l_type = F_RDLCK; // read lock
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0; //lock whole file
// Aquire an exclusive lock // create new file
if (flock(fd,LOCK_EX) == -1) { if ((fd = open(fileName.c_str(), O_RDWR|O_CREAT, 0644)) >= 0)
log.writeLog(__LINE__, "ERROR: file lock failure on " + fileName, LOG_TYPE_ERROR ); { // lock file
close(fd);
return -1; if (fcntl(fd, F_SETLKW, &fl) != 0)
{
ostringstream oss;
oss << "ProcessMonitor::updateLog: error locking file " <<
fileName <<
": " <<
strerror(errno) <<
", proceding anyway.";
cerr << oss.str() << endl;
} }
copy(lines.begin(), lines.end(), ostream_iterator<string>(newFile, "\n")); copy(lines.begin(), lines.end(), ostream_iterator<string>(newFile, "\n"));
newFile.close(); newFile.close();
// Release lock fl.l_type = F_UNLCK; //unlock
if (flock(fd,LOCK_UN) == -1) fcntl(fd, F_SETLK, &fl);
{
log.writeLog(__LINE__, "ERROR: file unlock failure on " + fileName, LOG_TYPE_ERROR );
close(fd);
return -1;
}
close(fd); close(fd);
oam.syslogAction("restart"); oam.syslogAction("restart");
} }
else
{
ostringstream oss;
oss << "ProcessMonitor::updateLog: error opening file " <<
fileName <<
": " <<
strerror(errno);
throw runtime_error(oss.str());
}
}
//update file priviledges //update file priviledges
changeModLog(); changeModLog();