~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/repl_failsafe.cc

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2001-2006 MySQL AB & Sasha
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
16
 
/**
17
 
  @file
18
 
 
19
 
  All of the functions defined in this file which are not used (the ones to
20
 
  handle failsafe) are not used; their code has not been updated for more
21
 
  than one year now so should be considered as BADLY BROKEN. Do not enable
22
 
  it. The used functions (to handle LOAD DATA FROM MASTER, plus some small
23
 
  functions like register_slave()) are working.
24
 
*/
25
 
#include <drizzled/server_includes.h>
26
 
 
27
 
#ifdef HAVE_REPLICATION
28
 
 
29
 
#include "repl_failsafe.h"
30
 
#include "sql_repl.h"
31
 
#include "rpl_mi.h"
32
 
#include "rpl_filter.h"
33
 
#include "log_event.h"
34
 
 
35
 
#define SLAVE_LIST_CHUNK 128
36
 
#define SLAVE_ERRMSG_SIZE (FN_REFLEN+64)
37
 
 
38
 
 
39
 
RPL_STATUS rpl_status=RPL_NULL;
40
 
pthread_mutex_t LOCK_rpl_status;
41
 
pthread_cond_t COND_rpl_status;
42
 
HASH slave_list;
43
 
 
44
 
const char *rpl_role_type[] = {"MASTER","SLAVE",NullS};
45
 
TYPELIB rpl_role_typelib = {array_elements(rpl_role_type)-1,"",
46
 
                            rpl_role_type, NULL};
47
 
 
48
 
const char* rpl_status_type[]=
49
 
{
50
 
  "AUTH_MASTER","ACTIVE_SLAVE","IDLE_SLAVE", "LOST_SOLDIER","TROOP_SOLDIER",
51
 
  "RECOVERY_CAPTAIN","NULL",NullS
52
 
};
53
 
TYPELIB rpl_status_typelib= {array_elements(rpl_status_type)-1,"",
54
 
                             rpl_status_type, NULL};
55
 
 
56
 
 
57
 
void change_rpl_status(RPL_STATUS from_status, RPL_STATUS to_status)
58
 
{
59
 
  pthread_mutex_lock(&LOCK_rpl_status);
60
 
  if (rpl_status == from_status || rpl_status == RPL_ANY)
61
 
    rpl_status = to_status;
62
 
  pthread_cond_signal(&COND_rpl_status);
63
 
  pthread_mutex_unlock(&LOCK_rpl_status);
64
 
}
65
 
 
66
 
 
67
 
#define get_object(p, obj, msg) \
68
 
{\
69
 
  uint len = (uint)*p++;  \
70
 
  if (p + len > p_end || len >= sizeof(obj)) \
71
 
  {\
72
 
    errmsg= msg;\
73
 
    goto err; \
74
 
  }\
75
 
  strmake(obj,(char*) p,len); \
76
 
  p+= len; \
77
 
}\
78
 
 
79
 
 
80
 
static inline int cmp_master_pos(Slave_log_event* sev, LEX_MASTER_INFO* mi)
81
 
{
82
 
  return cmp_master_pos(sev->master_log, sev->master_pos, mi->log_file_name,
83
 
                        mi->pos);
84
 
}
85
 
 
86
 
 
87
 
void unregister_slave(THD* thd, bool only_mine, bool need_mutex)
88
 
{
89
 
  if (thd->server_id)
90
 
  {
91
 
    if (need_mutex)
92
 
      pthread_mutex_lock(&LOCK_slave_list);
93
 
 
94
 
    SLAVE_INFO* old_si;
95
 
    if ((old_si = (SLAVE_INFO*)hash_search(&slave_list,
96
 
                                           (uchar*)&thd->server_id, 4)) &&
97
 
        (!only_mine || old_si->thd == thd))
98
 
    hash_delete(&slave_list, (uchar*)old_si);
99
 
 
100
 
    if (need_mutex)
101
 
      pthread_mutex_unlock(&LOCK_slave_list);
102
 
  }
103
 
}
104
 
 
105
 
 
106
 
/**
107
 
  Register slave in 'slave_list' hash table.
108
 
 
109
 
  @return
110
 
    0   ok
111
 
  @return
112
 
    1   Error.   Error message sent to client
113
 
*/
114
 
 
115
 
int register_slave(THD* thd, uchar* packet, uint packet_length)
116
 
