summaryrefslogtreecommitdiffstats
path: root/ldap/servers/plugins/replication/repl5_protocol.c
blob: 8fdf8125a56ff1828dd54aed891ac1dc3c0f8590 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/** BEGIN COPYRIGHT BLOCK
 * 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., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/* repl5_protocol.c */
/*

 The replication protocol object manages the replication protocol for
 a given replica. It determines which protocol(s) are appropriate to
 use when updating a given replica. It also knows how to arbitrate
 incremental and total update protocols for a given replica.

*/

#include "repl5.h"
#include "repl5_prot_private.h"

#define PROTOCOL_5_INCREMENTAL 1
#define PROTOCOL_5_TOTAL 2
#define PROTOCOL_4_INCREMENTAL 3
#define PROTOCOL_4_TOTAL 4
#define PROTOCOL_WINDOWS_INCREMENTAL 5
#define PROTOCOL_WINDOWS_TOTAL 6

typedef struct repl_protocol
{
	Private_Repl_Protocol *prp_incremental; /* inc protocol to use */
	Private_Repl_Protocol *prp_total; /* total protocol to use */
	Private_Repl_Protocol *prp_active_protocol; /* Pointer to active protocol */
	Repl_Agmt *agmt; /* The replication agreement we're servicing */
	Repl_Connection *conn; /* Connection to remote server */
	Object *replica_object; /* Local replica. If non-NULL, replica object is acquired */
	int state;
	int next_state;
	PRLock *lock;
} repl_protocol;


/* States */
#define STATE_FINISHED 503
#define STATE_BAD_STATE_SHOULD_NEVER_HAPPEN 599

/* Forward declarations */
static Private_Repl_Protocol *private_protocol_factory(Repl_Protocol *rp, int type);




/*
 * Create a new protocol instance.
 */
Repl_Protocol *
prot_new(Repl_Agmt *agmt, int protocol_state)
{
	Slapi_DN *replarea_sdn = NULL;
	Repl_Protocol *rp = (Repl_Protocol *)slapi_ch_malloc(sizeof(Repl_Protocol));

	rp->prp_incremental = rp->prp_total = rp->prp_active_protocol = NULL;
	if (protocol_state == STATE_PERFORMING_TOTAL_UPDATE)
	{
		rp->state = STATE_PERFORMING_TOTAL_UPDATE;
	}
	else
	{
		rp->state = STATE_PERFORMING_INCREMENTAL_UPDATE;
	}
	rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE; 
	if ((rp->lock = PR_NewLock()) == NULL)
	{
		goto loser;
	}
	rp->agmt = agmt;
	/* now done in private_protocol_factory
	if ((rp->conn = conn_new(agmt)) == NULL)
	{
		goto loser;
	} */
	/* Acquire the local replica object */
	replarea_sdn = agmt_get_replarea(agmt);
	rp->replica_object = replica_get_replica_from_dn(replarea_sdn);
	if (NULL == rp->replica_object)
	{
		/* Whoa, no local replica!?!? */
		slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
			"%s: Unable to locate replica object for local replica %s\n",
			agmt_get_long_name(agmt),
			slapi_sdn_get_dn(replarea_sdn));
		goto loser;
	}

	if (get_agmt_agreement_type(agmt) == REPLICA_TYPE_MULTIMASTER)
	{
	rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_5_INCREMENTAL);
	rp->prp_total = private_protocol_factory(rp, PROTOCOL_5_TOTAL);
	} 
	else if  (get_agmt_agreement_type(agmt) == REPLICA_TYPE_WINDOWS)
	{
	rp->prp_incremental = private_protocol_factory(rp, PROTOCOL_WINDOWS_INCREMENTAL);
	rp->prp_total = private_protocol_factory(rp, PROTOCOL_WINDOWS_TOTAL);
	}

	/* XXXggood register callback handlers for entries updated, and
		schedule window enter/leave. */
	slapi_sdn_free(&replarea_sdn);
	
	return rp;
loser:
	prot_delete(&rp);
	return NULL;
}





Object *
prot_get_replica_object(Repl_Protocol *rp)
{
	PR_ASSERT(NULL != rp);
	return rp->replica_object;
}





Repl_Agmt *
prot_get_agreement(Repl_Protocol *rp)
{
        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
	if (NULL == rp) return NULL;
	return rp->agmt;
}




void
prot_free(Repl_Protocol **rpp)
{
    Repl_Protocol *rp = NULL;

    if (rpp == NULL || *rpp == NULL) return;

    rp = *rpp;

    PR_Lock(rp->lock);
    if (NULL != rp->prp_incremental)
    {
        rp->prp_incremental->delete(&rp->prp_incremental);
    }
    if (NULL != rp->prp_total)
    {
        rp->prp_total->delete(&rp->prp_total);
    }
    if (NULL != rp->replica_object)
    {
        object_release(rp->replica_object);
    }
    if (NULL != rp->conn)
    {
        conn_delete(rp->conn);
    }
    rp->prp_active_protocol = NULL;
    PR_Unlock(rp->lock);
    slapi_ch_free((void **)rpp);
}

