rpl_reporting.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program 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, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef RPL_REPORTING_H
  19. #define RPL_REPORTING_H
  20. #include <stdarg.h>
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <time.h>
  24. #include "my_compiler.h"
  25. #include "my_inttypes.h"
  26. #include "my_loglevel.h"
  27. #include "my_systime.h" //my_getsystime
  28. #include "mysql/components/services/mysql_mutex_bits.h"
  29. #include "mysql/psi/mysql_mutex.h"
  30. /**
  31. Maximum size of an error message from a slave thread.
  32. */
  33. #define MAX_SLAVE_ERRMSG 1024
  34. class THD;
  35. /**
  36. Mix-in to handle the message logging and reporting for relay log
  37. info and master log info structures.
  38. By inheriting from this class, the class is imbued with
  39. capabilities to do slave reporting.
  40. */
  41. class Slave_reporting_capability {
  42. public:
  43. /** lock used to synchronize m_last_error on 'SHOW SLAVE STATUS' **/
  44. mutable mysql_mutex_t err_lock;
  45. /**
  46. Constructor.
  47. @param thread_name Printable name of the slave thread that is reporting.
  48. */
  49. Slave_reporting_capability(char const *thread_name);
  50. /**
  51. Writes a message and, if it's an error message, to Last_Error
  52. (which will be displayed by SHOW SLAVE STATUS).
  53. @param level The severity level
  54. @param err_code The error code
  55. @param msg The message (usually related to the error
  56. code, but can contain more information), in
  57. printf() format.
  58. */
  59. virtual void report(loglevel level, int err_code, const char *msg, ...) const
  60. MY_ATTRIBUTE((format(printf, 4, 5)));
  61. void va_report(loglevel level, int err_code, const char *prefix_msg,
  62. const char *msg, va_list v_args) const
  63. MY_ATTRIBUTE((format(printf, 5, 0)));
  64. /**
  65. Clear errors. They will not show up under <code>SHOW SLAVE
  66. STATUS</code>.
  67. */
  68. void clear_error() {
  69. mysql_mutex_lock(&err_lock);
  70. m_last_error.clear();
  71. mysql_mutex_unlock(&err_lock);
  72. }
  73. /**
  74. Check if the current error is of temporary nature or not.
  75. */
  76. int has_temporary_error(THD *thd, uint error_arg = 0,
  77. bool *silent = nullptr) const;
  78. /**
  79. Error information structure.
  80. */
  81. class Error {
  82. friend class Slave_reporting_capability;
  83. public:
  84. Error() { clear(); }
  85. void clear() {
  86. number = 0;
  87. message[0] = '\0';
  88. timestamp[0] = '\0';
  89. }
  90. void update_timestamp() {
  91. struct tm tm_tmp;
  92. struct tm *start;
  93. time_t tt_tmp;
  94. skr = my_getsystime() / 10;
  95. tt_tmp = skr / 1000000;
  96. localtime_r(&tt_tmp, &tm_tmp);
  97. start = &tm_tmp;
  98. snprintf(timestamp, sizeof(timestamp), "%02d%02d%02d %02d:%02d:%02d",
  99. start->tm_year % 100, start->tm_mon + 1, start->tm_mday,
  100. start->tm_hour, start->tm_min, start->tm_sec);
  101. timestamp[15] = '\0';
  102. }
  103. /** Error code */
  104. uint32 number;
  105. /** Error message */
  106. char message[MAX_SLAVE_ERRMSG];
  107. /** Error timestamp as string */
  108. char timestamp[64];
  109. /** Error timestamp in microseconds. Used in performance_schema */
  110. ulonglong skr;
  111. };
  112. Error const &last_error() const { return m_last_error; }
  113. bool is_error() const { return last_error().number != 0; }
  114. /*
  115. For MSR, there is a need to introduce error messages per channel.
  116. Instead of changing the error messages in share/errmsg-utf8.txt to
  117. introduce the clause, FOR CHANNEL "%s", we construct a string like this.
  118. There might be problem with a client applications which could print
  119. error messages and see no %s.
  120. @TODO: fix this.
  121. */
  122. virtual const char *get_for_channel_str(bool upper_case) const = 0;
  123. virtual ~Slave_reporting_capability() = 0;
  124. protected:
  125. virtual void do_report(loglevel level, int err_code, const char *msg,
  126. va_list v_args) const
  127. MY_ATTRIBUTE((format(printf, 4, 0)));
  128. /**
  129. Last error produced by the I/O or SQL thread respectively.
  130. */
  131. mutable Error m_last_error;
  132. private:
  133. char const *const m_thread_name;
  134. // not implemented
  135. Slave_reporting_capability(const Slave_reporting_capability &rhs);
  136. Slave_reporting_capability &operator=(const Slave_reporting_capability &rhs);
  137. };
  138. inline void Slave_reporting_capability::do_report(loglevel level, int err_code,
  139. const char *msg,
  140. va_list v_args) const {
  141. va_report(level, err_code, nullptr, msg, v_args);
  142. }
  143. #endif // RPL_REPORTING_H