{
117
 
  int res;
118
 
  SLAVE_INFO *si;
119
 
  uchar *p= packet, *p_end= packet + packet_length;
120
 
  const char *errmsg= "Wrong parameters to function register_slave";
121
 
 
122
 
  if (!(si = (SLAVE_INFO*)my_malloc(sizeof(SLAVE_INFO), MYF(MY_WME))))
123
 
    goto err2;
124
 
 
125
 
  thd->server_id= si->server_id= uint4korr(p);
126
 
  p+= 4;
127
 
  get_object(p,si->host, "Failed to register slave: too long 'report-host'");
128
 
  get_object(p,si->user, "Failed to register slave: too long 'report-user'");
129
 
  get_object(p,si->password, "Failed to register slave; too long 'report-password'");
130
 
  if (p+10 > p_end)
131
 
    goto err;
132
 
  si->port= uint2korr(p);
133
 
  p += 2;
134
 
  si->rpl_recovery_rank= uint4korr(p);
135
 
  p += 4;
136
 
  if (!(si->master_id= uint4korr(p)))
137
 
    si->master_id= server_id;
138
 
  si->thd= thd;
139
 
 
140
 
  pthread_mutex_lock(&LOCK_slave_list);
141
 
  unregister_slave(thd,0,0);
142
 
  res= my_hash_insert(&slave_list, (uchar*) si);
143
 
  pthread_mutex_unlock(&LOCK_slave_list);
144
 
  return res;
145
 
 
146
 
err:
147
 
  my_free(si, MYF(MY_WME));
148
 
  my_message(ER_UNKNOWN_ERROR, errmsg, MYF(0)); /* purecov: inspected */
149
 
err2:
150
 
  return 1;
151
 
}
152
 
 
153
 
extern "C" uint32_t
154
 
*slave_list_key(SLAVE_INFO* si, size_t *len,
155
 
                bool not_used __attribute__((unused)))
156
 
{
157
 
  *len = 4;
158
 
  return &si->server_id;
159
 
}
160
 
 
161
 
extern "C" void slave_info_free(void *s)
162
 
{
163
 
  my_free(s, MYF(MY_WME));
164
 
}
165
 
 
166
 
void init_slave_list()
167
 
{
168
 
  hash_init(&slave_list, system_charset_info, SLAVE_LIST_CHUNK, 0, 0,
169
 
            (hash_get_key) slave_list_key, (hash_free_key) slave_info_free, 0);
170
 
  pthread_mutex_init(&LOCK_slave_list, MY_MUTEX_INIT_FAST);
171
 
}
172
 
 
173
 
void end_slave_list()
174
 
{
175
 
  /* No protection by a mutex needed as we are only called at shutdown */
176
 
  if (hash_inited(&slave_list))
177
 
  {
178
 
    hash_free(&slave_list);
179
 
    pthread_mutex_destroy(&LOCK_slave_list);
180
 
  }
181
 
}
182
 
 
183
 
 
184
 
bool show_slave_hosts(THD* thd)
185
 
{
186
 
  List<Item> field_list;
187
 
  Protocol *protocol= thd->protocol;
188
 
 
189
 
  field_list.push_back(new Item_return_int("Server_id", 10,
190
 
                                           DRIZZLE_TYPE_LONG));
191
 
  field_list.push_back(new Item_empty_string("Host", 20));
192
 
  if (opt_show_slave_auth_info)
193
 
  {
194
 
    field_list.push_back(new Item_empty_string("User",20));
195
 
    field_list.push_back(new Item_empty_string("Password",20));
196
 
  }
197
 
  field_list.push_back(new Item_return_int("Port", 7, DRIZZLE_TYPE_LONG));
198
 
  field_list.push_back(new Item_return_int("Rpl_recovery_rank", 7,
199
 
                                           DRIZZLE_TYPE_LONG));
200
 
  field_list.push_back(new Item_return_int("Master_id", 10,
201
 
                                           DRIZZLE_TYPE_LONG));
202
 
 
203
 
  if (protocol->send_fields(&field_list,
204
 
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
205
 
    return(true);
206
 
 
207
 
  pthread_mutex_lock(&LOCK_slave_list);
208
 
 
209
 
  for (uint i = 0; i < slave_list.records; ++i)
210
 
  {
211
 
    SLAVE_INFO* si = (SLAVE_INFO*) hash_element(&slave_list, i);
212
 
    protocol->prepare_for_resend();
213
 
    protocol->store((uint32_t) si->server_id);
214
 
    protocol->store(si->host, &my_charset_bin);
215
 
    if (opt_show_slave_auth_info)
216
 
    {
217
 
      protocol->store(si->user, &my_charset_bin);
218
 
      protocol->store(si->password, &my_charset_bin);
219
 
    }
220
 
    protocol->store((uint32_t) si->port);
221
 
    protocol->store((uint32_t) si->rpl_recovery_rank);
222
 
    protocol->store((uint32_t) si->master_id);
223
 
    if (protocol->write())
224
 
    {
225
 
      pthread_mutex_unlock(&LOCK_slave_list);
226
 
      return(true);
227
 
    }
228
 
  }
229
 
  pthread_mutex_unlock(&LOCK_slave_list);
230
 
  my_eof(thd);
231
 
  return(false);
232
 
}
233
 
 
234
 
#endif /* HAVE_REPLICATION */
235