CROSS-REFERENCE TO RELATED APPLICATIONS

This application claims priority to Chinese Patent Application No. 201811123463.1, filed with the China National Intellectual Property Administration (CNIPA) on Sep. 26, 2018, the content of which is incorporated herein by reference in its entirety.

TECHNICAL FIELD

Embodiments of the present disclosure relate to the field of computer technology, and specifically to a method and apparatus for outputting information.

BACKGROUND

A relational database typically provides storage services using a cluster mode of a master-slave architecture. Since write requests for businesses are all sent to a master database, once the master database fails, the Recovery Time Objective (RTO) of the services is affected and a loss to the business is directly caused. A failure perception, as a prerequisite for a high availability of a service, needs to meet diverse and complex actual environments. A misinterpretation or a missed interpretation occurs easily in the failure perception, which will affect the Recovery Time Objective (RTO) of the service. Accordingly, a quick, accurate and automatic perception on the failure of the master database is in demand.

Traditional approaches to determine whether the master database is alive includes: (1) Whether the database is alive is determined through a check on whether the port is alive. In this case, in the situations such as that a machine is suspended, an instance of the database is suspended, or a disk fails, the port is alive and an actual failure is difficult to be determined. (2) Whether the database is alive is determined by accessing the database or sending to the database a request. By monitoring similar semantics, this determination is more accurate, but the database is easily misinterpreted as abnormal in the situation where the database is under a large pressure. These two determination approaches are relatively simple, but are difficult to be applied to actually complicated application scenarios.

SUMMARY

Embodiments of the present disclosure propose a method and apparatus for outputting information.

In a first aspect, the embodiments of the present disclosure provide a method for outputting information. The method includes: connecting a database, in response to receiving a request for detecting the database; performing a write operation on a heartbeat table in the database, in response to detecting the connection with the database being normal; performing a read operation on the heartbeat table in the database, in response to detecting the write operation for the heartbeat table being abnormal; performing on the database an operation of writing a file to a hard disk, in response to detecting the read operation on the heartbeat table being normal; and outputting first information for representing a cause of a database failure, in response to detecting the operation of writing the file to the hard disk being abnormal.

In some embodiments, the method further includes: determining, in response to detecting the connection with the database being abnormal, whether a number of connections with the database is greater than a predetermined threshold; performing on the database the operation of writing the file to the hard disk in response to the number of connections being greater than the predetermined threshold; and outputting the first information, in response to detecting the operation of writing the file to the hard disk being abnormal.

In some embodiments, the method further includes: outputting second information for representing a cause of the database failure in response to the number of connections being less than the predetermined threshold.

In some embodiments, the method further includes: outputting, in response to detecting the read operation on the heartbeat table being abnormal, third information for representing a cause of the database failure.

In some embodiments, the method further includes: acquiring master-slave synchronization status information of at least one slave database connecting with the database; and outputting fourth information for representing a cause of the database failure, in response to master-slave connections of slave databases exceeding a first predetermined ratio in the at least one database being interrupted being determined based on the master-slave synchronization status information.

In some embodiments, the outputting fourth information for representing a cause of the database failure, in response to master-slave connections of slave databases exceeding a first predetermined ratio in the at least one database being interrupted being determined based on the master-slave synchronization status information includes: sending, in response to master-slave connections of some slave databases in the at least one database being uninterrupted being determined based on the master-slave synchronization status information, a request for re-establishing a connection to a slave database having an uninterrupted master-slave connection in the at least one slave database; and outputting the fourth information, in response to detecting that abnormal re-established connections in the slave databases having the uninterrupted master-slave connections exceeds a second predetermined ratio.

In some embodiments, the method further includes: acquiring, in response to detecting the database failure, switching time of a previous database switching operation and a surplus capacity of a candidate database; and performing a database switching operation, in response to determining a difference between current time and the switching time being greater than or equal to a predetermined time interval threshold and the surplus capacity of the candidate database being greater than or equal to a used capacity of the database.

In some embodiments, the performing a database switching operation includes: sending switching indication information to an administrator terminal of the database; and switching, in response to receiving switching confirmation information fed back by the administrator terminal, data in the database to the candidate database.

In a second aspect, the embodiments of the present disclosure provide an apparatus for outputting information. The apparatus includes: a connecting unit, configured to connect a database, in response to receiving a request for detecting the database; a heartbeat writing unit, configured to perform a write operation on a heartbeat table in the database, in response to detecting the connection with the database being normal; a heartbeat reading unit, configured to perform a read operation on the heartbeat table in the database, in response to detecting the write operation for the heartbeat table being abnormal; a file writing unit, configured to perform on the database an operation of writing a file to a hard disk, in response to detecting the read operation for the heartbeat table being normal; and a first detecting unit, configured to output first information for representing a cause of a database failure, in response to detecting the operation of writing the file to the hard disk being abnormal.

In some embodiments, the apparatus further includes a second detecting unit. The second detecting unit is configured to: determine, in response to detecting the connection with the database being abnormal, whether a number of connections with the database is greater than a predetermined threshold; perform on the database the operation of writing the file to the hard disk in response to the number of connections being greater than the predetermined threshold; and output the first information, in response to detecting the operation of writing the file to the hard disk being abnormal.

In some embodiments, the second detecting unit is further configured to: output second information for representing a cause of the database failure in response to the number of connections being less than the predetermined threshold.

In some embodiments, the apparatus further includes a third detecting unit. The third detecting unit is configured to: output, in response to detecting the read operation on the heartbeat table being abnormal, third information for representing a cause of the database failure.

In some embodiments, the apparatus further includes a fourth detecting unit. The fourth detecting unit is configured to: acquire master-slave synchronization status information of at least one slave database connecting with the database; and output fourth information for representing a cause of the database failure, in response to master-slave connections of slave databases exceeding a first predetermined ratio in the at least one database being interrupted being determined based on the master-slave synchronization status information.

In some embodiments, the fourth detecting unit is further configured to: send, in response to master-slave connections of some slave databases in the at least one database being uninterrupted being determined based on the master-slave synchronization status information, a request for re-establishing a connection to a slave database having an uninterrupted master-slave connection in the at least one slave database; and output the fourth information, in response to detecting that abnormal re-established connections in the slave databases having the uninterrupted master-slave connections being abnormal exceeds a second predetermined ration.

In some embodiments, the apparatus further includes a switching unit. The switching unit is configured to: acquire, in response to detecting the database failure, switching time of a previous database switching operation and a surplus capacity of a candidate database; and perform a database switching operation, in response to determining a difference between current time and the switching time being greater than or equal to a predetermined time interval threshold and the surplus capacity of the candidate database being greater than or equal to a used capacity of the database.

In some embodiments, the switching unit is further configured to: send switching indication information to an administrator terminal of the database; and switch, in response to receiving switching confirmation information fed back by the administrator terminal, data in the database to the candidate database.

In a third aspect, the embodiments of the present disclosure provide an electronic device. The electronic device includes: one or more processors; and a storage device, configured to store one or more programs. The one or more programs, when executed by the one or more processors, cause the one or more processors to implement the method in any of the embodiments in the first aspect.

In a fourth aspect, the embodiments of the present disclosure provide a computer readable medium storing a computer program. The program, when executed by a processor, implements the method in any of the embodiments in the first aspect.

According to the method and apparatus for outputting information provided by the embodiments of the present disclosure, whether the database failure occurs is detected by performing operations such as a connection operation, a read or write operation, a hard disk operation. Therefore, various kinds of operational information of the database is effectively utilized, thus improving the accuracy of the detection on the database failure.

