File: pengine.c

package info (click to toggle)
pacemaker 1.0.9.1%2Bhg15626-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 34,668 kB
  • ctags: 5,645
  • sloc: xml: 87,444; ansic: 57,853; python: 11,479; sh: 5,255; makefile: 663; perl: 387; sed: 262
file content (341 lines) | stat: -rwxr-xr-x 9,362 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* 
 * Copyright (C) 2004 Andrew Beekhof <andrew@beekhof.net>
 * 
 * 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; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This software 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <crm_internal.h>

#include <sys/param.h>
#include <crm/crm.h>
#include <crmd_fsa.h>

#include <sys/types.h>
#include <sys/wait.h>

#include <unistd.h>			/* for access */



#include <sys/types.h>	/* for calls to open */
#include <sys/stat.h>	/* for calls to open */
#include <fcntl.h>	/* for calls to open */
#include <pwd.h>	/* for getpwuid */
#include <grp.h>	/* for initgroups */

#include <sys/time.h>	/* for getrlimit */
#include <sys/resource.h>/* for getrlimit */

#include <errno.h>

#include <crm/msg_xml.h>
#include <crm/common/xml.h>
#include <crm/common/cluster.h>
#include <crmd_messages.h>
#include <crmd_callbacks.h>

#include <crm/cib.h>
#include <crmd.h>


static GCHSource *pe_source = NULL;
struct crm_subsystem_s *pe_subsystem  = NULL;
void do_pe_invoke_callback(xmlNode *msg, int call_id, int rc,
			   xmlNode *output, void *user_data);


static void
save_cib_contents(xmlNode *msg, int call_id, int rc, xmlNode *output, void *user_data) 
{
    char *id = user_data;
    register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __FUNCTION__);
    CRM_CHECK(id != NULL, return);

    if(rc == cib_ok) {
	int len = 15;
	char *filename = NULL;
	
	len += strlen(id);
	len += strlen(PE_STATE_DIR);

	crm_malloc0(filename, len);
	CRM_CHECK(filename != NULL, return);

	sprintf(filename, PE_STATE_DIR "/pe-core-%s.bz2", id);
	if(write_xml_file(output, filename, TRUE) < 0) {
	    crm_err("Could not save CIB contents after PE crash to %s", filename);
	} else {
	    crm_notice("Saved CIB contents after PE crash to %s", filename);
	}

	crm_free(filename);
    }
    
    crm_free(id);
}

static void
pe_connection_destroy(gpointer user_data)
{
    pe_source = NULL;
    clear_bit_inplace(fsa_input_register, pe_subsystem->flag_connected);
    if(is_set(fsa_input_register, pe_subsystem->flag_required)) {
	int rc = cib_ok;
	cl_uuid_t new_uuid;
	char uuid_str[UU_UNPARSE_SIZEOF];

	cl_uuid_generate(&new_uuid);
	cl_uuid_unparse(&new_uuid, uuid_str);

	crm_crit("Connection to the Policy Engine failed (pid=%d, uuid=%s)",
		 pe_subsystem->pid, uuid_str);
	
	    /*
	     *The PE died...
	     *
	     * Save the current CIB so that we have a chance of
	     * figuring out what killed it.
	     *
	     * Delay raising the I_ERROR until the query below completes or
	     * 5s is up, whichever comes first.
	     *
	     */
	rc = fsa_cib_conn->cmds->query(fsa_cib_conn, NULL, NULL, cib_scope_local);
	fsa_cib_conn->cmds->register_callback(
	    fsa_cib_conn, rc, 5, FALSE, crm_strdup(uuid_str),
	    "save_cib_contents", save_cib_contents);
	
    } else {
	crm_info("Connection to the Policy Engine released");
    }
    
    pe_subsystem->pid = -1;
    pe_subsystem->ipc = NULL;
    pe_subsystem->client = NULL;

    mainloop_set_trigger(fsa_source);
    return;
}

