Skip to content

memcached.sess_persistent not working with memcached.sess_binary_protocol = On #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vvidic opened this issue Dec 27, 2017 · 5 comments
Closed
Milestone

Comments

@vvidic
Copy link

vvidic commented Dec 27, 2017

Looking at the lsof listing of the apache2 process the port number changes after every request to that process:

apache2 930 www-data   12u     IPv4 125149      0t0    TCP 127.0.0.1:34156->127.0.0.1:11211 (ESTABLISHED)
apache2 930 www-data   12u     IPv4 169421      0t0    TCP 127.0.0.1:34204->127.0.0.1:11211 (ESTABLISHED)
apache2 930 www-data   12u     IPv4 169453      0t0    TCP 127.0.0.1:34228->127.0.0.1:11211 (ESTABLISHED)

so the persistence is not working as expected since the old TCP connection is dropped and a new one created for each PHP request being handled.

Setting memcached.sess_binary_protocol = Off fixes the problem and the TCP connections are retained between requests.

The problem seems to be that setting the binary protocol kills existing connections:

PS_OPEN_FUNC(memcached)
                 ....
                 memc = (memcached_st *) le_p->ptr;
                 if (!s_configure_from_ini_values(memc, 1)) {
                 ...

zend_bool s_configure_from_ini_values(memcached_st *memc, zend_bool silent)
                       ...
                       if (MEMC_SESS_INI(binary_protocol_enabled)) {
                           check_set_behavior(MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1);
                       }
                       ...

 memcached_return_t memcached_behavior_set(memcached_st *shell,
                                      const memcached_behavior_t flag,
                                      uint64_t data)

                          ....
                          case MEMCACHED_BEHAVIOR_BINARY_PROTOCOL:
                          send_quit(ptr); // We need t shutdown all of the connections to make sure we do the correct protocol
                          ....

Since setting some options closes the existing connection the solution might be to first get the current behavior with memcached_behavior_get and only call memcached_behavior_set if the value is not as expected?

@sodabrew
Copy link
Contributor

Interesting! I haven't seen this locally. I will try to reproduce. In the mean time, can you run locally with tcpdump while you're testing and post the output? This should do it:

tcpdump -i eth0 -s 65535 -A port 11211

@vvidic
Copy link
Author

vvidic commented Jan 18, 2018 via email

@sodabrew
Copy link
Contributor

Thank you!! Due to a limitation in GitHub ("email replies do not support markdown") I'm reposting the contents of your message:

11:20:04.008358 IP6 localhost.44266 > localhost.11211: Flags [S], seq 281157516, win 43690, options [mss 65476,sackOK,TS val 49614 ecr 0,nop,wscale 7], length 0
`..o.(.@..................................+..............0.........
............
11:20:04.008364 IP6 localhost.11211 > localhost.44266: Flags [R.], seq 0, ack 281157517, win 0, length 0
`......@................................+...........P.......
11:20:04.008410 IP localhost.35126 > localhost.11211: Flags [S], seq 3832056521, win 43690, options [mss 65495,sackOK,TS val 49614 ecr 0,nop,wscale 7], length 0
E..<K.@[email protected]+..h...........0.........
............
11:20:04.008416 IP localhost.11211 > localhost.35126: Flags [S.], seq 3387271028, ack 3832056522, win 43690, options [mss 65495,sackOK,TS val 49614 ecr 49614,nop,wscale 7], length 0
E..<..@.@.<.........+..6...t.h.......0.........
............
11:20:04.008421 IP localhost.35126 > localhost.11211: Flags [.], ack 1, win 342, options [nop,nop,TS val 49614 ecr 49614], length 0
E..4K.@[email protected]+..h.....u...V.(.....
........
11:20:04.008452 IP localhost.35126 > localhost.11211: Flags [P.], seq 1:81, ack 1, win 342, options [nop,nop,TS val 49614 ecr 49614], length 80
E...K   @[email protected]+..h.....u...V.x.....
.........../.......8...................,memc.sess.lock.3akt9t4flksa8o3l2ioc86i5o83agmbb1
11:20:04.008456 IP localhost.11211 > localhost.35126: Flags [.], ack 81, win 342, options [nop,nop,TS val 49614 ecr 49614], length 0
E..4f.@.@...........+..6...u.h.....V.(.....
........
11:20:04.008545 IP localhost.11211 > localhost.35126: Flags [P.], seq 1:25, ack 81, win 342, options [nop,nop,TS val 49614 ecr 49614], length 24
E..Lf.@.@...........+..6...u.h.....V.@.....
................................
11:20:04.008553 IP localhost.35126 > localhost.11211: Flags [.], ack 25, win 342, options [nop,nop,TS val 49614 ecr 49614], length 0
E..4K
@[email protected]+..h.........V.(.....
........
11:20:04.008616 IP localhost.35126 > localhost.11211: Flags [P.], seq 81:147, ack 25, win 342, options [nop,nop,TS val 49614 ecr 49614], length 66
E..vK.@[email protected]+..h.........V.j.....
...........*.......*............memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbb
11:20:04.008664 IP localhost.11211 > localhost.35126: Flags [P.], seq 25:107, ack 147, win 342, options [nop,nop,TS val 49614 ecr 49614], length 82
E...f.@.@...........+..6.....h.\...V.z.....
...........*.......:...........
....memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbbcounter|i:5;
11:20:04.008777 IP localhost.35126 > localhost.11211: Flags [P.], seq 147:233, ack 107, win 342, options [nop,nop,TS val 49614 ecr 49614], length 86
E...K.@.@.._.........6+..h.\.......V.~.....
...........*.......>..................
.memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbbcounter|i:6;
11:20:04.008800 IP localhost.11211 > localhost.35126: Flags [P.], seq 107:131, ack 233, win 342, options [nop,nop,TS val 49614 ecr 49614], length 24
E..Lf.@.@...........+..6.....h.....V.@.....
................................
11:20:04.008831 IP localhost.35126 > localhost.11211: Flags [P.], seq 233:304, ack 131, win 342, options [nop,nop,TS val 49614 ecr 49614], length 71
E..{K.@[email protected]+..h.........V.o.....
.........../......./............memc.sess.lock.3akt9t4flksa8o3l2ioc86i5o83agmbb
11:20:04.008871 IP localhost.11211 > localhost.35126: Flags [P.], seq 131:155, ack 304, win 342, options [nop,nop,TS val 49614 ecr 49614], length 24
E..Lf.@.@...........+..6.....h.....V.@.....
................................
11:20:04.049389 IP localhost.35126 > localhost.11211: Flags [.], ack 155, win 342, options [nop,nop,TS val 49625 ecr 49614], length 0
E..4K.@[email protected]+..h.........V.(.....
........

11:21:39.076823 IP localhost.35106 > localhost.11211: Flags [P.], seq 2318597165:2318597189, ack 1132311151, win 342, options [nop,nop,TS val 73381 ecr 41781], length 24
E..LY.@.@............"+..2.-C}.o...V.@.....
.......5........................
11:21:39.076840 IP localhost.35106 > localhost.11211: Flags [F.], seq 24, ack 1, win 342, options [nop,nop,TS val 73381 ecr 41781], length 0
E..4Y.@.@............"+..2.EC}.o...V.(.....
.......5
11:21:39.076896 IP localhost.11211 > localhost.35106: Flags [P.], seq 1:25, ack 25, win 342, options [nop,nop,TS val 73381 ecr 73381], length 24
E..L..@.@.).........+.."C}.o.2.F...V.@.....
................................
11:21:39.076911 IP localhost.35106 > localhost.11211: Flags [R], seq 2318597190, win 0, length 0
E..(j.@.@............"+..2.F....P...vw..
11:21:39.076922 IP localhost.35128 > localhost.11211: Flags [S], seq 3404154419, win 43690, options [mss 65495,sackOK,TS val 73381 ecr 0,nop,wscale 7], length 0
E..<X.@.@..#.........8+...F3.........0.........
............
11:21:39.076928 IP localhost.11211 > localhost.35128: Flags [S.], seq 2609678559, ack 3404154420, win 43690, options [mss 65495,sackOK,TS val 73381 ecr 73381,nop,wscale 7], length 0
E..<..@.@.<.........+..8......F4.....0.........
............
11:21:39.076933 IP localhost.35128 > localhost.11211: Flags [.], ack 1, win 342, options [nop,nop,TS val 73381 ecr 73381], length 0
E..4X.@.@..*.........8+...F4.......V.(.....
........
11:21:39.076949 IP localhost.35128 > localhost.11211: Flags [P.], seq 1:81, ack 1, win 342, options [nop,nop,TS val 73381 ecr 73381], length 80
E...X.@[email protected]+...F4.......V.x.....
.........../.......8...................,memc.sess.lock.3akt9t4flksa8o3l2ioc86i5o83agmbb1
11:21:39.076951 IP localhost.11211 > localhost.35128: Flags [.], ack 81, win 342, options [nop,nop,TS val 73381 ecr 73381], length 0
E..4.E@.@..|........+..8......F....V.(.....
........
11:21:39.077186 IP localhost.11211 > localhost.35128: Flags [P.], seq 1:25, ack 81, win 342, options [nop,nop,TS val 73381 ecr 73381], length 24
E..L.F@[email protected]........+..8......F....V.@.....
...............................
11:21:39.077193 IP localhost.35128 > localhost.11211: Flags [.], ack 25, win 342, options [nop,nop,TS val 73381 ecr 73381], length 0
E..4X.@.@..(.........8+...F........V.(.....
........
11:21:39.077221 IP localhost.35128 > localhost.11211: Flags [P.], seq 81:147, ack 25, win 342, options [nop,nop,TS val 73381 ecr 73381], length 66
E..vX.@[email protected]+...F........V.j.....
...........*.......*............memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbb
11:21:39.077256 IP localhost.11211 > localhost.35128: Flags [P.], seq 25:107, ack 147, win 342, options [nop,nop,TS val 73381 ecr 73381], length 82
E....G@.@..(........+..8......F....V.z.....
...........*.......:................memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbbcounter|i:6;
11:21:39.077321 IP localhost.35128 > localhost.11211: Flags [P.], seq 147:233, ack 107, win 342, options [nop,nop,TS val 73381 ecr 73381], length 86
E...X.@[email protected]+...F....J...V.~.....
...........*.......>..................
.memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbbcounter|i:7;
11:21:39.077403 IP localhost.11211 > localhost.35128: Flags [P.], seq 107:131, ack 233, win 342, options [nop,nop,TS val 73382 ecr 73381], length 24
E..L.H@[email protected]........+..8...J..G....V.@.....
................................
11:21:39.077448 IP localhost.35128 > localhost.11211: Flags [P.], seq 233:304, ack 131, win 342, options [nop,nop,TS val 73382 ecr 73382], length 71
E..{X.@[email protected]+...G....b...V.o.....
.........../......./............memc.sess.lock.3akt9t4flksa8o3l2ioc86i5o83agmbb
11:21:39.077486 IP localhost.11211 > localhost.35128: Flags [P.], seq 131:155, ack 304, win 342, options [nop,nop,TS val 73382 ecr 73382], length 24
E..L.I@.@..`........+..8...b..Gc...V.@.....
................................
11:21:39.121476 IP localhost.35128 > localhost.11211: Flags [.], ack 155, win 342, options [nop,nop,TS val 73393 ecr 73382], length 0
E..4X.@.@..$.........8+...Gc...z...V.(.....
........
11:21:57.540460 IP localhost.35110 > localhost.11211: Flags [P.], seq 304:328, ack 155, win 342, options [nop,nop,TS val 77997 ecr 42634], length 24
E..L..@[email protected]..........&+..[...p.....V.@.....
..0.............................
11:21:57.540484 IP localhost.35110 > localhost.11211: Flags [F.], seq 328, ack 155, win 342, options [nop,nop,TS val 77997 ecr 42634], length 0
E..4..@[email protected]..........&+..[...p.....V.(.....
..0.....
11:21:57.540511 IP localhost.11211 > localhost.35110: Flags [P.], seq 155:179, ack 329, win 342, options [nop,nop,TS val 77997 ecr 77997], length 24
E..L2~@.@.
,........+..&.p...[.....V.@.....
..0...0.........................
11:21:57.540524 IP localhost.35110 > localhost.11211: Flags [R], seq 2841358295, win 0, length 0
E..(t.@.@............&+..[......P.......
11:21:57.540547 IP localhost.35130 > localhost.11211: Flags [S], seq 2102928291, win 43690, options [mss 65495,sackOK,TS val 77997 ecr 0,nop,wscale 7], length 0
E..<..@.@.)..........:+.}X#..........0.........
..0.........
11:21:57.540552 IP localhost.11211 > localhost.35130: Flags [S.], seq 478607423, ack 2102928292, win 43690, options [mss 65495,sackOK,TS val 77997 ecr 77997,nop,wscale 7], length 0
E..<..@.@.<.........+..:...?}X#......0.........
..0...0.....
11:21:57.540557 IP localhost.35130 > localhost.11211: Flags [.], ack 1, win 342, options [nop,nop,TS val 77997 ecr 77997], length 0
E..4..@.@.)..........:+.}X#[email protected].(.....
..0...0.
11:21:57.540572 IP localhost.35130 > localhost.11211: Flags [P.], seq 1:81, ack 1, win 342, options [nop,nop,TS val 77997 ecr 77997], length 80
E.....@.@.)b.........:+.}X#[email protected].....
..0...0..../.......8...................,memc.sess.lock.3akt9t4flksa8o3l2ioc86i5o83agmbb1
11:21:57.540574 IP localhost.11211 > localhost.35130: Flags [.], ack 81, win 342, options [nop,nop,TS val 77997 ecr 77997], length 0
E..4..@[email protected].........+..:...@}X#....V.(.....
..0...0.
11:21:57.540838 IP localhost.11211 > localhost.35130: Flags [P.], seq 1:25, ack 81, win 342, options [nop,nop,TS val 77997 ecr 77997], length 24
E..L..@[email protected].........+..:...@}X#....V.@.....
..0...0.........................
11:21:57.540845 IP localhost.35130 > localhost.11211: Flags [.], ack 25, win 342, options [nop,nop,TS val 77997 ecr 77997], length 0
E..4..@.@.)..........:+.}X#....X...V.(.....
..0...0.
11:21:57.540887 IP localhost.35130 > localhost.11211: Flags [P.], seq 81:147, ack 25, win 342, options [nop,nop,TS val 77997 ecr 77997], length 66
E..v..@.@.)n.........:+.}X#....X...V.j.....
..0...0....*.......*............memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbb
11:21:57.540965 IP localhost.11211 > localhost.35130: Flags [P.], seq 25:107, ack 147, win 342, options [nop,nop,TS val 77997 ecr 77997], length 82
E.....@[email protected].........+..:...X}X$6...V.z.....
..0...0....*.......:................memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbbcounter|i:7;
11:21:57.541057 IP localhost.35130 > localhost.11211: Flags [P.], seq 147:233, ack 107, win 342, options [nop,nop,TS val 77997 ecr 77997], length 86
E.....@.@.)Y.........:+.}X$6.......V.~.....
..0...0....*.......>..................
.memc.sess.3akt9t4flksa8o3l2ioc86i5o83agmbbcounter|i:8;
11:21:57.541113 IP localhost.11211 > localhost.35130: Flags [P.], seq 107:131, ack 233, win 342, options [nop,nop,TS val 77997 ecr 77997], length 24
E..L..@[email protected].........+..:....}X$....V.@.....
..0...0.........................
11:21:57.541139 IP localhost.35130 > localhost.11211: Flags [P.], seq 233:304, ack 131, win 342, options [nop,nop,TS val 77997 ecr 77997], length 71
E..{..@.@.)g.........:+.}X$........V.o.....
..0...0..../......./.   ..........memc.sess.lock.3akt9t4flksa8o3l2ioc86i5o83agmbb
11:21:57.541201 IP localhost.11211 > localhost.35130: Flags [P.], seq 131:155, ack 304, win 342, options [nop,nop,TS val 77997 ecr 77997], length 24
E..L..@[email protected].........+..:....}X$....V.@.....
..0...0..............   ..........
11:21:57.581480 IP localhost.35130 > localhost.11211: Flags [.], ack 155, win 342, options [nop,nop,TS val 78008 ecr 77997], length 0
E..4..@.@.)..........:+.}X$........V.(.....
..0...0.

@sodabrew
Copy link
Contributor

Ok, so just confirming what you found in code, that the reconnect is initiated by the client and not because the server is responding in an unexpected way. Your suggestion to check the current state of the binary protocol flag on the persistent connection makes sense.

@sodabrew
Copy link
Contributor

Before:

$ php -S 127.0.0.1:8080 -d session.save_handler=memcached -d extension=memcached.so -d memcached.sess_persistent=1

$ curl localhost:8080/tests/session_basic.php

$ netstat -atnp tcp | grep 112
tcp4       0      0  127.0.0.1.11211        127.0.0.1.51009        ESTABLISHED

$ curl localhost:8080/tests/session_basic.php

$ netstat -atnp tcp | grep 112
tcp4       0      0  127.0.0.1.11211        127.0.0.1.51017        ESTABLISHED

After #379:

$ netstat -atnp tcp | grep 112
tcp4       0      0  127.0.0.1.11211        127.0.0.1.51171        ESTABLISHED

$ curl localhost:8080/tests/session_basic.php

$ netstat -atnp tcp | grep 112
tcp4       0      0  127.0.0.1.11211        127.0.0.1.51171        ESTABLISHED

@sodabrew sodabrew modified the milestones: 3.0.5, 3.1.0 Jan 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants