0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StatusManager.cc
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 
29 #include "StatusManager.h"
30 
31 #include <Common/String.h>
32 
33 #include <cstring>
34 
35 using namespace Hypertable;
36 using namespace Hypertable::FsBroker::Lib;
37 using namespace std;
38 
39 namespace {
40  // Minimum elapsed time before transitioning back to OK status
41  const chrono::steady_clock::duration CLEAR_CHANGE_INTERVAL = chrono::milliseconds(60000);
42 }
43 
45  m_current_status = m_status.get();
46 }
47 
48 void StatusManager::set_read_status(Status::Code code, const std::string &text) {
49  if (code == Status::Code::OK)
50  clear_status();
51  else {
52  set_status(code, text);
53  m_last_read_error = chrono::steady_clock::now();
54  }
55 }
56 
57 void StatusManager::set_write_status(Status::Code code, const std::string &text) {
58  if (code == Status::Code::OK)
59  clear_status();
60  else {
61  set_status(code, text);
62  m_last_write_error = chrono::steady_clock::now();
63  }
64 }
65 
67  if (error == 0)
68  clear_status();
69  else {
70  set_error(error);
71  m_last_read_error = chrono::steady_clock::now();
72  }
73 }
74 
76  if (error == 0)
77  clear_status();
78  else {
79  set_error(error);
80  m_last_write_error = chrono::steady_clock::now();
81  }
82 }
83 
85  if (m_current_status == Status::Code::OK)
86  return;
87  auto now = chrono::steady_clock::now();
88  lock_guard<mutex> lock(m_mutex);
89  if (now - m_last_read_error > CLEAR_CHANGE_INTERVAL &&
90  now - m_last_write_error > CLEAR_CHANGE_INTERVAL) {
91  m_status.set(Status::Code::OK, "");
92  m_current_status = Status::Code::OK;
93  }
94 }
95 
96 void StatusManager::set_status(Status::Code code, const std::string &text) {
97  lock_guard<mutex> lock(m_mutex);
98  m_current_status = code;
99  m_status.set(m_current_status, text);
100 }
101 
102 void StatusManager::set_error(int error) {
103  char errtext[128];
104  errtext[0] = 0;
105  strerror_r(errno, errtext, 128);
106  lock_guard<mutex> lock(m_mutex);
107  m_current_status = Status::Code::CRITICAL;
108  m_status.set(m_current_status, errtext);
109 }
void set_status(Status::Code code, const std::string &text)
Sets status.
STL namespace.
Code
Enumeration for status codes.
Definition: Status.h:47
void set_write_status(Status::Code code, const std::string &text)
Sets write status.
void set_read_status(Status::Code code, const std::string &text)
Sets read status.
Compatibility Macros for C/C++.
void set_error(int error)
Sets status to CRITICAL with status text associated with errno.
void set_write_error(int error)
Sets status to CRITICAL with status text associated with errno.
void set_read_error(int error)
Sets status to CRITICAL with status text associated with errno.
Hypertable definitions
A String class based on std::string.
void clear_status()
Clears status by setting it to OK.
File system broker framework and client library.
Definition: Broker.h:44
Declarations for StatusManager.