static gboolean
pe_msg_dispatch(IPC_Channel *client, gpointer user_data) 
{
    xmlNode *msg = NULL;
    gboolean stay_connected = TRUE;
	
    while(IPC_ISRCONN(client)
	  && client->ops->is_message_pending(client)) {

	msg = xmlfromIPC(client, MAX_IPC_DELAY);
	if (msg != NULL) {
	    route_message(C_IPC_MESSAGE, msg);
	    free_xml(msg);
	}
    }
    
    if (client->ch_status != IPC_CONNECT) {
	crm_info("Received HUP from %s:[%d]", pe_subsystem->name, pe_subsystem->pid);	
	stay_connected = FALSE;
    }

    mainloop_set_trigger(fsa_source);
    return stay_connected;
}

/*	 A_PE_START, A_PE_STOP, A_TE_RESTART	*/
void
do_pe_control(long long action,
	      enum crmd_fsa_cause cause,
	      enum crmd_fsa_state cur_state,
	      enum crmd_fsa_input current_input,
	      fsa_data_t *msg_data)
{
    struct crm_subsystem_s *this_subsys = pe_subsystem;

    long long stop_actions = A_PE_STOP;
    long long start_actions = A_PE_START;
    
    if(action & stop_actions) {
	clear_bit_inplace(fsa_input_register, pe_subsystem->flag_required);

	if(is_heartbeat_cluster()) {
	    stop_subsystem(this_subsys, FALSE);

	} else {    
	    if(pe_source) {
		G_main_del_IPC_Channel(pe_source);
		pe_source = NULL;
	    }
	    
	    if(pe_subsystem->ipc) {
		pe_subsystem->ipc->ops->destroy(pe_subsystem->ipc);
		pe_subsystem->ipc = NULL;
	    }
	    clear_bit_inplace(fsa_input_register, pe_subsystem->flag_connected);
	}
    }
    
    if(action & start_actions) {
	if(cur_state != S_STOPPING) {
	    if(is_openais_cluster()) {
		set_bit_inplace(fsa_input_register, pe_subsystem->flag_required);		

	    } else if(is_heartbeat_cluster()) {
		if(start_subsystem(this_subsys) == FALSE) {
		    register_fsa_error(C_FSA_INTERNAL, I_FAIL, NULL);
		    return;
		} 
		sleep(3);		
	    }

	    pe_subsystem->ipc = init_client_ipc_comms_nodispatch(CRM_SYSTEM_PENGINE);
	    if(pe_subsystem->ipc == NULL) {
		crm_warn("Setup of client connection failed,"
			 " not adding channel to mainloop");
		register_fsa_error(C_FSA_INTERNAL, I_FAIL, NULL);	    
		return;
	    }

	    if(is_openais_cluster()) {
		pe_subsystem->pid = pe_subsystem->ipc->farside_pid;;
	    }
	    
	    set_bit_inplace(fsa_input_register, pe_subsystem->flag_connected);
	    pe_source = G_main_add_IPC_Channel(
		G_PRIORITY_HIGH, pe_subsystem->ipc, FALSE, pe_msg_dispatch, NULL, 
		pe_connection_destroy);
	    
	} else {
	    crm_info("Ignoring request to start %s while shutting down",
		     this_subsys->name);
	}
    }
}

int fsa_pe_query = 0;
char *fsa_pe_ref = NULL;

