In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
What is the redis server multiplexing mechanism? I believe that most people do not understand, in order to let you better understand, the editor summed up the following content, do not say much, let's look down together.
When redis server starts, call bind () to pass in the file descriptor fd6 bind port 6379, call listen () to listen on the port, and wait for the connection through accept ()
Root@pmghong-VirtualBox:/usr/local/redis/bin# strace-ff-o / data/redis_strace/redis. / redis-server root@pmghong-VirtualBox:/proc/22330/fd# ls / data/redis_strace/-ltotal 48 redis.25105-rw-r--r-- redis.25105-rw-r--r-- 1 root root 34741 March 14 10:37 redis.25102-rw-r--r-- 1 root root 14 10:37 redis.25105-rw-r--r-- 1 root root 1343 March 14 10:37 redis.25106-rw-r--r-- 1 root root 134March 14 10:37 redis.25107root@pmghong-VirtualBox:/proc/22330/fd# vi / data/redis_strace/redis.25102... Epoll_create (1024) = 5socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP) = 6setsockopt (6, SOL_IPV6, IPV6_V6ONLY, [1], 4) = 0setsockopt (6, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0bind (6, {sa_family=AF_INET6, sin6_port=htons (6379), inet_pton (AF_INET6, ":", & sin6_addr), sin6_flowinfo=0, sin6_scope_id=0} 28) = 0listen (6,511) = 0fcntl (6, F_GETFL) = 0x2 (flags O_RDWR) fcntl (6, F_SETFL, O_RDWR | O_NONBLOCK) = 0. .. root@pmghong-VirtualBox:/proc/25102/fd# lltotal 0dr root root 0 March 14 12:05. / dr-xr-xr-x 9 root root 0 March 14 10:37.. / lrwx- 1 root root 64 March 14 12:28 0-> / dev/pts/0lrwx- 1 root root 64 March 14 12:28 1-> / dev/pts/0lrwx- 1 Root root 64 March 14 12:05 2-> / dev/pts/0lr-x- 1 root root 64 March 14 12:28 3-> pipe: [104062] l lrwx- WX-1 root root 64 March 14 12:28 4-> pipe: [104062] lrwx- 1 root root 64 March 14 12:28 5-> anon_inode: [even tpoll] lrwx- 1 root root 64 March 14 12:28 6-> socket: [104063] lrwx- 1 root root 64 March 14 12:28 7-> socket: [104064] lrwx- 1 root root 64 March 14 12:28 8-> socket: [256344]
Phase 1: BIO (blocking IO)
Redis Server listens to the system kernel through the file descriptor fd6 after startup
Client1 / Client2 respectively request access to redis via fd7,fd8
In the BIO scenario, redis server will call the read () method and enter the blocking state, that is, it will not be able to process other requests until the fd7 has a request.
The obvious disadvantage of this model is that blocking IO leads to inefficiency.
Phase II NIO (non-blocking IO)
The difference from BIO is that when read (fd7) is called, if no data is requested, an error is returned to redis server immediately
When redis server receives this type of error, it can know that there is no data coming from the current connection, and can proceed to process the next request to improve processing efficiency.
Bind (6, {sa_family=AF_INET6, sin6_port=htons (6379), inet_pton (AF_INET6, ":", & sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0listen (6511) = 0fcntl (6, F_GETFL) = 0x2 (flags O_RDWR) fcntl (6, F_SETFL, O_RDWR | O_NONBLOCK) = 0
The problem with this mode is that regular polling calls read (fdx) system calls, when multiple client requests come, kernel mode / user mode switching needs to be done frequently, and context switching is expensive.
The third stage select synchronization is non-blocking
Int select (int nfds, fd_set * readfds, fd_set * writefds,fd_set * exceptfds, struct timeval * timeout); select () and pselect () allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some classof I allow a program to monitor multiple file descriptors O operation (e.g.input possible). A file descriptor is considered ready if it is possible to perform a corresponding O operation (e.g., read (2) without blocking, or a sufficiently small write (2)).
The goal is to listen to multiple fd at the same time, and it is not until one or more fd enters the ready state that system calls such as read () are called to process the business logic, unlike the above NIO scenario, where x read () is called.
Select can only solve the problem of event notification (that is, which processes can and cannot be read), but in the kernel state, you still need to traverse x fd in the kernel to see which client has IO, and then tell select to return the result set to the server, and then the sever side initiates the read () system call to the specified fd.
The fourth stage of epoll multiplexing
/ / epoll_create// description epoll_create () creates an epoll (7) instance.// function signature int epoll_create (int size); / / return value On success, these system calls return a nonnegative file descriptor. On error,-1 is returned, and errno is set to indicate the error.//epoll_ctl// indicates This system call performs control operations on the epoll (7) instance referred to by the file descriptor epfd. It requests that the operation op be per-formed for the target file descriptor, fd.// function signature int epoll_ctl (int epfd, int op, int fd, struct epoll_event * event); / / op type EPOLL_CTL_ADD / EPOLL_CTL_MOD / EPOLL_CTL_DEL// returns the value When successful, epoll_ctl () returns zero. When an error occurs, epoll_ctl () returns-1 and errno is set appropriately.//epoll_ctl// explains The epoll_wait () system call waits for events on the epoll (7) instance referred to by the file descriptor epfd. The memory area pointed to by events willcontain the events that will be available for the caller. Up to maxevents are returned by epoll_wait (). The maxevents argument must be greater than zero.// function signature int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout); / / return value When successful, epoll_wait () returns the number of file descriptors ready for therequested I _ Band O, or zero if no file descriptor became ready during therequested timeout milliseconds. When an error occurs, epoll_wait () returns-1 and errno is set appropriately.epoll_create (1024) = 5. Bind (6, {sa_family=AF_INET6, sin6_port=htons (6379), inet_pton (AF_INET6, ":", & sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, 28) = 0listen (6511) = 0. Bind (7, {sa_family=AF_INET, sin_port=htons (6379), sin_addr=inet_addr ("0.0.0.0")}, 16) = 0listen (7511) = 0. Epoll_ctl (5, EPOLL_CTL_ADD, 6, {EPOLLIN, {u32, 6, u64, 6}}) = 0epoll_ctl (5, EPOLL_CTL_ADD, 7, {EPOLLIN, {u32, 7, u64, 7}}) = 0epoll_ctl (5, EPOLL_CTL_ADD, 3, {EPOLLIN, {u32, 3, u64, 3}) = 0write (...) read (...) epoll_wait (5, [], 10128, 0) = 0.
1. Create epoll instance with epoll_create () when the process starts, and return a non-negative fdn if it succeeds. If it fails, it returns-1 with an error code.
3. Call epoll_wait () to listen for kernel events and return the fd when the call is successful. For example, when C1 requests redisserver, you first need to establish a connection through fd6. In this case, you can listen to the request through the accept () call to fd6 in epoll_ctl () and pass the fd6 to epoll_wait ().
4. The redis server side obtains the fd that requires IO operation from epoll_wait (), finds that C1 establishes a connection through the fd6 request, assigns fd7 to it, and registers a listener in epoll_ctl (), such as epoll_ctl (fdn,op, fd7,)
More articles on related knowledge points:
Example Analysis of redis Multiplexing Technology
After reading the above, do you have a general understanding of the multiplexing mechanism of redis server? If you want to know more about the content of the article, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.