summaryrefslogtreecommitdiffstats
path: root/extra/mariabackup/wsrep.cc
blob: 15463a85476499e651b6f899fa20ec2dc9c73720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/******************************************************
Percona XtraBackup: hot backup tool for InnoDB
(c) 2009-2014 Percona LLC and/or its affiliates
Originally Created 3/3/2009 Yasufumi Kinoshita
Written by Alexey Kopytov, Aleksandr Kuzminsky, Stewart Smith, Vadim Tkachenko,
Yasufumi Kinoshita, Ignacio Nin and Baron Schwartz.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA

*******************************************************

This file incorporates work covered by the following copyright and
permission notice:

   Copyright 2010 Codership Oy <http://www.codership.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA

*******************************************************/

#include <my_global.h>
#include <my_base.h>
#include <handler.h>
#include <trx0rseg.h>
#include <mysql/service_wsrep.h>

#include "common.h"
#ifdef WITH_WSREP

#include <wsrep_api.h>

/*! Name of file where Galera info is stored on recovery */
#define XB_GALERA_INFO_FILENAME "xtrabackup_galera_info"
#define XB_GALERA_DONOR_INFO_FILENAME "donor_galera_info"

/* backup copy of galera info file as sent by donor */
#define XB_GALERA_INFO_FILENAME_SST "xtrabackup_galera_info_SST"

/***********************************************************************
Store Galera checkpoint info in the 'xtrabackup_galera_info' file, if that
information is present in the trx system header. Otherwise, do nothing. */
void
xb_write_galera_info(bool incremental_prepare)
/*==================*/
{
	FILE*		fp;
	XID		xid;
	char		uuid_str[40];
	long long	seqno;
	MY_STAT		statinfo;

	xid.null();

	/* try to read last wsrep XID from innodb rsegs, we will use it
	   instead of galera info file received from donor
	*/
	if (!trx_rseg_read_wsrep_checkpoint(xid)) {
		/* no worries yet, SST may have brought in galera info file
		   from some old MariaDB version, which does not support
		   wsrep XID storing in innodb rsegs
		*/
		return;
	}

	/* if SST brought in galera info file, copy it as *_SST file
	   this will not be used, saved just for future reference
	*/
	if (my_stat(XB_GALERA_INFO_FILENAME, &statinfo, MYF(0)) != NULL) {
		FILE* fp_in  = fopen(XB_GALERA_INFO_FILENAME, "r");
		FILE* fp_out = fopen(XB_GALERA_INFO_FILENAME_SST, "w");

		char buf[BUFSIZ] = {'\0'};
		size_t size;
		while ((size = fread(buf, 1, BUFSIZ, fp_in))) {
			if (fwrite(buf, 1, size, fp_out) != strlen(buf)) {
				die(
				    "could not write to "
				    XB_GALERA_INFO_FILENAME_SST
				    ", errno = %d\n",
				    errno);
			}
		}
		if (!feof(fp_in)) {
			die(
			    XB_GALERA_INFO_FILENAME_SST
			    " not fully copied\n"
			);
		}
		fclose(fp_out);
		fclose(fp_in);
	}

	wsrep_uuid_t uuid;
	memcpy(uuid.data, wsrep_xid_uuid(&xid), sizeof(uuid.data));
	if (wsrep_uuid_print(&uuid, uuid_str,
			     sizeof(uuid_str)) < 0) {
		return;
	}

	fp = fopen(XB_GALERA_INFO_FILENAME, "w");
	if (fp == NULL) {

		die(
		    "could not create " XB_GALERA_INFO_FILENAME
		    ", errno = %d\n",
		    errno);
	}

	seqno = wsrep_xid_seqno(&xid);

	msg("mariabackup: Recovered WSREP position: %s:%lld domain_id: %lld\n",
	    uuid_str, (long long) seqno, (long long)wsrep_get_domain_id());

	if (fprintf(fp, "%s:%lld %lld", uuid_str, (long long) seqno,
		    (long long)wsrep_get_domain_id()) < 0) {

		die(
		    "could not write to " XB_GALERA_INFO_FILENAME
		    ", errno = %d\n",
		    errno);;
	}

	fclose(fp);
}
#endif