MySQL for the Internet of Things

(Steven Felgate) #1

ChapTEr 8 ■ DEmonsTraTion of high availabiliTy TEChniquEs


Notice we have the connection information for the master that we use to get the slave information.
Also, notice that we get this list each time the next server is requested. In this way, the code will keep the list
updated so that if a server is added or removed (and it is dropped by the master), the connection list will
update automatically.
However, the output of the SHOW SLAVE HOSTS statement is unordered and may not appear in server ID
order. Thus, I added a sort method to ensure the round-robin mechanism loops through the server IDs in
ascending order.


■Tip it can take a few seconds for the slave hosts view to update if a slave disconnects unexpectedly. so,


you may want to consider adding code to check that the slave is still connected if you plan to use this code in a


production environment.


Test the Sketch


Now let’s test the code. All you need to do to run it on your system is to change the master information to
match your setup and execute the code with the python command. Listing 8-5 shows a sample run. I added
code to print out the results from the class as well as printing the SQL statement used to update the table so
that you can see it is indeed selecting the next server in the list and repeating the loop starting from the first
server ID in the list.


Listing 8-5. Testing the Read Scaling Selector


$ python ./read_scaling_demo.py



UPDATE read_scaling.current_slave SET server_id=2,host='localhost',port=13002
0 next read server = {'host': u'localhost', 'id': 2, 'port': 13002}
UPDATE read_scaling.current_slave SET server_id=3,host='localhost',port=13003
1 next read server = {'host': u'localhost', 'id': 3, 'port': 13003}
UPDATE read_scaling.current_slave SET server_id=4,host='localhost',port=13004
2 next read server = {'host': u'localhost', 'id': 4, 'port': 13004}
UPDATE read_scaling.current_slave SET server_id=2,host='localhost',port=13002
3 next read server = {'host': u'localhost', 'id': 2, 'port': 13002}
UPDATE read_scaling.current_slave SET server_id=3,host='localhost',port=13003
4 next read server = {'host': u'localhost', 'id': 3, 'port': 13003}
UPDATE read_scaling.current_slave SET server_id=4,host='localhost',port=13004
5 next read server = {'host': u'localhost', 'id': 4, 'port': 13004}
UPDATE read_scaling.current_slave SET server_id=2,host='localhost',port=13002
6 next read server = {'host': u'localhost', 'id': 2, 'port': 13002}
UPDATE read_scaling.current_slave SET server_id=3,host='localhost',port=13003
7 next read server = {'host': u'localhost', 'id': 3, 'port': 13003}
UPDATE read_scaling.current_slave SET server_id=4,host='localhost',port=13004
8 next read server = {'host': u'localhost', 'id': 4, 'port': 13004}
UPDATE read_scaling.current_slave SET server_id=2,host='localhost',port=13002
9 next read server = {'host': u'localhost', 'id': 2, 'port': 13002}



Notice we call the method to get the next server ten times. Notice also that the code successfully loops
through the slaves in a round-robin selection updating the database with each new selection. Cool, eh?

Free download pdf