/*
 * Destroy a protocol instance XXXggood not complete
 */
void
prot_delete(Repl_Protocol **rpp)
{
	Repl_Protocol *rp;

	PR_ASSERT(NULL != rpp);
	rp = *rpp;
	/* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
	if (NULL != rp)
	{
		prot_stop(rp);
                prot_free(rpp);
	}
}





/*
 * Get the connection object.
 */
Repl_Connection *
prot_get_connection(Repl_Protocol *rp)
{
	Repl_Connection *return_value;

	PR_ASSERT(NULL != rp);
	PR_Lock(rp->lock);
	return_value = rp->conn;
	PR_Unlock(rp->lock);
	return return_value;
}




/*
 * This function causes the total protocol to start.
 * This is accomplished by registering a state transition
 * to a new state, and then signaling the incremental
 * protocol to stop.
 */
void 
prot_initialize_replica(Repl_Protocol *rp)
{
	PR_ASSERT(NULL != rp);

	PR_Lock(rp->lock);
    /* check that total protocol is not running */
	rp->next_state = STATE_PERFORMING_TOTAL_UPDATE;
	/* Stop the incremental protocol, if running */
	rp->prp_incremental->stop(rp->prp_incremental);
	if (rp->prp_total) agmt_set_last_init_status(rp->prp_total->agmt, 0, 0, NULL);
    PR_Unlock(rp->lock);
}





/*
 * Main thread for protocol manager.

This is a simple state machine. State transition table:

Initial state: incremental update

STATE                   EVENT                   NEXT STATE
-----                   -----                   ----------
incremental update      shutdown                finished
incremental update      total update requested  total update
total update            shutdown                finished
total update            update complete         incremental update
finished                (any)                   finished

*/

static void
prot_thread_main(void *arg)
{
	Repl_Protocol *rp = (Repl_Protocol *)arg;
	int done;

	PR_ASSERT(NULL != rp);

	if (rp->agmt) {
		set_thread_private_agmtname (agmt_get_long_name(rp->agmt));
	}

	done = 0;

	while (!done)
	  {
	    switch (rp->state)
	      {
	      case STATE_PERFORMING_INCREMENTAL_UPDATE:
		/* Run the incremental update protocol */
		PR_Lock(rp->lock);
		dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): begin");
		rp->prp_active_protocol = rp->prp_incremental;
		PR_Unlock(rp->lock);
		rp->prp_incremental->run(rp->prp_incremental);
		dev_debug("prot_thread_main(STATE_PERFORMING_INCREMENTAL_UPDATE): end");
		break;
	      case STATE_PERFORMING_TOTAL_UPDATE:
		PR_Lock(rp->lock);
    
		/* stop incremental protocol if running */
		rp->prp_active_protocol = rp->prp_total;
		/* After total protocol finished, return to incremental */
		rp->next_state = STATE_PERFORMING_INCREMENTAL_UPDATE;
		PR_Unlock(rp->lock);
		/* Run the total update protocol */
		dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): begin");
		rp->prp_total->run(rp->prp_total);
		dev_debug("prot_thread_main(STATE_PERFORMING_TOTAL_UPDATE): end");
		/* update the agreement entry to notify clients that 
		   replica initialization is completed. */
		agmt_replica_init_done (rp->agmt);
    
		break;
	      case STATE_FINISHED:
		dev_debug("prot_thread_main(STATE_FINISHED): exiting prot_thread_main");
		done = 1;
		break;
	      }
	    rp->state = rp->next_state;
	  }
}


/*
 * Start a thread to handle the replication protocol.
 */
void
prot_start(Repl_Protocol *rp)
{
	PR_ASSERT(NULL != rp);
	if (NULL != rp)
	{
		if (PR_CreateThread(PR_USER_THREAD, prot_thread_main, (void *)rp,
#if defined(__hpux) && defined(__ia64)
			PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 524288L ) == NULL)
#else
			PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
#endif
        {
            PRErrorCode prerr = PR_GetError();

            slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
			"%s: Unable to create protocol thread; NSPR error - %d, %s\n",
			agmt_get_long_name(rp->agmt),
			prerr, slapd_pr_strerror(prerr));   
        }
	}
	else
	{
		slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to start "
			"protocol object - NULL protocol object passed to prot_start.\n");
	}
}





/*
 * Stop a protocol instance. 
 */
