Disclosure of Invention
The invention mainly aims to provide a session processing system and method based on a server load state, and aims to solve the technical problems that the prior art cannot effectively ensure that a server connected with a client can stably run, and session interruption caused by server downtime is avoided.
To achieve the above object, the present invention provides a session processing system based on server load status, the system comprising:
the client is used for acquiring a server to be connected corresponding to the session connection request when receiving the session connection request;
the client is also used for calling a preset thread to acquire comprehensive load information corresponding to the server to be connected from a server registration center and judging whether the server to be connected meets preset connection conditions or not according to the comprehensive load information;
the client is also used for selecting a target server address from a local pre-stored cluster server address list through a preset address list manager when the server to be connected does not meet the preset connection condition;
and the client is also used for determining a corresponding target server according to the target server address and establishing session connection with the target server.
Preferably, the integrated load information includes: system load, central processing unit utilization rate, memory occupancy rate and disk utilization rate;
and the client is also used for judging whether the server to be connected meets preset connection conditions according to the system load, the utilization rate of the central processing unit, the memory occupancy rate and the disk utilization rate.
Preferably, the client is further configured to calculate, according to the system load, the utilization rate of the central processing unit, the memory occupancy rate, and the disk utilization rate, a comprehensive load value corresponding to the server to be connected through a preset formula;
the client is further used for determining a current load state corresponding to the server to be connected according to the comprehensive load value and judging whether the server to be connected meets a preset connection condition or not according to the current load state;
wherein the preset formula is as follows:
CHSLoad=θload+θcpu+θmem+θi/o
wherein CHSLoad is the combined load value, θloadTo system load, θcpuFor central processor utilization, thetamemFor memory occupancy, θi/oThe disk utilization.
Preferably, the client is further configured to call the preset thread to connect to the server registration center, monitor the registered server in the server registration center to obtain the comprehensive load information corresponding to the registered server, and read the comprehensive load information corresponding to the server to be connected from the obtained comprehensive load information.
Preferably, the client is further configured to obtain, by the preset address list manager, a current server address list corresponding to the distributed cluster from the server registry;
the client is further used for detecting whether the current server address list is consistent with the cluster server address list, and if not, network delay information of a server corresponding to each server address in the current server address list is obtained;
the client is further configured to sequentially adjust the server addresses included in the current server address list according to the network delay information to obtain a server address list to be cached;
and the client is also used for caching the address list of the server to be cached as a new cluster server address list.
The preset address list manager is used for acquiring the address list length corresponding to a local pre-stored cluster server address list, detecting whether the address list length is smaller than the preset length, and if so, selecting an available server address carrying a preset mark from the local pre-stored cluster server address list;
preferably, the preset address list manager is further configured to verify whether a server corresponding to the available server address is in a high load state, and if not, select a target server address from the available server addresses.
In addition, to achieve the above object, the present invention further provides a session processing method based on a server load status, where the method includes:
when a session connection request is received, acquiring a server to be connected corresponding to the session connection request;
calling a preset thread to acquire comprehensive load information corresponding to the server to be connected from a server registration center, and judging whether the server to be connected meets preset connection conditions or not according to the comprehensive load information;
when the server to be connected does not meet the preset connection condition, selecting a target server address from a local pre-stored cluster server address list through a preset address list manager;
and determining a corresponding target server according to the target server address, and establishing session connection with the target server.
Preferably, the integrated load information includes: system load, central processing unit utilization rate, memory occupancy rate and disk utilization rate;
the step of judging whether the server to be connected meets the preset connection condition according to the comprehensive load information comprises the following steps:
and judging whether the server to be connected meets a preset connection condition or not according to the system load, the utilization rate of the central processing unit, the memory occupancy rate and the disk utilization rate.
Preferably, the step of determining whether the server to be connected satisfies a preset connection condition according to the system load, the utilization rate of the central processing unit, the memory occupancy rate, and the disk utilization rate includes:
calculating a comprehensive load value corresponding to the server to be connected through a preset formula according to the system load, the utilization rate of the central processing unit, the memory occupancy rate and the utilization rate of the disk;
determining a current load state corresponding to the server to be connected according to the comprehensive load value, and judging whether the server to be connected meets a preset connection condition or not according to the current load state;
wherein the preset formula is as follows:
CHSLoad=θload+θcpu+θmem+θi/o
wherein CHSLoad is the combined load value, θloadTo system load, θcpuFor central processor utilization, thetamemFor memory occupancy, θi/oThe disk utilization.
Preferably, the step of calling the preset thread to obtain the comprehensive load information corresponding to the server to be connected from a server registration center, and determining whether the server to be connected meets the preset connection condition according to the comprehensive load information includes:
calling the preset thread to connect the server registration center, and monitoring the registered server in the server registration center to acquire comprehensive load information corresponding to the registered server;
and reading the comprehensive load information corresponding to the server to be connected from the obtained comprehensive load information, and judging whether the server to be connected meets a preset connection condition according to the read comprehensive load information.
When a client receives a session connection request, a server to be connected corresponding to the session connection request is obtained; calling a preset thread to acquire comprehensive load information corresponding to the server to be connected from the server registration center, and judging whether the server to be connected meets preset connection conditions or not according to the comprehensive load information; if not, selecting a target server address from a local pre-stored cluster server address list through a preset address list manager; and then determining a corresponding target server according to the address of the target server, and establishing session connection with the target server. According to the method and the system, the servers to be connected are screened according to the comprehensive load information corresponding to the servers, and the target server is reselected from the local cluster server to establish session connection when the screened servers do not meet the connection condition, so that the screened target server can be ensured to stably provide session service for the client, and the occurrence of session interruption caused by the downtime of the server due to high load is avoided to a greater extent.
Detailed Description
It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
Referring to fig. 1, fig. 1 is a schematic structural diagram of a session processing system based on a server load status according to the present invention.
As shown in fig. 1, the session processing system based on the server load status may include: a client 1001, a preset address list manager 1002, and a server registry 1003. In this embodiment, the preset address list manager 1002 may obtain load information of each server in the ZooKeeper cluster and a network delay condition of each server from a cache. The server registration center 1003 may be implemented by using a ZooKeeper server, and mainly provides information storage for all service registrations, for the ZooKeeper client to call.
Those skilled in the art will appreciate that the architecture shown in fig. 1 does not constitute a limitation of the present embodiment of a server load state-based session processing system, and may include more or fewer components than those shown, or some components may be combined, or a different arrangement of components.
In this embodiment, the session processing system based on the server load status includes:
a client 1001, configured to obtain a to-be-connected server corresponding to a session connection request when receiving the session connection request;
it should be noted that the session connection request may be an instruction sent by the ZooKeeper client as the service requester to establish a session link with the target server. In this embodiment, the client 1001 needs to acquire the comprehensive load information of the servers to be connected from the server registry 1003, and then decide which server should be connected according to the comprehensive load information.
In a specific implementation, when receiving a session connection request triggered by a user, the client 1001 determines a server to be accessed according to request resource information included in a request header of the session connection request. It should be understood that, in a distributed cluster scenario, the request resource information is usually stored in a plurality of servers, that is, the number of the servers to be connected in this embodiment may be one or more.
The client 1001 is further configured to invoke a preset thread to obtain comprehensive load information corresponding to the server to be connected from the server registration center 1003, and determine whether the server to be connected meets a preset connection condition according to the comprehensive load information;
it should be understood that the server registry 1003 is used in the present embodiment to provide a storage and management service of registration information for the ZooKeeper server. When each ZooKeeper server registers in the server registration center 1003, a directory "/loadInfo" may be established in the ZooKeeper registration center, then when the ZooKeeper server is started, a pre-configured script program for acquiring hardware resource information of the ZooKeeper server is also started, and a temporary node directory is created under the "/loadInfo" directory, where the name of the node directory may be an Internet Protocol Address (IP Address) and a port number of the ZooKeeper server where the script program is located; and then saving the ZooKeeper server hardware resource information (including system load, central processing unit utilization rate, memory occupancy rate, disk utilization rate and the like) acquired by the script program to a local storage space. The load comprehensive information can be obtained by the ZooKeeper server through calculation according to the hardware resource information.
It should be noted that the preset thread may be a preset ZKLoadThread, and the thread is used to connect to the server registration center 1003, acquire child node information registered under the loadInfo, and monitor the loadInfo node, and once the loadInfo node changes, the ZKLoadThread may acquire the latest load information of the ZooKeeper server in time. Specifically, the client 1001 may call the preset ZKLoadThread to connect to the server registration center 1003, monitor a server registered in the server registration center 1003 to obtain the comprehensive load information corresponding to the registered server, and then read the comprehensive load information corresponding to the server to be connected from the obtained comprehensive load information.
In this embodiment, the manner of determining whether the server to be connected satisfies the preset connection condition may be to detect whether a parameter value corresponding to one of the pieces of information in the combined load information exceeds or does not exceed a preset value, or to detect whether a weighted average value corresponding to two or more pieces of information in the combined load information exceeds or does not exceed a preset value.
In a specific implementation, the client 1001 may call a preset ZKLoadThread to obtain, from the server registration center 1003, comprehensive load information corresponding to the server to be connected, and then determine whether the server to be connected satisfies the preset connection condition according to the comprehensive load information.
Further, considering that there are many load information affecting the operation rate of the server, in order to improve the session connection efficiency, the comprehensive load information in this embodiment mainly includes a system load, a central processing unit utilization rate, a memory occupancy rate, and a disk utilization rate. Correspondingly, in this embodiment, the client 1001 is further configured to determine whether the server to be connected meets a preset connection condition according to the system load, the utilization rate of the central processing unit, the memory occupancy rate, and the disk utilization rate.
The system load is defined as the average number of processes in the running queue in a specific time interval, and the system load comprises information which is not the utilization rate condition of the CPU but the statistical information of the sum of the number of processes which are processed by the CPU and wait for the CPU to process in a period of time, namely the statistical information of the length of the CPU utilization queue, and can be obtained by the proc/loadavg file. For example, the system load may be collected over 5 minutes and then the system load θ for a single server may be calculatedloadNumber of cores of server NumcpuCan be obtained by the command grep 'model name'/proc/cpuininfo | wc-l.
The calculation formula of the server system load in this embodiment is as follows:
Numcpu=grep'model name'/proc/cpuinfo|wc-l
where Lavg _ n, n ═ 5 is the average system load over 5 minutes.
Furthermore, the utilization rate of the central processing unit, namely the CPU utilization rate, can be obtained through a proc/stat file, and real-time CPU state information is stored in the proc/stat file. The CPU utilization rate is the ratio of the time slices in the busy stage to the total CPU time slices, which represents the running condition of the machine at a certain time point, and the higher the CPU utilization rate is, the server is busy, otherwise, the server is idle.
In this embodiment, the state data of the CPU is counted every 5 seconds, and the Total time slice Total2 and the free time slice Total1 of the CPU and the CPU utilization rate θ are calculated by the following formulacpuIn the following formula, the parameter code corresponding to the total time slice is "2" and the parameter code corresponding to the idle time slice is "1")
Total1=user1+nice1+system1+idle1+iowait1+irq1+softirq1+steal1+guest1
Total2=user2+nice2+system2+idle2+iowait2+irq2+softirq2+steal2+guest2
In the formula, USER is the time of the CPU in the USER mode, nice is the process execution time of the CPU in the USER mode at low priority, System is the time of the CPU in the core mode, Idle is the time of the System in the Idle process, the value is the value of the second field in the USER _ HZ multiplied by/proc/update file, Iowait is the time of the CPU in I/O waiting, Irq is the time overhead of the CPU in interrupting a service program, Softirq is the time of the CPU processing software interruption, Steal is the CPU time occupied by other operating systems in a virtual environment, and Guest is the time of the CPU virtually under the control of a kernel to access a client operating System.
In this embodiment, the memory occupancy rate θ ismemThe method can be obtained by reading a proc/meminfo file, and the main indexes involved in calculating the memory occupancy rate include: MenTotal, MemFree, Buffers and Cached, and the calculation formula of the memory occupancy rate is as follows:
in the formula, MemTotal represents the size of all available ram of the system, MemFree represents the unused memory of the system, Buffers represents the size used for buffering files, and Cached represents the size of the memory used by the cache.
Further, the disk utilization θ in this embodimenti/oThe utilization rate of the disk I/O is also called, and the utilization rate can be obtained through an iostat command under a terminal of a Linux system. Typically, the percentage of disk active time is expressed by% util, i.e., disk I/O utilization disk utilization is proportional to the degree of resource contention and inversely proportional to performance. If the disk utilization exceeds 70%, it takes longer for the read and write operations to complete, and when this occurs, most processes will be blocked or hibernated while waiting. The calculation formula of the disk I/O utilization rate in the embodiment is as follows: thetai/o=%util。
In a specific implementation, the client 1001 may determine whether the to-be-connected server meets a preset connection condition according to the obtained system load, the cpu utilization rate, the memory occupancy rate, and the disk utilization rate, and if the preset connection condition is met, directly use the to-be-connected server as a target server and establish a session connection with the target server, or select one server from the to-be-connected servers as the target server to establish the session connection.
The client 1001 is further configured to select a target server address from a locally pre-stored cluster server address list through a preset address list manager 1002 when the server to be connected does not satisfy the preset connection condition;
it should be noted that, in this embodiment, the preset address list manager 1002 may call a serveraddressethread to periodically obtain a ZooKeeper server address from the server registration center 1003, then compare the obtained server address with a server address in a locally pre-stored cluster server address list, if it is found that address list update is needed, sort the obtained server addresses according to network delay, and update the server addresses to the cluster server address list, so as to obtain a latest cluster server address list.
In a specific implementation, if the client 1001 detects that the server to be connected does not satisfy the preset connection condition, the preset address list manager 1002 may select a target server address from a local pre-stored cluster server address list. The selection manner of the target server address in this embodiment may be random selection, or may be selection by setting a screening policy (for example, selecting a server with the minimum network delay) according to an actual situation, which is not limited in this embodiment.
The client 1001 is further configured to determine a corresponding target server according to the target server address, and establish a session connection with the target server.
In a specific implementation, after the address of the target server is screened out, the client 1001 may determine the corresponding target server according to the address of the target server, and then establish a session connection with the target server, and the target server performs subsequent request response.
When receiving a session connection request, a client acquires a to-be-connected server corresponding to the session connection request; calling a preset thread to acquire comprehensive load information corresponding to the server to be connected from the server registration center, and judging whether the server to be connected meets preset connection conditions or not according to the comprehensive load information; if not, selecting a target server address from a local pre-stored cluster server address list through a preset address list manager; and then determining a corresponding target server according to the address of the target server, and establishing session connection with the target server. In the embodiment, the servers to be connected are screened according to the comprehensive load information corresponding to the servers, and when the screened servers do not meet the connection condition, the target servers are reselected from the local cluster servers to establish session connection, so that the screened target servers can be ensured to stably provide session service for the client, and the occurrence of session interruption caused by the downtime of the servers due to high load is avoided to a greater extent.
Based on the first embodiment of the session processing system based on the server load status, the second embodiment of the session processing system based on the server load status is proposed.
In this embodiment, the client 1001 is further configured to calculate, according to the system load, the utilization rate of the central processing unit, the memory occupancy rate, and the disk utilization rate, a comprehensive load value corresponding to the server to be connected through a preset formula; wherein the preset formula is as follows:
CHSLoad=θload+θcpu+θmem+θi/o
in the above formula, CHSLoad is the integrated load value, θloadTo system load, θcpuFor central processor utilization, thetamemFor memory occupancy, θi/oThe disk utilization.
In a specific implementation, the client 1001 may calculate, according to the obtained system load, the cpu utilization, the memory occupancy, and the disk utilization, a comprehensive load value corresponding to the server to be connected according to the preset formula.
The client 1001 is further configured to determine a current load state corresponding to the server to be connected according to the comprehensive load value, and determine whether the server to be connected meets a preset connection condition according to the current load state;
it should be noted that the current load state in this embodiment includes a low load, a normal load, and a high load. In this embodiment, the comprehensive load value CHSLoad and the system load θ corresponding to each load stateloadCentral processor utilization factor thetacpuMemory occupancy rate θmemAnd disk utilization factor thetai/oSpecifically, table 1 below shows the load state index evaluation table in table 1.
Table 1 load state index evaluation table
As can be seen from table 1 above, when the integrated load value of the server to be connected belongs to the range of [0, 2.40), the state is in the low load state, when the integrated load value of the server to be connected belongs to the range of [2.40, 3.45), the state is in the normal load state, and when the integrated load value of the server to be connected belongs to the range of [3.45, + ∞) the state is in the high load state.
The client side calculates a comprehensive load value corresponding to the server to be connected according to the system load, the utilization rate of the central processing unit, the memory occupancy rate and the utilization rate of the disk through a preset formula; and then determining the current load state corresponding to the server to be connected according to the comprehensive load value, and then judging whether the server to be connected meets the preset connection condition or not according to the current load state, so that whether servers with good load conditions except the server to be connected need to be selected to establish session connection or not can be determined, the selected target server can be ensured to run stably to the maximum extent, and the condition that the session is interrupted due to the downtime of the server can be avoided.
A third embodiment of the server load status based session processing system of the present invention is proposed based on the above-mentioned first embodiment of the server load status based session processing system.
In this embodiment, the client 1001 is further configured to obtain, from the server registry 1003, a current server address list corresponding to the distributed cluster through the preset address list manager;
it should be noted that, in this embodiment, the distributed cluster is a distributed server cluster in which a server registered in the server center 1003 is located. The current server address list is the server address list that is updated in the server center 1003 last time.
In a specific implementation, the client 1001 obtains a current server address list corresponding to the distributed cluster from the server registry 1003 through the preset address list manager.
The client 1001 is further configured to detect whether the current server address list is consistent with the cluster server address list, and if not, obtain network delay information of a server corresponding to each server address in the current server address list;
it should be noted that the network delay information generally refers to the round-trip time of sending a data packet from the user's computer to the web server and then immediately returning the data packet from the web server to the user's computer. In popular terms, it is the time taken for data to pass from the computer to the computer. The network latency described in this embodiment refers to the network latency between the client 1001 and the Zookeeper server. The client can send the same ping command to all servers in the current server address list and then record the sending time t of the ping command1And a receiving time t of receiving the ping command feedback result4Then, each server record is obtained and fed back to the receiving time t of the ping command received by the client2And a return time t when a feedback result is returned to the client when the ping command response is finished3Finally, the client end sets the network delay as (t) according to the formula2-t1)+(t4-t3) And then the current network delay of each server can be calculated.
It should be understood that, in this embodiment, if the client 1001 detects that the current server address list is consistent with the cluster server address list, it indicates that the cluster server address list locally stored by the client 1001 does not need to be updated, otherwise, the update is needed.
In a specific implementation, if the client 1001 detects that the current server address list is inconsistent with the cluster server address list, network delay information of a server corresponding to each server address in the current server address list is obtained.
The client 1001 is further configured to sequentially adjust the server addresses included in the current server address list according to the network delay information, so as to obtain a server address list to be cached;
it should be noted that, in this embodiment, the manner of sequentially adjusting the servers may be to perform size sorting on the network delays included in the network delay information in a sequence from small to large, and then sequentially adjust the server addresses included in the current server address list according to a sorting result, so as to obtain the server address list to be cached.
The client 1001 is further configured to cache the server address list to be cached as a new cluster server address list.
In a specific implementation, after obtaining the address list of the server to be cached, the client 1001 may cache the address list as a new cluster server address list.
The client side of the embodiment acquires a current server address list corresponding to the distributed cluster from the server registration center through a preset address list manager; detecting whether the current server address list is consistent with the cluster server address list or not, and if not, acquiring network delay information of a server corresponding to each server address in the current server address list; sequentially adjusting the server addresses contained in the current server address list according to the network delay information to obtain a server address list to be cached; the server address list to be cached is taken as a new cluster server address list for caching, so that the server address stored in the cluster server address list locally stored by the client can be ensured to be the latest server address, and the client can conveniently select the server address corresponding to the server with smaller network delay from the server address list.
Based on the first embodiment of the session processing system based on the server load status, a fourth embodiment of the session processing system based on the server load status is proposed.
In this embodiment, the preset address list manager 1002 is configured to obtain an address list length corresponding to a locally pre-stored cluster server address list, detect whether the address list length is smaller than a preset length, and select an available server address carrying a preset flag from the locally pre-stored cluster server address list if the address list length is smaller than the preset length;
it should be noted that in the ZooKeeper client session, the statehomeprodivider is adopted as the default to implement the homeprodivider interface, and the preset address list manager 1002 in this embodiment implements the homeprodivier interface through the dynami homeprodivider. The preset address list manager 1002 of this embodiment stores a pre-programmed hardware resource monitoring script program: DynamicHostProvider class. In the DynamicHostProvider class, the next (long) method is a core method of the class and is also a core control module for load balancing of the client. And the subscript corresponding to the preset mark, namely the server address, is index.
In a specific implementation, when calling a next (long) method for the first time, the preset address list manager 1002 first obtains an address list length (index) corresponding to a locally pre-stored cluster server address list, initializes the index to 0, then detects whether the address list length is smaller than a preset length (before it is required to verify whether the address list length is empty), and if so, randomly selects a server address within a specified address length range (i.e., the preset length), and then takes a server corresponding to the server address as a target server.
Similarly, if the preset address list manager 1002 detects that the length of the address list is smaller than the preset length, an available server address carrying a preset flag (index) is selected from a local pre-stored cluster server address list.
The preset address list manager 1002 is further configured to verify whether a server corresponding to the available server address is in a high load state, and if not, select a target server address from the available server addresses.
In a specific implementation, the preset address list manager 1002 further verifies whether a server corresponding to the available server address is in a high load state, and selects a target server address from the available server addresses if the server is not in the high load state. For the verification standard of the specific load status of the server, reference may be made to the second embodiment described above, and details thereof are not repeated here.
Considering that the server addresses in the cluster server address list are arranged from small to large according to the network delay, the preset address list manager 1002 in this embodiment may further select a non-high-load server with the smallest network delay from the available server addresses as a target server, thereby ensuring stable establishment of session connection and improving session connection efficiency.
Referring to fig. 2, fig. 2 is a flowchart illustrating a session processing method based on a server load status according to a first embodiment of the present invention.
In this embodiment, the session processing method based on the server load status includes:
step S10: when a session connection request is received, acquiring a server to be connected corresponding to the session connection request;
it should be noted that the execution main body of the method of this embodiment may be a client program having data processing, program running, and network communication functions, which is hereinafter referred to as a client.
It should be understood that the session connection request may be an instruction sent by a client (ZooKeeper) as a service requester to establish a session link with a target server. In this embodiment, the client needs to obtain the comprehensive load information of the server to be connected from the server registry, and then determine which server should be connected according to the comprehensive load information. The server registration center can be realized by adopting a ZooKeeper server, and mainly provides information storage of all service registrations for being called by a (ZooKeeper) client.
In a specific implementation, when a client receives a session connection request triggered by a user, a server to be accessed is determined according to request resource information contained in a request header of the session connection request. It should be understood that, in a distributed cluster scenario, the request resource information is usually stored in a plurality of servers, that is, the number of the servers to be connected in this embodiment may be one or more.
Step S20: calling a preset thread to acquire comprehensive load information corresponding to the server to be connected from a server registration center, and judging whether the server to be connected meets preset connection conditions or not according to the comprehensive load information;
it should be understood that the server registry described in this embodiment is used to provide a storage and management service of registration information for the ZooKeeper server. When each ZooKeeper server registers in the server registration center, firstly establishing a directory "/loadInfo" in the ZooKeeper registration center, then when the ZooKeeper server is started, starting a pre-configured script program for acquiring hardware resource information of the ZooKeeper server, and creating a temporary node directory under the "/loadInfo" directory, wherein the name of the node directory can be an Internet Protocol Address (IP Address) and a port number of the ZooKeeper server where the script program is located; and then saving the ZooKeeper server hardware resource information (including system load, central processing unit utilization rate, memory occupancy rate, disk utilization rate and the like) acquired by the script program to a local storage space. The load comprehensive information can be obtained by the ZooKeeper server through calculation according to the hardware resource information.
It should be noted that the preset thread may be a preset ZKLoadThread, and the thread is used to connect to a server registration center, acquire child node information registered under the loadInfo, and monitor the loadInfo node, and once the loadInfo node changes, the ZKLoadThread may acquire the latest load information of the ZooKeeper server in time.
In this embodiment, the manner of determining whether the server to be connected satisfies the preset connection condition may be to detect whether a parameter value corresponding to one of the pieces of information in the combined load information exceeds or does not exceed a preset value, or to detect whether a weighted average value corresponding to two or more pieces of information in the combined load information exceeds or does not exceed a preset value.
In a specific implementation, the client may call a preset ZKLoadThread to obtain comprehensive load information corresponding to the server to be connected from the server registration center, and then determine whether the server to be connected satisfies the preset connection condition according to the comprehensive load information.
Further, considering that there are many load information affecting the operation rate of the server, in order to improve the session connection efficiency, the comprehensive load information in this embodiment mainly includes a system load, a central processing unit utilization rate, a memory occupancy rate, and a disk utilization rate. Correspondingly, the client in this embodiment is further configured to determine whether the server to be connected meets a preset connection condition according to the system load, the utilization rate of the central processing unit, the memory occupancy rate, and the disk utilization rate.
The system load is defined as the average number of processes in the running queue in a specific time interval, and the system load comprises information which is not the utilization rate condition of the CPU but the statistical information of the sum of the number of processes which are processed by the CPU and wait for the CPU to process in a period of time, namely the statistical information of the length of the CPU utilization queue, and can be obtained by the proc/loadavg file. For example, a system load value within 5 minutes may be collected and then a system load θ of a single server may be calculatedloadNumber of cores of server NumcpuCan be obtained by the command grep 'model name'/proc/cpuininfo | wc-l.
The calculation formula of the server system load in this embodiment is as follows:
Numcpu=grep'model name'/proc/cpuinfo|wc-l
where Lavg _ n, n ═ 5 is the average system load over 5 minutes.
Furthermore, the utilization rate of the central processing unit, namely the CPU utilization rate, can be obtained through a proc/stat file, and real-time CPU state information is stored in the proc/stat file. The CPU utilization rate is the ratio of the time slices in the busy stage to the total CPU time slices, which represents the running condition of the machine at a certain time point, and the higher the CPU utilization rate is, the server is busy, otherwise, the server is idle.
In this embodiment, the state data of the CPU is counted every 5 seconds, and the Total time slice Total2 and the free time slice Total1 of the CPU and the CPU utilization rate θ are calculated by the following formulacpuIn the following formula, the parameter code corresponding to the total time slice is "2" and the parameter code corresponding to the idle time slice is "1"),
Total1=user1+nice1+system1+idle1+iowait1+irq1+softirq1+steal1+guest1
Total2=user2+nice2+system2+idle2+iowait2+irq2+softirq2+steal2+guest2
in the formula, USER is the time of the CPU in the USER mode, nice is the process execution time of the CPU in the USER mode at low priority, System is the time of the CPU in the core mode, Idle is the time of the System in the Idle process, the value is the value of the second field in the USER _ HZ multiplied by/proc/update file, Iowait is the time of the CPU in I/O waiting, Irq is the time overhead of the CPU in interrupting a service program, Softirq is the time of the CPU processing software interruption, Steal is the CPU time occupied by other operating systems in a virtual environment, and Guest is the time of the CPU virtually under the control of a kernel to access a client operating System.
In this embodiment, the memory occupancy rate θ ismemThe method can be obtained by reading a proc/meminfo file, and the main indexes involved in calculating the memory occupancy rate include: MenTotal, MemFree, Buffers and Cached, and the calculation formula of the memory occupancy rate is as follows:
in the formula, MemTotal represents the size of all available ram of the system, MemFree represents the unused memory of the system, Buffers represents the size used for buffering files, and Cached represents the size of the memory used by the cache.
Further, the disk utilization θ in this embodimenti/oThe utilization rate of the disk I/O is also called, and the utilization rate can be obtained through an iostat command under a terminal of a Linux system. Typically, the percentage of disk active time is expressed by% util, i.e., disk I/O utilization disk utilization is proportional to the degree of resource contention and inversely proportional to performance. If the disk utilization exceeds 70%, it takes longer for the read and write to complete the I/O operation, when this happensAfter a situation occurs, most processes will be blocked or hibernated while waiting. The calculation formula of the disk I/O utilization rate in the embodiment is as follows: thetai/o=%util。
In a specific implementation, the client may determine whether the to-be-connected server meets a preset connection condition according to the obtained system load, the central processing unit utilization rate, the memory occupancy rate, and the disk utilization rate, and if the preset connection condition is met, directly use the to-be-connected server as a target server and establish session connection with the target server, or select one server from the to-be-connected servers as the target server to establish session connection.
Step S30: when the server to be connected does not meet the preset connection condition, selecting a target server address from a local pre-stored cluster server address list through a preset address list manager;
it should be noted that, in this embodiment, the preset address list manager may call a serveraddressethread to periodically obtain a ZooKeeper server address from a server registration center, then compare the obtained server address with a server address in a locally pre-stored cluster server address list, if it is found that address list update is needed, sort the obtained server address according to network delay, and update the server address into the cluster server address list, so as to obtain a latest cluster server address list.
In a specific implementation, if the client detects that the server to be connected does not meet the preset connection condition, a target server address can be selected from a local pre-stored cluster server address list through the preset address list manager. The selection manner of the target server address in this embodiment may be random selection, or may be selection by setting a screening policy (for example, selecting a server with the minimum network delay) according to an actual situation, which is not limited in this embodiment.
Step S40: and determining a corresponding target server according to the target server address, and establishing session connection with the target server.
In the specific implementation, after the target server address is screened out by the client, the corresponding target server can be determined according to the target server address, then session connection is established with the target server, and the target server carries out subsequent request response.
When receiving a session connection request, a client acquires a to-be-connected server corresponding to the session connection request; calling a preset thread to acquire comprehensive load information corresponding to the server to be connected from the server registration center, and judging whether the server to be connected meets preset connection conditions or not according to the comprehensive load information; if not, selecting a target server address from a local pre-stored cluster server address list through a preset address list manager; and then determining a corresponding target server according to the address of the target server, and establishing session connection with the target server. In the embodiment, the servers to be connected are screened according to the comprehensive load information corresponding to the servers, and when the screened servers do not meet the connection condition, the target servers are reselected from the local cluster servers to establish session connection, so that the screened target servers can be ensured to stably provide session service for the client, and the occurrence of session interruption caused by the downtime of the servers due to high load is avoided to a greater extent.
Referring to fig. 3, fig. 3 is a flowchart illustrating a session processing method based on a server load status according to a second embodiment of the present invention.
Based on the first embodiment of the session processing method based on the server load status, in this embodiment, the comprehensive load information includes: system load, central processor utilization, memory occupancy, and disk utilization. Accordingly, the step S20 can be further refined to include:
step S201: calling a preset thread to obtain comprehensive load information corresponding to the server to be connected from a server registration center, wherein the comprehensive load information comprises: system load, central processing unit utilization rate, memory occupancy rate and disk utilization rate;
in a specific implementation, the client may call a preset ZKLoadThread to obtain the comprehensive load information corresponding to the server to be connected from the server registration center.
Step S202: and judging whether the server to be connected meets a preset connection condition or not according to the system load, the utilization rate of the central processing unit, the memory occupancy rate and the disk utilization rate.
In a specific implementation, the client may calculate, according to the system load, the utilization rate of the central processing unit, the memory occupancy rate, and the disk utilization rate, a comprehensive load value corresponding to the server to be connected through a preset formula; determining a current load state corresponding to the server to be connected according to the comprehensive load value, and judging whether the server to be connected meets a preset connection condition or not according to the current load state; wherein the preset formula is as follows:
CHSLoad=θload+θcpu+θmem+θi/o
wherein CHSLoad is the combined load value, θloadTo system load, θcpuFor central processor utilization, thetamemFor memory occupancy, θi/oThe disk utilization.
It should be noted that the current load state in this embodiment includes a low load, a normal load, and a high load. In this embodiment, the comprehensive load value CHSLoad and the system load θ corresponding to each load stateloadAs can be seen from table 1 in the above-described embodiment of the session processing system based on the server load status, when the integrated load value of the server to be connected belongs to the range of [0, 2.40), the state is in the low load state, when the integrated load value of the server to be connected belongs to the range of [2.40, 3.45), the state is in the normal load state, and when the integrated load value of the server to be connected belongs to the range of [3.45, + ∞) the state is in the high load state.
The client side calculates a comprehensive load value corresponding to the server to be connected according to the system load, the utilization rate of the central processing unit, the memory occupancy rate and the utilization rate of the disk through a preset formula; and then determining the current load state corresponding to the server to be connected according to the comprehensive load value, and then judging whether the server to be connected meets the preset connection condition or not according to the current load state, so that whether servers with good load conditions except the server to be connected need to be selected to establish session connection or not can be determined, the selected target server can be ensured to run stably to the maximum extent, and the condition that the session is interrupted due to the downtime of the server can be avoided.
Referring to fig. 4, fig. 4 is a flowchart illustrating a session processing method based on a server load status according to a third embodiment of the present invention.
Based on the foregoing embodiment, a third embodiment of the session processing method based on the server load status according to the present invention is proposed, and in this embodiment, the step S20 may include:
step S201': calling the preset thread to connect the server registration center, and monitoring the registered server in the server registration center to acquire comprehensive load information corresponding to the registered server;
it should be noted that the preset thread may be a preset ZKLoadThread, and the thread is used to connect to the server registration center, acquire the child node information registered under the loadInfo, and monitor the loadInfo node, and once the/loadInfo node changes, the ZKLoadThread may acquire the latest load information of the ZooKeeper server in time, so as to ensure that the comprehensive load information has higher timeliness.
Step S202': and reading the comprehensive load information corresponding to the server to be connected from the obtained comprehensive load information, and judging whether the server to be connected meets a preset connection condition according to the read comprehensive load information.
In a specific implementation, after acquiring the comprehensive load information corresponding to each server, the client may read the comprehensive load information corresponding to the server to be connected according to the server identifier corresponding to the server to be connected, and then determine whether the server to be connected meets the preset connection condition according to the read comprehensive load information.
Further, to ensure that the server address stored in the cluster server address list stored in the client is the latest server address, the session processing method based on the server load state in this embodiment further includes the following steps:
step 1: acquiring a current server address list corresponding to the distributed cluster from the server registration center through the preset address list manager;
it should be noted that, in this embodiment, the distributed cluster is a distributed server cluster in which a registered server in a server center is located. The current server address list is a server address list which is updated in the server center last time.
In a specific implementation, a client acquires a current server address list corresponding to a distributed cluster from a server registration center through a preset address list manager.
Step 2: detecting whether the current server address list is consistent with the cluster server address list or not, and if not, acquiring network delay information of a server corresponding to each server address in the current server address list;
it should be noted that the network delay information generally refers to the round-trip time of sending a data packet from the user's computer to the web server and then immediately returning the data packet from the web server to the user's computer. In popular terms, it is the time taken for data to pass from the computer to the computer. The network delay in this embodiment refers to a network delay between the client and the Zookeeper server. The client can send the same ping command to all servers in the current server address list and then record the sending time t of the ping command1And a receiving time t of receiving the ping command feedback result4Then, each server record is obtained and fed back to the receiving time t of the ping command received by the client2And a return time t when a feedback result is returned to the client when the ping command response is finished3Finally, the client end sets the network delay as (t) according to the formula2-t1)+(t4-t3) And then the current network delay of each server can be calculated.
It should be understood that, in this embodiment, if the client detects that the current server address list is consistent with the cluster server address list, it indicates that the cluster server address list locally stored by the client does not need to be updated, otherwise, the cluster server address list needs to be updated.
In a specific implementation, if the client detects that the current server address list is inconsistent with the cluster server address list, network delay information of a server corresponding to each server address in the current server address list is obtained.
And step 3: sequentially adjusting the server addresses contained in the current server address list according to the network delay information to obtain a server address list to be cached;
it should be noted that, in this embodiment, the manner of sequentially adjusting the servers may be to perform size sorting on the network delays included in the network delay information in a sequence from small to large, and then sequentially adjust the server addresses included in the current server address list according to a sorting result, so as to obtain the server address list to be cached.
And 4, step 4: and caching the address list of the server to be cached as a new cluster server address list.
In a specific implementation, after the client obtains the address list of the server to be cached, the address list of the server to be cached can be used as a new cluster server address list for caching, so that the server address stored in the cluster server address list locally stored by the client can be ensured to be the latest server address, and the client can conveniently select the server address corresponding to the server with smaller network delay from the server address list.
Further, in order to ensure stable establishment of session connection and improve session connection efficiency, the session processing method based on the server load status in this embodiment further includes the following steps:
and 5: the method comprises the steps that a preset address list manager obtains the address list length corresponding to a locally pre-stored cluster server address list, detects whether the address list length is smaller than the preset length or not, and selects an available server address carrying a preset mark from the locally pre-stored cluster server address list if the address list length is smaller than the preset length;
it should be noted that in the ZooKeeper client session, the statichonsrovider is adopted as a default to implement the HostProvider interface, and the preset address list manager in this embodiment implements the HostProvider interface through the dynamhstprovider. In this embodiment, a pre-programmed hardware resource monitoring script program is stored in the preset address list manager: DynamicHostProvider class. In the DynamicHostProvider class, the next (long) method is a core method of the class and is also a core control module for load balancing of the client. And the subscript corresponding to the preset mark, namely the server address, is index.
In a specific implementation, when calling a next (long) spindelay method for the first time, the preset address list manager first obtains an address list length (index) corresponding to a locally pre-stored cluster server address list, initializes the index to 0, then detects whether the address list length is smaller than a preset length (before it is required to verify whether the address list length is empty), and if so, randomly selects an address within a specified address length range (i.e., the preset length), and then takes a server corresponding to the address as a target server.
Similarly, if the preset address list manager detects that the length of the address list is smaller than the preset length, an available server address carrying a preset label (index is subscript) is selected from a local pre-stored cluster server address list.
Step 6: and the preset address list manager verifies whether the server corresponding to the available server address is in a high-load state, and if not, a target server address is selected from the available server addresses.
In a specific implementation, the preset address list manager also verifies whether a server corresponding to the available server address is in a high load state, and selects a target server address from the available server addresses if the server is not in the high load state.
Considering that the server addresses in the cluster server address list are arranged from small to large according to the network delay, in this embodiment, the preset address list manager further selects a non-high-load server with the smallest network delay from the available server addresses as the target server, thereby ensuring stable establishment of session connection and improving session connection efficiency.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or system. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or system that comprises the element.
The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
Through the above description of the embodiments, those skilled in the art will clearly understand that the method of the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but in many cases, the former is a better implementation manner. Based on such understanding, the technical solutions of the present invention may be embodied in the form of a software product, which is stored in a storage medium (e.g., a rom/ram, a magnetic disk, an optical disk) and includes instructions for enabling a terminal device (e.g., a mobile phone, a computer, a server, an air conditioner, or a network device) to execute the method according to the embodiments of the present invention.
The above description is only a preferred embodiment of the present invention, and not intended to limit the scope of the present invention, and all modifications of equivalent structures and equivalent processes, which are made by using the contents of the present specification and the accompanying drawings, or directly or indirectly applied to other related technical fields, are included in the scope of the present invention.