...
 
Commits (5)
......@@ -28,7 +28,7 @@ AC_ARG_WITH([rst2man],
m4_ifndef([VARNISH_PREREQ], AC_MSG_ERROR([Need varnish.m4 -- see README.rst]))
VARNISH_PREREQ([5.2])
VARNISH_PREREQ([4.1],[5.2])
VARNISH_VMODS([weightadjust])
VMOD_TESTS="$(cd $srcdir/src && echo tests/*.vtc)"
......
<?php
exec('uptime', $output);
// get result
$load = [0 => 0];
if (preg_match('/average: ([0-9]+\.[0-9]{2}), ([0-9]+\.[0-9]{2}), ([0-9]+\.[0-9]{2})$/', $output[0], $result)) {
$load[0] = (float)$result[1];
$load[1] = (float)$result[2];
$load[2] = (float)$result[3];
}
if ($load[0] < 0.5) {
$currentLoad = 100;
}
elseif ($load[0] < 0.7) {
$currentLoad = 95;
}
elseif ($load[0] < 1.0) {
$currentLoad = 90;
}
elseif ($load[0] < 1.5) {
$currentLoad = 80;
}
elseif ($load[0] < 2.0) {
$currentLoad = 70;
}
elseif ($load[0] < 2.5) {
$currentLoad = 60;
}
elseif ($load[0] < 3.5) {
$currentLoad = 50;
}
elseif ($load[0] < 4.5) {
$currentLoad = 50;
}
elseif ($load[0] < 5.0) {
$currentLoad = 40;
}
elseif ($load[0] < 6.0) {
$currentLoad = 30;
}
elseif ($load[0] < 7.0) {
$currentLoad = 20;
}
elseif ($load[0] < 8.0) {
$currentLoad = 10;
}
else {
$currentLoad = 1;
}
header('X-Weight: '.number_format($currentLoad, 2));
echo @php_uname('n') . "\r\n" . 'Weight: '.$currentLoad;
......@@ -17,6 +17,9 @@ nodist_libvmod_weightadjust_la_SOURCES = \
vcc_weightadjust_if.c \
vcc_weightadjust_if.h
vmod_weightadjust.lo: vcc_weightadjust_if.h
random.lo: vcc_weightadjust_if.h
dist_man_MANS = vmod_weightadjust.3
@BUILD_VMOD_WEIGHTADJUST@
......
......@@ -29,12 +29,12 @@
#include "config.h"
#include <stdlib.h>
#include <math.h>
#include "cache/cache.h"
#include "cache/cache_director.h"
#include "vbm.h"
#include "vrnd.h"
#include "vrt.h"
#include "vdir.h"
......@@ -69,7 +69,7 @@ vmod_random_resolve(const struct director *dir, struct worker *wrk,
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
r = scalbn(VRND_RandomTestable(), -31);
r = scalbn(random(), -31);
assert(r >= 0 && r < 1.0);
be = vdir_pick_be(rr->vd, r, bo);
return (be);
......
varnishtest "Deeper test of random director"
random
server s1 {
rxreq
txresp -body "1"
rxreq
txresp -body "1"
} -start
server s2 {
......@@ -12,17 +12,15 @@ server s2 {
txresp -body "22"
rxreq
txresp -body "22"
rxreq
txresp -body "22"
rxreq
txresp -body "22"
} -start
server s3 {
rxreq
txresp -body "333"
rxreq
txresp -body "333"
rxreq
txresp -body "333"
rxreq
txresp -body "333"
} -start
server s4 {
......@@ -30,13 +28,17 @@ server s4 {
txresp -body "4444"
rxreq
txresp -body "4444"
rxreq
txresp -body "4444"
rxreq
txresp -body "4444"
} -start
varnish v1 -vcl+backend {
import ${vmod_weightadjust};
import ${vmod_directors};
sub vcl_init {
new foo = weightadjust.random();
new foo = directors.random();
foo.add_backend(s1, 1);
foo.add_backend(s2, 1);
foo.add_backend(s3, 1);
......@@ -52,37 +54,40 @@ varnish v1 -vcl+backend {
}
} -start
varnish v1 -cliok "debug.srandom"
# NB: Do not change the number 1
# NB: Only srandom(1) is standardized as deterministic.
varnish v1 -cliok "debug.srandom 1"
client c1 {
txreq
rxresp
expect resp.bodylen == 3
expect resp.bodylen == 2
txreq
rxresp
expect resp.bodylen == 4
txreq
rxresp
expect resp.bodylen == 1
expect resp.bodylen == 4
txreq
rxresp
expect resp.bodylen == 1
expect resp.bodylen == 4
txreq
rxresp
expect resp.bodylen == 2
expect resp.bodylen == 1
txreq
rxresp
expect resp.bodylen == 2
txreq
rxresp
expect resp.bodylen == 3
expect resp.bodylen == 4
txreq
rxresp
expect resp.bodylen == 4
expect resp.bodylen == 2
txreq
rxresp
expect resp.bodylen == 3
txreq
rxresp
expect resp.bodylen == 3
expect resp.bodylen == 2
} -run
......@@ -72,7 +72,7 @@ vdir_new(struct vdir **vdp, const char *name, const char *vcl_name,
vd->dir->priv = priv;
vd->dir->healthy = healthy;
vd->dir->resolve = resolve;
vd->vbm = vbit_new(8);
vd->vbm = vbit_init(8);
AN(vd->vbm);
}
......@@ -81,7 +81,11 @@ vdir_delete(struct vdir **vdp)
{
struct vdir *vd;
TAKE_OBJ_NOTNULL(vd, vdp, VDIR_MAGIC);
AN(vdp);
vd = *vdp;
*vdp = NULL;
CHECK_OBJ_NOTNULL(vd, VDIR_MAGIC);
free(vd->backend);
free(vd->weight);
......
/*-
* Copyright 2017 UPLEX Nils Goroll Systemoptimierung
*
* code taken from varnish-cache bin/varnishd/cache/cache_backend_probe.c:
* Portions Copyright (c) 2006 Verdens Gang AS
* Portions Copyright (c) 2006-2011 Varnish Software AS
*
* code taken from lib/libvmod_directors/random.c:
* Portions Copyright (c) 2013-2015 Varnish Software AS
*
......@@ -50,15 +46,14 @@
#include "cache/cache.h"
#include "vrt.h"
#include "vcl.h"
#include "vrt.h"
#include "vre.h"
#include "vsa.h"
#include "vtim.h"
#include "vtcp.h"
#include "vsb.h"
#include "waiter/waiter.h"
#include "cache/cache_director.h"
#include "cache/cache_backend.h"
......
#
# Copyright YOU (c) 1900.
#
# (vmodtool requires this format.)
#
#-
# This document is copyright and licensed under the same conditions as
# the libvmod-weightadjust project. See LICENSE for details.
$Module weightadjust 3 Weightadjust VMOD
......@@ -10,7 +8,6 @@ DESCRIPTION
===========
$Event event_function
$Object random(PRIV_VCL)
Description
......
/*-
* Copyright 2017 UPLEX Nils Goroll Systemoptimierung
*
* code taken from varnish-cache bin/varnishd/cache/cache_backend_probe.c:
* Portions Copyright (c) 2006 Verdens Gang AS
* Portions Copyright (c) 2006-2011 Varnish Software AS
*
* All rights reserved.
*
* Author: Nils Goroll <nils.goroll@uplex.de>
......@@ -41,14 +45,13 @@
#include "cache/cache.h"
#include "vcl.h"
#include "vrt.h"
#include "vre.h"
#include "vsa.h"
#include "vtim.h"
#include "vtcp.h"
#include "vrnd.h"
#include "waiter/waiter.h"
#include "cache/cache_director.h"
#include "cache/cache_backend.h"
......@@ -184,7 +187,7 @@ wadj_update(void *arg)
POKE_TRACE(pr, "starting");
due.tv_sec = (long)VTIM_real();
due.tv_nsec = 1e9 * VRND_RandomTestableDouble();
due.tv_nsec = 1e9 * (double)random() / (double)RAND_MAX;
AZ(pthread_mutex_lock(&wa->mtx));
while (pr->run >= STARTING) {
......