void
prot_stop(Repl_Protocol *rp)
{
	PR_ASSERT(NULL != rp);
	if (NULL != rp)
	{
		PR_Lock(rp->lock);
		rp->next_state = STATE_FINISHED;
        if (NULL != rp->prp_incremental)
        {
		    if (rp->prp_incremental->stop(rp->prp_incremental) != 0)
			{
				slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
					"Warning: incremental protocol for replica \"%s\" "
					"did not shut down properly.\n",
					agmt_get_long_name(rp->agmt));
			}
        }
        if (NULL != rp->prp_total)
        {
		    if (rp->prp_total->stop(rp->prp_total) != 0)
			{
				slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
					"Warning: total protocol for replica \"%s\" "
					"did not shut down properly.\n",
					agmt_get_long_name(rp->agmt));
			}
        }
		PR_Unlock(rp->lock);
	}
	else
	{
		slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Error: prot_stop() "
			" called on NULL protocol instance.\n");
	}
}





/*
 * Call the notify_update method of the incremental or total update
 * protocol, is either is active.
 */
void
prot_notify_update(Repl_Protocol *rp)
{
        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
        if (NULL == rp) return;

	PR_Lock(rp->lock);
	if (NULL != rp->prp_active_protocol)
	{
		rp->prp_active_protocol->notify_update(rp->prp_active_protocol);
	}
	PR_Unlock(rp->lock);
}


/*
 * Call the notify_agmt_changed method of the incremental or total update
 * protocol, is either is active.
 */
void
prot_notify_agmt_changed(Repl_Protocol *rp, char * agmt_name)
{
        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
        if (NULL == rp) {
		slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
			"Replication agreement for %s could not be updated. "
			"For replication to take place, please enable the suffix "
			"and restart the server\n", agmt_name);
		return;
	}

	PR_Lock(rp->lock);
	if (NULL != rp->prp_active_protocol)
	{
		rp->prp_active_protocol->notify_agmt_changed(rp->prp_active_protocol);
	}
	PR_Unlock(rp->lock);
}


void 
prot_notify_window_opened (Repl_Protocol *rp)
{
        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
        if (NULL == rp) return;

	PR_Lock(rp->lock);
	if (NULL != rp->prp_active_protocol)
	{
		rp->prp_active_protocol->notify_window_opened(rp->prp_active_protocol);
	}
	PR_Unlock(rp->lock);
}


void 
prot_notify_window_closed (Repl_Protocol *rp)
{
        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
        if (NULL == rp) return;

	PR_Lock(rp->lock);
	if (NULL != rp->prp_active_protocol)
	{
		rp->prp_active_protocol->notify_window_closed(rp->prp_active_protocol);
	}
	PR_Unlock(rp->lock);
}


int
prot_status(Repl_Protocol *rp)
{
	int return_status = PROTOCOL_STATUS_UNKNOWN;

        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */
	if (NULL != rp)
	{
		PR_Lock(rp->lock);
		if (NULL != rp->prp_active_protocol)
		{
			return_status = rp->prp_active_protocol->status(rp->prp_active_protocol);
		}
		PR_Unlock(rp->lock);
	}
	return return_status;
}


/*
 * Start an incremental protocol session, even if we're not
 * currently in a schedule window.
 * If the total protocol is active, do nothing.
 * Otherwise, notify the incremental protocol that it should
 * run once.
 */
void
prot_replicate_now(Repl_Protocol *rp)
{
        /* MAB: rp might be NULL for disabled suffixes. Don't ASSERT on it */

	if (NULL != rp)
	{
		PR_Lock(rp->lock);
		if (rp->prp_incremental == rp->prp_active_protocol)
		{
			rp->prp_active_protocol->update_now(rp->prp_active_protocol);
		}
		PR_Unlock(rp->lock);
	}
}

/*
 * A little factory function to create a protocol
 * instance of the correct type.
 */
static Private_Repl_Protocol *
private_protocol_factory(Repl_Protocol *rp, int type)
{
	Private_Repl_Protocol *prp;

	switch (type)
	{
		case PROTOCOL_5_INCREMENTAL:
			if ((rp->conn = conn_new(rp->agmt)) != NULL)
			prp = Repl_5_Inc_Protocol_new(rp);
			break;
		case PROTOCOL_5_TOTAL:
			if ((rp->conn = conn_new(rp->agmt)) != NULL)
			prp = Repl_5_Tot_Protocol_new(rp);
			break;
		case PROTOCOL_WINDOWS_INCREMENTAL: 
			if ((rp->conn = windows_conn_new(rp->agmt)) != NULL)
			prp = Windows_Inc_Protocol_new(rp);
			break;
		case PROTOCOL_WINDOWS_TOTAL: 
			if ((rp->conn = windows_conn_new(rp->agmt)) != NULL)
			prp = Windows_Tot_Protocol_new(rp);
			break;
	}
	return prp;
}