events.h 5.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*  Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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/>.
*/

#pragma once

#include <pthread.h>
#include <stdbool.h>

22
#include "common-knot/evsched.h"
23
#include "libknot/internal/namedb/namedb.h"
24 25
#include "knot/worker/pool.h"

26
/* Timer special values. */
27
#define ZONE_EVENT_NOW 0
28

29
struct zone;
30

31
struct server;
32 33 34 35

typedef enum zone_event_type {
	ZONE_EVENT_INVALID = -1,
	// supported event types
36
	ZONE_EVENT_RELOAD = 0,
37
	ZONE_EVENT_REFRESH,
38
	ZONE_EVENT_XFER,
39
	ZONE_EVENT_UPDATE,
40
	ZONE_EVENT_EXPIRE,
41
	ZONE_EVENT_FLUSH,
42
	ZONE_EVENT_NOTIFY,
43 44 45 46 47 48
	ZONE_EVENT_DNSSEC,
	// terminator
	ZONE_EVENT_COUNT,
} zone_event_type_t;

typedef struct zone_events {
49 50
	pthread_mutex_t mx;		//!< Mutex protecting the struct.
	bool running;			//!< Some zone event is being run.
51
	bool frozen;			//!< Terminated, don't schedule new events.
52

53 54
	event_t *event;			//!< Scheduler event.
	worker_pool_t *pool;		//!< Server worker pool.
Daniel Salzman's avatar
Daniel Salzman committed
55
	namedb_t *timers_db;		//!< Persistent zone timers database.
56 57 58 59 60 61 62 63

	task_t task;			//!< Event execution context.
	time_t time[ZONE_EVENT_COUNT];	//!< Event execution times.
} zone_events_t;

/*!
 * \brief Initialize zone events.
 *
64 65 66 67
 * The function will not set up the scheduling, use \ref zone_events_enable
 * to do that.
 *
 * \param zone  Pointer to zone (context of execution).
68 69
 *
 * \return KNOT_E*
70
 */
71
int zone_events_init(struct zone *zone);
72 73 74 75

/*!
 * \brief Set up zone events execution.
 *
76
 * \param zone       Zone to setup.
77 78
 * \param workers    Worker thread pool.
 * \param scheduler  Event scheduler.
79
 * \param timers_db  Persistent timers database. Can be NULL.
80 81
 *
 * \return KNOT_E*
82
 */
83
int zone_events_setup(struct zone *zone, worker_pool_t *workers,
84
                      evsched_t *scheduler, namedb_t *timers_db);
85 86 87

/*!
 * \brief Deinitialize zone events.
88 89
 *
 * \param zone  Zone whose events we want to deinitialize.
90
 */
91
void zone_events_deinit(struct zone *zone);
92

93 94 95 96 97 98 99 100 101
/*!
 * \brief Enqueue event type for asynchronous execution.
 *
 * \note This is similar to the scheduling an event for NOW, but it can
 *       bypass the event scheduler if no event is running at the moment.
 *
 * \param zone  Zone to schedule new event for.
 * \param type  Type of event.
 */
102
void zone_events_enqueue(struct zone *zone, zone_event_type_t type);
103

104
/*!
105
 * \brief Schedule new zone event to absolute time.
106 107 108 109
 *
 * \param zone  Zone to schedule new event for.
 * \param type  Type of event.
 * \param time  Absolute time.
110
 */
111
void zone_events_schedule_at(struct zone *zone, zone_event_type_t type, time_t time);
112 113 114

/*!
 * \brief Schedule new zone event using relative time to current time.
115 116 117 118
 *
 * \param zone  Zone to schedule new event for.
 * \param type  Type of event.
 * \param dt    Relative time.
119
 */
120
void zone_events_schedule(struct zone *zone, zone_event_type_t type, unsigned dt);
121

122 123 124 125 126 127
/*!
 * \brief Check if zone event is scheduled.
 *
 * \param zone  Zone to check event of.
 * \param type  Type of event.
 */
128
bool zone_events_is_scheduled(struct zone *zone, zone_event_type_t type);
129

130 131
/*!
 * \brief Cancel one zone event.
132 133 134
 *
 * \param zone  Zone to cancel event in.
 * \param type  Type of event to cancel.
135
 */
136
void zone_events_cancel(struct zone *zone, zone_event_type_t type);
137 138

/*!
139
 * \brief Freeze all zone events and prevent new events from running.
140 141
 *
 * \param zone  Zone to freeze events for.
142
 */
143
void zone_events_freeze(struct zone *zone);
144 145 146

/*!
 * \brief Start the events processing.
147 148
 *
 * \param zone  Zone to start processing for.
149
 */
150
void zone_events_start(struct zone *zone);
151 152 153 154

/*!
 * \brief Return time of the occurence of the given event.
 *
155 156
 * \param zone  Zone to get event time from.
 * \param type  Event type.
157
 *
158
 * \retval time of the event when event found
159 160
 * \retval 0 when the event is not planned
 * \retval negative value if event is invalid
161
 */
162
time_t zone_events_get_time(const struct zone *zone, zone_event_type_t type);
163 164 165

/*!
 * \brief Return text name of the event.
166 167 168 169 170
 *
 * \param type  Type of event.
 *
 * \retval String with event name if it exists.
 * \retval NULL if the event does not exist.
171 172 173 174 175 176
 */
const char *zone_events_get_name(zone_event_type_t type);

/*!
 * \brief Return time and type of the next event.
 *
177 178 179
 * \param zone  Zone to get next event from.
 * \param type  [out] Type of the next event will be stored in the parameter.
 *
180 181
 * \return time of the next event or an error (negative number)
 */
182
time_t zone_events_get_next(const struct zone *zone, zone_event_type_t *type);
183 184 185 186 187 188 189

/*!
 * \brief Replans zone events after config change. Will reuse events where applicable.
 *
 * \param zone      Zone with new config.
 * \param old_zone  Zone with old config.
 */
190
void zone_events_update(struct zone *zone, struct zone *old_zone);
191 192 193 194 195 196 197

/*!
 * \brief Replans DDNS processing event if DDNS queue is not empty.
 *
 * \param zone      Zone with new config.
 * \param old_zone  Zone with old config.
 */
198
void zone_events_replan_ddns(struct zone *zone, const struct zone *old_zone);
199 200 201 202 203 204

/*!
 * \brief Write persistent timers to timers database.
 *
 * \return KNOT_E*
 */
205
int zone_events_write_persistent(struct zone *zone);