aboutsummaryrefslogtreecommitdiffstats
path: root/src/upstream.h
blob: 101476bdff2a18a05d1efd9e224734a39b93f84b (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
#ifndef UPSTREAM_H
#define UPSTREAM_H

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

/*
 * Structure of generic upstream
 */
struct upstream {
	guint errors;						/**< Errors for this upstream 	*/
	time_t time;						/**< Time of marking 			*/
	guint dead;							/**< Dead flag					*/
	guint priority;						/**< Fixed priority				*/
	gint16 weight;						/**< Dynamic weight				*/
	guint32 *ketama_points;				/**< Ketama points array		*/
	size_t ketama_points_size;			/**< Ketama array size			*/
};

/*
 * Upstream error logic
 * 1. During error time we count upstream_ok and upstream_fail
 * 2. If failcount is more then maxerrors then we mark upstream as unavailable for dead time
 * 3. After dead time we mark upstream as alive and go to the step 1
 * 4. If all upstreams are dead, marks every upstream as alive
 */

/*
 * Add an error to an upstream
 */
void upstream_fail (struct upstream *up, time_t now);

/*
 * Increase upstream successes count
 */
void upstream_ok (struct upstream *up, time_t now);

/*
 * Make all upstreams alive
 */
void revive_all_upstreams (void *ups, size_t members, size_t msize);

/*
 * Add ketama points for upstream
 */
gint upstream_ketama_add (struct upstream *up, gchar *up_key, size_t keylen, size_t keypoints);

/*
 * Get a random upstream from array of upstreams
 * @param ups array of structures that contains struct upstream as their first element
 * @param members number of elements in array
 * @param msize size of each member
 * @param now current time
 * @param error_timeout time during which we are counting errors
 * @param revive_timeout time during which we counts upstream dead
 * @param max_errors maximum errors during error_timeout to mark upstream dead
 */
struct upstream* get_random_upstream   (void *ups, size_t members, size_t msize, 
										time_t now, time_t error_timeout, 
										time_t revive_timeout, size_t max_errors);

/*
 * Get upstream based on hash from array of upstreams
 * @param ups array of structures that contains struct upstream as their first element
 * @param members number of elements in array
 * @param msize size of each member
 * @param now current time
 * @param error_timeout time during which we are counting errors
 * @param revive_timeout time during which we counts upstream dead
 * @param max_errors maximum errors during error_timeout to mark upstream dead
 * @param key key for hashing
 * @param keylen length of the key
 */
struct upstream* get_upstream_by_hash  (void *ups, size_t members, size_t msize, 
										time_t now,  time_t error_timeout, 
										time_t revive_timeout, size_t max_errors,
										gchar *key, size_t keylen);

/*
 * Get an upstream from array of upstreams based on its current weight
 * @param ups array of structures that contains struct upstream as their first element
 * @param members number of elements in array
 * @param msize size of each member
 * @param now current time
 * @param error_timeout time during which we are counting errors
 * @param revive_timeout time during which we counts upstream dead
 * @param max_errors maximum errors during error_timeout to mark upstream dead
 */
struct upstream* get_upstream_round_robin (void *ups, size_t members, size_t msize, 
										time_t now, time_t error_timeout, 
										time_t revive_timeout, size_t max_errors);

/*
 * Get upstream based on hash from array of upstreams, this functions is using ketama algorithm
 * @param ups array of structures that contains struct upstream as their first element
 * @param members number of elements in array
 * @param msize size of each member
 * @param now current time
 * @param error_timeout time during which we are counting errors
 * @param revive_timeout time during which we counts upstream dead
 * @param max_errors maximum errors during error_timeout to mark upstream dead
 * @param key key for hashing
 * @param keylen length of the key
 */
struct upstream* get_upstream_by_hash_ketama (void *ups, size_t members, size_t msize, time_t now, 
										time_t error_timeout, time_t revive_timeout, size_t max_errors,
										gchar *key, size_t keylen);

/*
 * Get an upstream from array of upstreams based on its current priority (not weight)
 * @param ups array of structures that contains struct upstream as their first element
 * @param members number of elements in array
 * @param msize size of each member
 * @param now current time
 * @param error_timeout time during which we are counting errors
 * @param revive_timeout time during which we counts upstream dead
 * @param max_errors maximum errors during error_timeout to mark upstream dead
 */
struct upstream* get_upstream_master_slave (void *ups, size_t members, size_t msize,
										time_t now, time_t error_timeout,
										time_t revive_timeout, size_t max_errors);


#endif /* UPSTREAM_H */
/* 
 * vi:ts=4 
 */