-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathp_libcluster.h
More file actions
226 lines (194 loc) · 5.33 KB
/
Copy pathp_libcluster.h
File metadata and controls
226 lines (194 loc) · 5.33 KB
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
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright (c) 2015-2017 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef P_LIBCLUSTER_H_
# define P_LIBCLUSTER_H_ 1
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
# include <errno.h>
# ifdef HAVE_UNISTD_H
# include <unistd.h>
# endif
# ifdef HAVE_SYSLOG_H
# include <syslog.h>
# endif
# ifdef WITH_PTHREAD
# include <pthread.h>
# endif
# ifdef WITH_LIBUUID
# include <uuid/uuid.h>
# endif
# ifdef ENABLE_SQL
# include <libsql.h>
# endif
# ifdef ENABLE_ETCD
# include "libetcd.h"
# endif
# include "libcluster.h"
/* Default environment name, overridden with cluster_set_env() */
# define CLUSTER_DEFAULT_ENV "production"
/* Default etcd key time-to-live */
# define CLUSTER_DEFAULT_TTL 120
/* Default etcd refresh time */
# define CLUSTER_DEFAULT_REFRESH 30
/* Maximum length of a job identifier */
# define CLUSTER_JOB_ID_LEN 32
/* Maximum length of a job tag */
# define CLUSTER_JOB_TAG_LEN 7
/* Maximum length of a log message */
# define CLUSTER_JOB_LOG_LEN 511
/* Maximum length of a job name */
# define CLUSTER_JOB_NAME_LEN 32
/* We only use syslog for the LOG_xxx constants; if they aren't available
* we can provide generic values instead.
*/
# ifndef LOG_EMERG
# define LOG_EMERG 0
# endif
# ifndef LOG_ALERT
# define LOG_ALERT 1
# endif
# ifndef LOG_CRIT
# define LOG_CRIT 2
# endif
# ifndef LOG_ERR
# define LOG_ERR 3
# endif
# ifndef LOG_WARNING
# define LOG_WARNING 4
# endif
# ifndef LOG_NOTICE
# define LOG_NOTICE 5
# endif
# ifndef LOG_INFO
# define LOG_INFO 6
# endif
# ifndef LOG_DEBUG
# define LOG_DEBUG 7
# endif
typedef enum
{
CT_STATIC,
CT_ETCD,
CT_SQL
} CLUSTERTYPE;
typedef enum
{
CF_NONE = 0,
CF_JOINED = (1<<0),
CF_LEAVING = (1<<1),
CF_VERBOSE = (1<<2),
CF_PASSIVE = (1<<3)
} CLUSTERFLAGS;
struct cluster_struct
{
CLUSTER *next;
CLUSTER *prev;
CLUSTERTYPE type;
CLUSTERFLAGS flags;
CLUSTERFORK forkmode;
# ifdef WITH_PTHREAD
pthread_rwlock_t lock;
# endif
char *instid;
char *key;
char *env;
char *registry;
char *partition;
/* Current state */
int inst_index;
int inst_threads;
int total_threads;
/* Callbacks */
# ifdef ENABLE_LOGGING
void (*logger)(int priority, const char *format, va_list ap);
# endif
CLUSTERBALANCE balancer;
# if defined(ENABLE_ETCD) || defined(ENABLE_SQL)
int ttl;
int refresh;
# endif
# ifdef ENABLE_ETCD
/* etcd-based clustering */
ETCD *etcd_root;
ETCD *etcd_clusterdir;
ETCD *etcd_partitiondir;
ETCD *etcd_envdir;
# endif /*ENABLE_ETCD*/
# ifdef ENABLE_SQL
SQL *pingdb;
SQL *balancedb;
SQL *jobdb;
# endif /*ENABLE_SQL*/
# ifdef WITH_PTHREAD
pthread_t ping_thread;
pthread_t balancer_thread;
# endif /*WITH_PTHREAD*/
};
struct cluster_job_struct
{
CLUSTER *cluster;
char id[CLUSTER_JOB_ID_LEN+1];
char parent[CLUSTER_JOB_ID_LEN+1];
char tag[CLUSTER_JOB_TAG_LEN+1];
char name[CLUSTER_JOB_NAME_LEN+1];
int total;
int progress;
char *logbuf;
};
void cluster_logf_(CLUSTER *cluster, int priority, const char *format, ...);
void cluster_vlogf_(CLUSTER *cluster, int priority, const char *format, va_list ap);
void cluster_logf_locked_(CLUSTER *cluster, int priority, const char *format, ...);
void cluster_vlogf_locked_(CLUSTER *cluster, int priority, const char *format, va_list ap);
# ifndef ENABLE_LOGGING
# if __STDC_VERSION__ >= 199901L
# define cluster_logf_(...) /* */
# define cluster_logf_locked_(...) /* */
# elif __GNUC__
# define cluster_logf_(cluster...) /* */
# define cluster_logf_locked_(cluster...) /* */
# else
# define NEED_LOGGING_NOOPS 1
# endif
# define cluster_vlogf_locked_(c, p, f, a) /* */
# endif
void cluster_rdlock_(CLUSTER *cluster);
void cluster_wrlock_(CLUSTER *cluster);
void cluster_unlock_(CLUSTER *cluster);
int cluster_rebalanced_(CLUSTER *cluster);
int cluster_static_join_(CLUSTER *cluster);
int cluster_static_leave_(CLUSTER *cluster);
# ifdef ENABLE_ETCD
int cluster_etcd_join_(CLUSTER *cluster);
int cluster_etcd_leave_(CLUSTER *cluster);
void cluster_etcd_prepare_(CLUSTER *cluster);
void cluster_etcd_child_(CLUSTER *cluster);
void cluster_etcd_parent_(CLUSTER *cluster);
# endif
# ifdef ENABLE_SQL
int cluster_sql_join_(CLUSTER *cluster);
int cluster_sql_leave_(CLUSTER *cluster);
void cluster_sql_prepare_(CLUSTER *cluster);
void cluster_sql_child_(CLUSTER *cluster);
void cluster_sql_parent_(CLUSTER *cluster);
int cluster_sql_job_create_(CLUSTERJOB *job);
# endif
int cluster_reset_instance_locked_(CLUSTER *cluster);
/* Deprecated public methods retained for binary compatibility */
int cluster_set_threads(CLUSTER *cluster, int nthreads);
#endif /*!LIBCLUSTER_H_*/