Thursday, March 16, 2017

Redis: (error) CROSSSLOT Keys in request don't hash to the same slot

Redis: (error) CROSSSLOT Keys in request don't hash to the same slot

# redis-cli -c -p 30001

127.0.0.1:30001> SADD mycolor1 R G B
-> Redirected to slot [12383] located at 127.0.0.1:30003
(integer) 3
127.0.0.1:30003> SADD mycolor2 G B Y
-> Redirected to slot [60] located at 127.0.0.1:30001
(integer) 3
127.0.0.1:30001> SUNION mycolor1 mycolor2
(error) CROSSSLOT Keys in request don't hash to the same slot

In a cluster topology, the keyspace is divided into hash slots. Different nodes will hold a subset of hash slots.

Multiple keys operations, transactions, or Lua scripts involving multiple keys are allowed only if all the keys involved are in hash slots belonging to the same node.

Redis Cluster implements all the single key commands available in the non-distributed version of Redis. Commands performing complex multi-key operations like Set type unions or intersections are implemented as well as long as the keys all belong to the same node.

You can force the keys to belong to the same node by using Hash Tags:

> SADD {colorlib}.mycolor1 R G B
> SADD {colorlib}.mycolor2 G B Y
> SUNION {colorlib}.mycolor1 {colorlib}.mycolor2

Reference:

http://stackoverflow.com/questions/38042629/redis-cross-slot-error

http://redis.io/topics/cluster-spec#keys-hash-tag

Assign multiple IP addresses to one single network interface

Assign multiple IP addresses to one single network interface

Method 1:

# cd /etc/sysconfig/network-scripts/
# vim ifcfg-eno16777736

IPADDR0="192.168.6.60"
PREFIX0="24"

IPADDR1="192.168.6.61"
PREFIX1="24"

IPADDR2="10.0.0.2"
PREFIX2="24"
GATEWAY2="10.0.0.1"

Note: I am using a different subnet 10.0.0.0/24 here, too.

Note: If you run ifconfig command, these IP aliases would not show up. Because ifconfig is essentially deprecated. The replacement is the ip command.

# systemctl restart network.service
# ip addr

2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:40:a1:f2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.6.10/24 brd 192.168.6.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 10.0.0.2/24 brd 10.0.0.255 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.6.60/24 brd 192.168.6.255 scope global secondary eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.6.61/24 brd 192.168.6.255 scope global secondary eno16777736
       valid_lft forever preferred_lft forever

Method 2:

# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eno16777736 ifcfg-eno16777736:0
# cp ifcfg-eno16777736 ifcfg-eno16777736:1

# vim ifcfg-eno16777736:0

DEVICE="eno16777736:0"
IPADDR="192.168.6.60"

# vim ifcfg-eno16777736:1

DEVICE="eno16777736:1"
IPADDR="192.168.6.61"

# systemctl restart network.service

Method 3:

If you would like to create a range of multiple IP addresses to a particular network interface:

# cd /etc/sysconfig/network-scripts/
# vim ifcfg-eno16777736

NM_CONTROLLED=NO

Note: This setting is required on Redhat/CentOS 7.x for enabling the range files, which allows us to utilize the range files by having the interface no longer be controlled by the Network Manager system.

# touch ifcfg-eno16777736-range0
# vim ifcfg-eno16777736-range0

IPADDR_START="192.168.6.63"
IPADDR_END="192.168.6.68"
PREFIX="24"
CLONENUM_START="0"

Note: CLONENUM_START is the number of virtual device the first IP address will be assigned to. If you have more than one range file, then you need to make sure that this number is set to the next available number.

# systemctl restart network.service

Reference:

http://www.tecmint.com/create-multiple-ip-addresses-to-one-single-network-interface/

https://www.ubiquityhosting.com/blog/configure-ip-ranges-on-centos-7-redhat-7/

http://askubuntu.com/questions/227457/ifconfig-not-showing-all-ips-bound-to-the-machine

Wednesday, March 15, 2017