/*	 A_PE_INVOKE	*/
void
do_pe_invoke(long long action,
	     enum crmd_fsa_cause cause,
	     enum crmd_fsa_state cur_state,
	     enum crmd_fsa_input current_input,
	     fsa_data_t *msg_data)
{
    	if(AM_I_DC == FALSE) {
		crm_err("Not DC: No need to invoke the PE (anymore): %s",
			fsa_action2string(action));
		return;
	}

	if(is_set(fsa_input_register, R_PE_CONNECTED) == FALSE){
	    if(is_set(fsa_input_register, R_SHUTDOWN)) {
		crm_err("Cannot shut down gracefully without the PE");
		register_fsa_input_before(C_FSA_INTERNAL, I_TERMINATE, NULL);

	    } else {
		crm_info("Waiting for the PE to connect");
		crmd_fsa_stall(NULL);
	    }
	    return;
	}

	if(is_set(fsa_input_register, R_HAVE_CIB) == FALSE) {
		crm_err("Attempted to invoke the PE without a consistent"
			" copy of the CIB!");

		/* start the join from scratch */
		register_fsa_input_before(C_FSA_INTERNAL, I_ELECTION, NULL);
		return;		
	}
	
	fsa_pe_query = fsa_cib_conn->cmds->query(
		fsa_cib_conn, NULL, NULL, cib_scope_local);

	crm_info("Query %d: Requesting the current CIB: %s", fsa_pe_query, fsa_state2string(fsa_state));

	/* Make sure any queued calculations are discarded */
	crm_free(fsa_pe_ref);
	fsa_pe_ref = NULL;

	fsa_cib_conn->cmds->register_callback(
	    fsa_cib_conn, fsa_pe_query, 60, FALSE, NULL,
	    "do_pe_invoke_callback", do_pe_invoke_callback);
}

void
do_pe_invoke_callback(xmlNode *msg, int call_id, int rc,
		      xmlNode *output, void *user_data)
{
	xmlNode *cmd = NULL;

	if(rc != cib_ok) {
	    crm_err("Cant retrieve the CIB: %s", cib_error2string(rc));
	    register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __FUNCTION__);
	    return;

	} else if(call_id != fsa_pe_query) {
		crm_debug_2("Skipping superceeded CIB query: %d (current=%d)",
			    call_id, fsa_pe_query);
		return;
		
	} else if(AM_I_DC == FALSE
	   || is_set(fsa_input_register, R_PE_CONNECTED) == FALSE) {
		crm_debug("No need to invoke the PE anymore");
		return;

	} else if(fsa_state != S_POLICY_ENGINE) {
		crm_debug("Discarding PE request in state: %s",
			  fsa_state2string(fsa_state));
		return;

	} else if(last_peer_update != 0) {
	    crm_debug("Re-asking for the CIB: peer update %d still pending",
		      last_peer_update);
	    
	    sleep(1);
	    register_fsa_action(A_PE_INVOKE);
	    return;

	} else if(fsa_state != S_POLICY_ENGINE) {
	    crm_err("Invoking PE in state: %s", fsa_state2string(fsa_state));
	    return;
	}

	CRM_DEV_ASSERT(output != NULL);

	crm_xml_add(output, XML_ATTR_DC_UUID, fsa_our_uuid);	    
	crm_xml_add_int(output, XML_ATTR_HAVE_QUORUM, fsa_has_quorum);

	if(ever_had_quorum && crm_have_quorum == FALSE) {
	    crm_xml_add_int(output, XML_ATTR_QUORUM_PANIC, 1);	    
	}
	
	cmd = create_request(CRM_OP_PECALC, output, NULL,
			     CRM_SYSTEM_PENGINE, CRM_SYSTEM_DC, NULL);

	crm_free(fsa_pe_ref);
	fsa_pe_ref = crm_element_value_copy(cmd, XML_ATTR_REFERENCE);

	if(send_ipc_message(pe_subsystem->ipc, cmd) == FALSE) {
	    crm_err("Could not contact the pengine");
	    register_fsa_error_adv(C_FSA_INTERNAL, I_ERROR, NULL, NULL, __FUNCTION__);
	}
	
	crm_info("Invoking the PE: query=%d, ref=%s, seq=%llu, quorate=%d",
		  fsa_pe_query, fsa_pe_ref, crm_peer_seq, fsa_has_quorum);
	free_xml(cmd);
}