summaryrefslogtreecommitdiff
path: root/source4/dsdb/repl/drepl_periodic.c
blob: d6b94679e0e7f548f8f0e4183c8e50a720720d9e (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
/* 
   Unix SMB/CIFS mplementation.
   DSDB replication service periodic handling
   
   Copyright (C) Stefan Metzmacher 2007
    
   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 3 of the License, or
   (at your option) any later version.
   
   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, see <http://www.gnu.org/licenses/>.
   
*/

#include "includes.h"
#include "lib/events/events.h"
#include "dsdb/samdb/samdb.h"
#include "auth/auth.h"
#include "smbd/service.h"
#include "dsdb/repl/drepl_service.h"
#include <ldb_errors.h>
#include "../lib/util/dlinklist.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "librpc/gen_ndr/ndr_drsuapi.h"
#include "librpc/gen_ndr/ndr_drsblobs.h"

static void dreplsrv_periodic_run(struct dreplsrv_service *service);

static void dreplsrv_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
					 struct timeval t, void *ptr)
{
	struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
	WERROR status;

	service->periodic.te = NULL;

	dreplsrv_periodic_run(service);

	status = dreplsrv_periodic_schedule(service, service->periodic.interval);
	if (!W_ERROR_IS_OK(status)) {
		task_server_terminate(service->task, win_errstr(status), false);
		return;
	}
}

WERROR dreplsrv_periodic_schedule(struct dreplsrv_service *service, uint32_t next_interval)
{
	TALLOC_CTX *tmp_mem;
	struct tevent_timer *new_te;
	struct timeval next_time;

	/* prevent looping */
	if (next_interval == 0) next_interval = 1;

	next_time = timeval_current_ofs(next_interval, 50);

	if (service->periodic.te) {
		/*
		 * if the timestamp of the new event is higher,
		 * as current next we don't need to reschedule
		 */
		if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
			return WERR_OK;
		}
	}

	/* reset the next scheduled timestamp */
	service->periodic.next_event = next_time;

	new_te = tevent_add_timer(service->task->event_ctx, service,
			         service->periodic.next_event,
			         dreplsrv_periodic_handler_te, service);
	W_ERROR_HAVE_NO_MEMORY(new_te);

	tmp_mem = talloc_new(service);
	DEBUG(4,("dreplsrv_periodic_schedule(%u) %sscheduled for: %s\n",
		next_interval,
		(service->periodic.te?"re":""),
		nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
	talloc_free(tmp_mem);

	talloc_free(service->periodic.te);
	service->periodic.te = new_te;

	return WERR_OK;
}

static void dreplsrv_periodic_run(struct dreplsrv_service *service)
{
	TALLOC_CTX *mem_ctx;

	DEBUG(4,("dreplsrv_periodic_run(): schedule pull replication\n"));

	/*
	 * KCC or some administrative tool
	 * might have changed Topology graph
	 * i.e. repsFrom/repsTo
	 */
	dreplsrv_refresh_partitions(service);

	mem_ctx = talloc_new(service);
	dreplsrv_schedule_pull_replication(service, mem_ctx);
	talloc_free(mem_ctx);

	DEBUG(4,("dreplsrv_periodic_run(): run pending_ops memory=%u\n", 
		 (unsigned)talloc_total_blocks(service)));

	dreplsrv_ridalloc_check_rid_pool(service);

	dreplsrv_run_pending_ops(service);
}

/*
  run the next pending op, either a notify or a pull
 */
void dreplsrv_run_pending_ops(struct dreplsrv_service *s)
{
	if (!s->ops.notifies && !s->ops.pending) {
		return;
	}
	if (!s->ops.notifies ||
	    (s->ops.pending &&
	     s->ops.notifies->schedule_time > s->ops.pending->schedule_time)) {
		dreplsrv_run_pull_ops(s);
	} else {
		dreplsrv_notify_run_ops(s);
	}
}

static void dreplsrv_pending_run(struct dreplsrv_service *service);

static void dreplsrv_pending_handler_te(struct tevent_context *ev, struct tevent_timer *te,
					struct timeval t, void *ptr)
{
	struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);

	service->pending.te = NULL;

	dreplsrv_pending_run(service);
}

WERROR dreplsrv_pendingops_schedule(struct dreplsrv_service *service, uint32_t next_interval)
{
	TALLOC_CTX *tmp_mem;
	struct tevent_timer *new_te;
	struct timeval next_time;

	/* prevent looping */
	if (next_interval == 0) {
		next_interval = 1;
	}

	next_time = timeval_current_ofs(next_interval, 50);

	if (service->pending.te) {
		/*
		 * if the timestamp of the new event is higher,
		 * as current next we don't need to reschedule
		 */
		if (timeval_compare(&next_time, &service->pending.next_event) > 0) {
			return WERR_OK;
		}
	}

	/* reset the next scheduled timestamp */
	service->pending.next_event = next_time;

	new_te = tevent_add_timer(service->task->event_ctx, service,
				  service->pending.next_event,
			         dreplsrv_pending_handler_te, service);
	W_ERROR_HAVE_NO_MEMORY(new_te);

	tmp_mem = talloc_new(service);
	DEBUG(4,("dreplsrv_pending_schedule(%u) %sscheduled for: %s\n",
		next_interval,
		(service->pending.te?"re":""),
		nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
	talloc_free(tmp_mem);

	talloc_free(service->pending.te);
	service->pending.te = new_te;

	return WERR_OK;
}

static void dreplsrv_pending_run(struct dreplsrv_service *service)
{
	dreplsrv_run_pending_ops(service);
}