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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
/**
@file Quorum daemon scoring functions + thread.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <ccs.h>
#include <liblogthread.h>
#include <sched.h>
#include <sys/mman.h>
#include "disk.h"
#include "score.h"
static pthread_mutex_t sc_lock = PTHREAD_MUTEX_INITIALIZER;
static int _score = 0, _maxscore = 0, _score_thread_running = 0;
static pthread_t score_thread = (pthread_t)0;
extern void set_priority(int, int);
struct h_arg {
struct h_data *h;
int sched_queue;
int sched_prio;
int count;
};
/*
XXX Messy, but works for now...
*/
static void
nullify(void)
{
int fd[3];
close(0);
close(1);
close(2);
fd[0] = open("/dev/null", O_RDONLY);
if (fd[0] != 0)
dup2(fd[0], 0);
fd[1] = open("/dev/null", O_WRONLY);
if (fd[1] != 1)
dup2(fd[1], 1);
fd[2] = open("/dev/null", O_WRONLY);
if (fd[2] != 2)
dup2(fd[2], 2);
}
/**
Set all signal handlers to default for exec of a script.
ONLY do this after a fork().
*/
static void
restore_signals(void)
{
sigset_t set;
int x;
for (x = 1; x < _NSIG; x++)
signal(x, SIG_DFL);
sigfillset(&set);
sigprocmask(SIG_UNBLOCK, &set, NULL);
}
/**
Spin off a user-defined heuristic
*/
static int
fork_heuristic(struct h_data *h)
{
int pid;
char *argv[4];
time_t now;
if (h->childpid) {
errno = EINPROGRESS;
return -1;
}
now = time(NULL);
if (now < h->nextrun)
return 0;
h->nextrun = now + h->interval;
pid = fork();
if (pid < 0)
return -1;
if (pid) {
h->childpid = pid;
return 0;
}
/*
* always use SCHED_OTHER for the child processes
* nice -1 is fine; but we don't know what the child process
* might do, so leaving it (potentially) in SCHED_RR or SCHED_FIFO
* is out of the question
*
* XXX if you set SCHED_OTHER in the conf file and nice 20, the below
* will make the heuristics a higher prio than qdiskd. This should be
* fine in practice, because running qdiskd at nice 20 will cause all
* sorts of problems on a busy system.
*/
set_priority(SCHED_OTHER, -1);
munlockall();
restore_signals();
argv[0] = strdup("/bin/sh");
argv[1] = strdup("-c");
argv[2] = h->program;
argv[3] = NULL;
nullify();
execv("/bin/sh", argv);
free(argv[0]);
free(argv[1]);
logt_print(LOG_ERR, "Execv failed\n");
return 0;
}
/**
Total our current score
*/
static void
total_score(struct h_data *h, int max, int *score, int *maxscore)
{
int x;
*score = 0;
*maxscore = 0;
/* Allow operation w/o any heuristics */
if (!max) {
*score = *maxscore = 1;
return;
}
for (x = 0; x < max; x++) {
*maxscore += h[x].score;
if (h[x].available)
*score += h[x].score;
}
}
/**
Check for response from a user-defined heuristic / script
*/
static int
check_heuristic(struct h_data *h, int block)
{
int ret;
int status;
if (h->childpid == 0)
/* No child to check */
return 0;
ret = waitpid(h->childpid, &status, block?0:WNOHANG);
if (!block && ret == 0)
/* No children exited */
return 0;
h->childpid = 0;
if (ret < 0 && errno == ECHILD)
/* wrong child? */
goto miss;
if (!WIFEXITED(status)) {
ret = 0;
goto miss;
}
if (WEXITSTATUS(status) != 0) {
ret = 0;
goto miss;
}
/* Returned 0 and was not killed */
if (!h->available) {
h->available = 1;
logt_print(LOG_INFO, "Heuristic: '%s' UP\n", h->program);
}
h->misses = 0;
return 0;
miss:
if (h->available) {
h->misses++;
if (h->misses >= h->tko) {
logt_print(LOG_INFO,
"Heuristic: '%s' DOWN (%d/%d)\n",
h->program, h->misses, h->tko);
h->available = 0;
} else {
logt_print(LOG_DEBUG,
"Heuristic: '%s' missed (%d/%d)\n",
h->program, h->misses, h->tko);
}
}
return ret;
}
/**
Kick off all available heuristics
*/
static int
fork_heuristics(struct h_data *h, int max)
{
int x;
for (x = 0; x < max; x++)
fork_heuristic(&h[x]);
return 0;
}
/**
Check all available heuristics
*/
static int
check_heuristics(struct h_data *h, int max, int block)
{
int x;
for (x = 0; x < max; x++)
check_heuristic(&h[x], block);
return 0;
}
/**
Read configuration data from CCS into the array provided
*/
int
configure_heuristics(int ccsfd, struct h_data *h, int max)
{
int x = 0;
char *val;
char query[128];
if (!h || !max)
return -1;
do {
h[x].program = NULL;
h[x].available = 0;
h[x].misses = 0;
h[x].interval = 2;
h[x].tko = 1;
h[x].score = 1;
h[x].childpid = 0;
h[x].nextrun = 0;
/* Get program */
snprintf(query, sizeof(query),
"/cluster/quorumd/heuristic[%d]/@program", x+1);
if (ccs_get(ccsfd, query, &val) != 0)
/* No more */
break;
h[x].program = val;
/* Get score */
snprintf(query, sizeof(query),
"/cluster/quorumd/heuristic[%d]/@score", x+1);
if (ccs_get(ccsfd, query, &val) == 0) {
h[x].score = atoi(val);
free(val);
if (h[x].score <= 0)
h[x].score = 1;
}
/* Get query interval */
snprintf(query, sizeof(query),
"/cluster/quorumd/heuristic[%d]/@interval", x+1);
if (ccs_get(ccsfd, query, &val) == 0) {
h[x].interval = atoi(val);
free(val);
if (h[x].interval <= 0)
h[x].interval = 2;
}
/* Get tko for this heuristic */
snprintf(query, sizeof(query),
"/cluster/quorumd/heuristic[%d]/@tko", x+1);
if (ccs_get(ccsfd, query, &val) == 0) {
h[x].tko= atoi(val);
free(val);
if (h[x].tko <= 0)
h[x].tko = 1;
}
logt_print(LOG_DEBUG,
"Heuristic: '%s' score=%d interval=%d tko=%d\n",
h[x].program, h[x].score, h[x].interval, h[x].tko);
} while (++x < max);
logt_print(LOG_DEBUG, "%d heuristics loaded\n", x);
return x;
}
/**
Return the current score + maxscore to the caller
*/
int
get_my_score(int *score, int *maxscore)
{
pthread_mutex_lock(&sc_lock);
*score = _score;
*maxscore = _maxscore;
pthread_mutex_unlock(&sc_lock);
return 0;
}
/**
Call this if no heuristics are set to run in master-wins mode
*/
int
fudge_scoring(void)
{
pthread_mutex_lock(&sc_lock);
_score = _maxscore = 1;
pthread_mutex_unlock(&sc_lock);
return 0;
}
/**
Loop for the scoring thread.
*/
static void *
score_thread_main(void *arg)
{
struct h_arg *args = (struct h_arg *)arg;
int score, maxscore;
set_priority(args->sched_queue, args->sched_prio);
while (_score_thread_running) {
fork_heuristics(args->h, args->count);
check_heuristics(args->h, args->count, 0);
total_score(args->h, args->count, &score, &maxscore);
pthread_mutex_lock(&sc_lock);
_score = score;
_maxscore = maxscore;
pthread_mutex_unlock(&sc_lock);
if (_score_thread_running)
sleep(1);
}
free(args->h);
free(args);
logt_print(LOG_INFO, "Score thread going away\n");
return (NULL);
}
/**
Start the score thread. h is copied into an argument which is
passed in as the arg parameter in the score thread, so it is safe
to pass in h if it was allocated on the stack.
*/
int
start_score_thread(qd_ctx *ctx, struct h_data *h, int count)
{
pthread_attr_t attrs;
struct h_arg *args;
if (!h || !count)
return -1;
args = malloc(sizeof(struct h_arg));
if (!args)
return -1;
args->h = malloc(sizeof(struct h_data) * count);
if (!args->h) {
free(args);
return -1;
}
memcpy(args->h, h, (sizeof(struct h_data) * count));
args->count = count;
args->sched_queue = ctx->qc_sched;
args->sched_prio = ctx->qc_sched_prio;
_score_thread_running = 1;
pthread_attr_init(&attrs);
pthread_attr_setinheritsched(&attrs, PTHREAD_INHERIT_SCHED);
pthread_create(&score_thread, &attrs, score_thread_main, args);
pthread_attr_destroy(&attrs);
if (score_thread)
return 0;
_score_thread_running = 0;
return -1;
}
|