-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
1486 lines (1231 loc) · 51 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright (c) 2013,2014, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""Define interfaces to manage servers, specifically MySQL Servers.
A server is uniquely identified through a *UUID* (Universally Unique
Identifier) and has an *Address* (i.e., Hostname:Port) which is
used to connect to it through the Python Database API. If a server
process such as MySQL already provides a uuid, the server's class
used to create a MySQL object must ensure that they match otherwise
the different uuids may cause problems in other modules.
Any sort of provisioning must not be performed when the server object
is instantiated. The provisioning steps must be done in other modules.
Servers are organized into groups which have unique names. This aims
at defining administrative domains and easing management activities.
"""
import threading
import uuid as _uuid
import logging
import functools
import re
from datetime import (
datetime,
)
from mysql.fabric import (
errors as _errors,
persistence as _persistence,
server_utils as _server_utils,
utils as _utils,
failure_detector as _detector,
error_log as _error_log,
config as _config,
)
from mysql.fabric.handler import (
MySQLHandler,
)
_LOGGER = logging.getLogger(__name__)
def server_logging(function):
"""This logs information on functions being called within server
instances.
"""
@functools.wraps(function)
def wrapper_check(*args, **kwrds):
"""Inner function that logs information on wrapped function.
"""
_LOGGER.debug(
"Start executing function: %s(%s, %s).", function.__name__,
args, kwrds
)
try:
ret = function(*args, **kwrds)
except Exception:
_LOGGER.debug("Error executing function: %s.", function.__name__)
raise
else:
_LOGGER.debug("Finish executing function: %s.", function.__name__)
return ret
return wrapper_check
class Group(_persistence.Persistable):
"""Provide interfaces to organize servers into groups.
:param group_id: The id that uniquely identifies the group.
:param description: The group's description.
:param master: The master's uuid in the group.
:rtype master: UUID
:param status: Group's status.
:rtype status: ACTIVE or INACTIVE.
"""
CREATE_GROUP = ("CREATE TABLE groups"
"(group_id VARCHAR(64) NOT NULL, "
"description VARCHAR(256), "
"master_uuid VARCHAR(40), "
"master_defined TIMESTAMP(6) NULL, "
"status BIT(1) NOT NULL, "
"CONSTRAINT pk_group_id PRIMARY KEY (group_id))")
#Create the table that stores the group replication relationship.
CREATE_GROUP_REPLICATION = (
"CREATE TABLE group_replication"
"(master_group_id VARCHAR(64) NOT NULL, "
"slave_group_id VARCHAR(64) NOT NULL, "
"CONSTRAINT pk_master_slave_group_id "
"PRIMARY KEY(master_group_id, slave_group_id), "
"CONSTRAINT FOREIGN KEY(master_group_id) "
"REFERENCES groups(group_id), "
"CONSTRAINT FOREIGN KEY(slave_group_id) "
"REFERENCES groups(group_id), "
"INDEX idx_slave_group_id(slave_group_id))")
#SQL statement for inserting a new group into the table
INSERT_GROUP = ("INSERT INTO groups(group_id, description, status) "
"VALUES(%s, %s, %s)")
#SQL statement for selecting all groups
QUERY_GROUPS = ("SELECT group_id FROM groups")
#SQL statement for selecting all groups
QUERY_GROUPS_BY_STATUS = ("SELECT group_id FROM groups WHERE status = %s")
#Query the group that is the master of this group.
QUERY_GROUP_REPLICATION_MASTER = ("SELECT master_group_id FROM "
"group_replication WHERE slave_group_id = %s")
#Query the groups that are the slaves of this group.
QUERY_GROUP_REPLICATION_SLAVES = ("SELECT slave_group_id FROM "
"group_replication WHERE master_group_id = %s")
#SQL statement for updating the group table identified by the group id.
UPDATE_GROUP = ("UPDATE groups SET description = %s WHERE group_id = %s")
#SQL statement used for deleting the group identified by the group id.
REMOVE_GROUP = ("DELETE FROM groups WHERE group_id = %s")
#Remove the mapping between a master and a slave group.
DELETE_MASTER_SLAVE_GROUP_MAPPING = ("DELETE FROM group_replication "
"WHERE slave_group_id = %s")
#Delete all the master to slave mappings for a given master group. A
#given master group can have multiple slaves.
DELETE_SLAVE_GROUPS = ("DELETE FROM group_replication "
"WHERE master_group_id = %s")
#Add a Master - Slave Group mapping.
INSERT_MASTER_SLAVE_GROUP_MAPPING = \
("INSERT INTO group_replication"
"(master_group_id, slave_group_id)"
" VALUES(%s, %s)")
#SQL Statement to retrieve a specific group from the state_store.
QUERY_GROUP = ("SELECT group_id, description, master_uuid, "
"master_defined, status FROM groups WHERE group_id = %s")
#SQL Statement to update the group's master.
UPDATE_MASTER = ("UPDATE groups SET master_uuid = %s, master_defined = %s "
"WHERE group_id = %s")
#SQL Statement to update the group's status.
UPDATE_STATUS = ("UPDATE groups SET status = %s WHERE group_id = %s")
#Create the referential integrity constraint with the servers table
ADD_FOREIGN_KEY_CONSTRAINT_MASTER_UUID = (
"ALTER TABLE groups ADD CONSTRAINT fk_master_uid_servers "
"FOREIGN KEY(master_uuid) REFERENCES servers(server_uuid)"
)
#Group's statuses
INACTIVE, ACTIVE = range(0, 2)
#List with Group's statuses
GROUP_STATUS = [INACTIVE, ACTIVE]
#Failover interval
_FAILOVER_INTERVAL = _DEFAULT_FAILOVER_INTERVAL = 3600
def __init__(self, group_id, description=None, master=None,
master_defined=None, status=INACTIVE):
"""Constructor for the Group.
"""
assert(isinstance(group_id, basestring))
assert(description is None or isinstance(description, basestring))
assert(master is None or isinstance(master, _uuid.UUID))
assert(master_defined is None or isinstance(master_defined, datetime))
assert(status in Group.GROUP_STATUS)
super(Group, self).__init__()
self.__group_id = group_id
self.__description = description
self.__master = master
self.__master_defined = master_defined
self.__status = status
def __eq__(self, other):
"""Two groups are equal if they have the same id.
"""
return isinstance(other, Group) and \
self.__group_id == other.group_id
def __hash__(self):
"""A group is hashable through its uuid.
"""
return hash(self.__group_id)
@property
def group_id(self):
"""Return the group's id.
"""
return self.__group_id
@property
def slave_group_ids(self):
"""Property that gives access to the list of Groups that are
slaves to this group.
"""
return self.fetch_slave_group_ids()
def fetch_slave_group_ids(self, persister=None):
"""Return the list of Groups that are slaves to this group.
:param persister: The DB server that can be used to access the
state store.
"""
ret = []
rows = persister.exec_stmt(Group.QUERY_GROUP_REPLICATION_SLAVES,
{"fetch" : True, "params" : (self.__group_id,)})
if not rows:
return ret
for row in rows:
ret.append(row[0])
return ret
@property
def master_group_id(self):
"""Property that returns the ID of the master group from which this
group replicates.
"""
return self.fetch_master_group_id()
def fetch_master_group_id(self, persister=None):
"""Return the ID of the master group from which this group replicates.
:param persister: The DB server that can be used to access the
state store.
"""
row = persister.exec_stmt(Group.QUERY_GROUP_REPLICATION_MASTER,
{"fetch" : True, "params" : (self.__group_id,)})
if not row:
return None
return row[0][0]
def add_slave_group_id(self, slave_group_id, persister=None):
"""Insert a slave group ID into the slave group ID list. Register a
slave to this group.
:param slave_group_id: the group ID of the slave group that needs to
be added.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(Group.INSERT_MASTER_SLAVE_GROUP_MAPPING,
{"params": (self.__group_id, slave_group_id)})
def remove_slave_group_id(self, slave_group_id, persister=None):
"""Remove a slave group ID from the slave group ID list. Unregister a
slave group.
:param slave_group_id: the group ID of the slave group that needs to
be removed.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(Group.DELETE_MASTER_SLAVE_GROUP_MAPPING,
{"params": (slave_group_id, )})
def remove_slave_group_ids(self, persister=None):
"""Remove slave group ids for a particular group. Unregisters
all the slave of this group.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(Group.DELETE_SLAVE_GROUPS,
{"params": (self.__group_id, )})
def add_master_group_id(self, master_group_id, persister=None):
"""Set the master group ID. Register a group as a master. This Group
basically is a slave to the registered group.
:param master_group_id: The group ID of the master that needs to be
added.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(Group.INSERT_MASTER_SLAVE_GROUP_MAPPING,
{"params": (master_group_id, self.__group_id)})
def remove_master_group_id(self, persister=None):
"""Remove the master group ID. Unregister a master group.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(Group.DELETE_MASTER_SLAVE_GROUP_MAPPING,
{"params": (self.__group_id, )})
def add_server(self, server):
"""Add a server into this group.
:param server: The Server object that needs to be added to this
Group.
"""
assert(isinstance(server, MySQLServer))
assert(server.group_id == None)
server.group_id = self.__group_id
def remove_server(self, server):
"""Remove a server from this group.
:param server: The Server object that needs to be removed from this
Group.
"""
assert(isinstance(server, MySQLServer))
assert(server.group_id == self.__group_id)
server.group_id = None
@property
def description(self):
"""Return the description for the group.
"""
return self.__description
@description.setter
def description(self, description=None, persister=None):
"""Set the description for this group. Update the description for the
Group in the state store.
:param persister: The DB server that can be used to access the
state store.
:param description: The new description for the group that needs to be
updated.
"""
persister.exec_stmt(Group.UPDATE_GROUP,
{"params":(description, self.__group_id)})
self.__description = description
@property
def master(self):
"""Return the master for the group.
"""
return self.__master
@property
def master_defined(self):
"""Return the last time the master has changed.
"""
return self.__master_defined
@master.setter
def master(self, master, persister=None):
"""Set the master for this group.
:param persister: The DB server that can be used to access the
state store.
:param master: The master for the group that needs to be updated.
"""
assert(master is None or isinstance(master, _uuid.UUID))
if master is None:
param_master = None
else:
param_master = str(master)
_LOGGER.info("Master has changed from %s to %s.", self.__master, master,
extra={
"subject": self.__group_id,
"category": MySQLHandler.GROUP,
"type" : MySQLHandler.PROMOTE if master else \
MySQLHandler.DEMOTE
}
)
persister.exec_stmt(Group.UPDATE_MASTER,
{"params":(param_master, _utils.get_time(), self.__group_id)})
self.__master = master
def servers(self):
"""Return a list with the servers in this group.
"""
return MySQLServer.servers(self.__group_id)
@property
def status(self):
"""Return the group's status.
"""
return self.__status
@status.setter
def status(self, status, persister=None):
"""Set the group's status.
:param status: The new group's status.
:param persister: The DB server that can be used to access the
state store.
"""
assert(status in Group.GROUP_STATUS)
# Check the maximum number of threads.
_utils.check_number_threads(1)
persister.exec_stmt(Group.UPDATE_STATUS,
{"params":(status, self.__group_id)})
self.__status = status
def can_set_server_faulty(self, server, now):
"""Check whether is ipossible to set a new master.
If `now - master_defined` > Group._FAILOVER_INTERVAL, it is safe
to set a new master without making the system unstable.
"""
if self.__master_defined is None:
return True
diff = now - self.__master_defined
interval = _utils.get_time_delta(Group._FAILOVER_INTERVAL)
if (self.__master == server.uuid and diff >= interval) or \
self.__master != server.uuid:
return True
return False
@staticmethod
def groups_by_status(status, persister=None):
"""Return the group_ids of all the available groups.
:param persister: Persister to persist the object to.
"""
assert(status in Group.GROUP_STATUS)
return persister.exec_stmt(Group.QUERY_GROUPS_BY_STATUS,
{"params":(status, )}
)
@staticmethod
def groups(persister=None):
"""Return the group_ids of all the available groups.
:param persister: Persister to persist the object to.
"""
return persister.exec_stmt(Group.QUERY_GROUPS)
@staticmethod
def remove(group, persister=None):
"""Remove the group object from the state store.
:param group: A reference to a group.
:param persister: Persister to persist the object to.
"""
persister.exec_stmt(
Group.REMOVE_GROUP, {"params" : (group.group_id, )}
)
@staticmethod
def fetch(group_id, persister=None):
"""Return the group object, by loading the attributes for the group_id
from the state store.
:param group_id: The group_id for the Group object that needs to be
retrieved.
:param persister: Persister to persist the object to.
:return: The Group object corresponding to the group_id
None if the Group object does not exist.
"""
group = None
cur = persister.exec_stmt(
Group.QUERY_GROUP, {"fetch" : False, "raw" : False,
"params" : (group_id,)}
)
row = cur.fetchone()
if row:
group_id, description, master, master_defined, status = row
if master:
master = _uuid.UUID(master)
group = Group(
group_id=group_id, description=description, master=master,
master_defined=master_defined, status=status
)
return group
@staticmethod
def add(group, persister=None):
"""Write a group object into the state store.
:param group: A reference to a group.
:param persister: Persister to persist the object to.
"""
persister.exec_stmt(Group.INSERT_GROUP,
{"params": (group.group_id, group.description, group.status)}
)
@staticmethod
def create(persister=None):
"""Create the objects(tables) that will store the Group information in
the state store.
:param persister: The DB server that can be used to access the
state store.
:raises: DatabaseError If the table already exists.
"""
persister.exec_stmt(Group.CREATE_GROUP)
persister.exec_stmt(Group.CREATE_GROUP_REPLICATION)
@staticmethod
def add_constraints(persister=None):
"""Add the constraints to the groups table.
:param persister: The DB server that can be used to access the
state store.
"""
persister.exec_stmt(
Group.ADD_FOREIGN_KEY_CONSTRAINT_MASTER_UUID
)
return True
class ConnectionPool(_utils.Singleton):
"""Manages MySQL Servers' connections.
The pool is internally implemented as a dictionary that maps a server's
uuid to a sequence of connections.
"""
def __init__(self):
"""Creates a ConnectionPool object.
"""
super(ConnectionPool, self).__init__()
self.__pool = {}
self.__lock = threading.RLock()
def get_connection(self, uuid, user):
"""Get a connection.
The method gets a connection from a pool if there is any.
"""
assert(isinstance(uuid, _uuid.UUID))
with self.__lock:
try:
while len(self.__pool[uuid]):
cnx = self.__pool[uuid].pop()
assert(user != None and cnx.user == user)
if _server_utils.is_valid_mysql_connection(cnx):
return cnx
except (KeyError, IndexError):
pass
return None
def release_connection(self, uuid, cnx):
"""Release a connection to the pool.
It is up to the developer to check if the connection is still
valid and belongs to this server before returning it to the pool.
"""
assert(isinstance(uuid, _uuid.UUID))
with self.__lock:
if uuid not in self.__pool:
self.__pool[uuid] = []
self.__pool[uuid].append(cnx)
def get_number_connections(self, uuid):
"""Return the number of connections available in the pool.
"""
assert(isinstance(uuid, _uuid.UUID))
with self.__lock:
try:
return len(self.__pool[uuid])
except KeyError:
pass
return 0
def purge_connections(self, uuid):
"""Close and remove all connections that belongs to a MySQL Server
which is identified by its uuid.
"""
assert(isinstance(uuid, _uuid.UUID))
with self.__lock:
try:
for cnx in self.__pool[uuid]:
_server_utils.destroy_mysql_connection(cnx)
del self.__pool[uuid]
except KeyError:
pass
class MySQLServer(_persistence.Persistable):
"""Proxy class that provides an interface to access a MySQL Server
Instance.
To create a MySQLServer object, one needs to provide at least two
parameters: uuid and address (i.e., host:port). If the uuid is not known
beforehand, one can find it out as follows::
address = "localhost:13000"
uuid = MySQLServer.discover_uuid(address)
uuid = _uuid.UUID(uuid)
server = MySQLServer(uuid=uuid, address=address)
After creating the object, it is necessary to connect it to the MySQL
Server by explicitly calling connect(). This is required because all the
necessary information to connect to the server may not have been defined
at initialization time. For example, one may have created the object
before reading its state from a persistence layer and setting them.
So after connecting the object to a server, users may execute statements
by calling exec_stmt() as follows::
server.connect()
ret = server.exec_stmt("SELECT VERSION()")
print "MySQL Server has version", ret[0][0]
Changing the value of the properties user or password triggers a call to
disconnect.
:param uuid: The uuid of the server.
:param address: The address of the server.
:param user: The user's name used to access the server.
:param passwd: The password used to access the server.
:param mode: Server's mode.
:type mode: OFFLINE, READ_ONLY, READ_WRITE.
:param status: Server's status.
:type status: FAULTY, SPARE, SECONDARY, PRIMARY.
:param weight: Server's weight which determines its likelihood of
receiving requests.
:type weight: Float
:param group_id: Group's id which the server belongs to.
"""
#SQL Statement for creating the table used to store details about the
#server.
CREATE_SERVER = (
"CREATE TABLE servers "
"(server_uuid VARCHAR(40) NOT NULL, "
"server_address VARCHAR(128) NOT NULL, "
"mode INTEGER NOT NULL, "
"status INTEGER NOT NULL, "
"weight FLOAT NOT NULL, "
"group_id VARCHAR(64), "
"CONSTRAINT pk_server_uuid PRIMARY KEY (server_uuid), "
"INDEX idx_group_id (group_id))"
)
#Create the referential integrity constraint with the groups table
ADD_FOREIGN_KEY_CONSTRAINT_GROUP_ID = (
"ALTER TABLE servers ADD CONSTRAINT fk_group_id_servers "
"FOREIGN KEY(group_id) REFERENCES groups(group_id)"
)
#SQL statement for inserting a new server into the table
INSERT_SERVER = ("INSERT INTO servers(server_uuid, server_address, mode, "
"status, weight, group_id) values(%s, %s, %s, %s, %s, %s)")
#SQL statement for updating the server table identified by the server id.
UPDATE_SERVER_MODE = (
"UPDATE servers SET mode = %s WHERE server_uuid = %s"
)
#SQL statement for updating the server table identified by the server id.
UPDATE_SERVER_STATUS = (
"UPDATE servers SET status = %s WHERE server_uuid = %s"
)
#SQL statement for updating the server table identified by the server id.
UPDATE_SERVER_WEIGHT = (
"UPDATE servers SET weight = %s WHERE server_uuid = %s"
)
#SQL statement for updating the server table identified by the server id.
UPDATE_SERVER_GROUP_ID = (
"UPDATE servers SET group_id = %s WHERE server_uuid = %s"
)
#SQL statement used for deleting the server identified by the server id.
REMOVE_SERVER = ("DELETE FROM servers WHERE server_uuid = %s")
#SQL Statement to retrieve the server from the state_store.
QUERY_SERVER = (
"SELECT server_uuid, server_address, mode, status, weight, group_id "
"FROM servers WHERE server_uuid = %s"
)
#SQL Statement to retrieve a set of servers in a group from the state_store.
QUERY_SERVER_GROUP_ID = (
"SELECT server_uuid, server_address, mode, status, weight "
"FROM servers WHERE group_id = %s"
)
#SQL Statement to retrieve the servers belonging to a group.
DUMP_SERVERS = (
"SELECT server_uuid, group_id, server_address, mode, status, weight "
"FROM servers WHERE group_id LIKE %s AND group_id IS NOT NULL AND "
"status != %s ORDER BY group_id, server_address, server_uuid"
)
#Default weight for the server
DEFAULT_WEIGHT = 1.0
# Define a session context for a variable.
SESSION_CONTEXT = "SESSION"
# Define a global context for a variable.
GLOBAL_CONTEXT = "GLOBAL"
# Set of contexts.
CONTEXTS = [SESSION_CONTEXT, GLOBAL_CONTEXT]
# Define the offline mode.
OFFLINE = "OFFLINE"
# Define the read-only mode.
READ_ONLY = "READ_ONLY"
# Define the write-only mode.
WRITE_ONLY = "WRITE_ONLY"
# Define the read-write mode.
READ_WRITE = "READ_WRITE"
# Define default mode
DEFAULT_MODE = READ_ONLY
# Set of possible modes.
SERVER_MODE = [OFFLINE, READ_ONLY, WRITE_ONLY, READ_WRITE]
# Define the faulty status.
FAULTY = "FAULTY"
# Define the spare status.
SPARE = "SPARE"
# Define the secondary status.
SECONDARY = "SECONDARY"
# Define the primary status.
PRIMARY = "PRIMARY"
# Define default status
DEFAULT_STATUS = SECONDARY
# Set of possible statuses.
SERVER_STATUS = [FAULTY, SPARE, SECONDARY, PRIMARY]
USER = None
PASSWD = None
ALL_PRIVILEGES = [ "ALL PRIVILEGES" ]
NO_USER_DATABASES = ["performance_schema", "information_schema", "mysql"]
def __init__(self, uuid, address, user=None, passwd=None, mode=DEFAULT_MODE,
status=DEFAULT_STATUS, weight=DEFAULT_WEIGHT, group_id=None):
"""Constructor for MySQLServer.
"""
super(MySQLServer, self).__init__()
assert(isinstance(uuid, _uuid.UUID))
assert(mode in MySQLServer.SERVER_MODE)
assert(status in MySQLServer.SERVER_STATUS)
self.__cnx = None
self.__uuid = uuid
self.__address = address
self.__group_id = group_id
self.__pool = ConnectionPool()
self.__user = user
self.__passwd = passwd
self.__mode = mode
self.__status = status
self.__weight = weight
self.__read_only = None
self.__server_id = None
self.__version = None
self.__gtid_enabled = None
self.__binlog_enabled = None
@staticmethod
@server_logging
def discover_uuid(address, user=None, passwd=None,
connection_timeout=None):
"""Retrieve the uuid from a server.
:param address: Server's address.
:param user: The user's name used to access the server.
:param passwd: The password used to access the server.
:param connection_timeout: Time in seconds after which an error is
reported if the UUID is not retrieved.
:return: UUID.
"""
host, port = _server_utils.split_host_port(
address, _server_utils.MYSQL_DEFAULT_PORT
)
port = int(port)
user = user or MySQLServer.USER
passwd = passwd or MySQLServer.PASSWD
cnx = _server_utils.create_mysql_connection(
user=user, passwd=passwd, host=host, port=port, autocommit=True,
use_unicode=False, connection_timeout=connection_timeout
)
try:
row = _server_utils.exec_mysql_stmt(cnx,
"SELECT @@GLOBAL.SERVER_UUID")
server_uuid = row[0][0]
finally:
_server_utils.destroy_mysql_connection(cnx)
return server_uuid
def _do_connection(self):
"""Get a connection from the pool provided there is one or create
a fresh connection.
"""
cnx = self.__pool.get_connection(self.__uuid, self.user)
if cnx:
return cnx
return self._do_create_connection()
def _do_create_connection(self, connection_timeout=None):
"""Create a new connection.
"""
host, port = _server_utils.split_host_port(self.address,
_server_utils.MYSQL_DEFAULT_PORT)
return _server_utils.create_mysql_connection(
autocommit=True, use_unicode=False, host=host, port=port,
user=self.user, passwd=self.passwd,
connection_timeout=connection_timeout
)
def connect(self):
"""Connect to a MySQL Server instance.
"""
self.disconnect()
# Set up an internal connection.
self.__cnx = self._do_connection()
_LOGGER.debug("Using connection (%s).", self.__cnx)
# Get server's uuid
ret_uuid = self.get_variable("SERVER_UUID")
ret_uuid = _uuid.UUID(ret_uuid)
if ret_uuid != self.uuid:
self.disconnect()
raise _errors.UuidError(
"UUIDs do not match (stored (%s), read (%s))." %
(self.uuid, ret_uuid)
)
# Get server's id.
self.__server_id = int(self.get_variable("SERVER_ID"))
# Get server's version.
self.__version = self.get_variable("VERSION")
# Get information on gtid support.
if not self.check_version_compat((5, 6, 5)):
self.__gtid_enabled = False
else:
ret_gtid = self.get_variable("GTID_MODE")
self.__gtid_enabled = ret_gtid in ("ON", "1")
ret_binlog = self.get_variable("LOG_BIN")
self.__binlog_enabled = not ret_binlog in ("OFF", "0")
self._check_read_only()
_LOGGER.debug("Connected to server with uuid (%s), server_id (%d), "
"version (%s), gtid (%s), binlog (%s), read_only (%s).",
self.uuid, self.__server_id, self.__version,
self.__gtid_enabled, self.__binlog_enabled,
self.__read_only)
def disconnect(self):
"""Disconnect from the server.
"""
if self.__cnx is not None:
_LOGGER.debug("Disconnecting from server with uuid (%s), "
"server_id (%s), version (%s), gtid (%s), "
"binlog (%s), read_only (%s).", self.uuid,
self.__server_id, self.__version,
self.__gtid_enabled, self.__binlog_enabled,
self.__read_only)
self.__pool.release_connection(self.__uuid, self.__cnx)
self.__cnx = None
self.__read_only = None
self.__server_id = None
self.__version = None
self.__gtid_enabled = None
self.__binlog_enabled = None
def user_databases(self):
"""Return user databases.
"""
exc_dbs = MySQLServer.NO_USER_DATABASES
dbs = self.exec_stmt("SHOW DATABASES")
dbs = [db[0] for db in dbs if db[0] not in exc_dbs]
return dbs
def has_privileges(self, required_privileges, level=None):
"""Check whether the current user has the required privileges.
:param required_privileges: List or tuple of required privileges which
a user who is connected to the current
server must have.
:param level: Level of the set of privileges.
"""
assert(isinstance(required_privileges, list) or \
isinstance(required_privileges, tuple))
required_privileges = set(required_privileges)
required_level = level or "*.*"
all_privileges = "ALL PRIVILEGES"
all_level = "*.*"
ret = self.exec_stmt("SHOW GRANTS")
check = re.compile("GRANT (?P<privileges>.*?) ON (?P<level>.*?) TO")
for row in ret:
res = check.match(row[0])
if res:
privileges = [ privilege.strip() \
for privilege in res.group("privileges").split(",")
]
level = res.group("level").replace('`', "")
if (all_privileges in privileges and all_level == level) or \
(required_privileges.issubset(set(privileges)) and \
required_level == level):
return True
return False
def has_required_privileges(self):
"""Check whether the current user has a minimum set of privileges:
MySQLServer.ALL_PRIVILEGES on *.*.
"""
return self.has_privileges(MySQLServer.ALL_PRIVILEGES)
def is_connected(self):
"""Determine whether the proxy object (i.e. the server) is connected
to actual server.
"""
return self.__cnx is not None
def is_alive(self, connection_timeout=None):
"""Determine whether the server is dead or alive by trying to create
a new connection.
:param connection_timeout: Timeout waiting for getting a new
connection.
"""
res = False
try:
cnx = self._do_create_connection(connection_timeout)
res = True
_server_utils.destroy_mysql_connection(cnx)
except _errors.DatabaseError:
pass
return res
def _check_read_only(self):
"""Check if the database was set to read-only mode.
"""
ret_read_only = self.get_variable("READ_ONLY")
self.__read_only = not ret_read_only in ("OFF", "0")
@property
def uuid(self):
"""Return the server's uuid.
"""
return self.__uuid
@property
def address(self):
"""Return the server's address.
"""
return self.__address
@property
def read_only(self):
"""Check read only mode on/off.
:return True If read_only is set
False If read_only is not set.
"""
return self.__read_only
@read_only.setter
def read_only(self, enabled):
"""Turn read only mode on/off. Persist the information in the state
store.
:param enabled The read_only flag value.
"""
self.set_variable("READ_ONLY", "ON" if enabled else "OFF")