Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
unique-xids
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
uplex-varnish
unique-xids
Commits
12e571b3
Commit
12e571b3
authored
Mar 14, 2013
by
Poul-Henning Kamp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a hash director to vmod.directors.
parent
90b058b8
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
181 additions
and
36 deletions
+181
-36
v00026.vtc
bin/varnishtest/tests/v00026.vtc
+10
-7
Makefile.am
lib/libvmod_directors/Makefile.am
+1
-0
hash.c
lib/libvmod_directors/hash.c
+123
-0
random.c
lib/libvmod_directors/random.c
+2
-26
vdir.c
lib/libvmod_directors/vdir.c
+37
-1
vdir.h
lib/libvmod_directors/vdir.h
+3
-2
vmod.vcc
lib/libvmod_directors/vmod.vcc
+5
-0
No files found.
bin/varnishtest/tests/v00026.vtc
View file @
12e571b3
...
@@ -16,13 +16,16 @@ server s2 {
...
@@ -16,13 +16,16 @@ server s2 {
varnish v1 -vcl+backend {
varnish v1 -vcl+backend {
director h1 hash {
import directors from "${topbuild}/lib/libvmod_directors/.libs/libvmod_directors.so" ;
{ .backend = s1; .weight = 1; }
{ .backend = s2; .weight = 1; }
sub vcl_init {
new h1 = directors.hash();
h1.add_backend(s1, 1);
h1.add_backend(s2, 1);
}
}
sub vcl_recv {
sub vcl_recv {
set req.backend = h1;
set req.backend = h1
.backend(req.url)
;
return(pass);
return(pass);
}
}
...
@@ -30,11 +33,11 @@ varnish v1 -vcl+backend {
...
@@ -30,11 +33,11 @@ varnish v1 -vcl+backend {
client c1 {
client c1 {
txreq -url /1
2
txreq -url /1
rxresp
rxresp
expect resp.http.foo == "1"
expect resp.http.foo == "1"
txreq -url /
1
txreq -url /
3
rxresp
rxresp
expect resp.http.foo == "2"
expect resp.http.foo == "2"
...
@@ -42,7 +45,7 @@ client c1 {
...
@@ -42,7 +45,7 @@ client c1 {
rxresp
rxresp
expect resp.http.foo == "3"
expect resp.http.foo == "3"
txreq -url /
15
txreq -url /
8
rxresp
rxresp
expect resp.http.foo == "4"
expect resp.http.foo == "4"
...
...
lib/libvmod_directors/Makefile.am
View file @
12e571b3
...
@@ -17,6 +17,7 @@ libvmod_directors_la_SOURCES = \
...
@@ -17,6 +17,7 @@ libvmod_directors_la_SOURCES = \
vdir.c
\
vdir.c
\
vdir.h
\
vdir.h
\
fall_back.c
\
fall_back.c
\
hash.c
\
random.c
\
random.c
\
round_robin.c
round_robin.c
...
...
lib/libvmod_directors/hash.c
0 → 100644
View file @
12e571b3
/*-
* Copyright (c) 2013 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <math.h>
#include <stdlib.h>
#include "cache/cache.h"
#include "cache/cache_backend.h"
#include "vrt.h"
#include "vbm.h"
#include "vend.h"
#include "vsha256.h"
#include "vdir.h"
#include "vcc_if.h"
struct
vmod_directors_hash
{
unsigned
magic
;
#define VMOD_DIRECTORS_HASH_MAGIC 0xc08dd611
struct
vdir
*
vd
;
unsigned
nloops
;
struct
vbitmap
*
vbm
;
};
VCL_VOID
__match_proto__
()
vmod_hash__init
(
struct
req
*
req
,
struct
vmod_directors_hash
**
rrp
,
const
char
*
vcl_name
)
{
struct
vmod_directors_hash
*
rr
;
AZ
(
req
);
AN
(
rrp
);
AZ
(
*
rrp
);
ALLOC_OBJ
(
rr
,
VMOD_DIRECTORS_HASH_MAGIC
);
AN
(
rr
);
rr
->
vbm
=
vbit_init
(
8
);
AN
(
rr
->
vbm
);
rr
->
nloops
=
3
;
//
*
rrp
=
rr
;
vdir_new
(
&
rr
->
vd
,
vcl_name
,
NULL
,
NULL
,
rr
);
}
VCL_VOID
__match_proto__
()
vmod_hash__fini
(
struct
req
*
req
,
struct
vmod_directors_hash
**
rrp
)
{
struct
vmod_directors_hash
*
rr
;
AZ
(
req
);
rr
=
*
rrp
;
*
rrp
=
NULL
;
CHECK_OBJ_NOTNULL
(
rr
,
VMOD_DIRECTORS_HASH_MAGIC
);
vdir_delete
(
&
rr
->
vd
);
vbit_destroy
(
rr
->
vbm
);
FREE_OBJ
(
rr
);
}
VCL_VOID
__match_proto__
()
vmod_hash_add_backend
(
struct
req
*
req
,
struct
vmod_directors_hash
*
rr
,
VCL_BACKEND
be
,
double
w
)
{
(
void
)
req
;
CHECK_OBJ_NOTNULL
(
rr
,
VMOD_DIRECTORS_HASH_MAGIC
);
(
void
)
vdir_add_backend
(
rr
->
vd
,
be
,
w
);
}
VCL_BACKEND
__match_proto__
()
vmod_hash_backend
(
struct
req
*
req
,
struct
vmod_directors_hash
*
rr
,
const
char
*
arg
,
...)
{
struct
SHA256Context
ctx
;
va_list
ap
;
const
char
*
p
;
unsigned
char
sha256
[
SHA256_LEN
];
VCL_BACKEND
be
;
double
r
;
(
void
)
req
;
CHECK_OBJ_NOTNULL
(
rr
,
VMOD_DIRECTORS_HASH_MAGIC
);
SHA256_Init
(
&
ctx
);
va_start
(
ap
,
arg
);
p
=
arg
;
while
(
p
!=
vrt_magic_string_end
)
{
SHA256_Update
(
&
ctx
,
arg
,
strlen
(
arg
));
p
=
va_arg
(
ap
,
const
char
*
);
}
va_end
(
ap
);
SHA256_Final
(
sha256
,
&
ctx
);
r
=
vbe32dec
(
sha256
);
r
=
scalbn
(
r
,
-
32
);
be
=
vdir_pick_be
(
rr
->
vd
,
req
,
r
,
rr
->
nloops
);
return
(
be
);
}
lib/libvmod_directors/random.c
View file @
12e571b3
...
@@ -62,36 +62,12 @@ static struct vbc * __match_proto__(vdi_getfd_f)
...
@@ -62,36 +62,12 @@ static struct vbc * __match_proto__(vdi_getfd_f)
vmod_rr_getfd
(
const
struct
director
*
dir
,
struct
req
*
req
)
vmod_rr_getfd
(
const
struct
director
*
dir
,
struct
req
*
req
)
{
{
struct
vmod_directors_random
*
rr
;
struct
vmod_directors_random
*
rr
;
struct
vbitmap
*
vbm
=
NULL
;
VCL_BACKEND
be
;
unsigned
u
,
v
,
l
;
VCL_BACKEND
be
=
NULL
;
double
tw
;
int
nbe
;
double
r
;
double
r
;
CAST_OBJ_NOTNULL
(
rr
,
dir
->
priv
,
VMOD_DIRECTORS_RANDOM_MAGIC
);
CAST_OBJ_NOTNULL
(
rr
,
dir
->
priv
,
VMOD_DIRECTORS_RANDOM_MAGIC
);
tw
=
rr
->
vd
->
total_weight
;
nbe
=
rr
->
vd
->
n_backend
;
r
=
scalbn
(
random
(),
-
31
);
r
=
scalbn
(
random
(),
-
31
);
assert
(
r
>=
0
.
0
&&
r
<=
1
.
0
);
be
=
vdir_pick_be
(
rr
->
vd
,
req
,
r
,
rr
->
nloops
);
vdir_lock
(
rr
->
vd
);
for
(
l
=
0
;
nbe
>
0
&&
tw
>
0
.
0
&&
l
<
rr
->
nloops
;
l
++
)
{
u
=
vdir_pick_by_weight
(
rr
->
vd
,
r
*
tw
,
vbm
);
be
=
rr
->
vd
->
backend
[
u
];
CHECK_OBJ_NOTNULL
(
be
,
DIRECTOR_MAGIC
);
if
(
be
->
healthy
(
be
,
req
))
break
;
if
(
l
==
0
)
{
vbm
=
rr
->
vbm
;
for
(
v
=
0
;
v
<
nbe
;
v
++
)
vbit_clr
(
vbm
,
v
);
}
vbit_set
(
vbm
,
u
);
nbe
--
;
tw
-=
rr
->
vd
->
weight
[
u
];
be
=
NULL
;
}
vdir_unlock
(
rr
->
vd
);
if
(
be
==
NULL
)
if
(
be
==
NULL
)
return
(
NULL
);
return
(
NULL
);
return
(
be
->
getfd
(
be
,
req
));
return
(
be
->
getfd
(
be
,
req
));
...
...
lib/libvmod_directors/vdir.c
View file @
12e571b3
...
@@ -70,6 +70,8 @@ vdir_new(struct vdir **vdp, const char *vcl_name, vdi_healthy *healthy,
...
@@ -70,6 +70,8 @@ vdir_new(struct vdir **vdp, const char *vcl_name, vdi_healthy *healthy,
vd
->
dir
->
priv
=
priv
;
vd
->
dir
->
priv
=
priv
;
vd
->
dir
->
healthy
=
healthy
;
vd
->
dir
->
healthy
=
healthy
;
vd
->
dir
->
getfd
=
getfd
;
vd
->
dir
->
getfd
=
getfd
;
vd
->
vbm
=
vbit_init
(
8
);
AN
(
vd
->
vbm
);
}
}
void
void
...
@@ -87,6 +89,7 @@ vdir_delete(struct vdir **vdp)
...
@@ -87,6 +89,7 @@ vdir_delete(struct vdir **vdp)
free
(
vd
->
weight
);
free
(
vd
->
weight
);
AZ
(
pthread_mutex_destroy
(
&
vd
->
mtx
));
AZ
(
pthread_mutex_destroy
(
&
vd
->
mtx
));
FREE_OBJ
(
vd
->
dir
);
FREE_OBJ
(
vd
->
dir
);
vbit_destroy
(
vd
->
vbm
);
FREE_OBJ
(
vd
);
FREE_OBJ
(
vd
);
}
}
...
@@ -145,7 +148,7 @@ vdir_any_healthy(struct vdir *vd, const struct req *req)
...
@@ -145,7 +148,7 @@ vdir_any_healthy(struct vdir *vd, const struct req *req)
return
(
retval
);
return
(
retval
);
}
}
unsigned
static
unsigned
vdir_pick_by_weight
(
const
struct
vdir
*
vd
,
double
w
,
vdir_pick_by_weight
(
const
struct
vdir
*
vd
,
double
w
,
const
struct
vbitmap
*
blacklist
)
const
struct
vbitmap
*
blacklist
)
{
{
...
@@ -165,3 +168,36 @@ vdir_pick_by_weight(const struct vdir *vd, double w,
...
@@ -165,3 +168,36 @@ vdir_pick_by_weight(const struct vdir *vd, double w,
WRONG
(
""
);
WRONG
(
""
);
}
}
VCL_BACKEND
vdir_pick_be
(
struct
vdir
*
vd
,
const
struct
req
*
req
,
double
w
,
unsigned
nloops
)
{
struct
vbitmap
*
vbm
=
NULL
;
unsigned
u
,
v
,
l
;
VCL_BACKEND
be
=
NULL
;
double
tw
;
int
nbe
;
tw
=
vd
->
total_weight
;
nbe
=
vd
->
n_backend
;
assert
(
w
>=
0
.
0
&&
w
<=
1
.
0
);
vdir_lock
(
vd
);
for
(
l
=
0
;
nbe
>
0
&&
tw
>
0
.
0
&&
l
<
nloops
;
l
++
)
{
u
=
vdir_pick_by_weight
(
vd
,
w
*
tw
,
vbm
);
be
=
vd
->
backend
[
u
];
CHECK_OBJ_NOTNULL
(
be
,
DIRECTOR_MAGIC
);
if
(
be
->
healthy
(
be
,
req
))
break
;
if
(
l
==
0
)
{
vbm
=
vd
->
vbm
;
for
(
v
=
0
;
v
<
nbe
;
v
++
)
vbit_clr
(
vbm
,
v
);
}
vbit_set
(
vbm
,
u
);
nbe
--
;
tw
-=
vd
->
weight
[
u
];
be
=
NULL
;
}
vdir_unlock
(
vd
);
return
(
be
);
}
lib/libvmod_directors/vdir.h
View file @
12e571b3
...
@@ -38,6 +38,7 @@ struct vdir {
...
@@ -38,6 +38,7 @@ struct vdir {
double
*
weight
;
double
*
weight
;
double
total_weight
;
double
total_weight
;
struct
director
*
dir
;
struct
director
*
dir
;
struct
vbitmap
*
vbm
;
};
};
void
vdir_new
(
struct
vdir
**
vdp
,
const
char
*
vcl_name
,
vdi_healthy
*
healthy
,
void
vdir_new
(
struct
vdir
**
vdp
,
const
char
*
vcl_name
,
vdi_healthy
*
healthy
,
...
@@ -47,5 +48,5 @@ void vdir_lock(struct vdir *vd);
...
@@ -47,5 +48,5 @@ void vdir_lock(struct vdir *vd);
void
vdir_unlock
(
struct
vdir
*
vd
);
void
vdir_unlock
(
struct
vdir
*
vd
);
unsigned
vdir_add_backend
(
struct
vdir
*
vd
,
VCL_BACKEND
be
,
double
weight
);
unsigned
vdir_add_backend
(
struct
vdir
*
vd
,
VCL_BACKEND
be
,
double
weight
);
unsigned
vdir_any_healthy
(
struct
vdir
*
vd
,
const
struct
req
*
);
unsigned
vdir_any_healthy
(
struct
vdir
*
vd
,
const
struct
req
*
);
unsigned
vdir_pick_by_weight
(
const
struct
vdir
*
vd
,
double
w
,
VCL_BACKEND
vdir_pick_be
(
struct
vdir
*
vd
,
const
struct
req
*
req
,
double
w
,
const
struct
vbitmap
*
blacklist
);
unsigned
nloops
);
lib/libvmod_directors/vmod.vcc
View file @
12e571b3
...
@@ -41,3 +41,8 @@ Object random() {
...
@@ -41,3 +41,8 @@ Object random() {
Method VOID .add_backend(BACKEND, REAL)
Method VOID .add_backend(BACKEND, REAL)
Method BACKEND .backend()
Method BACKEND .backend()
}
}
Object hash() {
Method VOID .add_backend(BACKEND, REAL)
Method BACKEND .backend(STRING_LIST)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment