r12391: use the new periodic schedule system for the pull replication too
[kai/samba.git] / source4 / wrepl_server / wrepl_out_pull.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "dlinklist.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "lib/messaging/irpc.h"
30 #include "librpc/gen_ndr/ndr_winsrepl.h"
31 #include "wrepl_server/wrepl_server.h"
32 #include "nbt_server/wins/winsdb.h"
33 #include "ldb/include/ldb.h"
34 #include "libcli/composite/composite.h"
35 #include "libcli/wrepl/winsrepl.h"
36 #include "wrepl_server/wrepl_out_helpers.h"
37
38 static void wreplsrv_out_pull_reschedule(struct wreplsrv_partner *partner, uint32_t interval)
39 {
40         NTSTATUS status;
41
42         partner->pull.next_run = timeval_current_ofs(interval, 0);
43         status = wreplsrv_periodic_schedule(partner->service, interval);
44         if (!NT_STATUS_IS_OK(status)) {
45                 DEBUG(0,("wreplsrv_periodic_schedule() failed\n"));
46         }
47 }
48
49 static void wreplsrv_pull_handler_creq(struct composite_context *creq)
50 {
51         struct wreplsrv_partner *partner = talloc_get_type(creq->async.private_data, struct wreplsrv_partner);
52         struct wreplsrv_pull_cycle_io *old_cycle_io;
53         struct wrepl_table inform_in;
54
55         partner->pull.last_status = wreplsrv_pull_cycle_recv(partner->pull.creq);
56         partner->pull.creq = NULL;
57
58         old_cycle_io = partner->pull.cycle_io;
59         partner->pull.cycle_io = NULL;
60
61         if (NT_STATUS_IS_OK(partner->pull.last_status)) {
62                 partner->pull.error_count = 0;
63                 DEBUG(2,("wreplsrv_pull_cycle(%s): %s\n",
64                          partner->address, nt_errstr(partner->pull.last_status)));
65                 goto done;
66         }
67
68         partner->pull.error_count++;
69
70         if (partner->pull.error_count > 1) {
71                 uint32_t retry_interval;
72
73                 retry_interval = partner->pull.error_count * partner->pull.retry_interval;
74                 retry_interval = MIN(retry_interval, partner->pull.interval);
75
76                 DEBUG(1,("wreplsrv_pull_cycle(%s): %s: error_count: %u: reschedule(%u)\n",
77                          partner->address, nt_errstr(partner->pull.last_status),
78                          partner->pull.error_count, retry_interval));
79
80                 wreplsrv_out_pull_reschedule(partner, retry_interval);
81                 goto done;
82         }
83
84         DEBUG(1,("wreplsrv_pull_cycle(%s): %s: error_count:%u retry\n",
85                  partner->address, nt_errstr(partner->pull.last_status),
86                  partner->pull.error_count));
87         inform_in.partner_count = old_cycle_io->in.num_owners;
88         inform_in.partners      = old_cycle_io->in.owners;
89         wreplsrv_out_partner_pull(partner, &inform_in);
90 done:
91         talloc_free(old_cycle_io);
92 }
93
94 void wreplsrv_out_partner_pull(struct wreplsrv_partner *partner, struct wrepl_table *inform_in)
95 {
96         /* there's already a pull in progress, so we're done */
97         if (partner->pull.creq) return;
98
99         partner->pull.cycle_io = talloc(partner, struct wreplsrv_pull_cycle_io);
100         if (!partner->pull.cycle_io) {
101                 goto nomem;
102         }
103
104         partner->pull.cycle_io->in.partner      = partner;
105         partner->pull.cycle_io->in.wreplconn    = NULL;
106         if (inform_in) {
107                 partner->pull.cycle_io->in.num_owners   = inform_in->partner_count;
108                 partner->pull.cycle_io->in.owners       = inform_in->partners;
109                 talloc_steal(partner->pull.cycle_io, inform_in->partners);
110         } else {
111                 partner->pull.cycle_io->in.num_owners   = 0;
112                 partner->pull.cycle_io->in.owners       = NULL;
113         }
114         partner->pull.creq = wreplsrv_pull_cycle_send(partner->pull.cycle_io, partner->pull.cycle_io);
115         if (!partner->pull.creq) {
116                 DEBUG(1,("wreplsrv_pull_cycle_send(%s) failed\n",
117                          partner->address));
118                 goto nomem;
119         }
120
121         partner->pull.creq->async.fn            = wreplsrv_pull_handler_creq;
122         partner->pull.creq->async.private_data  = partner;
123
124         return;
125 nomem:
126         talloc_free(partner->pull.cycle_io);
127         partner->pull.cycle_io = NULL;
128         DEBUG(0,("wreplsrv_out_partner_pull(%s): no memory? (ignoring)\n",
129                 partner->address));
130         return;
131 }
132
133 NTSTATUS wreplsrv_out_pull_run(struct wreplsrv_service *service)
134 {
135         struct wreplsrv_partner *partner;
136
137         for (partner = service->partners; partner; partner = partner->next) {
138                 /* if it's not a pull partner, go to the next partner */
139                 if (!(partner->type & WINSREPL_PARTNER_PULL)) continue;
140
141                 /* if pulling is disabled for the partner, go to the next one */
142                 if (partner->pull.interval == 0) continue;
143
144                 /* if the next timer isn't reached, go to the next partner */
145                 if (!timeval_expired(&partner->pull.next_run)) continue;
146
147                 wreplsrv_out_pull_reschedule(partner, partner->pull.interval);
148
149                 wreplsrv_out_partner_pull(partner, NULL);
150         }
151
152         return NT_STATUS_OK;
153 }