What's the difference between Unix socket and TCP/IP socket when setting up PHP-FPM?

What's the difference between Unix socket and TCP/IP socket when setting up PHP-FPM?

A UNIX socket is an inter-process communication mechanism that allows bidirectional data exchange between processes running on the same machine.

IP sockets (especially TCP/IP sockets) are a mechanism allowing communication between processes over the network. In some cases, you can use TCP/IP sockets to talk with processes running on the same computer (by using the loopback interface).

UNIX domain sockets know that they’re executing on the same system, so they can avoid some checks and operations (like routing); which makes them faster and lighter than IP sockets. So if you plan to communicate with processes on the same host, this is a better option than IP sockets.

Edit: As per Nils Toedtmann's comment: UNIX domain sockets are subject to file system permissions, while TCP sockets can be controlled only on the packet filter level.
===
When you are using TCP, you are also using the whole network stack. Even if you are on the same machine, this implies that packets are encapsulated and decapsulated to use the network stack and the related protocols.

If you use unix domain sockets, you will not be forced to go through all the network protocols that are required otherwise. The sockets are identified solely by the inodes on your hard drive.
===
I believe that UNIX domain sockets in theory give better throughput than TCP sockets on the loopback interface, but in practice the difference is probably negligible.

Data carried over UNIX domain sockets don't have to go up and down through the IP stack layers.

re: Alexander's answer. AFAIK you shouldn't get any more than one context switch or data copy in each direction (i.e. for each read() or write()), hence why I believe the difference will be negligble. The IP stack doesn't need to copy the packet as it moves between layers, but it does have to manipulate internal data structures to add and remove higher-layer packet headers.

unix domain socket (UDS) work like system pipes and it send ONLY data, not send checksum and other additional info, not use three-way handshake as TCP sockets.
===
Unix sockets can have owners - users and groups, TCP sockets cannot. Therefore, Unix sockets are more secure - but you cannot separate your webserver (eg, NginX) from your PHP application server (eg. PHP5-FPM) across the network.
===

Reference:

http://serverfault.com/questions/124517/whats-the-difference-between-unix-socket-and-tcp-ip-socket

http://stackoverflow.com/questions/257433/postgresql-unix-domain-sockets-vs-tcp-sockets/257479#257479

http://unix.stackexchange.com/questions/91774/performance-of-unix-sockets-vs-tcp-ports

https://www.reddit.com/r/webhosting/comments/2mgyzg/unix_domain_socket_vs_tcp_can_someone_explain_and/

Wednesday, March 1, 2017

PHPStorm + XDebug Setup Walkthrough

# vim /etc/php/7.0/fpm/conf.d/20-xdebug.ini

zend_extension=xdebug.so

xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.profiler_output_dir="/tmp/xdebug"
xdebug.idekey="PHPSTORM"
xdebug.remote_autostart=1
xdebug.remote_host=localhost
xdebug.remote_mode=req
xdebug.remote_connect_back=1
xdebug.max_nesting_level=200
xdebug.var_display_max_depth=1000
xdebug.var_display_max_children=256
xdebug.var_display_max_data=4096

3. setup the IDE settings

preference > languages and framework > PHP >

3.1. set the language level to the correct PHP version of this project

3.2. set an interpreter (set the parent directory of where the bin directory of PHP executable is loaded)

3.2.1. click the … button > click the + button > other local > set PHP Excitable path,

to find the path type in the terminal: $ which php

example: /usr/local/Cellar/php56/5.6.5/bin/php

4. restart phpstorm

5. now let's make it work

5.1. run > edit configuration > click the green + button on the left > select b. php web application

5.2. name: anything example ur {application name - debugger}

5.3. server: localhost (browse > + > name: whatever | host: localhost or 127.0.0.1)

5.4. click ok

5.5. start url: the link of ur project homepage: http://127.0.0.1:80/SomethingNew/

5.6. click ok

Reference:

http://stackoverflow.com/questions/9183179/phpstorm-xdebug-setup-walkthrough