rpl_gtid.h 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914
  1. /* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef RPL_GTID_H_INCLUDED
  19. #define RPL_GTID_H_INCLUDED
  20. #include <atomic>
  21. #include <list>
  22. #include "libbinlogevents/include/uuid.h"
  23. #include "map_helpers.h"
  24. #include "my_dbug.h"
  25. #include "my_thread_local.h"
  26. #include "mysql/psi/mysql_cond.h"
  27. #include "mysql/psi/mysql_rwlock.h" // mysql_rwlock_t
  28. #include "prealloced_array.h" // Prealloced_array
  29. #include "sql/rpl_reporting.h" // MAX_SLAVE_ERRMSG
  30. #include "template_utils.h"
  31. #include "typelib.h"
  32. struct TABLE_LIST;
  33. class THD;
  34. /**
  35. Report an error from code that can be linked into either the server
  36. or mysqlbinlog. There is no common error reporting mechanism, so we
  37. have to duplicate the error message (write it out in the source file
  38. for mysqlbinlog, write it in share/errmsg-utf8.txt for the server).
  39. @param MYSQLBINLOG_ERROR arguments to mysqlbinlog's 'error'
  40. function, including the function call parentheses
  41. @param SERVER_ERROR arguments to my_error, including the function
  42. call parentheses.
  43. */
  44. #ifndef MYSQL_SERVER
  45. #define BINLOG_ERROR(MYSQLBINLOG_ERROR, SERVER_ERROR) error MYSQLBINLOG_ERROR
  46. #else
  47. #define BINLOG_ERROR(MYSQLBINLOG_ERROR, SERVER_ERROR) my_error SERVER_ERROR
  48. #endif
  49. extern PSI_memory_key key_memory_Gtid_set_to_string;
  50. extern PSI_memory_key key_memory_Owned_gtids_to_string;
  51. extern PSI_memory_key key_memory_Gtid_state_to_string;
  52. extern PSI_memory_key key_memory_Gtid_cache_to_string;
  53. extern PSI_memory_key key_memory_Gtid_set_Interval_chunk;
  54. extern PSI_memory_key key_memory_Gtid_state_group_commit_sidno;
  55. extern std::atomic<ulong> gtid_mode_counter;
  56. /**
  57. This macro is used to check that the given character, pointed to by the
  58. character pointer, is a space or not.
  59. */
  60. #define SKIP_WHITESPACE() \
  61. while (my_isspace(&my_charset_utf8_general_ci, *s)) s++
  62. /*
  63. This macro must be used to filter out parts of the code that
  64. is not used now but may be useful in future. In other words,
  65. we want to keep such code until we make up our minds on whether
  66. it should be removed or not.
  67. */
  68. #undef NON_DISABLED_GTID
  69. /*
  70. This macro must be used to filter out parts of the code that
  71. is not used now but we are not sure if there is a bug around
  72. them. In other words, we want to keep such code until we have
  73. time to investigate it.
  74. */
  75. #undef NON_ERROR_GTID
  76. #ifdef MYSQL_SERVER
  77. class String;
  78. class THD;
  79. #endif // ifdef MYSQL_SERVER
  80. /// Type of SIDNO (source ID number, first component of GTID)
  81. typedef int rpl_sidno;
  82. /// Type of GNO, the second (numeric) component of GTID
  83. typedef long long int rpl_gno;
  84. typedef int64 rpl_binlog_pos;
  85. /**
  86. Generic return type for many functions that can succeed or fail.
  87. This is used in conjuction with the macros below for functions where
  88. the return status either indicates "success" or "failure". It
  89. provides the following features:
  90. - The macros can be used to conveniently propagate errors from
  91. called functions back to the caller.
  92. - If a function is expected to print an error using my_error before
  93. it returns an error status, then the macros assert that my_error
  94. has been called.
  95. - Does a DBUG_PRINT before returning failure.
  96. */
  97. enum enum_return_status {
  98. /// The function completed successfully.
  99. RETURN_STATUS_OK = 0,
  100. /// The function completed with error but did not report it.
  101. RETURN_STATUS_UNREPORTED_ERROR = 1,
  102. /// The function completed with error and has called my_error.
  103. RETURN_STATUS_REPORTED_ERROR = 2
  104. };
  105. /**
  106. @def __CHECK_RETURN_STATUS
  107. Lowest level macro used in the PROPAGATE_* and RETURN_* macros
  108. below.
  109. If DBUG_OFF is defined, does nothing. Otherwise, if STATUS is
  110. RETURN_STATUS_OK, does nothing; otherwise, make a dbug printout and
  111. (if ALLOW_UNREPORTED==0) assert that STATUS !=
  112. RETURN_STATUS_UNREPORTED.
  113. @param STATUS The status to return.
  114. @param ACTION A text that describes what we are doing: either
  115. "Returning" or "Propagating" (used in DBUG_PRINT macros)
  116. @param STATUS_NAME The stringified version of the STATUS (used in
  117. DBUG_PRINT macros).
  118. @param ALLOW_UNREPORTED If false, the macro asserts that STATUS is
  119. not RETURN_STATUS_UNREPORTED_ERROR.
  120. */
  121. #ifdef DBUG_OFF
  122. #define __CHECK_RETURN_STATUS(STATUS, ACTION, STATUS_NAME, ALLOW_UNREPORTED)
  123. #else
  124. extern void check_return_status(enum_return_status status, const char *action,
  125. const char *status_name, int allow_unreported);
  126. #define __CHECK_RETURN_STATUS(STATUS, ACTION, STATUS_NAME, ALLOW_UNREPORTED) \
  127. check_return_status(STATUS, ACTION, STATUS_NAME, ALLOW_UNREPORTED);
  128. #endif
  129. /**
  130. Low-level macro that checks if STATUS is RETURN_STATUS_OK; if it is
  131. not, then RETURN_VALUE is returned.
  132. @see __DO_RETURN_STATUS
  133. */
  134. #define __PROPAGATE_ERROR(STATUS, RETURN_VALUE, ALLOW_UNREPORTED) \
  135. do { \
  136. enum_return_status __propagate_error_status = STATUS; \
  137. if (__propagate_error_status != RETURN_STATUS_OK) { \
  138. __CHECK_RETURN_STATUS(__propagate_error_status, "Propagating", #STATUS, \
  139. ALLOW_UNREPORTED); \
  140. DBUG_RETURN(RETURN_VALUE); \
  141. } \
  142. } while (0)
  143. /// Low-level macro that returns STATUS. @see __DO_RETURN_STATUS
  144. #define __RETURN_STATUS(STATUS, ALLOW_UNREPORTED) \
  145. do { \
  146. enum_return_status __return_status_status = STATUS; \
  147. __CHECK_RETURN_STATUS(__return_status_status, "Returning", #STATUS, \
  148. ALLOW_UNREPORTED); \
  149. DBUG_RETURN(__return_status_status); \
  150. } while (0)
  151. /**
  152. If STATUS (of type enum_return_status) returns RETURN_STATUS_OK,
  153. does nothing; otherwise, does a DBUG_PRINT and returns STATUS.
  154. */
  155. #define PROPAGATE_ERROR(STATUS) \
  156. __PROPAGATE_ERROR(STATUS, __propagate_error_status, true)
  157. /**
  158. If STATUS (of type enum_return_status) returns RETURN_STATUS_OK,
  159. does nothing; otherwise asserts that STATUS ==
  160. RETURN_STATUS_REPORTED_ERROR, does a DBUG_PRINT, and returns STATUS.
  161. */
  162. #define PROPAGATE_REPORTED_ERROR(STATUS) \
  163. __PROPAGATE_ERROR(STATUS, __propagate_error_status, false)
  164. /**
  165. If STATUS (of type enum_return_status) returns RETURN_STATUS_OK,
  166. does nothing; otherwise asserts that STATUS ==
  167. RETURN_STATUS_REPORTED_ERROR, does a DBUG_PRINT, and returns 1.
  168. */
  169. #define PROPAGATE_REPORTED_ERROR_INT(STATUS) __PROPAGATE_ERROR(STATUS, 1, false)
  170. /**
  171. If STATUS returns something else than RETURN_STATUS_OK, does a
  172. DBUG_PRINT. Then, returns STATUS.
  173. */
  174. #define RETURN_STATUS(STATUS) __RETURN_STATUS(STATUS, true)
  175. /**
  176. Asserts that STATUS is not RETURN_STATUS_UNREPORTED_ERROR. Then, if
  177. STATUS is RETURN_STATUS_REPORTED_ERROR, does a DBUG_PRINT. Then,
  178. returns STATUS.
  179. */
  180. #define RETURN_REPORTED_STATUS(STATUS) __RETURN_STATUS(STATUS, false)
  181. /// Returns RETURN_STATUS_OK.
  182. #define RETURN_OK DBUG_RETURN(RETURN_STATUS_OK)
  183. /// Does a DBUG_PRINT and returns RETURN_STATUS_REPORTED_ERROR.
  184. #define RETURN_REPORTED_ERROR RETURN_STATUS(RETURN_STATUS_REPORTED_ERROR)
  185. /// Does a DBUG_PRINT and returns RETURN_STATUS_UNREPORTED_ERROR.
  186. #define RETURN_UNREPORTED_ERROR RETURN_STATUS(RETURN_STATUS_UNREPORTED_ERROR)
  187. /**
  188. enum to map the result of Uuid::parse to the above Macros
  189. */
  190. inline enum_return_status map_macro_enum(int status) {
  191. DBUG_ENTER("map status error with the return value of uuid::parse_method");
  192. if (status == 0)
  193. RETURN_OK;
  194. else
  195. RETURN_UNREPORTED_ERROR;
  196. }
  197. /// Possible values for @@GLOBAL.GTID_MODE.
  198. enum enum_gtid_mode {
  199. /**
  200. New transactions are anonymous. Replicated transactions must be
  201. anonymous; replicated GTID-transactions generate an error.
  202. */
  203. GTID_MODE_OFF = 0,
  204. DEFAULT_GTID_MODE = GTID_MODE_OFF,
  205. /**
  206. New transactions are anonyomus. Replicated transactions can be
  207. either anonymous or GTID-transactions.
  208. */
  209. GTID_MODE_OFF_PERMISSIVE = 1,
  210. /**
  211. New transactions are GTID-transactions. Replicated transactions
  212. can be either anonymous or GTID-transactions.
  213. */
  214. GTID_MODE_ON_PERMISSIVE = 2,
  215. /**
  216. New transactions are GTID-transactions. Replicated transactions
  217. must be GTID-transactions; replicated anonymous transactions
  218. generate an error.
  219. */
  220. GTID_MODE_ON = 3
  221. };
  222. /**
  223. The gtid_mode.
  224. Please do not access this directly - use the getters and setters
  225. defined below.
  226. It is ulong rather than enum_gtid_mode because of how sys_vars are
  227. updated.
  228. */
  229. extern ulong _gtid_mode;
  230. /**
  231. Strings holding the enumeration values for gtid_mode. Use
  232. get_gtid_mode_string instead of accessing this directly.
  233. */
  234. extern const char *gtid_mode_names[];
  235. /**
  236. 'Typelib' for the mode names. Use get_gtid_mode_string instead
  237. of accessing this directly.
  238. */
  239. extern TYPELIB gtid_mode_typelib;
  240. /**
  241. Return the given string GTID_MODE as an enumeration value.
  242. @param string The string to decode.
  243. @param[out] error If the string does not represent a valid
  244. GTID_MODE, this is set to true, otherwise it is left untouched.
  245. @return The GTID_MODE.
  246. */
  247. inline enum_gtid_mode get_gtid_mode(const char *string, bool *error) {
  248. int ret = find_type(string, &gtid_mode_typelib, 1);
  249. if (ret == 0) {
  250. *error = true;
  251. return GTID_MODE_OFF;
  252. } else
  253. return (enum_gtid_mode)(ret - 1);
  254. }
  255. /// Return the given GTID_MODE as a string.
  256. inline const char *get_gtid_mode_string(enum_gtid_mode gtid_mode_arg) {
  257. return gtid_mode_names[gtid_mode_arg];
  258. }
  259. /**
  260. Locks needed to access gtid_mode.
  261. When writing, all these locks must be held (for the rwlocks, the
  262. wrlock must be held).
  263. When reading, one of them must be held (for the wrlocks, the rdlock
  264. suffices).
  265. */
  266. enum enum_gtid_mode_lock {
  267. /// No lock held.
  268. GTID_MODE_LOCK_NONE,
  269. /// The specific gtid_mode_lock is held.
  270. GTID_MODE_LOCK_GTID_MODE,
  271. /// global_sid_lock held.
  272. GTID_MODE_LOCK_SID,
  273. /// read or write lock on channel_map lock is held.
  274. GTID_MODE_LOCK_CHANNEL_MAP
  275. /*
  276. Currently, no function that calls get_gtid_mode needs
  277. this. Uncomment this, and uncomment the case in get_gtid_mode, if it
  278. is ever needed.
  279. /// mysql_bin_log.get_log_lock() held.
  280. GTID_MODE_LOCK_LOG
  281. */
  282. };
  283. /**
  284. Return the current GTID_MODE as an enumeration value.
  285. This variable can be read while holding any one of the locks
  286. enumerated in enum_gtid_mode_lock (see above).
  287. When the variable is updated by a SET GTID_MODE statement, all these
  288. locks will be taken (the wrlock on global_sid_map).
  289. To avoid the mistake of reading the GTID_MODE with no lock, the
  290. caller has to pass the lock type as a parameter. The function will
  291. assert that the corresponding lock is held. If no lock is held, it
  292. will acquire and release global_sid_lock.rdlock.
  293. @param have_lock The lock type held by the caller.
  294. */
  295. enum_gtid_mode get_gtid_mode(enum_gtid_mode_lock have_lock);
  296. #ifndef DBUG_OFF
  297. /**
  298. Return the current GTID_MODE as a string. Used only for debugging.
  299. @param have_lock Pass this parameter to get_gtid_mode(bool).
  300. */
  301. inline const char *get_gtid_mode_string(enum_gtid_mode_lock have_lock) {
  302. return get_gtid_mode_string(get_gtid_mode(have_lock));
  303. }
  304. #endif // ifndef DBUG_OFF
  305. /**
  306. Possible values for ENFORCE_GTID_CONSISTENCY.
  307. */
  308. enum enum_gtid_consistency_mode {
  309. GTID_CONSISTENCY_MODE_OFF = 0,
  310. GTID_CONSISTENCY_MODE_ON = 1,
  311. GTID_CONSISTENCY_MODE_WARN = 2
  312. };
  313. /**
  314. Strings holding the enumeration values for
  315. gtid_consistency_mode_names. Use get_gtid_consistency_mode_string
  316. instead of accessing this directly.
  317. */
  318. extern const char *gtid_consistency_mode_names[];
  319. /**
  320. Current value for ENFORCE_GTID_CONSISTENCY.
  321. Don't use this directly; use get_gtid_consistency_mode.
  322. */
  323. extern ulong _gtid_consistency_mode;
  324. /**
  325. Return the current value of ENFORCE_GTID_CONSISTENCY.
  326. Caller must hold global_sid_lock.rdlock.
  327. */
  328. enum_gtid_consistency_mode get_gtid_consistency_mode();
  329. /// Return the given GTID_CONSISTENCY_MODE as a string.
  330. inline const char *get_gtid_consistency_mode_string(
  331. enum_gtid_consistency_mode mode) {
  332. return gtid_consistency_mode_names[(int)mode];
  333. }
  334. /**
  335. Return the current value of ENFORCE_GTID_CONSISTENCY as a string.
  336. Caller must hold global_sid_lock.rdlock.
  337. */
  338. inline const char *get_gtid_consistency_mode_string() {
  339. return get_gtid_consistency_mode_string(get_gtid_consistency_mode());
  340. }
  341. /// The maximum value of GNO
  342. const rpl_gno MAX_GNO = LLONG_MAX;
  343. /// The length of MAX_GNO when printed in decimal.
  344. const int MAX_GNO_TEXT_LENGTH = 19;
  345. /// The maximal possible length of thread_id when printed in decimal.
  346. const int MAX_THREAD_ID_TEXT_LENGTH = 19;
  347. /**
  348. Parse a GNO from a string.
  349. @param s Pointer to the string. *s will advance to the end of the
  350. parsed GNO, if a correct GNO is found.
  351. @retval GNO if a correct GNO (i.e., 0 or positive number) was found.
  352. @retval -1 otherwise.
  353. */
  354. rpl_gno parse_gno(const char **s);
  355. /**
  356. Formats a GNO as a string.
  357. @param s The buffer.
  358. @param gno The GNO.
  359. @return Length of the generated string.
  360. */
  361. int format_gno(char *s, rpl_gno gno);
  362. typedef binary_log::Uuid rpl_sid;
  363. /**
  364. This has the functionality of mysql_rwlock_t, with two differences:
  365. 1. It has additional operations to check if the read and/or write lock
  366. is held at the moment.
  367. 2. It is wrapped in an object-oriented interface.
  368. Note that the assertions do not check whether *this* thread has
  369. taken the lock (that would be more complicated as it would require a
  370. dynamic data structure). Luckily, it is still likely that the
  371. assertions find bugs where a thread forgot to take a lock, because
  372. most of the time most locks are only used by one thread at a time.
  373. The assertions are no-ops when DBUG is off.
  374. */
  375. class Checkable_rwlock {
  376. public:
  377. /// Initialize this Checkable_rwlock.
  378. Checkable_rwlock(
  379. #if defined(HAVE_PSI_INTERFACE)
  380. PSI_rwlock_key psi_key MY_ATTRIBUTE((unused)) = 0
  381. #endif
  382. ) {
  383. #ifndef DBUG_OFF
  384. lock_state.store(0);
  385. dbug_trace = true;
  386. #else
  387. is_write_lock = false;
  388. #endif
  389. #if defined(HAVE_PSI_INTERFACE)
  390. mysql_rwlock_init(psi_key, &rwlock);
  391. #else
  392. mysql_rwlock_init(0, &rwlock);
  393. #endif
  394. }
  395. /// Destroy this Checkable_lock.
  396. ~Checkable_rwlock() { mysql_rwlock_destroy(&rwlock); }
  397. /// Acquire the read lock.
  398. inline void rdlock() {
  399. mysql_rwlock_rdlock(&rwlock);
  400. assert_no_wrlock();
  401. #ifndef DBUG_OFF
  402. if (dbug_trace) DBUG_PRINT("info", ("%p.rdlock()", this));
  403. ++lock_state;
  404. #endif
  405. }
  406. /// Acquire the write lock.
  407. inline void wrlock() {
  408. mysql_rwlock_wrlock(&rwlock);
  409. assert_no_lock();
  410. #ifndef DBUG_OFF
  411. if (dbug_trace) DBUG_PRINT("info", ("%p.wrlock()", this));
  412. lock_state.store(-1);
  413. #else
  414. is_write_lock = true;
  415. #endif
  416. }
  417. /// Release the lock (whether it is a write or read lock).
  418. inline void unlock() {
  419. assert_some_lock();
  420. #ifndef DBUG_OFF
  421. if (dbug_trace) DBUG_PRINT("info", ("%p.unlock()", this));
  422. int val = lock_state.load();
  423. if (val > 0)
  424. --lock_state;
  425. else if (val == -1)
  426. lock_state.store(0);
  427. else
  428. DBUG_ASSERT(0);
  429. #else
  430. is_write_lock = false;
  431. #endif
  432. mysql_rwlock_unlock(&rwlock);
  433. }
  434. /**
  435. Return true if the write lock is held. Must only be called by
  436. threads that hold a lock.
  437. */
  438. inline bool is_wrlock() {
  439. assert_some_lock();
  440. #ifndef DBUG_OFF
  441. return get_state() == -1;
  442. #else
  443. return is_write_lock;
  444. #endif
  445. }
  446. /**
  447. Return 0 if the write lock is held, otherwise an error will be returned.
  448. */
  449. inline int trywrlock() {
  450. int ret = mysql_rwlock_trywrlock(&rwlock);
  451. if (ret == 0) {
  452. assert_no_lock();
  453. #ifndef DBUG_OFF
  454. if (dbug_trace) DBUG_PRINT("info", ("%p.wrlock()", this));
  455. lock_state.store(-1);
  456. #else
  457. is_write_lock = true;
  458. #endif
  459. }
  460. return ret;
  461. }
  462. /// Assert that some thread holds either the read or the write lock.
  463. inline void assert_some_lock() const { DBUG_ASSERT(get_state() != 0); }
  464. /// Assert that some thread holds the read lock.
  465. inline void assert_some_rdlock() const { DBUG_ASSERT(get_state() > 0); }
  466. /// Assert that some thread holds the write lock.
  467. inline void assert_some_wrlock() const { DBUG_ASSERT(get_state() == -1); }
  468. /// Assert that no thread holds the write lock.
  469. inline void assert_no_wrlock() const { DBUG_ASSERT(get_state() >= 0); }
  470. /// Assert that no thread holds the read lock.
  471. inline void assert_no_rdlock() const { DBUG_ASSERT(get_state() <= 0); }
  472. /// Assert that no thread holds read or write lock.
  473. inline void assert_no_lock() const { DBUG_ASSERT(get_state() == 0); }
  474. #ifndef DBUG_OFF
  475. /// If enabled, print any lock/unlock operations to the DBUG trace.
  476. bool dbug_trace;
  477. private:
  478. /**
  479. The state of the lock:
  480. 0 - not locked
  481. -1 - write locked
  482. >0 - read locked by that many threads
  483. */
  484. std::atomic<int32> lock_state;
  485. /// Read lock_state atomically and return the value.
  486. inline int32 get_state() const { return lock_state.load(); }
  487. #else
  488. private:
  489. bool is_write_lock;
  490. #endif
  491. /// The rwlock.
  492. mysql_rwlock_t rwlock;
  493. };
  494. /// Protects Gtid_state. See comment above gtid_state for details.
  495. extern Checkable_rwlock *global_sid_lock;
  496. /// One of the locks that protects GTID_MODE. See
  497. /// get_gtid_mode(enum_gtid_mode_lock).
  498. extern Checkable_rwlock *gtid_mode_lock;
  499. /**
  500. Represents a bidirectional map between SID and SIDNO.
  501. SIDNOs are always numbers greater or equal to 1.
  502. This data structure OPTIONALLY knows of a read-write lock that
  503. protects the number of SIDNOs. The lock is provided by the invoker
  504. of the constructor and it is generally the caller's responsibility
  505. to acquire the read lock. If the lock is not NULL, access methods
  506. assert that the caller already holds the read (or write) lock. If
  507. the lock is not NULL and a method of this class grows the number of
  508. SIDNOs, then the method temporarily upgrades this lock to a write
  509. lock and then degrades it to a read lock again; there will be a
  510. short period when the lock is not held at all.
  511. */
  512. class Sid_map {
  513. public:
  514. /**
  515. Create this Sid_map.
  516. @param sid_lock Read-write lock that protects updates to the
  517. number of SIDNOs.
  518. */
  519. Sid_map(Checkable_rwlock *sid_lock);
  520. /// Destroy this Sid_map.
  521. ~Sid_map();
  522. /**
  523. Clears this Sid_map (for RESET SLAVE)
  524. @return RETURN_STATUS_OK or RETURN_STAUTS_REPORTED_ERROR
  525. */
  526. enum_return_status clear();
  527. /**
  528. Add the given SID to this map if it does not already exist.
  529. The caller must hold the read lock or write lock on sid_lock
  530. before invoking this function. If the SID does not exist in this
  531. map, it will release the read lock, take a write lock, update the
  532. map, release the write lock, and take the read lock again.
  533. @param sid The SID.
  534. @retval SIDNO The SIDNO for the SID (a new SIDNO if the SID did
  535. not exist, an existing if it did exist).
  536. @retval negative Error. This function calls my_error.
  537. */
  538. rpl_sidno add_sid(const rpl_sid &sid);
  539. /**
  540. Get the SIDNO for a given SID
  541. The caller must hold the read lock on sid_lock before invoking
  542. this function.
  543. @param sid The SID.
  544. @retval SIDNO if the given SID exists in this map.
  545. @retval 0 if the given SID does not exist in this map.
  546. */
  547. rpl_sidno sid_to_sidno(const rpl_sid &sid) const {
  548. if (sid_lock != nullptr) sid_lock->assert_some_lock();
  549. const auto it = _sid_to_sidno.find(sid);
  550. if (it == _sid_to_sidno.end()) return 0;
  551. return it->second->sidno;
  552. }
  553. /**
  554. Get the SID for a given SIDNO.
  555. Raises an assertion if the SIDNO is not valid.
  556. If need_lock is true, acquires sid_lock->rdlock; otherwise asserts
  557. that it is held already.
  558. @param sidno The SIDNO.
  559. @param need_lock If true, and sid_lock!=NULL, this function will
  560. acquire sid_lock before looking up the sid, and then release
  561. it. If false, and sid_lock!=NULL, this function will assert the
  562. sid_lock is already held. If sid_lock==NULL, nothing is done
  563. w.r.t. locking.
  564. @retval NULL The SIDNO does not exist in this map.
  565. @retval pointer Pointer to the SID. The data is shared with this
  566. Sid_map, so should not be modified. It is safe to read the data
  567. even after this Sid_map is modified, but not if this Sid_map is
  568. destroyed.
  569. */
  570. const rpl_sid &sidno_to_sid(rpl_sidno sidno, bool need_lock = false) const {
  571. if (sid_lock != nullptr) {
  572. if (need_lock)
  573. sid_lock->rdlock();
  574. else
  575. sid_lock->assert_some_lock();
  576. }
  577. DBUG_ASSERT(sidno >= 1 && sidno <= get_max_sidno());
  578. const rpl_sid &ret = (_sidno_to_sid[sidno - 1])->sid;
  579. if (sid_lock != nullptr && need_lock) sid_lock->unlock();
  580. return ret;
  581. }
  582. /**
  583. Return the n'th smallest sidno, in the order of the SID's UUID.
  584. The caller must hold the read or write lock on sid_lock before
  585. invoking this function.
  586. @param n A number in the interval [0, get_max_sidno()-1], inclusively.
  587. */
  588. rpl_sidno get_sorted_sidno(rpl_sidno n) const {
  589. if (sid_lock != nullptr) sid_lock->assert_some_lock();
  590. return _sorted[n];
  591. }
  592. /**
  593. Return the biggest sidno in this Sid_map.
  594. The caller must hold the read or write lock on sid_lock before
  595. invoking this function.
  596. */
  597. rpl_sidno get_max_sidno() const {
  598. if (sid_lock != nullptr) sid_lock->assert_some_lock();
  599. return static_cast<rpl_sidno>(_sidno_to_sid.size());
  600. }
  601. /// Return the sid_lock.
  602. Checkable_rwlock *get_sid_lock() const { return sid_lock; }
  603. /**
  604. Deep copy this Sid_map to dest.
  605. The caller must hold:
  606. * the read lock on this sid_lock
  607. * the write lock on the dest sid_lock
  608. before invoking this function.
  609. @param[out] dest The Sid_map to which the sids and sidnos will
  610. be copied.
  611. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  612. */
  613. enum_return_status copy(Sid_map *dest);
  614. private:
  615. /// Node pointed to by both the hash and the array.
  616. struct Node {
  617. rpl_sidno sidno;
  618. rpl_sid sid;
  619. };
  620. static const uchar *sid_map_get_key(const uchar *ptr, size_t *length) {
  621. const Node *node = pointer_cast<const Node *>(ptr);
  622. *length = binary_log::Uuid::BYTE_LENGTH;
  623. return node->sid.bytes;
  624. }
  625. /**
  626. Create a Node from the given SIDNO and SID and add it to
  627. _sidno_to_sid, _sid_to_sidno, and _sorted.
  628. The caller must hold the write lock on sid_lock before invoking
  629. this function.
  630. @param sidno The SIDNO to add.
  631. @param sid The SID to add.
  632. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  633. */
  634. enum_return_status add_node(rpl_sidno sidno, const rpl_sid &sid);
  635. /// Read-write lock that protects updates to the number of SIDNOs.
  636. mutable Checkable_rwlock *sid_lock;
  637. /**
  638. Array that maps SIDNO to SID; the element at index N points to a
  639. Node with SIDNO N-1.
  640. */
  641. Prealloced_array<Node *, 8> _sidno_to_sid;
  642. /**
  643. Hash that maps SID to SIDNO.
  644. */
  645. malloc_unordered_map<rpl_sid, unique_ptr_my_free<Node>, binary_log::Hash_Uuid>
  646. _sid_to_sidno{key_memory_Sid_map_Node};
  647. /**
  648. Array that maps numbers in the interval [0, get_max_sidno()-1] to
  649. SIDNOs, in order of increasing SID.
  650. @see Sid_map::get_sorted_sidno.
  651. */
  652. Prealloced_array<rpl_sidno, 8> _sorted;
  653. };
  654. extern Sid_map *global_sid_map;
  655. /**
  656. Represents a growable array where each element contains a mutex and
  657. a condition variable.
  658. Each element can be locked, unlocked, broadcast, or waited for, and
  659. it is possible to call "THD::enter_cond" for the condition. The
  660. allowed indexes range from 0, inclusive, to get_max_index(),
  661. inclusive. Initially there are zero elements (and get_max_index()
  662. returns -1); more elements can be allocated by calling
  663. ensure_index().
  664. This data structure has a read-write lock that protects the number
  665. of elements. The lock is provided by the invoker of the constructor
  666. and it is generally the caller's responsibility to acquire the read
  667. lock. Access methods assert that the caller already holds the read
  668. (or write) lock. If a method of this class grows the number of
  669. elements, then the method temporarily upgrades this lock to a write
  670. lock and then degrades it to a read lock again; there will be a
  671. short period when the lock is not held at all.
  672. */
  673. class Mutex_cond_array {
  674. public:
  675. /**
  676. Create a new Mutex_cond_array.
  677. @param global_lock Read-write lock that protects updates to the
  678. number of elements.
  679. */
  680. Mutex_cond_array(Checkable_rwlock *global_lock);
  681. /// Destroy this object.
  682. ~Mutex_cond_array();
  683. /// Lock the n'th mutex.
  684. inline void lock(int n) const {
  685. assert_not_owner(n);
  686. mysql_mutex_lock(&get_mutex_cond(n)->mutex);
  687. }
  688. /// Unlock the n'th mutex.
  689. inline void unlock(int n) const {
  690. assert_owner(n);
  691. mysql_mutex_unlock(&get_mutex_cond(n)->mutex);
  692. }
  693. /// Broadcast the n'th condition.
  694. inline void broadcast(int n) const {
  695. mysql_cond_broadcast(&get_mutex_cond(n)->cond);
  696. }
  697. /**
  698. Assert that this thread owns the n'th mutex.
  699. This is a no-op if DBUG_OFF is on.
  700. */
  701. inline void assert_owner(int n MY_ATTRIBUTE((unused))) const {
  702. #ifndef DBUG_OFF
  703. mysql_mutex_assert_owner(&get_mutex_cond(n)->mutex);
  704. #endif
  705. }
  706. /**
  707. Assert that this thread does not own the n'th mutex.
  708. This is a no-op if DBUG_OFF is on.
  709. */
  710. inline void assert_not_owner(int n MY_ATTRIBUTE((unused))) const {
  711. #ifndef DBUG_OFF
  712. mysql_mutex_assert_not_owner(&get_mutex_cond(n)->mutex);
  713. #endif
  714. }
  715. /**
  716. Wait for signal on the n'th condition variable.
  717. The caller must hold the read lock or write lock on sid_lock, as
  718. well as the nth mutex lock, before invoking this function. The
  719. sid_lock will be released, whereas the mutex will be released
  720. during the wait and (atomically) re-acquired when the wait ends
  721. or the timeout is reached.
  722. @param[in] thd THD object for the calling thread.
  723. @param[in] sidno Condition variable to wait for.
  724. @param[in] abstime The absolute point in time when the wait times
  725. out and stops, or NULL to wait indefinitely.
  726. @retval false Success.
  727. @retval true Failure: either timeout or thread was killed. If
  728. thread was killed, the error has been generated.
  729. */
  730. inline bool wait(const THD *thd, int sidno, struct timespec *abstime) const {
  731. DBUG_ENTER("Mutex_cond_array::wait");
  732. int error = 0;
  733. Mutex_cond *mutex_cond = get_mutex_cond(sidno);
  734. global_lock->unlock();
  735. mysql_mutex_assert_owner(&mutex_cond->mutex);
  736. if (is_thd_killed(thd)) DBUG_RETURN(true);
  737. if (abstime != nullptr)
  738. error =
  739. mysql_cond_timedwait(&mutex_cond->cond, &mutex_cond->mutex, abstime);
  740. else
  741. mysql_cond_wait(&mutex_cond->cond, &mutex_cond->mutex);
  742. mysql_mutex_assert_owner(&mutex_cond->mutex);
  743. DBUG_RETURN(is_timeout(error));
  744. }
  745. #ifdef MYSQL_SERVER
  746. /// Execute THD::enter_cond for the n'th condition variable.
  747. void enter_cond(THD *thd, int n, PSI_stage_info *stage,
  748. PSI_stage_info *old_stage) const;
  749. #endif // ifdef MYSQL_SERVER
  750. /// Return the greatest addressable index in this Mutex_cond_array.
  751. inline int get_max_index() const {
  752. global_lock->assert_some_lock();
  753. return static_cast<int>(m_array.size() - 1);
  754. }
  755. /**
  756. Grows the array so that the given index fits.
  757. If the array is grown, the global_lock is temporarily upgraded to
  758. a write lock and then degraded again; there will be a
  759. short period when the lock is not held at all.
  760. @param n The index.
  761. @return RETURN_OK or RETURN_REPORTED_ERROR
  762. */
  763. enum_return_status ensure_index(int n);
  764. private:
  765. /**
  766. Return true if the given THD is killed.
  767. @param[in] thd - The thread object
  768. @retval true - thread is killed
  769. false - thread not killed
  770. */
  771. bool is_thd_killed(const THD *thd) const;
  772. /// A mutex/cond pair.
  773. struct Mutex_cond {
  774. mysql_mutex_t mutex;
  775. mysql_cond_t cond;
  776. };
  777. /// Return the Nth Mutex_cond object
  778. inline Mutex_cond *get_mutex_cond(int n) const {
  779. global_lock->assert_some_lock();
  780. DBUG_ASSERT(n <= get_max_index());
  781. Mutex_cond *ret = m_array[n];
  782. DBUG_ASSERT(ret);
  783. return ret;
  784. }
  785. /// Read-write lock that protects updates to the number of elements.
  786. mutable Checkable_rwlock *global_lock;
  787. Prealloced_array<Mutex_cond *, 8> m_array;
  788. };
  789. /**
  790. Holds information about a GTID interval: the sidno, the first gno
  791. and the last gno of this interval.
  792. */
  793. struct Gtid_interval {
  794. /* SIDNO of this Gtid interval. */
  795. rpl_sidno sidno;
  796. /* The first GNO of this Gtid interval. */
  797. rpl_gno gno_start;
  798. /* The last GNO of this Gtid interval. */
  799. rpl_gno gno_end;
  800. void set(rpl_sidno sid_no, rpl_gno start, rpl_gno end) {
  801. sidno = sid_no;
  802. gno_start = start;
  803. gno_end = end;
  804. }
  805. };
  806. /**
  807. TODO: Move this structure to libbinlogevents/include/control_events.h
  808. when we start using C++11.
  809. Holds information about a GTID: the sidno and the gno.
  810. This is a POD. It has to be a POD because it is part of
  811. Gtid_specification, which has to be a POD because it is used in
  812. THD::variables.
  813. */
  814. struct Gtid {
  815. /// SIDNO of this Gtid.
  816. rpl_sidno sidno;
  817. /// GNO of this Gtid.
  818. rpl_gno gno;
  819. /// Set both components to 0.
  820. void clear() {
  821. sidno = 0;
  822. gno = 0;
  823. }
  824. /// Set both components to the given, positive values.
  825. void set(rpl_sidno sidno_arg, rpl_gno gno_arg) {
  826. DBUG_ASSERT(sidno_arg > 0);
  827. DBUG_ASSERT(gno_arg > 0);
  828. sidno = sidno_arg;
  829. gno = gno_arg;
  830. }
  831. /**
  832. Return true if sidno is zero (and assert that gno is zero too in
  833. this case).
  834. */
  835. bool is_empty() const {
  836. // check that gno is not set inconsistently
  837. if (sidno <= 0)
  838. DBUG_ASSERT(gno == 0);
  839. else
  840. DBUG_ASSERT(gno > 0);
  841. return sidno == 0;
  842. }
  843. /**
  844. The maximal length of the textual representation of a SID, not
  845. including the terminating '\0'.
  846. */
  847. static const int MAX_TEXT_LENGTH =
  848. binary_log::Uuid::TEXT_LENGTH + 1 + MAX_GNO_TEXT_LENGTH;
  849. /**
  850. Return true if parse() would succeed, but don't store the
  851. result anywhere.
  852. */
  853. static bool is_valid(const char *text);
  854. /**
  855. Convert a Gtid to a string.
  856. @param sid the sid to use. This overrides the sidno of this Gtid.
  857. @param[out] buf Buffer to store the Gtid in (normally
  858. MAX_TEXT_LENGTH+1 bytes long).
  859. @return Length of the string, not counting '\0'.
  860. */
  861. int to_string(const rpl_sid &sid, char *buf) const;
  862. /**
  863. Convert this Gtid to a string.
  864. @param sid_map sid_map to use when converting sidno to a SID.
  865. @param[out] buf Buffer to store the Gtid in (normally
  866. MAX_TEXT_LENGTH+1 bytes long).
  867. @param need_lock If true, the function will acquire sid_map->sid_lock;
  868. otherwise it will assert that the lock is held.
  869. @return Length of the string, not counting '\0'.
  870. */
  871. int to_string(const Sid_map *sid_map, char *buf,
  872. bool need_lock = false) const;
  873. /// Returns true if this Gtid has the same sid and gno as 'other'.
  874. bool equals(const Gtid &other) const {
  875. return sidno == other.sidno && gno == other.gno;
  876. }
  877. /**
  878. Parses the given string and stores in this Gtid.
  879. @param sid_map sid_map to use when converting SID to a sidno.
  880. @param text The text to parse
  881. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  882. */
  883. enum_return_status parse(Sid_map *sid_map, const char *text);
  884. #ifndef DBUG_OFF
  885. /// Debug only: print this Gtid to stdout.
  886. void print(const Sid_map *sid_map) const {
  887. char buf[MAX_TEXT_LENGTH + 1];
  888. to_string(sid_map, buf);
  889. printf("%s\n", buf);
  890. }
  891. #endif
  892. /// Print this Gtid to the trace file if debug is enabled; no-op otherwise.
  893. void dbug_print(const Sid_map *sid_map MY_ATTRIBUTE((unused)),
  894. const char *text MY_ATTRIBUTE((unused)) = "",
  895. bool need_lock MY_ATTRIBUTE((unused)) = false) const {
  896. #ifndef DBUG_OFF
  897. char buf[MAX_TEXT_LENGTH + 1];
  898. to_string(sid_map, buf, need_lock);
  899. DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", buf));
  900. #endif
  901. }
  902. };
  903. /// Structure to store the GTID and timing information.
  904. struct Trx_monitoring_info {
  905. /// GTID being monitored.
  906. Gtid gtid;
  907. /// OCT of the GTID being monitored.
  908. ulonglong original_commit_timestamp;
  909. /// ICT of the GTID being monitored.
  910. ulonglong immediate_commit_timestamp;
  911. /// When the GTID transaction started to be processed.
  912. ulonglong start_time;
  913. /// When the GTID transaction finished to be processed.
  914. ulonglong end_time;
  915. /// True if the GTID is being applied but will be skipped.
  916. bool skipped;
  917. /// True when this information contains useful data.
  918. bool is_info_set;
  919. /// Number of the last transient error of this transaction
  920. uint last_transient_error_number;
  921. /// Message of the last transient error of this transaction
  922. char last_transient_error_message[MAX_SLAVE_ERRMSG];
  923. /// Timestamp in microseconds of the last transient error of this transaction
  924. ulonglong last_transient_error_timestamp;
  925. /// Number of times this transaction was retried
  926. ulong transaction_retries;
  927. /// True when the transaction is retrying
  928. bool is_retrying;
  929. /// Constructor
  930. Trx_monitoring_info();
  931. /// Copy constructor
  932. Trx_monitoring_info(const Trx_monitoring_info &info);
  933. Trx_monitoring_info &operator=(const Trx_monitoring_info &) = default;
  934. /// Clear all fields of the structure.
  935. void clear();
  936. /**
  937. Copies this transaction monitoring information to the output parameters
  938. passed as input, which are the corresponding fields in a replication
  939. performance schema table.
  940. @param[in] sid_map The SID map for the GTID.
  941. @param[out] gtid_arg GTID field in the PS table.
  942. @param[out] gtid_length_arg Length of the GTID as string.
  943. @param[out] original_commit_ts_arg The original commit timestamp.
  944. @param[out] immediate_commit_ts_arg The immediate commit timestamp.
  945. @param[out] start_time_arg The start time field.
  946. */
  947. void copy_to_ps_table(Sid_map *sid_map, char *gtid_arg, uint *gtid_length_arg,
  948. ulonglong *original_commit_ts_arg,
  949. ulonglong *immediate_commit_ts_arg,
  950. ulonglong *start_time_arg);
  951. /**
  952. Copies this transaction monitoring information to the output parameters
  953. passed as input, which are the corresponding fields in a replication
  954. performance schema table.
  955. @param[in] sid_map The SID map for the GTID.
  956. @param[out] gtid_arg GTID field in the PS table.
  957. @param[out] gtid_length_arg Length of the GTID as string.
  958. @param[out] original_commit_ts_arg The original commit timestamp.
  959. @param[out] immediate_commit_ts_arg The immediate commit timestamp.
  960. @param[out] start_time_arg The start time field.
  961. @param[out] end_time_arg The end time field. This can be null
  962. when the PS table fields are for the
  963. "still processing" information.
  964. */
  965. void copy_to_ps_table(Sid_map *sid_map, char *gtid_arg, uint *gtid_length_arg,
  966. ulonglong *original_commit_ts_arg,
  967. ulonglong *immediate_commit_ts_arg,
  968. ulonglong *start_time_arg, ulonglong *end_time_arg);
  969. /**
  970. Copies this transaction monitoring information to the output parameters
  971. passed as input, which are the corresponding fields in a replication
  972. performance schema table.
  973. @param[in] sid_map The SID map for the GTID.
  974. @param[out] gtid_arg GTID field in the PS table.
  975. @param[out] gtid_length_arg Length of the GTID as string.
  976. @param[out] original_commit_ts_arg The original commit timestamp.
  977. @param[out] immediate_commit_ts_arg The immediate commit timestamp.
  978. @param[out] start_time_arg The start time field.
  979. @param[out] last_transient_errno_arg The last transient error
  980. number.
  981. @param[out] last_transient_errmsg_arg The last transient error
  982. message.
  983. @param[out] last_transient_errmsg_length_arg Length of the last transient
  984. error message.
  985. @param[out] last_transient_timestamp_arg The last transient error
  986. timestamp.
  987. @param[out] retries_count_arg The total number of retries for
  988. this transaction.
  989. */
  990. void copy_to_ps_table(
  991. Sid_map *sid_map, char *gtid_arg, uint *gtid_length_arg,
  992. ulonglong *original_commit_ts_arg, ulonglong *immediate_commit_ts_arg,
  993. ulonglong *start_time_arg, uint *last_transient_errno_arg,
  994. char *last_transient_errmsg_arg, uint *last_transient_errmsg_length_arg,
  995. ulonglong *last_transient_timestamp_arg, ulong *retries_count_arg);
  996. /**
  997. Copies this transaction monitoring information to the output parameters
  998. passed as input, which are the corresponding fields in a replication
  999. performance schema table.
  1000. @param[in] sid_map The SID map for the GTID.
  1001. @param[out] gtid_arg GTID field in the PS table.
  1002. @param[out] gtid_length_arg Length of the GTID as string.
  1003. @param[out] original_commit_ts_arg The original commit timestamp.
  1004. @param[out] immediate_commit_ts_arg The immediate commit timestamp.
  1005. @param[out] start_time_arg The start time field.
  1006. @param[out] end_time_arg The end time field. This can be
  1007. null when the PS table fields
  1008. are for the "still processing"
  1009. information.
  1010. @param[out] last_transient_errno_arg The last transient error
  1011. number.
  1012. @param[out] last_transient_errmsg_arg The last transient error
  1013. message.
  1014. @param[out] last_transient_errmsg_length_arg Length of the last transient
  1015. error message.
  1016. @param[out] last_transient_timestamp_arg The last transient error
  1017. timestamp.
  1018. @param[out] retries_count_arg The total number of retries for
  1019. this transaction.
  1020. */
  1021. void copy_to_ps_table(Sid_map *sid_map, char *gtid_arg, uint *gtid_length_arg,
  1022. ulonglong *original_commit_ts_arg,
  1023. ulonglong *immediate_commit_ts_arg,
  1024. ulonglong *start_time_arg, ulonglong *end_time_arg,
  1025. uint *last_transient_errno_arg,
  1026. char *last_transient_errmsg_arg,
  1027. uint *last_transient_errmsg_length_arg,
  1028. ulonglong *last_transient_timestamp_arg,
  1029. ulong *retries_count_arg);
  1030. };
  1031. /**
  1032. Stores information to monitor a transaction during the different replication
  1033. stages.
  1034. */
  1035. class Gtid_monitoring_info {
  1036. public:
  1037. /**
  1038. Create this GTID monitoring info object.
  1039. @param atomic_mutex_arg When specified, this object will rely on the mutex
  1040. to arbitrate the read/update access to object data.
  1041. This will be used by the receiver thread, relying
  1042. on mi->data_lock. When no mutex is specified, the
  1043. object will rely on its own atomic mechanism.
  1044. */
  1045. Gtid_monitoring_info(mysql_mutex_t *atomic_mutex_arg = nullptr);
  1046. /// Destroy this GTID monitoring info object.
  1047. ~Gtid_monitoring_info();
  1048. protected:
  1049. /// Holds information about transaction being processed.
  1050. Trx_monitoring_info *processing_trx;
  1051. /// Holds information about the last processed transaction.
  1052. Trx_monitoring_info *last_processed_trx;
  1053. private:
  1054. /**
  1055. Mutex arbitrating the atomic access to the object.
  1056. Some Gtid_monitoring_info will rely on replication thread locks
  1057. (i.e.: the Master_info's one rely on mi->data_lock, that is already
  1058. acquired every time the Gtid_monitoring_info needs to be updated).
  1059. Other Gtid_monitoring_info will rely on an atomic lock implemented
  1060. in this class to avoid overlapped reads and writes over the information.
  1061. (i.e.: the Relay_log_info's one sometimes is updated without rli locks).
  1062. When atomic_mutex is NULL, the object will rely on its own atomic
  1063. mechanism.
  1064. */
  1065. mysql_mutex_t *atomic_mutex;
  1066. /// The atomic locked flag.
  1067. std::atomic<bool> atomic_locked{false};
  1068. #ifndef DBUG_OFF
  1069. /// Flag to assert the atomic lock behavior.
  1070. bool is_locked = false;
  1071. #endif
  1072. public:
  1073. /**
  1074. Lock this object when no thread mutex is used to arbitrate the access.
  1075. */
  1076. void atomic_lock();
  1077. /**
  1078. Unlock this object when no thread mutex is used to arbitrate the access.
  1079. */
  1080. void atomic_unlock();
  1081. /**
  1082. Clear all monitoring information.
  1083. */
  1084. void clear();
  1085. /**
  1086. Clear only the processing_trx monitoring info.
  1087. */
  1088. void clear_processing_trx();
  1089. /**
  1090. Clear only the last_processed_trx monitoring info.
  1091. */
  1092. void clear_last_processed_trx();
  1093. /**
  1094. Sets the initial monitoring information.
  1095. @param gtid_arg The Gtid to be stored.
  1096. @param original_ts_arg The original commit timestamp of the GTID.
  1097. @param immediate_ts_arg The immediate commit timestamp of the GTID.
  1098. @param skipped_arg True if the GTID was already applied.
  1099. This only make sense for applier threads.
  1100. That's why it is false by default.
  1101. */
  1102. void start(Gtid gtid_arg, ulonglong original_ts_arg,
  1103. ulonglong immediate_ts_arg, bool skipped_arg = false);
  1104. /**
  1105. Sets the final information, copy processing info to last_processed
  1106. and clears processing info.
  1107. */
  1108. void finish();
  1109. /**
  1110. Copies both processing_trx and last_processed_trx info to other
  1111. Trx_monitoring_info structures.
  1112. @param[out] processing_dest The destination of processing_trx.
  1113. @param[out] last_processed_dest The destination of last_processed_trx.
  1114. */
  1115. void copy_info_to(Trx_monitoring_info *processing_dest,
  1116. Trx_monitoring_info *last_processed_dest);
  1117. /**
  1118. Copies all monitoring info to other Gtid_monitoring_info object.
  1119. @param[out] dest The destination Gtid_monitoring_info.
  1120. */
  1121. void copy_info_to(Gtid_monitoring_info *dest);
  1122. /// Returns true if the processing_trx is set, false otherwise.
  1123. bool is_processing_trx_set();
  1124. /// Returns the GTID of the processing_trx.
  1125. const Gtid *get_processing_trx_gtid();
  1126. /**
  1127. Stores the information about the last transient error in the current
  1128. transaction, namely: the error number, message and total number of retries.
  1129. It also sets the timestamp for this error.
  1130. @param transient_errno_arg The number of the transient error in this
  1131. transaction.
  1132. @param transient_err_message_arg The message of this transient error.
  1133. @param trans_retries_arg The number of times this transaction has
  1134. been retried.
  1135. */
  1136. void store_transient_error(uint transient_errno_arg,
  1137. const char *transient_err_message_arg,
  1138. ulong trans_retries_arg);
  1139. };
  1140. /**
  1141. Represents a set of GTIDs.
  1142. This is structured as an array, indexed by SIDNO, where each element
  1143. contains a linked list of intervals.
  1144. This data structure OPTIONALLY knows of a Sid_map that gives a
  1145. correspondence between SIDNO and SID. If the Sid_map is NULL, then
  1146. operations that require a Sid_map - printing and parsing - raise an
  1147. assertion.
  1148. This data structure OPTIONALLY knows of a read-write lock that
  1149. protects the number of SIDNOs. The lock is provided by the invoker
  1150. of the constructor and it is generally the caller's responsibility
  1151. to acquire the read lock. If the lock is not NULL, access methods
  1152. assert that the caller already holds the read (or write) lock. If
  1153. the lock is not NULL and a method of this class grows the number of
  1154. SIDNOs, then the method temporarily upgrades this lock to a write
  1155. lock and then degrades it to a read lock again; there will be a
  1156. short period when the lock is not held at all.
  1157. */
  1158. class Gtid_set {
  1159. public:
  1160. static PSI_mutex_key key_gtid_executed_free_intervals_mutex;
  1161. /**
  1162. Constructs a new, empty Gtid_set.
  1163. @param sid_map The Sid_map to use, or NULL if this Gtid_set
  1164. should not have a Sid_map.
  1165. @param sid_lock Read-write lock that protects updates to the
  1166. number of SIDs. This may be NULL if such changes do not need to be
  1167. protected.
  1168. */
  1169. Gtid_set(Sid_map *sid_map, Checkable_rwlock *sid_lock = nullptr);
  1170. /**
  1171. Constructs a new Gtid_set that contains the gtids in the given
  1172. string, in the same format as add_gtid_text(char *).
  1173. @param sid_map The Sid_map to use for SIDs.
  1174. @param text The text to parse.
  1175. @param status Will be set to RETURN_STATUS_OK on success or
  1176. RETURN_STATUS_REPORTED_ERROR on error.
  1177. @param sid_lock Read/write lock to protect changes in the number
  1178. of SIDs with. This may be NULL if such changes do not need to be
  1179. protected.
  1180. If sid_lock != NULL, then the read lock on sid_lock must be held
  1181. before calling this function. If the array is grown, sid_lock is
  1182. temporarily upgraded to a write lock and then degraded again;
  1183. there will be a short period when the lock is not held at all.
  1184. */
  1185. Gtid_set(Sid_map *sid_map, const char *text, enum_return_status *status,
  1186. Checkable_rwlock *sid_lock = nullptr);
  1187. private:
  1188. /// Worker for the constructor.
  1189. void init();
  1190. public:
  1191. /// Destroy this Gtid_set.
  1192. ~Gtid_set();
  1193. /**
  1194. Removes all gtids from this Gtid_set.
  1195. This does not deallocate anything: if gtids are added later,
  1196. existing allocated memory will be re-used.
  1197. */
  1198. void clear();
  1199. /**
  1200. Removes all gtids from this Gtid_set and clear all the sidnos
  1201. used by the Gtid_set and it's SID map.
  1202. This does not deallocate anything: if gtids are added later,
  1203. existing allocated memory will be re-used.
  1204. */
  1205. void clear_set_and_sid_map();
  1206. /**
  1207. Adds the given GTID to this Gtid_set.
  1208. The SIDNO must exist in the Gtid_set before this function is called.
  1209. @param sidno SIDNO of the GTID to add.
  1210. @param gno GNO of the GTID to add.
  1211. */
  1212. void _add_gtid(rpl_sidno sidno, rpl_gno gno) {
  1213. DBUG_ENTER("Gtid_set::_add_gtid(sidno, gno)");
  1214. Interval_iterator ivit(this, sidno);
  1215. Free_intervals_lock lock(this);
  1216. add_gno_interval(&ivit, gno, gno + 1, &lock);
  1217. DBUG_VOID_RETURN;
  1218. }
  1219. /**
  1220. Removes the given GTID from this Gtid_set.
  1221. @param sidno SIDNO of the GTID to remove.
  1222. @param gno GNO of the GTID to remove.
  1223. */
  1224. void _remove_gtid(rpl_sidno sidno, rpl_gno gno) {
  1225. DBUG_ENTER("Gtid_set::_remove_gtid(rpl_sidno, rpl_gno)");
  1226. if (sidno <= get_max_sidno()) {
  1227. Interval_iterator ivit(this, sidno);
  1228. Free_intervals_lock lock(this);
  1229. remove_gno_interval(&ivit, gno, gno + 1, &lock);
  1230. }
  1231. DBUG_VOID_RETURN;
  1232. }
  1233. /**
  1234. Adds the given GTID to this Gtid_set.
  1235. The SIDNO must exist in the Gtid_set before this function is called.
  1236. @param gtid Gtid to add.
  1237. */
  1238. void _add_gtid(const Gtid &gtid) { _add_gtid(gtid.sidno, gtid.gno); }
  1239. /**
  1240. Removes the given GTID from this Gtid_set.
  1241. @param gtid Gtid to remove.
  1242. */
  1243. void _remove_gtid(const Gtid &gtid) { _remove_gtid(gtid.sidno, gtid.gno); }
  1244. /**
  1245. Adds all gtids from the given Gtid_set to this Gtid_set.
  1246. If sid_lock != NULL, then the read lock must be held before
  1247. calling this function. If a new sidno is added so that the array
  1248. of lists of intervals is grown, sid_lock is temporarily upgraded
  1249. to a write lock and then degraded again; there will be a short
  1250. period when the lock is not held at all.
  1251. @param other The Gtid_set to add.
  1252. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  1253. */
  1254. enum_return_status add_gtid_set(const Gtid_set *other);
  1255. /**
  1256. Removes all gtids in the given Gtid_set from this Gtid_set.
  1257. @param other The Gtid_set to remove.
  1258. */
  1259. void remove_gtid_set(const Gtid_set *other);
  1260. /**
  1261. Removes all intervals of 'other' for a given SIDNO, from 'this'.
  1262. Example:
  1263. this = A:1-100, B:1-100
  1264. other = A:1-100, B:1-50, C:1-100
  1265. this.remove_intervals_for_sidno(other, B) = A:1-100, B:51-100
  1266. It is not required that the intervals exist in this Gtid_set.
  1267. @param other The set to remove.
  1268. @param sidno The sidno to remove.
  1269. */
  1270. void remove_intervals_for_sidno(Gtid_set *other, rpl_sidno sidno);
  1271. /**
  1272. Adds the set of GTIDs represented by the given string to this Gtid_set.
  1273. The string must have the format of a comma-separated list of zero
  1274. or more of the following items:
  1275. XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX(:NUMBER+(-NUMBER)?)*
  1276. | ANONYMOUS
  1277. Each X is a hexadecimal digit (upper- or lowercase).
  1278. NUMBER is a decimal, 0xhex, or 0oct number.
  1279. The start of an interval must be greater than 0. The end of an
  1280. interval may be 0, but any interval that has an endpoint that
  1281. is smaller than the start is discarded.
  1282. The string can start with an optional '+' appender qualifier
  1283. which triggers @c executed_gtids and @c lost_gtids set examination
  1284. on the matter of disjointness with the one being added.
  1285. If sid_lock != NULL, then the read lock on sid_lock must be held
  1286. before calling this function. If a new sidno is added so that the
  1287. array of lists of intervals is grown, sid_lock is temporarily
  1288. upgraded to a write lock and then degraded again; there will be a
  1289. short period when the lock is not held at all.
  1290. @param text The string to parse.
  1291. @param [in,out] anonymous If this is NULL, ANONYMOUS is not
  1292. allowed. If this is not NULL, it will be set to true if the
  1293. anonymous GTID was found; false otherwise.
  1294. @param[in,out] starts_with_plus If this is not NULL, the string may
  1295. optionally begin with a '+' character, and *starts_with_plus will
  1296. be set to true if the plus character is present. If this is NULL,
  1297. no plus is allowed at the begin of the string.
  1298. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  1299. */
  1300. enum_return_status add_gtid_text(const char *text, bool *anonymous = nullptr,
  1301. bool *starts_with_plus = nullptr);
  1302. /**
  1303. Decodes a Gtid_set from the given string.
  1304. @param encoded The string to parse.
  1305. @param length The number of bytes.
  1306. @param actual_length If this is not NULL, it is set to the number
  1307. of bytes used by the encoding (which may be less than 'length').
  1308. If this is NULL, an error is generated if the encoding is shorter
  1309. than the given 'length'.
  1310. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  1311. */
  1312. enum_return_status add_gtid_encoding(const uchar *encoded, size_t length,
  1313. size_t *actual_length = nullptr);
  1314. /// Return true iff the given GTID exists in this set.
  1315. bool contains_gtid(rpl_sidno sidno, rpl_gno gno) const;
  1316. /// Return true iff the given GTID exists in this set.
  1317. bool contains_gtid(const Gtid &gtid) const {
  1318. return contains_gtid(gtid.sidno, gtid.gno);
  1319. }
  1320. // Get last gno or 0 if this set is empty.
  1321. rpl_gno get_last_gno(rpl_sidno sidno) const;
  1322. /// Returns the maximal sidno that this Gtid_set currently has space for.
  1323. rpl_sidno get_max_sidno() const {
  1324. if (sid_lock) sid_lock->assert_some_lock();
  1325. return static_cast<rpl_sidno>(m_intervals.size());
  1326. }
  1327. /**
  1328. Allocates space for all sidnos up to the given sidno in the array of
  1329. intervals. The sidno must exist in the Sid_map associated with this
  1330. Gtid_set.
  1331. If sid_lock != NULL, then the read lock on sid_lock must be held
  1332. before calling this function. If the array is grown, sid_lock is
  1333. temporarily upgraded to a write lock and then degraded again;
  1334. there will be a short period when the lock is not held at all.
  1335. @param sidno The SIDNO.
  1336. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  1337. */
  1338. enum_return_status ensure_sidno(rpl_sidno sidno);
  1339. /// Returns true if this Gtid_set is a subset of the other Gtid_set.
  1340. bool is_subset(const Gtid_set *super) const;
  1341. /// Returns true if this Gtid_set is a non equal subset of the other Gtid_set.
  1342. bool is_subset_not_equals(const Gtid_set *super) const {
  1343. return (is_subset(super) && !equals(super));
  1344. }
  1345. /**
  1346. Returns true if this Gtid_set is a subset of the given gtid_set
  1347. on the given superset_sidno and subset_sidno.
  1348. @param super Gtid_set with which this->gtid_set needs to be
  1349. compared
  1350. @param superset_sidno The sidno that will be compared, relative to
  1351. super->sid_map.
  1352. @param subset_sidno The sidno that will be compared, relative to
  1353. this->sid_map.
  1354. @return true If 'this' Gtid_set is subset of given
  1355. 'super' Gtid_set.
  1356. false If 'this' Gtid_set is *not* subset of given
  1357. 'super' Gtid_set.
  1358. */
  1359. bool is_subset_for_sid(const Gtid_set *super, rpl_sidno superset_sidno,
  1360. rpl_sidno subset_sidno) const;
  1361. /// Returns true if there is a least one element of this Gtid_set in
  1362. /// the other Gtid_set.
  1363. bool is_intersection_nonempty(const Gtid_set *other) const;
  1364. /**
  1365. Add the intersection of this Gtid_set and the other Gtid_set to result.
  1366. @param other The Gtid_set to intersect with this Gtid_set
  1367. @param result Gtid_set where the result will be stored.
  1368. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  1369. */
  1370. enum_return_status intersection(const Gtid_set *other, Gtid_set *result);
  1371. /// Returns true if this Gtid_set is empty.
  1372. bool is_empty() const {
  1373. Gtid_iterator git(this);
  1374. return git.get().sidno == 0;
  1375. }
  1376. /**
  1377. What is the count of all the GTIDs in all intervals for a sidno
  1378. @param sidno The sidno that contains the intervals
  1379. @return the number of all GTIDs in all intervals
  1380. */
  1381. ulonglong get_interval_count(rpl_sidno sidno) const {
  1382. Const_interval_iterator ivit(this, sidno);
  1383. ulonglong ret = 0;
  1384. while (ivit.get() != nullptr) {
  1385. ret += ivit.get()->end - ivit.get()->start;
  1386. ivit.next();
  1387. }
  1388. return ret;
  1389. }
  1390. /**
  1391. What is the count of all the GTIDs for all sidno
  1392. @return the number of all GTIDs
  1393. */
  1394. ulonglong get_gtid_number() const {
  1395. if (sid_lock != nullptr) sid_lock->assert_some_wrlock();
  1396. rpl_sidno max_sidno = get_max_sidno();
  1397. ulonglong ret = 0;
  1398. for (rpl_sidno sidno = 1; sidno <= max_sidno; sidno++)
  1399. ret += get_interval_count(sidno);
  1400. return ret;
  1401. }
  1402. /**
  1403. Returns true if this Gtid_set contains at least one GTID with
  1404. the given SIDNO.
  1405. @param sidno The SIDNO to test.
  1406. @retval true The SIDNO is less than or equal to the max SIDNO, and
  1407. there is at least one GTID with this SIDNO.
  1408. @retval false The SIDNO is greater than the max SIDNO, or there is
  1409. no GTID with this SIDNO.
  1410. */
  1411. bool contains_sidno(rpl_sidno sidno) const {
  1412. DBUG_ASSERT(sidno >= 1);
  1413. if (sidno > get_max_sidno()) return false;
  1414. Const_interval_iterator ivit(this, sidno);
  1415. return ivit.get() != nullptr;
  1416. }
  1417. /**
  1418. Returns true if the given string is a valid specification of a
  1419. Gtid_set, false otherwise.
  1420. */
  1421. static bool is_valid(const char *text);
  1422. /**
  1423. Class Gtid_set::String_format defines the separators used by
  1424. Gtid_set::to_string.
  1425. */
  1426. struct String_format {
  1427. /// The generated string begins with this.
  1428. const char *begin;
  1429. /// The generated string begins with this.
  1430. const char *end;
  1431. /// In 'SID:GNO', this is the ':'
  1432. const char *sid_gno_separator;
  1433. /// In 'SID:GNO-GNO', this is the '-'
  1434. const char *gno_start_end_separator;
  1435. /// In 'SID:GNO:GNO', this is the second ':'
  1436. const char *gno_gno_separator;
  1437. /// In 'SID:GNO,SID:GNO', this is the ','
  1438. const char *gno_sid_separator;
  1439. /// If the set is empty and this is not NULL, then this string is generated.
  1440. const char *empty_set_string;
  1441. /// The following fields are the lengths of each field above.
  1442. const int begin_length;
  1443. const int end_length;
  1444. const int sid_gno_separator_length;
  1445. const int gno_start_end_separator_length;
  1446. const int gno_gno_separator_length;
  1447. const int gno_sid_separator_length;
  1448. const int empty_set_string_length;
  1449. };
  1450. /**
  1451. Returns the length of the output from to_string.
  1452. @warning This does not include the trailing '\0', so your buffer
  1453. needs space for get_string_length() + 1 characters.
  1454. @param string_format String_format object that specifies
  1455. separators in the resulting text.
  1456. @return The length.
  1457. */
  1458. size_t get_string_length(const String_format *string_format = nullptr) const;
  1459. /**
  1460. Formats this Gtid_set as a string and saves in a given buffer.
  1461. @param[out] buf Pointer to the buffer where the string should be
  1462. stored. This should have size at least get_string_length()+1.
  1463. @param need_lock If this Gtid_set has a sid_lock, then the write
  1464. lock must be held while generating the string. If this parameter
  1465. is true, then this function acquires and releases the lock;
  1466. otherwise it asserts that the caller holds the lock.
  1467. @param string_format String_format object that specifies
  1468. separators in the resulting text.
  1469. @return Length of the generated string.
  1470. */
  1471. size_t to_string(char *buf, bool need_lock = false,
  1472. const String_format *string_format = nullptr) const;
  1473. /**
  1474. Formats a Gtid_set as a string and saves in a newly allocated buffer.
  1475. @param[out] buf Pointer to pointer to string. The function will
  1476. set it to point to the newly allocated buffer, or NULL on out of memory.
  1477. @param need_lock If this Gtid_set has a sid_lock, then the write
  1478. lock must be held while generating the string. If this parameter
  1479. is true, then this function acquires and releases the lock;
  1480. otherwise it asserts that the caller holds the lock.
  1481. @param string_format Specifies how to format the string.
  1482. @retval Length of the generated string, or -1 on out of memory.
  1483. */
  1484. long to_string(char **buf, bool need_lock = false,
  1485. const String_format *string_format = nullptr) const;
  1486. #ifndef DBUG_OFF
  1487. /// Debug only: Print this Gtid_set to stdout.
  1488. /// For use with C `printf`
  1489. void print(bool need_lock = false,
  1490. const Gtid_set::String_format *sf = nullptr) const {
  1491. char *str;
  1492. to_string(&str, need_lock, sf);
  1493. printf("%s\n", str ? str : "out of memory in Gtid_set::print");
  1494. my_free(str);
  1495. }
  1496. /// For use with C++ `std::ostream`
  1497. inline friend std::ostream &operator<<(std::ostream &os, const Gtid_set &in) {
  1498. char *str;
  1499. in.to_string(&str, true, nullptr);
  1500. os << std::string(str) << std::flush;
  1501. my_free(str);
  1502. return os;
  1503. }
  1504. #endif
  1505. /**
  1506. Print this Gtid_set to the trace file if debug is enabled; no-op
  1507. otherwise.
  1508. */
  1509. void dbug_print(const char *text MY_ATTRIBUTE((unused)) = "",
  1510. bool need_lock MY_ATTRIBUTE((unused)) = false,
  1511. const Gtid_set::String_format *sf MY_ATTRIBUTE((unused)) =
  1512. nullptr) const {
  1513. #ifndef DBUG_OFF
  1514. char *str;
  1515. to_string(&str, need_lock, sf);
  1516. DBUG_PRINT("info", ("%s%s'%s'", text, *text ? ": " : "",
  1517. str ? str : "out of memory in Gtid_set::dbug_print"));
  1518. my_free(str);
  1519. #endif
  1520. }
  1521. /**
  1522. Gets all gtid intervals from this Gtid_set.
  1523. @param[out] gtid_intervals Store all gtid intervals from this Gtid_set.
  1524. */
  1525. void get_gtid_intervals(std::list<Gtid_interval> *gtid_intervals) const;
  1526. /**
  1527. The default String_format: the format understood by
  1528. add_gtid_text(const char *).
  1529. */
  1530. static const String_format default_string_format;
  1531. /**
  1532. String_format useful to generate an SQL string: the string is
  1533. wrapped in single quotes and there is a newline between SIDs.
  1534. */
  1535. static const String_format sql_string_format;
  1536. /**
  1537. String_format for printing the Gtid_set commented: the string is
  1538. not quote-wrapped, and every SID is on a new line with a leading '# '.
  1539. */
  1540. static const String_format commented_string_format;
  1541. /// Return the Sid_map associated with this Gtid_set.
  1542. Sid_map *get_sid_map() const { return sid_map; }
  1543. /**
  1544. Represents one element in the linked list of intervals associated
  1545. with a SIDNO.
  1546. */
  1547. struct Interval {
  1548. public:
  1549. /// The first GNO of this interval.
  1550. rpl_gno start;
  1551. /// The first GNO after this interval.
  1552. rpl_gno end;
  1553. /// Return true iff this interval is equal to the given interval.
  1554. bool equals(const Interval &other) const {
  1555. return start == other.start && end == other.end;
  1556. }
  1557. /// Pointer to next interval in list.
  1558. Interval *next;
  1559. };
  1560. /**
  1561. Provides an array of Intervals that this Gtid_set can use when
  1562. gtids are subsequently added. This can be used as an
  1563. optimization, to reduce allocation for sets that have a known
  1564. number of intervals.
  1565. @param n_intervals The number of intervals to add.
  1566. @param intervals_param Array of n_intervals intervals.
  1567. */
  1568. void add_interval_memory(int n_intervals, Interval *intervals_param) {
  1569. if (sid_lock != nullptr) mysql_mutex_lock(&free_intervals_mutex);
  1570. add_interval_memory_lock_taken(n_intervals, intervals_param);
  1571. if (sid_lock != nullptr) mysql_mutex_unlock(&free_intervals_mutex);
  1572. }
  1573. /**
  1574. Iterator over intervals for a given SIDNO.
  1575. This is an abstract template class, used as a common base class
  1576. for Const_interval_iterator and Interval_iterator.
  1577. The iterator always points to an interval pointer. The interval
  1578. pointer is either the initial pointer into the list, or the next
  1579. pointer of one of the intervals in the list.
  1580. */
  1581. template <typename Gtid_set_p, typename Interval_p>
  1582. class Interval_iterator_base {
  1583. public:
  1584. /**
  1585. Construct a new iterator over the GNO intervals for a given Gtid_set.
  1586. @param gtid_set The Gtid_set.
  1587. @param sidno The SIDNO.
  1588. */
  1589. Interval_iterator_base(Gtid_set_p gtid_set, rpl_sidno sidno) {
  1590. DBUG_ASSERT(sidno >= 1 && sidno <= gtid_set->get_max_sidno());
  1591. init(gtid_set, sidno);
  1592. }
  1593. /// Construct a new iterator over the free intervals of a Gtid_set.
  1594. Interval_iterator_base(Gtid_set_p gtid_set) {
  1595. p = const_cast<Interval_p *>(&gtid_set->free_intervals);
  1596. }
  1597. /// Reset this iterator.
  1598. inline void init(Gtid_set_p gtid_set, rpl_sidno sidno) {
  1599. p = const_cast<Interval_p *>(&gtid_set->m_intervals[sidno - 1]);
  1600. }
  1601. /// Advance current_elem one step.
  1602. inline void next() {
  1603. DBUG_ASSERT(*p != nullptr);
  1604. p = const_cast<Interval_p *>(&(*p)->next);
  1605. }
  1606. /// Return current_elem.
  1607. inline Interval_p get() const { return *p; }
  1608. protected:
  1609. /**
  1610. Holds the address of the 'next' pointer of the previous element,
  1611. or the address of the initial pointer into the list, if the
  1612. current element is the first element.
  1613. */
  1614. Interval_p *p;
  1615. };
  1616. /**
  1617. Iterator over intervals of a const Gtid_set.
  1618. */
  1619. class Const_interval_iterator
  1620. : public Interval_iterator_base<const Gtid_set *, const Interval *> {
  1621. public:
  1622. /// Create this Const_interval_iterator.
  1623. Const_interval_iterator(const Gtid_set *gtid_set, rpl_sidno sidno)
  1624. : Interval_iterator_base<const Gtid_set *, const Interval *>(gtid_set,
  1625. sidno) {}
  1626. /// Create this Const_interval_iterator.
  1627. Const_interval_iterator(const Gtid_set *gtid_set)
  1628. : Interval_iterator_base<const Gtid_set *, const Interval *>(gtid_set) {
  1629. }
  1630. };
  1631. /**
  1632. Iterator over intervals of a non-const Gtid_set, with additional
  1633. methods to modify the Gtid_set.
  1634. */
  1635. class Interval_iterator
  1636. : public Interval_iterator_base<Gtid_set *, Interval *> {
  1637. public:
  1638. /// Create this Interval_iterator.
  1639. Interval_iterator(Gtid_set *gtid_set, rpl_sidno sidno)
  1640. : Interval_iterator_base<Gtid_set *, Interval *>(gtid_set, sidno) {}
  1641. /// Destroy this Interval_iterator.
  1642. Interval_iterator(Gtid_set *gtid_set)
  1643. : Interval_iterator_base<Gtid_set *, Interval *>(gtid_set) {}
  1644. private:
  1645. /**
  1646. Set current_elem to the given Interval but do not touch the
  1647. next pointer of the given Interval.
  1648. */
  1649. inline void set(Interval *iv) { *p = iv; }
  1650. /// Insert the given element before current_elem.
  1651. inline void insert(Interval *iv) {
  1652. iv->next = *p;
  1653. set(iv);
  1654. }
  1655. /// Remove current_elem.
  1656. inline void remove(Gtid_set *gtid_set) {
  1657. DBUG_ASSERT(get() != nullptr);
  1658. Interval *next = (*p)->next;
  1659. gtid_set->put_free_interval(*p);
  1660. set(next);
  1661. }
  1662. /**
  1663. Only Gtid_set is allowed to use set/insert/remove.
  1664. They are not safe to use from other code because: (1) very easy
  1665. to make a mistakes (2) they don't clear cached_string_format or
  1666. cached_string_length.
  1667. */
  1668. friend class Gtid_set;
  1669. };
  1670. /**
  1671. Iterator over all gtids in a Gtid_set. This is a const
  1672. iterator; it does not allow modification of the Gtid_set.
  1673. */
  1674. class Gtid_iterator {
  1675. public:
  1676. Gtid_iterator(const Gtid_set *gs) : gtid_set(gs), sidno(0), ivit(gs) {
  1677. if (gs->sid_lock != nullptr) gs->sid_lock->assert_some_wrlock();
  1678. next_sidno();
  1679. }
  1680. /// Advance to next gtid.
  1681. inline void next() {
  1682. DBUG_ASSERT(gno > 0 && sidno > 0);
  1683. // go to next GTID in current interval
  1684. gno++;
  1685. // end of interval? then go to next interval for this sidno
  1686. if (gno == ivit.get()->end) {
  1687. ivit.next();
  1688. const Interval *iv = ivit.get();
  1689. // last interval for this sidno? then go to next sidno
  1690. if (iv == nullptr) {
  1691. next_sidno();
  1692. // last sidno? then don't try more
  1693. if (sidno == 0) return;
  1694. iv = ivit.get();
  1695. }
  1696. gno = iv->start;
  1697. }
  1698. }
  1699. /// Return next gtid, or {0,0} if we reached the end.
  1700. inline Gtid get() const {
  1701. Gtid ret = {sidno, gno};
  1702. return ret;
  1703. }
  1704. private:
  1705. /// Find the next sidno that has one or more intervals.
  1706. inline void next_sidno() {
  1707. const Interval *iv;
  1708. do {
  1709. sidno++;
  1710. if (sidno > gtid_set->get_max_sidno()) {
  1711. sidno = 0;
  1712. gno = 0;
  1713. return;
  1714. }
  1715. ivit.init(gtid_set, sidno);
  1716. iv = ivit.get();
  1717. } while (iv == nullptr);
  1718. gno = iv->start;
  1719. }
  1720. /// The Gtid_set we iterate over.
  1721. const Gtid_set *gtid_set;
  1722. /**
  1723. The SIDNO of the current element, or 0 if the iterator is past
  1724. the last element.
  1725. */
  1726. rpl_sidno sidno;
  1727. /**
  1728. The GNO of the current element, or 0 if the iterator is past the
  1729. last element.
  1730. */
  1731. rpl_gno gno;
  1732. /// Iterator over the intervals for the current SIDNO.
  1733. Const_interval_iterator ivit;
  1734. };
  1735. public:
  1736. /**
  1737. Encodes this Gtid_set as a binary string.
  1738. */
  1739. void encode(uchar *buf) const;
  1740. /**
  1741. Returns the length of this Gtid_set when encoded using the
  1742. encode() function.
  1743. */
  1744. size_t get_encoded_length() const;
  1745. private:
  1746. /**
  1747. Contains a list of intervals allocated by this Gtid_set. When a
  1748. method of this class needs a new interval and there are no more
  1749. free intervals, a new Interval_chunk is allocated and the
  1750. intervals of it are added to the list of free intervals.
  1751. */
  1752. struct Interval_chunk {
  1753. Interval_chunk *next;
  1754. Interval intervals[1];
  1755. };
  1756. /// The default number of intervals in an Interval_chunk.
  1757. static const int CHUNK_GROW_SIZE = 8;
  1758. /**
  1759. Return true if the given sidno of this Gtid_set contains the same
  1760. intervals as the given sidno of the other Gtid_set.
  1761. @param sidno SIDNO to check for this Gtid_set.
  1762. @param other Other Gtid_set
  1763. @param other_sidno SIDNO to check in other.
  1764. @return true if equal, false is not equal.
  1765. */
  1766. bool sidno_equals(rpl_sidno sidno, const Gtid_set *other,
  1767. rpl_sidno other_sidno) const;
  1768. /// Returns true if this Gtid_set is equal to the other Gtid_set.
  1769. bool equals(const Gtid_set *other) const;
  1770. /// Return the number of intervals for the given sidno.
  1771. int get_n_intervals(rpl_sidno sidno) const {
  1772. Const_interval_iterator ivit(this, sidno);
  1773. int ret = 0;
  1774. while (ivit.get() != nullptr) {
  1775. ret++;
  1776. ivit.next();
  1777. }
  1778. return ret;
  1779. }
  1780. /// Return the number of intervals in this Gtid_set.
  1781. int get_n_intervals() const {
  1782. if (sid_lock != nullptr) sid_lock->assert_some_wrlock();
  1783. rpl_sidno max_sidno = get_max_sidno();
  1784. int ret = 0;
  1785. for (rpl_sidno sidno = 1; sidno < max_sidno; sidno++)
  1786. ret += get_n_intervals(sidno);
  1787. return ret;
  1788. }
  1789. /**
  1790. Allocates a new chunk of Intervals and adds them to the list of
  1791. unused intervals.
  1792. @param size The number of intervals in this chunk
  1793. */
  1794. void create_new_chunk(int size);
  1795. /**
  1796. Returns a fresh new Interval object.
  1797. This usually does not require any real allocation, it only pops
  1798. the first interval from the list of free intervals. If there are
  1799. no free intervals, it calls create_new_chunk.
  1800. @param out The resulting Interval* will be stored here.
  1801. */
  1802. void get_free_interval(Interval **out);
  1803. /**
  1804. Puts the given interval in the list of free intervals. Does not
  1805. unlink it from its place in any other list.
  1806. */
  1807. void put_free_interval(Interval *iv);
  1808. /**
  1809. Like add_interval_memory, but does not acquire
  1810. free_intervals_mutex.
  1811. @see Gtid_set::add_interval_memory
  1812. */
  1813. void add_interval_memory_lock_taken(int n_ivs, Interval *ivs);
  1814. /// Read-write lock that protects updates to the number of SIDs.
  1815. mutable Checkable_rwlock *sid_lock;
  1816. /**
  1817. Lock protecting the list of free intervals. This lock is only
  1818. used if sid_lock is not NULL.
  1819. */
  1820. mysql_mutex_t free_intervals_mutex;
  1821. /**
  1822. Class representing a lock on free_intervals_mutex.
  1823. This is used by the add_* and remove_* functions. The lock is
  1824. declared by the top-level function and a pointer to the lock is
  1825. passed down to low-level functions. If the low-level function
  1826. decides to access the free intervals list, then it acquires the
  1827. lock. The lock is then automatically released by the destructor
  1828. when the top-level function returns.
  1829. The lock is not taken if Gtid_set->sid_lock == NULL; such
  1830. Gtid_sets are assumed to be thread-local.
  1831. */
  1832. class Free_intervals_lock {
  1833. public:
  1834. /// Create a new lock, but do not acquire it.
  1835. Free_intervals_lock(Gtid_set *_gtid_set)
  1836. : gtid_set(_gtid_set), locked(false) {}
  1837. /// Lock the lock if it is not already locked.
  1838. void lock_if_not_locked() {
  1839. if (gtid_set->sid_lock && !locked) {
  1840. mysql_mutex_lock(&gtid_set->free_intervals_mutex);
  1841. locked = true;
  1842. }
  1843. }
  1844. /// Lock the lock if it is locked.
  1845. void unlock_if_locked() {
  1846. if (gtid_set->sid_lock && locked) {
  1847. mysql_mutex_unlock(&gtid_set->free_intervals_mutex);
  1848. locked = false;
  1849. }
  1850. }
  1851. /// Destroy this object and unlock the lock if it is locked.
  1852. ~Free_intervals_lock() { unlock_if_locked(); }
  1853. private:
  1854. Gtid_set *gtid_set;
  1855. bool locked;
  1856. };
  1857. void assert_free_intervals_locked() {
  1858. if (sid_lock != nullptr) mysql_mutex_assert_owner(&free_intervals_mutex);
  1859. }
  1860. /**
  1861. Adds the interval (start, end) to the given Interval_iterator.
  1862. This is the lowest-level function that adds gtids; this is where
  1863. Interval objects are added, grown, or merged.
  1864. @param ivitp Pointer to iterator. After this function returns,
  1865. the current_element of the iterator will be the interval that
  1866. contains start and end.
  1867. @param start The first GNO in the interval.
  1868. @param end The first GNO after the interval.
  1869. @param lock If this function has to add or remove an interval,
  1870. then this lock will be taken unless it is already taken. This
  1871. mechanism means that the lock will be taken lazily by
  1872. e.g. add_gtid_set() the first time that the list of free intervals
  1873. is accessed, and automatically released when add_gtid_set()
  1874. returns.
  1875. */
  1876. void add_gno_interval(Interval_iterator *ivitp, rpl_gno start, rpl_gno end,
  1877. Free_intervals_lock *lock);
  1878. /**
  1879. Removes the interval (start, end) from the given
  1880. Interval_iterator. This is the lowest-level function that removes
  1881. gtids; this is where Interval objects are removed, truncated, or
  1882. split.
  1883. It is not required that the gtids in the interval exist in this
  1884. Gtid_set.
  1885. @param ivitp Pointer to iterator. After this function returns,
  1886. the current_element of the iterator will be the next interval
  1887. after end.
  1888. @param start The first GNO in the interval.
  1889. @param end The first GNO after the interval.
  1890. @param lock If this function has to add or remove an interval,
  1891. then this lock will be taken unless it is already taken. This
  1892. mechanism means that the lock will be taken lazily by
  1893. e.g. add_gtid_set() the first time that the list of free intervals
  1894. is accessed, and automatically released when add_gtid_set()
  1895. returns.
  1896. */
  1897. void remove_gno_interval(Interval_iterator *ivitp, rpl_gno start, rpl_gno end,
  1898. Free_intervals_lock *lock);
  1899. /**
  1900. Adds a list of intervals to the given SIDNO.
  1901. The SIDNO must exist in the Gtid_set before this function is called.
  1902. @param sidno The SIDNO to which intervals will be added.
  1903. @param ivit Iterator over the intervals to add. This is typically
  1904. an iterator over some other Gtid_set.
  1905. @param lock If this function has to add or remove an interval,
  1906. then this lock will be taken unless it is already taken. This
  1907. mechanism means that the lock will be taken lazily by
  1908. e.g. add_gtid_set() the first time that the list of free intervals
  1909. is accessed, and automatically released when add_gtid_set()
  1910. returns.
  1911. */
  1912. void add_gno_intervals(rpl_sidno sidno, Const_interval_iterator ivit,
  1913. Free_intervals_lock *lock);
  1914. /**
  1915. Removes a list of intervals from the given SIDNO.
  1916. It is not required that the intervals exist in this Gtid_set.
  1917. @param sidno The SIDNO from which intervals will be removed.
  1918. @param ivit Iterator over the intervals to remove. This is typically
  1919. an iterator over some other Gtid_set.
  1920. @param lock If this function has to add or remove an interval,
  1921. then this lock will be taken unless it is already taken. This
  1922. mechanism means that the lock will be taken lazily by
  1923. e.g. add_gtid_set() the first time that the list of free intervals
  1924. is accessed, and automatically released when add_gtid_set()
  1925. returns.
  1926. */
  1927. void remove_gno_intervals(rpl_sidno sidno, Const_interval_iterator ivit,
  1928. Free_intervals_lock *lock);
  1929. /// Returns true if every interval of sub is a subset of some
  1930. /// interval of super.
  1931. static bool is_interval_subset(Const_interval_iterator *sub,
  1932. Const_interval_iterator *super);
  1933. /// Returns true if at least one sidno in ivit1 is also in ivit2.
  1934. static bool is_interval_intersection_nonempty(Const_interval_iterator *ivit1,
  1935. Const_interval_iterator *ivit2);
  1936. /// Sid_map associated with this Gtid_set.
  1937. Sid_map *sid_map;
  1938. /**
  1939. Array where the N'th element contains the head pointer to the
  1940. intervals of SIDNO N+1.
  1941. */
  1942. Prealloced_array<Interval *, 8> m_intervals;
  1943. /// Linked list of free intervals.
  1944. Interval *free_intervals;
  1945. /// Linked list of chunks.
  1946. Interval_chunk *chunks;
  1947. /// If the string is cached.
  1948. mutable bool has_cached_string_length;
  1949. /// The string length.
  1950. mutable size_t cached_string_length;
  1951. /// The String_format that was used when cached_string_length was computed.
  1952. mutable const String_format *cached_string_format;
  1953. #ifndef DBUG_OFF
  1954. /**
  1955. The number of chunks. Used only to check some invariants when
  1956. DBUG is on.
  1957. */
  1958. int n_chunks;
  1959. #endif
  1960. /// Used by unit tests that need to access private members.
  1961. #ifdef FRIEND_OF_GTID_SET
  1962. friend FRIEND_OF_GTID_SET;
  1963. #endif
  1964. /// Only Free_intervals_lock is allowed to access free_intervals_mutex.
  1965. friend class Gtid_set::Free_intervals_lock;
  1966. };
  1967. /**
  1968. Holds information about a Gtid_set. Can also be NULL.
  1969. This is used as backend storage for @@session.gtid_next_list. The
  1970. idea is that we allow the user to set this to NULL, but we keep the
  1971. Gtid_set object so that we can re-use the allocated memory and
  1972. avoid costly allocations later.
  1973. This is stored in struct system_variables (defined in sql_class.h),
  1974. which is cleared using memset(0); hence the negated form of
  1975. is_non_null.
  1976. The convention is: if is_non_null is false, then the value of the
  1977. session variable is NULL, and the field gtid_set may be NULL or
  1978. non-NULL. If is_non_null is true, then the value of the session
  1979. variable is not NULL, and the field gtid_set has to be non-NULL.
  1980. This is a POD. It has to be a POD because it is stored in
  1981. THD::variables.
  1982. */
  1983. struct Gtid_set_or_null {
  1984. /// Pointer to the Gtid_set.
  1985. Gtid_set *gtid_set;
  1986. /// True if this Gtid_set is NULL.
  1987. bool is_non_null;
  1988. /// Return NULL if this is NULL, otherwise return the Gtid_set.
  1989. inline Gtid_set *get_gtid_set() const {
  1990. DBUG_ASSERT(!(is_non_null && gtid_set == nullptr));
  1991. return is_non_null ? gtid_set : nullptr;
  1992. }
  1993. /**
  1994. Do nothing if this object is non-null; set to empty set otherwise.
  1995. @return NULL if out of memory; Gtid_set otherwise.
  1996. */
  1997. Gtid_set *set_non_null(Sid_map *sm) {
  1998. if (!is_non_null) {
  1999. if (gtid_set == nullptr)
  2000. gtid_set = new Gtid_set(sm);
  2001. else
  2002. gtid_set->clear();
  2003. }
  2004. is_non_null = (gtid_set != nullptr);
  2005. return gtid_set;
  2006. }
  2007. /// Set this Gtid_set to NULL.
  2008. inline void set_null() { is_non_null = false; }
  2009. };
  2010. /**
  2011. Represents the set of GTIDs that are owned by some thread.
  2012. This data structure has a read-write lock that protects the number
  2013. of SIDNOs. The lock is provided by the invoker of the constructor
  2014. and it is generally the caller's responsibility to acquire the read
  2015. lock. Access methods assert that the caller already holds the read
  2016. (or write) lock. If a method of this class grows the number of
  2017. SIDNOs, then the method temporarily upgrades this lock to a write
  2018. lock and then degrades it to a read lock again; there will be a
  2019. short period when the lock is not held at all.
  2020. The internal representation is a multi-valued map from GTIDs to
  2021. threads, mapping GTIDs to one or more threads that owns it.
  2022. In Group Replication multiple threads can own a GTID whereas if GR
  2023. is disabeld there is at most one owner per GTID.
  2024. */
  2025. class Owned_gtids {
  2026. public:
  2027. /**
  2028. Constructs a new, empty Owned_gtids object.
  2029. @param sid_lock Read-write lock that protects updates to the
  2030. number of SIDs.
  2031. */
  2032. Owned_gtids(Checkable_rwlock *sid_lock);
  2033. /// Destroys this Owned_gtids.
  2034. ~Owned_gtids();
  2035. /**
  2036. Add a GTID to this Owned_gtids.
  2037. @param gtid The Gtid to add.
  2038. @param owner The my_thread_id of the gtid to add.
  2039. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  2040. */
  2041. enum_return_status add_gtid_owner(const Gtid &gtid, my_thread_id owner);
  2042. /*
  2043. Fill all gtids into the given Gtid_set object. It doesn't clear the given
  2044. gtid set before filling its owned gtids into it.
  2045. */
  2046. void get_gtids(Gtid_set &gtid_set) const;
  2047. /**
  2048. Removes the given GTID.
  2049. If the gtid does not exist in this Owned_gtids object, does
  2050. nothing.
  2051. @param gtid The Gtid.
  2052. @param owner thread_id of the owner thread
  2053. */
  2054. void remove_gtid(const Gtid &gtid, const my_thread_id owner);
  2055. /**
  2056. Ensures that this Owned_gtids object can accomodate SIDNOs up to
  2057. the given SIDNO.
  2058. If this Owned_gtids object needs to be resized, then the lock
  2059. will be temporarily upgraded to a write lock and then degraded to
  2060. a read lock again; there will be a short period when the lock is
  2061. not held at all.
  2062. @param sidno The SIDNO.
  2063. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  2064. */
  2065. enum_return_status ensure_sidno(rpl_sidno sidno);
  2066. /// Returns true if there is a least one element of this Owned_gtids
  2067. /// set in the other Gtid_set.
  2068. bool is_intersection_nonempty(const Gtid_set *other) const;
  2069. /// Returns true if this Owned_gtids is empty.
  2070. bool is_empty() const {
  2071. Gtid_iterator git(this);
  2072. return git.get().sidno == 0;
  2073. }
  2074. /// Returns the maximal sidno that this Owned_gtids currently has space for.
  2075. rpl_sidno get_max_sidno() const {
  2076. sid_lock->assert_some_lock();
  2077. return static_cast<rpl_sidno>(sidno_to_hash.size());
  2078. }
  2079. /**
  2080. Write a string representation of this Owned_gtids to the given buffer.
  2081. @param out Buffer to write to.
  2082. @return Number of characters written.
  2083. */
  2084. int to_string(char *out) const {
  2085. char *p = out;
  2086. rpl_sidno max_sidno = get_max_sidno();
  2087. rpl_sidno sid_map_max_sidno = global_sid_map->get_max_sidno();
  2088. for (rpl_sidno sid_i = 0; sid_i < sid_map_max_sidno; sid_i++) {
  2089. rpl_sidno sidno = global_sid_map->get_sorted_sidno(sid_i);
  2090. if (sidno > max_sidno) continue;
  2091. bool printed_sid = false;
  2092. for (const auto &key_and_value : *get_hash(sidno)) {
  2093. Node *node = key_and_value.second.get();
  2094. DBUG_ASSERT(node != nullptr);
  2095. if (!printed_sid) {
  2096. p += global_sid_map->sidno_to_sid(sidno).to_string(p);
  2097. printed_sid = true;
  2098. }
  2099. p += sprintf(p, ":%lld#%u", node->gno, node->owner);
  2100. }
  2101. }
  2102. *p = 0;
  2103. return (int)(p - out);
  2104. }
  2105. /**
  2106. Return an upper bound on the length of the string representation
  2107. of this Owned_gtids. The actual length may be smaller. This
  2108. includes the trailing '\0'.
  2109. */
  2110. size_t get_max_string_length() const {
  2111. rpl_sidno max_sidno = get_max_sidno();
  2112. size_t ret = 0;
  2113. for (rpl_sidno sidno = 1; sidno <= max_sidno; sidno++) {
  2114. size_t records = get_hash(sidno)->size();
  2115. if (records > 0)
  2116. ret +=
  2117. binary_log::Uuid::TEXT_LENGTH +
  2118. records * (1 + MAX_GNO_TEXT_LENGTH + 1 + MAX_THREAD_ID_TEXT_LENGTH);
  2119. }
  2120. return 1 + ret;
  2121. }
  2122. /**
  2123. Return true if the given thread is the owner of any gtids.
  2124. */
  2125. bool thread_owns_anything(my_thread_id thd_id) const {
  2126. Gtid_iterator git(this);
  2127. Node *node = git.get_node();
  2128. while (node != nullptr) {
  2129. if (node->owner == thd_id) return true;
  2130. git.next();
  2131. node = git.get_node();
  2132. }
  2133. return false;
  2134. }
  2135. #ifndef DBUG_OFF
  2136. /**
  2137. Debug only: return a newly allocated string representation of
  2138. this Owned_gtids.
  2139. */
  2140. char *to_string() const {
  2141. char *str = (char *)my_malloc(key_memory_Owned_gtids_to_string,
  2142. get_max_string_length(), MYF(MY_WME));
  2143. DBUG_ASSERT(str != nullptr);
  2144. to_string(str);
  2145. return str;
  2146. }
  2147. /// Debug only: print this Owned_gtids to stdout.
  2148. void print() const {
  2149. char *str = to_string();
  2150. printf("%s\n", str);
  2151. my_free(str);
  2152. }
  2153. #endif
  2154. /**
  2155. Print this Owned_gtids to the trace file if debug is enabled; no-op
  2156. otherwise.
  2157. */
  2158. void dbug_print(const char *text MY_ATTRIBUTE((unused)) = "") const {
  2159. #ifndef DBUG_OFF
  2160. char *str = to_string();
  2161. DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", str));
  2162. my_free(str);
  2163. #endif
  2164. }
  2165. /**
  2166. If thd_id==0, returns true when gtid is not owned by any thread.
  2167. If thd_id!=0, returns true when gtid is owned by that thread.
  2168. */
  2169. bool is_owned_by(const Gtid &gtid, const my_thread_id thd_id) const;
  2170. private:
  2171. /// Represents one owned GTID.
  2172. struct Node {
  2173. /// GNO of the GTID.
  2174. rpl_gno gno;
  2175. /// Owner of the GTID.
  2176. my_thread_id owner;
  2177. };
  2178. /// Read-write lock that protects updates to the number of SIDs.
  2179. mutable Checkable_rwlock *sid_lock;
  2180. /// Returns the hash for the given SIDNO.
  2181. malloc_unordered_multimap<rpl_gno, unique_ptr_my_free<Node>> *get_hash(
  2182. rpl_sidno sidno) const {
  2183. DBUG_ASSERT(sidno >= 1 && sidno <= get_max_sidno());
  2184. sid_lock->assert_some_lock();
  2185. return sidno_to_hash[sidno - 1];
  2186. }
  2187. /// Return true iff this Owned_gtids object contains the given gtid.
  2188. bool contains_gtid(const Gtid &gtid) const;
  2189. /// Growable array of hashes.
  2190. Prealloced_array<
  2191. malloc_unordered_multimap<rpl_gno, unique_ptr_my_free<Node>> *, 8>
  2192. sidno_to_hash;
  2193. public:
  2194. /**
  2195. Iterator over all gtids in a Owned_gtids set. This is a const
  2196. iterator; it does not allow modification of the set.
  2197. */
  2198. class Gtid_iterator {
  2199. public:
  2200. Gtid_iterator(const Owned_gtids *og)
  2201. : owned_gtids(og), sidno(1), hash(nullptr), node(nullptr) {
  2202. max_sidno = owned_gtids->get_max_sidno();
  2203. if (sidno <= max_sidno) {
  2204. hash = owned_gtids->get_hash(sidno);
  2205. node_it = hash->begin();
  2206. }
  2207. next();
  2208. }
  2209. /// Advance to next GTID.
  2210. inline void next() {
  2211. #ifndef DBUG_OFF
  2212. if (owned_gtids->sid_lock) owned_gtids->sid_lock->assert_some_wrlock();
  2213. #endif
  2214. while (sidno <= max_sidno) {
  2215. DBUG_ASSERT(hash != nullptr);
  2216. if (node_it != hash->end()) {
  2217. node = node_it->second.get();
  2218. DBUG_ASSERT(node != nullptr);
  2219. // Jump to next node on next iteration.
  2220. ++node_it;
  2221. return;
  2222. }
  2223. // hash is initialized on constructor or in previous iteration
  2224. // for current SIDNO, so we must increment for next iteration.
  2225. sidno++;
  2226. if (sidno <= max_sidno) {
  2227. hash = owned_gtids->get_hash(sidno);
  2228. node_it = hash->begin();
  2229. }
  2230. }
  2231. node = nullptr;
  2232. }
  2233. /// Return next GTID, or {0,0} if we reached the end.
  2234. inline Gtid get() const {
  2235. Gtid ret = {0, 0};
  2236. if (node) {
  2237. ret.sidno = sidno;
  2238. ret.gno = node->gno;
  2239. }
  2240. return ret;
  2241. }
  2242. /// Return the current GTID Node, or NULL if we reached the end.
  2243. inline Node *get_node() const { return node; }
  2244. private:
  2245. /// The Owned_gtids set we iterate over.
  2246. const Owned_gtids *owned_gtids;
  2247. /// The SIDNO of the current element, or 1 in the initial iteration.
  2248. rpl_sidno sidno;
  2249. /// Max SIDNO of the current iterator.
  2250. rpl_sidno max_sidno;
  2251. /// Current SIDNO hash.
  2252. malloc_unordered_multimap<rpl_gno, unique_ptr_my_free<Node>> *hash;
  2253. /// Current node iterator on current SIDNO hash.
  2254. malloc_unordered_multimap<rpl_gno, unique_ptr_my_free<Node>>::const_iterator
  2255. node_it;
  2256. /// Current node on current SIDNO hash.
  2257. Node *node;
  2258. };
  2259. };
  2260. /**
  2261. Represents the server's GTID state: the set of committed GTIDs, the
  2262. set of lost gtids, the set of owned gtids, the owner of each owned
  2263. gtid, and a Mutex_cond_array that protects updates to gtids of
  2264. each SIDNO.
  2265. Locking:
  2266. This data structure has a read-write lock that protects the number
  2267. of SIDNOs, and a Mutex_cond_array that contains one mutex per SIDNO.
  2268. The rwlock is always the global_sid_lock.
  2269. Access methods generally assert that the caller already holds the
  2270. appropriate lock:
  2271. - before accessing any global data, hold at least the rdlock.
  2272. - before accessing a specific SIDNO in a Gtid_set or Owned_gtids
  2273. (e.g., calling Gtid_set::_add_gtid(Gtid)), hold either the rdlock
  2274. and the SIDNO's mutex lock; or the wrlock. If you need to hold
  2275. multiple mutexes, they must be acquired in order of increasing
  2276. SIDNO.
  2277. - before starting an operation that needs to access all SIDs
  2278. (e.g. Gtid_set::to_string()), hold the wrlock.
  2279. The access type (read/write) does not matter; the write lock only
  2280. implies that the entire data structure is locked whereas the read
  2281. lock implies that everything except SID-specific data is locked.
  2282. */
  2283. class Gtid_state {
  2284. public:
  2285. /**
  2286. Constructs a new Gtid_state object.
  2287. @param _sid_lock Read-write lock that protects updates to the
  2288. number of SIDs.
  2289. @param _sid_map Sid_map used by this Gtid_state.
  2290. */
  2291. Gtid_state(Checkable_rwlock *_sid_lock, Sid_map *_sid_map)
  2292. : sid_lock(_sid_lock),
  2293. sid_map(_sid_map),
  2294. sid_locks(sid_lock),
  2295. lost_gtids(sid_map, sid_lock),
  2296. executed_gtids(sid_map, sid_lock),
  2297. gtids_only_in_table(sid_map, sid_lock),
  2298. previous_gtids_logged(sid_map, sid_lock),
  2299. owned_gtids(sid_lock),
  2300. commit_group_sidnos(key_memory_Gtid_state_group_commit_sidno) {}
  2301. /**
  2302. Add @@GLOBAL.SERVER_UUID to this binlog's Sid_map.
  2303. This can't be done in the constructor because the constructor is
  2304. invoked at server startup before SERVER_UUID is initialized.
  2305. The caller must hold the read lock or write lock on sid_locks
  2306. before invoking this function.
  2307. @retval 0 Success
  2308. @retval 1 Error (out of memory or IO error).
  2309. */
  2310. int init();
  2311. /**
  2312. Reset the state and persistor after RESET MASTER: remove all logged
  2313. and lost gtids, but keep owned gtids as they are.
  2314. The caller must hold the write lock on sid_lock before calling
  2315. this function.
  2316. @param thd Thread requesting to reset the persistor
  2317. @retval 0 Success
  2318. @retval -1 Error
  2319. */
  2320. int clear(THD *thd);
  2321. /**
  2322. Returns true if the given GTID is logged.
  2323. @param gtid The Gtid to check.
  2324. @retval true The gtid is logged in the binary log.
  2325. @retval false The gtid is not logged in the binary log.
  2326. */
  2327. bool is_executed(const Gtid &gtid) const {
  2328. DBUG_ENTER("Gtid_state::is_executed");
  2329. bool ret = executed_gtids.contains_gtid(gtid);
  2330. DBUG_RETURN(ret);
  2331. }
  2332. /**
  2333. Returns true if GTID is owned, otherwise returns 0.
  2334. @param gtid The Gtid to check.
  2335. @return true if some thread owns the gtid, false if the gtid is
  2336. not owned
  2337. */
  2338. bool is_owned(const Gtid &gtid) const {
  2339. return !owned_gtids.is_owned_by(gtid, 0);
  2340. }
  2341. #ifdef MYSQL_SERVER
  2342. /**
  2343. Acquires ownership of the given GTID, on behalf of the given thread.
  2344. The caller must lock the SIDNO before invoking this function.
  2345. @param thd The thread that will own the GTID.
  2346. @param gtid The Gtid to acquire ownership of.
  2347. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  2348. */
  2349. enum_return_status acquire_ownership(THD *thd, const Gtid &gtid);
  2350. /**
  2351. This function updates both the THD and the Gtid_state to reflect that
  2352. the transaction set of transactions has ended, and it does this for the
  2353. whole commit group (by following the thd->next_to_commit pointer).
  2354. It will:
  2355. - Clean up the thread state when a thread owned GTIDs is empty.
  2356. - Release ownership of all GTIDs owned by the THDs. This removes
  2357. the GTIDs from Owned_gtids and clears the ownership status in the
  2358. THDs object.
  2359. - Add the owned GTIDs to executed_gtids when the thread is committing.
  2360. - Decrease counters of GTID-violating transactions.
  2361. - Send a broadcast on the condition variable for every sidno for
  2362. which we released ownership.
  2363. @param first_thd The first thread of the group commit that needs GTIDs to
  2364. be updated.
  2365. */
  2366. void update_commit_group(THD *first_thd);
  2367. /**
  2368. Remove the GTID owned by thread from owned GTIDs, stating that
  2369. thd->owned_gtid was committed.
  2370. This will:
  2371. - remove owned GTID from owned_gtids;
  2372. - remove all owned GTIDS from thd->owned_gtid and thd->owned_gtid_set;
  2373. @param thd Thread for which owned gtids are updated.
  2374. */
  2375. void update_on_commit(THD *thd);
  2376. /**
  2377. Update the state after the given thread has rollbacked.
  2378. This will:
  2379. - release ownership of all GTIDs owned by the THD;
  2380. - remove owned GTID from owned_gtids;
  2381. - remove all owned GTIDS from thd->owned_gtid and thd->owned_gtid_set;
  2382. - send a broadcast on the condition variable for every sidno for
  2383. which we released ownership.
  2384. @param thd Thread for which owned gtids are updated.
  2385. */
  2386. void update_on_rollback(THD *thd);
  2387. /**
  2388. Acquire anonymous ownership.
  2389. The caller must hold either sid_lock.rdlock or
  2390. sid_lock.wrlock. (The caller must have taken the lock and checked
  2391. that gtid_mode!=ON before calling this function, or else the
  2392. gtid_mode could have changed to ON by a concurrent SET GTID_MODE.)
  2393. */
  2394. void acquire_anonymous_ownership() {
  2395. DBUG_ENTER("Gtid_state::acquire_anonymous_ownership");
  2396. sid_lock->assert_some_lock();
  2397. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_SID) != GTID_MODE_ON);
  2398. #ifndef DBUG_OFF
  2399. int32 new_value =
  2400. #endif
  2401. ++atomic_anonymous_gtid_count;
  2402. DBUG_PRINT("info",
  2403. ("atomic_anonymous_gtid_count increased to %d", new_value));
  2404. DBUG_ASSERT(new_value >= 1);
  2405. DBUG_VOID_RETURN;
  2406. }
  2407. /// Release anonymous ownership.
  2408. void release_anonymous_ownership() {
  2409. DBUG_ENTER("Gtid_state::release_anonymous_ownership");
  2410. sid_lock->assert_some_lock();
  2411. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_SID) != GTID_MODE_ON);
  2412. #ifndef DBUG_OFF
  2413. int32 new_value =
  2414. #endif
  2415. --atomic_anonymous_gtid_count;
  2416. DBUG_PRINT("info",
  2417. ("atomic_anonymous_gtid_count decreased to %d", new_value));
  2418. DBUG_ASSERT(new_value >= 0);
  2419. DBUG_VOID_RETURN;
  2420. }
  2421. /// Return the number of clients that hold anonymous ownership.
  2422. int32 get_anonymous_ownership_count() { return atomic_anonymous_gtid_count; }
  2423. /**
  2424. Increase the global counter when starting a GTID-violating
  2425. transaction having GTID_NEXT=AUTOMATIC.
  2426. */
  2427. void begin_automatic_gtid_violating_transaction() {
  2428. DBUG_ENTER("Gtid_state::begin_automatic_gtid_violating_transaction");
  2429. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_SID) <= GTID_MODE_OFF_PERMISSIVE);
  2430. DBUG_ASSERT(get_gtid_consistency_mode() != GTID_CONSISTENCY_MODE_ON);
  2431. #ifndef DBUG_OFF
  2432. int32 new_value =
  2433. #endif
  2434. ++atomic_automatic_gtid_violation_count;
  2435. DBUG_PRINT(
  2436. "info",
  2437. ("ongoing_automatic_gtid_violating_transaction_count increased to %d",
  2438. new_value));
  2439. DBUG_ASSERT(new_value >= 1);
  2440. DBUG_VOID_RETURN;
  2441. }
  2442. /**
  2443. Decrease the global counter when ending a GTID-violating
  2444. transaction having GTID_NEXT=AUTOMATIC.
  2445. */
  2446. void end_automatic_gtid_violating_transaction() {
  2447. DBUG_ENTER("Gtid_state::end_automatic_gtid_violating_transaction");
  2448. #ifndef DBUG_OFF
  2449. global_sid_lock->rdlock();
  2450. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_SID) <= GTID_MODE_OFF_PERMISSIVE);
  2451. DBUG_ASSERT(get_gtid_consistency_mode() != GTID_CONSISTENCY_MODE_ON);
  2452. global_sid_lock->unlock();
  2453. int32 new_value =
  2454. #endif
  2455. --atomic_automatic_gtid_violation_count;
  2456. DBUG_PRINT(
  2457. "info",
  2458. ("ongoing_automatic_gtid_violating_transaction_count decreased to %d",
  2459. new_value));
  2460. DBUG_ASSERT(new_value >= 0);
  2461. DBUG_VOID_RETURN;
  2462. }
  2463. /**
  2464. Return the number of ongoing GTID-violating transactions having
  2465. GTID_NEXT=AUTOMATIC.
  2466. */
  2467. int32 get_automatic_gtid_violating_transaction_count() {
  2468. return atomic_automatic_gtid_violation_count;
  2469. }
  2470. /**
  2471. Increase the global counter when starting a GTID-violating
  2472. transaction having GTID_NEXT=ANONYMOUS.
  2473. */
  2474. void begin_anonymous_gtid_violating_transaction() {
  2475. DBUG_ENTER("Gtid_state::begin_anonymous_gtid_violating_transaction");
  2476. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_SID) != GTID_MODE_ON);
  2477. DBUG_ASSERT(get_gtid_consistency_mode() != GTID_CONSISTENCY_MODE_ON);
  2478. #ifndef DBUG_OFF
  2479. int32 new_value =
  2480. #endif
  2481. ++atomic_anonymous_gtid_violation_count;
  2482. DBUG_PRINT("info", ("atomic_anonymous_gtid_violation_count increased to %d",
  2483. new_value));
  2484. DBUG_ASSERT(new_value >= 1);
  2485. DBUG_VOID_RETURN;
  2486. }
  2487. /**
  2488. Decrease the global counter when ending a GTID-violating
  2489. transaction having GTID_NEXT=ANONYMOUS.
  2490. */
  2491. void end_anonymous_gtid_violating_transaction() {
  2492. DBUG_ENTER("Gtid_state::end_anonymous_gtid_violating_transaction");
  2493. #ifndef DBUG_OFF
  2494. global_sid_lock->rdlock();
  2495. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_SID) != GTID_MODE_ON);
  2496. DBUG_ASSERT(get_gtid_consistency_mode() != GTID_CONSISTENCY_MODE_ON);
  2497. global_sid_lock->unlock();
  2498. int32 new_value =
  2499. #endif
  2500. --atomic_anonymous_gtid_violation_count;
  2501. DBUG_PRINT(
  2502. "info",
  2503. ("ongoing_anonymous_gtid_violating_transaction_count decreased to %d",
  2504. new_value));
  2505. DBUG_ASSERT(new_value >= 0);
  2506. DBUG_VOID_RETURN;
  2507. }
  2508. void end_gtid_violating_transaction(THD *thd);
  2509. /**
  2510. Return the number of ongoing GTID-violating transactions having
  2511. GTID_NEXT=AUTOMATIC.
  2512. */
  2513. int32 get_anonymous_gtid_violating_transaction_count() {
  2514. return atomic_anonymous_gtid_violation_count;
  2515. }
  2516. /**
  2517. Increase the global counter when starting a call to
  2518. WAIT_FOR_EXECUTED_GTID_SET or WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.
  2519. */
  2520. void begin_gtid_wait(
  2521. enum_gtid_mode_lock gtid_mode_lock MY_ATTRIBUTE((unused))) {
  2522. DBUG_ENTER("Gtid_state::begin_gtid_wait");
  2523. DBUG_ASSERT(get_gtid_mode(gtid_mode_lock) != GTID_MODE_OFF);
  2524. #ifndef DBUG_OFF
  2525. int32 new_value =
  2526. #endif
  2527. ++atomic_gtid_wait_count;
  2528. DBUG_PRINT("info", ("atomic_gtid_wait_count changed from %d to %d",
  2529. new_value - 1, new_value));
  2530. DBUG_ASSERT(new_value >= 1);
  2531. DBUG_VOID_RETURN;
  2532. }
  2533. /**
  2534. Decrease the global counter when ending a call to
  2535. WAIT_FOR_EXECUTED_GTID_SET or WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.
  2536. */
  2537. void end_gtid_wait() {
  2538. DBUG_ENTER("Gtid_state::end_gtid_wait");
  2539. DBUG_ASSERT(get_gtid_mode(GTID_MODE_LOCK_NONE) != GTID_MODE_OFF);
  2540. #ifndef DBUG_OFF
  2541. int32 new_value =
  2542. #endif
  2543. --atomic_gtid_wait_count;
  2544. DBUG_PRINT("info", ("atomic_gtid_wait_count changed from %d to %d",
  2545. new_value + 1, new_value));
  2546. DBUG_ASSERT(new_value >= 0);
  2547. DBUG_VOID_RETURN;
  2548. }
  2549. /**
  2550. Return the number of clients that have an ongoing call to
  2551. WAIT_FOR_EXECUTED_GTID_SET or WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.
  2552. */
  2553. int32 get_gtid_wait_count() { return atomic_gtid_wait_count; }
  2554. #endif // ifdef MYSQL_SERVER
  2555. private:
  2556. /**
  2557. Computes the next available GNO.
  2558. @param sidno The GTID's SIDNO.
  2559. @retval -1 The range of GNOs was exhausted (i.e., more than 1<<63-1
  2560. GTIDs with the same UUID have been generated).
  2561. @retval >0 The GNO for the GTID.
  2562. */
  2563. rpl_gno get_automatic_gno(rpl_sidno sidno) const;
  2564. /**
  2565. The next_free_gno variable will be set with the supposed next free GNO
  2566. every time a new GNO is delivered automatically or when a transaction is
  2567. rolled back, releasing a GNO smaller than the last one delivered.
  2568. It was introduced in an optimization of Gtid_state::get_automatic_gno and
  2569. Gtid_state::generate_automatic_gtid functions.
  2570. Locking scheme
  2571. This variable can be read and modified in four places:
  2572. - During server startup, holding global_sid_lock.wrlock;
  2573. - By a client thread holding global_sid_lock.wrlock (doing a RESET MASTER);
  2574. - By a client thread calling MYSQL_BIN_LOG::write_gtid function (often the
  2575. group commit FLUSH stage leader). It will call
  2576. Gtid_state::generate_automatic_gtid, that will acquire
  2577. global_sid_lock.rdlock and lock_sidno(get_server_sidno()) when getting a
  2578. new automatically generated GTID;
  2579. - By a client thread rolling back, holding global_sid_lock.rdlock
  2580. and lock_sidno(get_server_sidno()).
  2581. */
  2582. rpl_gno next_free_gno;
  2583. public:
  2584. /**
  2585. Return the last executed GNO for a given SIDNO, e.g.
  2586. for the following set: UUID:1-10, UUID:12, UUID:15-20
  2587. 20 will be returned.
  2588. @param sidno The GTID's SIDNO.
  2589. @retval The GNO or 0 if set is empty.
  2590. */
  2591. rpl_gno get_last_executed_gno(rpl_sidno sidno) const;
  2592. /**
  2593. Generates the GTID (or ANONYMOUS, if GTID_MODE = OFF or
  2594. OFF_PERMISSIVE) for the THD, and acquires ownership.
  2595. @param thd The thread.
  2596. @param specified_sidno Externaly generated sidno.
  2597. @param specified_gno Externaly generated gno.
  2598. @param[in,out] locked_sidno This parameter should be used when there is
  2599. a need of generating many GTIDs without having
  2600. to acquire/release a sidno_lock many times.
  2601. The caller must hold global_sid_lock and unlock
  2602. the locked_sidno after invocation when
  2603. locked_sidno > 0 if locked_sidno!=NULL.
  2604. The caller must not hold global_sid_lock when
  2605. locked_sidno==NULL.
  2606. See comments on function code to more details.
  2607. @return RETURN_STATUS_OK or RETURN_STATUS_ERROR. Error can happen
  2608. in case of out of memory or if the range of GNOs was exhausted.
  2609. */
  2610. enum_return_status generate_automatic_gtid(THD *thd,
  2611. rpl_sidno specified_sidno = 0,
  2612. rpl_gno specified_gno = 0,
  2613. rpl_sidno *locked_sidno = nullptr);
  2614. /// Locks a mutex for the given SIDNO.
  2615. void lock_sidno(rpl_sidno sidno) { sid_locks.lock(sidno); }
  2616. /// Unlocks a mutex for the given SIDNO.
  2617. void unlock_sidno(rpl_sidno sidno) { sid_locks.unlock(sidno); }
  2618. /// Broadcasts updates for the given SIDNO.
  2619. void broadcast_sidno(rpl_sidno sidno) { sid_locks.broadcast(sidno); }
  2620. /// Assert that we own the given SIDNO.
  2621. void assert_sidno_lock_owner(rpl_sidno sidno) {
  2622. sid_locks.assert_owner(sidno);
  2623. }
  2624. #ifdef MYSQL_SERVER
  2625. /**
  2626. Wait for a signal on the given SIDNO.
  2627. NOTE: This releases a lock!
  2628. This requires that the caller holds a read lock on sid_lock. It
  2629. will release the lock before waiting; neither global_sid_lock nor
  2630. the mutex lock on SIDNO will not be held when this function
  2631. returns.
  2632. @param thd THD object of the caller.
  2633. @param sidno Sidno to wait for.
  2634. @param[in] abstime The absolute point in time when the wait times
  2635. out and stops, or NULL to wait indefinitely.
  2636. @retval false Success.
  2637. @retval true Failure: either timeout or thread was killed. If
  2638. thread was killed, the error has been generated.
  2639. */
  2640. bool wait_for_sidno(THD *thd, rpl_sidno sidno, struct timespec *abstime);
  2641. /**
  2642. This is only a shorthand for wait_for_sidno, which contains
  2643. additional debug printouts and assertions for the case when the
  2644. caller waits for one specific GTID.
  2645. */
  2646. bool wait_for_gtid(THD *thd, const Gtid &gtid,
  2647. struct timespec *abstime = nullptr);
  2648. /**
  2649. Wait until the given Gtid_set is included in @@GLOBAL.GTID_EXECUTED.
  2650. @param thd The calling thread.
  2651. @param gtid_set Gtid_set to wait for.
  2652. @param[in] timeout The maximum number of milliseconds that the
  2653. function should wait, or 0 to wait indefinitely.
  2654. @retval false Success.
  2655. @retval true Failure: either timeout or thread was killed. If
  2656. thread was killed, the error has been generated.
  2657. */
  2658. bool wait_for_gtid_set(THD *thd, Gtid_set *gtid_set, double timeout);
  2659. #endif // ifdef MYSQL_SERVER
  2660. /**
  2661. Locks one mutex for each SIDNO where the given Gtid_set has at
  2662. least one GTID. Locks are acquired in order of increasing SIDNO.
  2663. */
  2664. void lock_sidnos(const Gtid_set *set);
  2665. /**
  2666. Unlocks the mutex for each SIDNO where the given Gtid_set has at
  2667. least one GTID.
  2668. */
  2669. void unlock_sidnos(const Gtid_set *set);
  2670. /**
  2671. Broadcasts the condition variable for each SIDNO where the given
  2672. Gtid_set has at least one GTID.
  2673. */
  2674. void broadcast_sidnos(const Gtid_set *set);
  2675. /**
  2676. Ensure that owned_gtids, executed_gtids, lost_gtids, gtids_only_in_table,
  2677. previous_gtids_logged and sid_locks have room for at least as many SIDNOs
  2678. as sid_map.
  2679. This function must only be called in one place:
  2680. Sid_map::add_sid().
  2681. Requires that the write lock on sid_locks is held. If any object
  2682. needs to be resized, then the lock will be temporarily upgraded to
  2683. a write lock and then degraded to a read lock again; there will be
  2684. a short period when the lock is not held at all.
  2685. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  2686. */
  2687. enum_return_status ensure_sidno();
  2688. /**
  2689. Adds the given Gtid_set to lost_gtids and executed_gtids.
  2690. lost_gtids must be a subset of executed_gtids.
  2691. purged_gtid and executed_gtid sets are appened with the argument set
  2692. provided the latter is disjoint with gtid_executed owned_gtids.
  2693. Requires that the caller holds global_sid_lock.wrlock.
  2694. @param[in,out] gtid_set The gtid_set to add. If the gtid_set
  2695. does not start with a plus sign (starts_with_plus is false),
  2696. @@GLOBAL.GTID_PURGED will be removed from the gtid_set.
  2697. @param starts_with_plus If true, the gtid_set passed is required to
  2698. be disjoint from @@GLOBAL.GTID_PURGED; if false, the gtid_set passed
  2699. is required to be a superset of @@GLOBAL.GTID_PURGED.
  2700. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  2701. */
  2702. enum_return_status add_lost_gtids(Gtid_set *gtid_set, bool starts_with_plus);
  2703. /** Updates previously logged GTID set before writing to table. */
  2704. void update_prev_gtids(Gtid_set *write_gtid_set);
  2705. /// Return a pointer to the Gtid_set that contains the lost gtids.
  2706. const Gtid_set *get_lost_gtids() const { return &lost_gtids; }
  2707. /*
  2708. Return a pointer to the Gtid_set that contains the stored gtids
  2709. in gtid_executed table.
  2710. */
  2711. const Gtid_set *get_executed_gtids() const { return &executed_gtids; }
  2712. /*
  2713. Return a pointer to the Gtid_set that contains the stored gtids
  2714. only in gtid_executed table, not in binlog files.
  2715. */
  2716. const Gtid_set *get_gtids_only_in_table() const {
  2717. return &gtids_only_in_table;
  2718. }
  2719. /*
  2720. Return a pointer to the Gtid_set that contains the previous stored
  2721. gtids in the last binlog file.
  2722. */
  2723. const Gtid_set *get_previous_gtids_logged() const {
  2724. return &previous_gtids_logged;
  2725. }
  2726. /// Return a pointer to the Owned_gtids that contains the owned gtids.
  2727. const Owned_gtids *get_owned_gtids() const { return &owned_gtids; }
  2728. /// Return the server's SID's SIDNO
  2729. rpl_sidno get_server_sidno() const { return server_sidno; }
  2730. /// Return the server's SID
  2731. const rpl_sid &get_server_sid() const {
  2732. return global_sid_map->sidno_to_sid(server_sidno);
  2733. }
  2734. #ifndef DBUG_OFF
  2735. /**
  2736. Debug only: Returns an upper bound on the length of the string
  2737. generated by to_string(), not counting '\0'. The actual length
  2738. may be shorter.
  2739. */
  2740. size_t get_max_string_length() const {
  2741. return owned_gtids.get_max_string_length() +
  2742. executed_gtids.get_string_length() + lost_gtids.get_string_length() +
  2743. gtids_only_in_table.get_string_length() +
  2744. previous_gtids_logged.get_string_length() + 150;
  2745. }
  2746. /// Debug only: Generate a string in the given buffer and return the length.
  2747. int to_string(char *buf) const {
  2748. char *p = buf;
  2749. p += sprintf(p, "Executed GTIDs:\n");
  2750. p += executed_gtids.to_string(p);
  2751. p += sprintf(p, "\nOwned GTIDs:\n");
  2752. p += owned_gtids.to_string(p);
  2753. p += sprintf(p, "\nLost GTIDs:\n");
  2754. p += lost_gtids.to_string(p);
  2755. p += sprintf(p, "\nGTIDs only_in_table:\n");
  2756. p += lost_gtids.to_string(p);
  2757. return (int)(p - buf);
  2758. }
  2759. /// Debug only: return a newly allocated string, or NULL on out-of-memory.
  2760. char *to_string() const {
  2761. char *str = (char *)my_malloc(key_memory_Gtid_state_to_string,
  2762. get_max_string_length(), MYF(MY_WME));
  2763. to_string(str);
  2764. return str;
  2765. }
  2766. /// Debug only: print this Gtid_state to stdout.
  2767. void print() const {
  2768. char *str = to_string();
  2769. printf("%s", str);
  2770. my_free(str);
  2771. }
  2772. #endif
  2773. /**
  2774. Print this Gtid_state to the trace file if debug is enabled; no-op
  2775. otherwise.
  2776. */
  2777. void dbug_print(const char *text MY_ATTRIBUTE((unused)) = "") const {
  2778. #ifndef DBUG_OFF
  2779. sid_lock->assert_some_wrlock();
  2780. char *str = to_string();
  2781. DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", str));
  2782. my_free(str);
  2783. #endif
  2784. }
  2785. /**
  2786. Save gtid owned by the thd into executed_gtids variable
  2787. and gtid_executed table.
  2788. @param thd Session to commit
  2789. @retval
  2790. 0 OK
  2791. @retval
  2792. -1 Error
  2793. */
  2794. int save(THD *thd);
  2795. /**
  2796. Insert the gtid set into table.
  2797. @param gtid_set contains a set of gtid, which holds
  2798. the sidno and the gno.
  2799. @retval
  2800. 0 OK
  2801. @retval
  2802. -1 Error
  2803. */
  2804. int save(const Gtid_set *gtid_set);
  2805. /**
  2806. Save the set of gtids logged in the last binlog into gtid_executed table.
  2807. @retval
  2808. 0 OK
  2809. @retval
  2810. -1 Error
  2811. */
  2812. int save_gtids_of_last_binlog_into_table();
  2813. /**
  2814. Fetch gtids from gtid_executed table and store them into
  2815. gtid_executed set.
  2816. @retval
  2817. 0 OK
  2818. @retval
  2819. 1 The table was not found.
  2820. @retval
  2821. -1 Error
  2822. */
  2823. int read_gtid_executed_from_table();
  2824. /**
  2825. Compress the gtid_executed table, read each row by the PK(sid, gno_start)
  2826. in increasing order, compress the first consecutive gtids range
  2827. (delete consecutive gtids from the second consecutive gtid, then
  2828. update the first gtid) within a single transaction.
  2829. @param thd Thread requesting to compress the table
  2830. @retval
  2831. 0 OK
  2832. @retval
  2833. 1 The table was not found.
  2834. @retval
  2835. -1 Error
  2836. */
  2837. int compress(THD *thd);
  2838. #ifdef MYSQL_SERVER
  2839. /**
  2840. Push a warning to client if user is modifying the gtid_executed
  2841. table explicitly by a non-XA transaction. Push an error to client
  2842. if user is modifying it explicitly by a XA transaction.
  2843. @param thd Thread requesting to access the table
  2844. @param table The table is being accessed.
  2845. @retval 0 No warning or error was pushed to the client.
  2846. @retval 1 Push a warning to client.
  2847. @retval 2 Push an error to client.
  2848. */
  2849. int warn_or_err_on_modify_gtid_table(THD *thd, TABLE_LIST *table);
  2850. #endif
  2851. private:
  2852. /**
  2853. Remove the GTID owned by thread from owned GTIDs.
  2854. This will:
  2855. - Clean up the thread state if the thread owned GTIDs is empty.
  2856. - Release ownership of all GTIDs owned by the THD. This removes
  2857. the GTID from Owned_gtids and clears the ownership status in the
  2858. THD object.
  2859. - Add the owned GTID to executed_gtids if the is_commit flag is
  2860. set.
  2861. - Decrease counters of GTID-violating transactions.
  2862. - Send a broadcast on the condition variable for every sidno for
  2863. which we released ownership.
  2864. @param[in] thd Thread for which owned gtids are updated.
  2865. @param[in] is_commit If true, the update is for a commit (not a rollback).
  2866. */
  2867. void update_gtids_impl(THD *thd, bool is_commit);
  2868. #ifdef HAVE_GTID_NEXT_LIST
  2869. /// Lock all SIDNOs owned by the given THD.
  2870. void lock_owned_sidnos(const THD *thd);
  2871. #endif
  2872. /// Unlock all SIDNOs owned by the given THD.
  2873. void unlock_owned_sidnos(const THD *thd);
  2874. /// Broadcast the condition for all SIDNOs owned by the given THD.
  2875. void broadcast_owned_sidnos(const THD *thd);
  2876. /// Read-write lock that protects updates to the number of SIDs.
  2877. mutable Checkable_rwlock *sid_lock;
  2878. /// The Sid_map used by this Gtid_state.
  2879. mutable Sid_map *sid_map;
  2880. /// Contains one mutex/cond pair for every SIDNO.
  2881. Mutex_cond_array sid_locks;
  2882. /**
  2883. The set of GTIDs that existed in some previously purged binary log.
  2884. This is always a subset of executed_gtids.
  2885. */
  2886. Gtid_set lost_gtids;
  2887. /*
  2888. The set of GTIDs that has been executed and
  2889. stored into gtid_executed table.
  2890. */
  2891. Gtid_set executed_gtids;
  2892. /*
  2893. The set of GTIDs that exists only in gtid_executed table, not in
  2894. binlog files.
  2895. */
  2896. Gtid_set gtids_only_in_table;
  2897. /* The previous GTIDs in the last binlog. */
  2898. Gtid_set previous_gtids_logged;
  2899. /// The set of GTIDs that are owned by some thread.
  2900. Owned_gtids owned_gtids;
  2901. /// The SIDNO for this server.
  2902. rpl_sidno server_sidno;
  2903. /// The number of anonymous transactions owned by any client.
  2904. std::atomic<int32> atomic_anonymous_gtid_count{0};
  2905. /// The number of GTID-violating transactions that use GTID_NEXT=AUTOMATIC.
  2906. std::atomic<int32> atomic_automatic_gtid_violation_count{0};
  2907. /// The number of GTID-violating transactions that use GTID_NEXT=AUTOMATIC.
  2908. std::atomic<int32> atomic_anonymous_gtid_violation_count{0};
  2909. /// The number of clients that are executing
  2910. /// WAIT_FOR_EXECUTED_GTID_SET or WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS.
  2911. std::atomic<int32> atomic_gtid_wait_count{0};
  2912. /// Used by unit tests that need to access private members.
  2913. #ifdef FRIEND_OF_GTID_STATE
  2914. friend FRIEND_OF_GTID_STATE;
  2915. #endif
  2916. /**
  2917. This is a sub task of update_on_rollback responsible only to handle
  2918. the case of a thread that needs to skip GTID operations when it has
  2919. "failed to commit".
  2920. Administrative commands [CHECK|REPAIR|OPTIMIZE|ANALYZE] TABLE
  2921. are written to the binary log even when they fail. When the
  2922. commands fail, they will call update_on_rollback; later they will
  2923. write the binary log. But we must not do any of the things in
  2924. update_gtids_impl if we are going to write the binary log. So
  2925. these statements set the skip_gtid_rollback flag, which tells
  2926. update_on_rollback to return early. When the statements are
  2927. written to the binary log they will call update_on_commit as
  2928. usual.
  2929. @param[in] thd - Thread to be evaluated.
  2930. @retval true The transaction should skip the rollback, false otherwise.
  2931. */
  2932. bool update_gtids_impl_check_skip_gtid_rollback(THD *thd);
  2933. /**
  2934. This is a sub task of update_gtids_impl responsible only to handle
  2935. the case of a thread that owns nothing and does not violate GTID
  2936. consistency.
  2937. If the THD does not own anything, there is nothing to do, so we can do an
  2938. early return of the update process. Except if there is a GTID consistency
  2939. violation; then we need to decrease the counter, so then we can continue
  2940. executing inside update_gtids_impl.
  2941. @param[in] thd - Thread to be evaluated.
  2942. @retval true The transaction can be skipped because it owns nothing and
  2943. does not violate GTID consistency, false otherwise.
  2944. */
  2945. bool update_gtids_impl_do_nothing(THD *thd);
  2946. /**
  2947. This is a sub task of update_gtids_impl responsible only to evaluate
  2948. if the thread is committing in the middle of a statement by checking
  2949. THD's is_commit_in_middle_of_statement flag.
  2950. This flag is true for anonymous transactions, when the
  2951. 'transaction' has been split into multiple transactions in the
  2952. binlog, and the present transaction is not the last one.
  2953. This means two things:
  2954. - We should not release anonymous ownership in case
  2955. gtid_next=anonymous. If we did, it would be possible for user
  2956. to set GTID_MODE=ON from a concurrent transaction, making it
  2957. impossible to commit the current transaction.
  2958. - We should not decrease the counters for GTID-violating
  2959. statements. If we did, it would be possible for a concurrent
  2960. client to set ENFORCE_GTID_CONSISTENCY=ON despite there is an
  2961. ongoing transaction that violates GTID consistency.
  2962. The flag is set in two cases:
  2963. 1. We are committing the statement cache when there are more
  2964. changes in the transaction cache.
  2965. This happens either because a single statement in the
  2966. beginning of a transaction updates both transactional and
  2967. non-transactional tables, or because we are committing a
  2968. non-transactional update in the middle of a transaction when
  2969. binlog_direct_non_transactional_updates=1.
  2970. In this case, the flag is set further down in this function.
  2971. 2. The statement is one of the special statements that may
  2972. generate multiple transactions: CREATE...SELECT, DROP TABLE,
  2973. DROP DATABASE. See comment for THD::owned_gtid in
  2974. sql/sql_class.h.
  2975. In this case, the THD::is_commit_in_middle_of_statement flag
  2976. is set by the caller and the flag becomes true here.
  2977. @param[in] thd - Thread to be evaluated.
  2978. @return The value of thread's is_commit_in_middle_of_statement flag.
  2979. */
  2980. bool update_gtids_impl_begin(THD *thd);
  2981. /**
  2982. Handle the case that the thread own a set of GTIDs.
  2983. This is a sub task of update_gtids_impl responsible only to handle
  2984. the case of a thread with a set of GTIDs being updated.
  2985. - Release ownership of the GTIDs owned by the THD. This removes
  2986. the GTID from Owned_gtids and clears the ownership status in the
  2987. THD object.
  2988. - Add the owned GTIDs to executed_gtids if the is_commit flag is set.
  2989. - Send a broadcast on the condition variable for the sidno which we
  2990. released ownership.
  2991. @param[in] thd - Thread for which owned GTID set should be updated.
  2992. @param[in] is_commit - If the thread is being updated by a commit.
  2993. */
  2994. void update_gtids_impl_own_gtid_set(THD *thd, bool is_commit);
  2995. /**
  2996. Lock a given sidno of a transaction being updated.
  2997. This is a sub task of update_gtids_impl responsible only to lock the
  2998. sidno of the GTID being updated.
  2999. @param[in] sidno - The sidno to be locked.
  3000. */
  3001. void update_gtids_impl_lock_sidno(rpl_sidno sidno);
  3002. /**
  3003. Locks the sidnos of all the GTIDs of the commit group starting on the
  3004. transaction passed as parameter.
  3005. This is a sub task of update_commit_group responsible only to lock the
  3006. sidno(s) of the GTID(s) being updated.
  3007. The function should follow thd->next_to_commit to lock all sidnos of all
  3008. transactions being updated in a group.
  3009. @param[in] thd - Thread that owns the GTID(s) to be updated or leader
  3010. of the commit group in the case of a commit group
  3011. update.
  3012. */
  3013. void update_gtids_impl_lock_sidnos(THD *thd);
  3014. /**
  3015. Handle the case that the thread own a single non-anonymous GTID.
  3016. This is a sub task of update_gtids_impl responsible only to handle
  3017. the case of a thread with a single non-anonymous GTID being updated
  3018. either for commit or rollback.
  3019. - Release ownership of the GTID owned by the THD. This removes
  3020. the GTID from Owned_gtids and clears the ownership status in the
  3021. THD object.
  3022. - Add the owned GTID to executed_gtids if the is_commit flag is set.
  3023. - Send a broadcast on the condition variable for the sidno which we
  3024. released ownership.
  3025. @param[in] thd - Thread to be updated that owns single non-anonymous GTID.
  3026. @param[in] is_commit - If the thread is being updated by a commit.
  3027. */
  3028. void update_gtids_impl_own_gtid(THD *thd, bool is_commit);
  3029. /**
  3030. Unlock a given sidno after broadcasting its changes.
  3031. This is a sub task of update_gtids_impl responsible only to
  3032. unlock the sidno of the GTID being updated after broadcasting
  3033. its changes.
  3034. @param[in] sidno - The sidno to be broadcasted and unlocked.
  3035. */
  3036. void update_gtids_impl_broadcast_and_unlock_sidno(rpl_sidno sidno);
  3037. /**
  3038. Unlocks all locked sidnos after broadcasting their changes.
  3039. This is a sub task of update_commit_group responsible only to
  3040. unlock the sidno(s) of the GTID(s) being updated after broadcasting
  3041. their changes.
  3042. */
  3043. void update_gtids_impl_broadcast_and_unlock_sidnos();
  3044. /**
  3045. Handle the case that the thread owns ANONYMOUS GTID.
  3046. This is a sub task of update_gtids_impl responsible only to handle
  3047. the case of a thread with an ANONYMOUS GTID being updated.
  3048. - Release ownership of the anonymous GTID owned by the THD and clears
  3049. the ownership status in the THD object.
  3050. - Decrease counters of GTID-violating transactions.
  3051. @param[in] thd - Thread to be updated that owns anonymous GTID.
  3052. @param[in,out] more_trx - If the 'transaction' has been split into
  3053. multiple transactions in the binlog.
  3054. This is firstly assigned with the return of
  3055. Gtid_state::update_gtids_impl_begin function, and
  3056. its value can be set to true when
  3057. Gtid_state::update_gtids_impl_anonymous_gtid
  3058. detects more content on the transaction cache.
  3059. */
  3060. void update_gtids_impl_own_anonymous(THD *thd, bool *more_trx);
  3061. /**
  3062. Handle the case that the thread owns nothing.
  3063. This is a sub task of update_gtids_impl responsible only to handle
  3064. the case of a thread that owns nothing being updated.
  3065. There are two cases when this happens:
  3066. - Normally, it is a rollback of an automatic transaction, so
  3067. the is_commit is false and gtid_next=automatic.
  3068. - There is also a corner case. This case may happen for a transaction
  3069. that uses GTID_NEXT=AUTOMATIC, and violates GTID_CONSISTENCY, and
  3070. commits changes to the database, but does not write to the binary log,
  3071. so that no GTID is generated. An example is CREATE TEMPORARY TABLE
  3072. inside a transaction when binlog_format=row. Despite the thread does
  3073. not own anything, the GTID consistency violation makes it necessary to
  3074. call end_gtid_violating_transaction. Therefore
  3075. MYSQL_BIN_LOG::gtid_end_transaction will call
  3076. gtid_state->update_on_commit in this case, and subsequently we will
  3077. reach this case.
  3078. @param[in] thd - Thread to be updated that owns anonymous GTID.
  3079. */
  3080. void update_gtids_impl_own_nothing(THD *thd);
  3081. /**
  3082. Handle the final part of update_gtids_impl.
  3083. This is a sub task of update_gtids_impl responsible only to handle
  3084. the call to end_gtid_violating_transaction function when there is no
  3085. more transactions split after the current transaction.
  3086. @param[in] thd - Thread for which owned GTID is updated.
  3087. @param[in] more_trx - This is the value returned from
  3088. Gtid_state::update_gtids_impl_begin and can be
  3089. changed for transactions owning anonymous GTID at
  3090. Gtid_state::update_gtids_impl_own_anonymous.
  3091. */
  3092. void update_gtids_impl_end(THD *thd, bool more_trx);
  3093. /**
  3094. This array is used by Gtid_state_update_gtids_impl* functions.
  3095. The array items (one per sidno of the sid_map) will be set as true for
  3096. each sidno that requires to be locked when updating a set of GTIDs
  3097. (at Gtid_set::update_gtids_impl_lock_sidnos).
  3098. The array items will be set false at
  3099. Gtid_set::update_gtids_impl_broadcast_and_unlock_sidnos.
  3100. It is used to so that lock, unlock, and broadcast operations are only
  3101. called once per sidno per commit group, instead of once per transaction.
  3102. Its access is protected by:
  3103. - global_sid_lock->wrlock when growing and cleaning up;
  3104. - MYSQL_BIN_LOG::LOCK_commit when setting true/false on array items.
  3105. */
  3106. Prealloced_array<bool, 8> commit_group_sidnos;
  3107. /**
  3108. Ensure that commit_group_sidnos have room for the SIDNO passed as
  3109. parameter.
  3110. This function must only be called in one place:
  3111. Gtid_state::ensure_sidno().
  3112. @param sidno The SIDNO.
  3113. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  3114. */
  3115. enum_return_status ensure_commit_group_sidnos(rpl_sidno sidno);
  3116. };
  3117. /*
  3118. BUG# #18089914 - REFACTORING: RENAME GROUP TO GTID
  3119. changed AUTOMATIC_GROUP to AUTOMATIC_GTID
  3120. changed ANONYMOUS_GROUP to ANONYMOUS_GTID
  3121. changed INVALID_GROUP to INVALID_GTID
  3122. changed UNDEFINED_GROUP to UNDEFINED_GTID
  3123. changed GTID_GROUPto ASSIGNED_GTID
  3124. changed NOT_YET_DETERMINED_GROUP to NOT_YET_DETERMINED_GTID
  3125. */
  3126. /**
  3127. Enumeration of different types of values for Gtid_specification,
  3128. i.e, the different internal states that @@session.gtid_next can be in.
  3129. */
  3130. enum enum_gtid_type {
  3131. /**
  3132. Specifies that the GTID has not been generated yet; it will be
  3133. generated on commit. It will depend on the GTID_MODE: if
  3134. GTID_MODE<=OFF_PERMISSIVE, then the transaction will be anonymous;
  3135. if GTID_MODE>=ON_PERMISSIVE, then the transaction will be assigned
  3136. a new GTID.
  3137. This is the default value: thd->variables.gtid_next has this state
  3138. when GTID_NEXT="AUTOMATIC".
  3139. It is important that AUTOMATIC_GTID==0 so that the default value
  3140. for thd->variables->gtid_next.type is AUTOMATIC_GTID.
  3141. */
  3142. AUTOMATIC_GTID = 0,
  3143. /**
  3144. Specifies that the transaction has been assigned a GTID (UUID:NUMBER).
  3145. thd->variables.gtid_next has this state when GTID_NEXT="UUID:NUMBER".
  3146. This is the state of GTID-transactions replicated to the slave.
  3147. */
  3148. ASSIGNED_GTID,
  3149. /**
  3150. Specifies that the transaction is anonymous, i.e., it does not
  3151. have a GTID and will never be assigned one.
  3152. thd->variables.gtid_next has this state when GTID_NEXT="ANONYMOUS".
  3153. This is the state of any transaction generated on a pre-GTID
  3154. server, or on a server with GTID_MODE==OFF.
  3155. */
  3156. ANONYMOUS_GTID,
  3157. /**
  3158. GTID_NEXT is set to this state after a transaction with
  3159. GTID_NEXT=='UUID:NUMBER' is committed.
  3160. This is used to protect against a special case of unsafe
  3161. non-transactional updates.
  3162. Background: Non-transactional updates are allowed as long as they
  3163. are sane. Non-transactional updates must be single-statement
  3164. transactions; they must not be mixed with transactional updates in
  3165. the same statement or in the same transaction. Since
  3166. non-transactional updates must be logged separately from
  3167. transactional updates, a single mixed statement would generate two
  3168. different transactions.
  3169. Problematic case: Consider a transaction, Tx1, that updates two
  3170. transactional tables on the master, t1 and t2. Then slave (s1) later
  3171. replays Tx1. However, t2 is a non-transactional table at s1. As such, s1
  3172. will report an error because it cannot split Tx1 into two different
  3173. transactions. Had no error been reported, then Tx1 would be split into Tx1
  3174. and Tx2, potentially causing severe harm in case some form of fail-over
  3175. procedure is later engaged by s1.
  3176. To detect this case on the slave and generate an appropriate error
  3177. message rather than causing an inconsistency in the GTID state, we
  3178. do as follows. When committing a transaction that has
  3179. GTID_NEXT==UUID:NUMBER, we set GTID_NEXT to UNDEFINED_GTID. When
  3180. the next part of the transaction is being processed, an error is
  3181. generated, because it is not allowed to execute a transaction when
  3182. GTID_NEXT==UNDEFINED. In the normal case, the error is not
  3183. generated, because there will always be a Gtid_log_event after the
  3184. next transaction.
  3185. */
  3186. UNDEFINED_GTID,
  3187. /*
  3188. GTID_NEXT is set to this state by the slave applier thread when it
  3189. reads a Format_description_log_event that does not originate from
  3190. this server.
  3191. Background: when the slave applier thread reads a relay log that
  3192. comes from a pre-GTID master, it must preserve the transactions as
  3193. anonymous transactions, even if GTID_MODE>=ON_PERMISSIVE. This
  3194. may happen, e.g., if the relay log was received when master and
  3195. slave had GTID_MODE=OFF or when master and slave were old, and the
  3196. relay log is applied when slave has GTID_MODE>=ON_PERMISSIVE.
  3197. So the slave thread should set GTID_NEXT=ANONYMOUS for the next
  3198. transaction when it starts to process an old binary log. However,
  3199. there is no way for the slave to tell if the binary log is old,
  3200. until it sees the first transaction. If the first transaction
  3201. begins with a Gtid_log_event, we have the GTID there; if it begins
  3202. with query_log_event, row events, etc, then this is an old binary
  3203. log. So at the time the binary log begins, we just set
  3204. GTID_NEXT=NOT_YET_DETERMINED_GTID. If it remains
  3205. NOT_YET_DETERMINED when the next transaction begins,
  3206. gtid_pre_statement_checks will automatically turn it into an
  3207. anonymous transaction. If a Gtid_log_event comes across before
  3208. the next transaction starts, then the Gtid_log_event will just set
  3209. GTID_NEXT='UUID:NUMBER' accordingly.
  3210. */
  3211. NOT_YET_DETERMINED_GTID
  3212. };
  3213. /// Global state of GTIDs.
  3214. extern Gtid_state *gtid_state;
  3215. /**
  3216. This struct represents a specification of a GTID for a statement to
  3217. be executed: either "AUTOMATIC", "ANONYMOUS", or "SID:GNO".
  3218. This is a POD. It has to be a POD because it is used in THD::variables.
  3219. */
  3220. struct Gtid_specification {
  3221. /// The type of this GTID
  3222. enum_gtid_type type;
  3223. /**
  3224. The GTID:
  3225. { SIDNO, GNO } if type == GTID;
  3226. { 0, 0 } if type == AUTOMATIC or ANONYMOUS.
  3227. */
  3228. Gtid gtid;
  3229. /// Set the type to ASSIGNED_GTID and SID, GNO to the given values.
  3230. void set(rpl_sidno sidno, rpl_gno gno) {
  3231. gtid.set(sidno, gno);
  3232. type = ASSIGNED_GTID;
  3233. }
  3234. /// Set the type to ASSIGNED_GTID and SID, GNO to the given Gtid.
  3235. void set(const Gtid &gtid_param) { set(gtid_param.sidno, gtid_param.gno); }
  3236. /// Set the type to AUTOMATIC_GTID.
  3237. void set_automatic() { type = AUTOMATIC_GTID; }
  3238. /// Set the type to ANONYMOUS_GTID.
  3239. void set_anonymous() { type = ANONYMOUS_GTID; }
  3240. /// Set the type to NOT_YET_DETERMINED_GTID.
  3241. void set_not_yet_determined() { type = NOT_YET_DETERMINED_GTID; }
  3242. /// Set to undefined. Must only be called if the type is ASSIGNED_GTID.
  3243. void set_undefined() {
  3244. DBUG_ASSERT(type == ASSIGNED_GTID);
  3245. type = UNDEFINED_GTID;
  3246. }
  3247. /// Return true if this Gtid_specification is equal to 'other'.
  3248. bool equals(const Gtid_specification &other) const {
  3249. return (type == other.type &&
  3250. (type != ASSIGNED_GTID || gtid.equals(other.gtid)));
  3251. }
  3252. /**
  3253. Return true if this Gtid_specification is a ASSIGNED_GTID with the
  3254. same SID, GNO as 'other_gtid'.
  3255. */
  3256. bool equals(const Gtid &other_gtid) const {
  3257. return type == ASSIGNED_GTID && gtid.equals(other_gtid);
  3258. }
  3259. #ifdef MYSQL_SERVER
  3260. /**
  3261. Parses the given string and stores in this Gtid_specification.
  3262. @param sid_map sid_map to use when converting SID to a sidno.
  3263. @param text The text to parse
  3264. @return RETURN_STATUS_OK or RETURN_STATUS_REPORTED_ERROR.
  3265. */
  3266. enum_return_status parse(Sid_map *sid_map, const char *text);
  3267. /// Returns true if the given string is a valid Gtid_specification.
  3268. static bool is_valid(const char *text);
  3269. #endif
  3270. static const int MAX_TEXT_LENGTH = Gtid::MAX_TEXT_LENGTH;
  3271. /**
  3272. Writes this Gtid_specification to the given string buffer.
  3273. @param sid_map Sid_map to use if the type of this
  3274. Gtid_specification is ASSIGNED_GTID.
  3275. @param [out] buf The buffer
  3276. @param need_lock If true, this function acquires global_sid_lock
  3277. before looking up the sidno in sid_map, and then releases it. If
  3278. false, this function asserts that the lock is held by the caller.
  3279. @retval The number of characters written.
  3280. */
  3281. int to_string(const Sid_map *sid_map, char *buf,
  3282. bool need_lock = false) const;
  3283. /**
  3284. Writes this Gtid_specification to the given string buffer.
  3285. @param sid SID to use if the type of this Gtid_specification is
  3286. ASSIGNED_GTID. Can be NULL if this Gtid_specification is
  3287. ANONYMOUS_GTID or AUTOMATIC_GTID.
  3288. @param[out] buf The buffer
  3289. @retval The number of characters written.
  3290. */
  3291. int to_string(const rpl_sid *sid, char *buf) const;
  3292. #ifndef DBUG_OFF
  3293. /// Debug only: print this Gtid_specification to stdout.
  3294. void print() const {
  3295. char buf[MAX_TEXT_LENGTH + 1];
  3296. to_string(global_sid_map, buf);
  3297. printf("%s\n", buf);
  3298. }
  3299. #endif
  3300. /**
  3301. Print this Gtid_specification to the trace file if debug is
  3302. enabled; no-op otherwise.
  3303. */
  3304. void dbug_print(const char *text MY_ATTRIBUTE((unused)) = "",
  3305. bool need_lock MY_ATTRIBUTE((unused)) = false) const {
  3306. #ifndef DBUG_OFF
  3307. char buf[MAX_TEXT_LENGTH + 1];
  3308. to_string(global_sid_map, buf, need_lock);
  3309. DBUG_PRINT("info", ("%s%s%s", text, *text ? ": " : "", buf));
  3310. #endif
  3311. }
  3312. };
  3313. /**
  3314. Indicates if a statement should be skipped or not. Used as return
  3315. value from gtid_before_statement.
  3316. */
  3317. enum enum_gtid_statement_status {
  3318. /// Statement can execute.
  3319. GTID_STATEMENT_EXECUTE,
  3320. /// Statement should be cancelled.
  3321. GTID_STATEMENT_CANCEL,
  3322. /**
  3323. Statement should be skipped, but there may be an implicit commit
  3324. after the statement if gtid_commit is set.
  3325. */
  3326. GTID_STATEMENT_SKIP
  3327. };
  3328. #ifdef MYSQL_SERVER
  3329. /**
  3330. Perform GTID-related checks before executing a statement:
  3331. - Check that the current statement does not contradict
  3332. enforce_gtid_consistency.
  3333. - Check that there is no implicit commit in a transaction when
  3334. GTID_NEXT==UUID:NUMBER.
  3335. - Change thd->variables.gtid_next.type to ANONYMOUS_GTID if it is
  3336. currently NOT_YET_DETERMINED_GTID.
  3337. - Check whether the statement should be cancelled.
  3338. @param thd THD object for the session.
  3339. @retval GTID_STATEMENT_EXECUTE The normal case: the checks
  3340. succeeded, and statement can execute.
  3341. @retval GTID_STATEMENT_CANCEL The checks failed; an
  3342. error has be generated and the statement must stop.
  3343. @retval GTID_STATEMENT_SKIP The checks succeeded, but the GTID has
  3344. already been executed (exists in GTID_EXECUTED). So the statement
  3345. must not execute; however, if there are implicit commits, then the
  3346. implicit commits must execute.
  3347. */
  3348. enum_gtid_statement_status gtid_pre_statement_checks(THD *thd);
  3349. /**
  3350. Perform GTID-related checks before executing a statement, but after
  3351. executing an implicit commit before the statement, if any:
  3352. If gtid_next=anonymous, but the thread does not hold anonymous
  3353. ownership, then acquire anonymous ownership. (Do this only if this
  3354. is not an 'innocent' statement, i.e., SET/SHOW/DO/SELECT that does
  3355. not invoke a stored function.)
  3356. It is important that this is done after the implicit commit, because
  3357. the implicit commit may release anonymous ownership.
  3358. @param thd THD object for the session
  3359. @retval false Success.
  3360. @retval true Error. Error can happen if GTID_MODE=ON. The error has
  3361. been reported by (a function called by) this function.
  3362. */
  3363. bool gtid_pre_statement_post_implicit_commit_checks(THD *thd);
  3364. /**
  3365. Acquire ownership of the given Gtid_specification.
  3366. The Gtid_specification must be of type ASSIGNED_GTID or ANONYMOUS_GTID.
  3367. The caller must hold global_sid_lock (normally the rdlock). The
  3368. lock may be temporarily released and acquired again. In the end,
  3369. the lock will be released, so the caller should *not* release the
  3370. lock.
  3371. The function will try to acquire ownership of the GTID and update
  3372. both THD::gtid_next, Gtid_state::owned_gtids, and
  3373. THD::owned_gtid / THD::owned_sid.
  3374. @param thd The thread that acquires ownership.
  3375. @param spec The Gtid_specification.
  3376. @retval false Success: either we have acquired ownership of the
  3377. GTID, or it is already included in GTID_EXECUTED and will be
  3378. skipped.
  3379. @retval true Failure; the thread was killed or an error occurred.
  3380. The error has been reported using my_error.
  3381. */
  3382. bool set_gtid_next(THD *thd, const Gtid_specification &spec);
  3383. #ifdef HAVE_GTID_NEXT_LIST
  3384. int gtid_acquire_ownership_multiple(THD *thd);
  3385. #endif
  3386. /**
  3387. Return sidno for a given sid, see Sid_map::add_sid() for details.
  3388. */
  3389. rpl_sidno get_sidno_from_global_sid_map(rpl_sid sid);
  3390. /**
  3391. Return last gno for a given sidno, see
  3392. Gtid_state::get_last_executed_gno() for details.
  3393. */
  3394. rpl_gno get_last_executed_gno(rpl_sidno sidno);
  3395. void gtid_set_performance_schema_values(const THD *thd);
  3396. /**
  3397. If gtid_next=ANONYMOUS or NOT_YET_DETERMINED, but the thread does
  3398. not hold anonymous ownership, acquire anonymous ownership.
  3399. @param thd Thread.
  3400. @retval true Error (can happen if gtid_mode=ON and
  3401. gtid_next=anonymous). The error has already been reported using
  3402. my_error.
  3403. @retval false Success.
  3404. */
  3405. bool gtid_reacquire_ownership_if_anonymous(THD *thd);
  3406. /**
  3407. The function commits or rolls back the gtid state if it needs to.
  3408. It's supposed to be invoked at the end of transaction commit or
  3409. rollback, as well as as at the end of XA prepare.
  3410. @param thd Thread context
  3411. @param needs_to The actual work will be done when the parameter is true
  3412. @param do_commit When true the gtid state changes are committed, otherwise
  3413. they are rolled back.
  3414. */
  3415. inline void gtid_state_commit_or_rollback(THD *thd, bool needs_to,
  3416. bool do_commit) {
  3417. if (needs_to) {
  3418. if (do_commit)
  3419. gtid_state->update_on_commit(thd);
  3420. else
  3421. gtid_state->update_on_rollback(thd);
  3422. }
  3423. }
  3424. #endif // ifdef MYSQL_SERVER
  3425. /**
  3426. An optimized way of checking GTID_MODE without acquiring locks every time.
  3427. GTID_MODE is a global variable that should not be changed often, but the
  3428. access to it is protected by any of the four locks described at
  3429. enum_gtid_mode_lock.
  3430. Every time a channel receiver thread connects to a master, and every time
  3431. a Gtid_log_event or an Anonymous_gtid_log_event is queued by a receiver
  3432. thread, there must be checked if the current GTID_MODE is compatible with
  3433. the operation.
  3434. There are some places where the verification is performed while already
  3435. holding one of the above mentioned locks, but there are other places that
  3436. rely on no lock and will rely on the global_sid_lock, blocking any other
  3437. GTID operation relying on the global_sid_map.
  3438. In order to avoid acquiring lock to check a variable that is not changed
  3439. often, there is a global (atomic) counter of how many times the GTID_MODE
  3440. was changed since the server startup.
  3441. This class holds a copy of the last GTID_MODE to be returned without the
  3442. need of acquiring locks if the local GTID mode counter has the same value
  3443. as the global atomic counter.
  3444. */
  3445. class Gtid_mode_copy {
  3446. public:
  3447. /**
  3448. Return the current server GTID_MODE without acquiring locks if possible.
  3449. @param have_lock The lock type held by the caller.
  3450. */
  3451. enum_gtid_mode get_gtid_mode_from_copy(enum_gtid_mode_lock have_lock) {
  3452. ulong current_gtid_mode_counter = gtid_mode_counter;
  3453. // Update out copy of GTID_MODE if needed
  3454. if (m_gtid_mode_counter != current_gtid_mode_counter) {
  3455. m_gtid_mode = get_gtid_mode(have_lock);
  3456. m_gtid_mode_counter = current_gtid_mode_counter;
  3457. }
  3458. return m_gtid_mode;
  3459. }
  3460. private:
  3461. /// The copy of the atomic counter of the last time we copied the GTID_MODE
  3462. ulong m_gtid_mode_counter = 0;
  3463. /// Local copy of the GTID_MODE
  3464. enum_gtid_mode m_gtid_mode = DEFAULT_GTID_MODE;
  3465. };
  3466. #endif /* RPL_GTID_H_INCLUDED */