[master] 746384b20 demo how a priv_task can veto being rolled back

Nils Goroll nils.goroll at uplex.de
Mon Feb 17 17:05:07 UTC 2020


commit 746384b20cbc24ff8afd2df35e1510087404fbf4
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Feb 17 18:02:19 2020 +0100

    demo how a priv_task can veto being rolled back
    
    or rather: fail the task if being rolled back

diff --git a/bin/varnishtest/tests/m00017.vtc b/bin/varnishtest/tests/m00017.vtc
index c31322d50..0f1ddba47 100644
--- a/bin/varnishtest/tests/m00017.vtc
+++ b/bin/varnishtest/tests/m00017.vtc
@@ -204,6 +204,7 @@ server s1 -wait
 # Dont panic
 varnish v1 -vcl+backend {
 	import std;
+	import debug;
 	import vtc;
 
 	sub vcl_recv {
@@ -211,6 +212,12 @@ varnish v1 -vcl+backend {
 	}
 
 	sub vcl_deliver {
+		if (req.url ~ "/veto") {
+			debug.fail_rollback();
+		}
+		if (req.url ~ "/ok") {
+			debug.ok_rollback();
+		}
 		std.rollback(req);
 		set resp.http.test = req.http.test;
 		vtc.workspace_alloc(client, -200);
@@ -218,9 +225,9 @@ varnish v1 -vcl+backend {
 }
 
 # XXX server keeps params, so we need to reset previous -repeat 5
-server s1 -repeat 1 {
+server s1 -repeat 3 {
 	rxreq
-	expect req.url == "/6"
+	expect req.url ~ "^/6"
 	txresp
 } -start
 
@@ -229,6 +236,13 @@ client c4 {
 	rxresp
 	expect resp.status == 200
 	expect resp.http.test == "1"
+	txreq -url /6/veto/ok -hdr "test: 1"
+	rxresp
+	expect resp.status == 200
+	expect resp.http.test == "1"
+	txreq -url /6/veto -hdr "test: 1"
+	rxresp
+	expect resp.status == 503
 } -run
 
 server s1 -wait
@@ -251,7 +265,7 @@ varnish v1 -vcl+backend {
 	}
 }
 
-server s1 {
+server s1 -repeat 1 {
 	rxreq
 	expect req.url == "/7"
 	txresp
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 35ce24631..5fe13d252 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -292,3 +292,11 @@ Get the stringified client ip from the session attr
 $Function STRING client_port()
 
 Get the stringified client port from the session attr
+
+$Function VOID fail_rollback()
+
+fail any rollback before ok_rollback() is called
+
+$Function VOID ok_rollback()
+
+Allow rollbacks. Must be called before the end of the task.
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 9f46a2436..ddd3b1f1d 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -1085,3 +1085,52 @@ xyzzy_client_port(VRT_CTX)
 
 	return (SES_Get_String_Attr(ctx->sp, SA_CLIENT_PORT));
 }
+
+static void
+fail_f(void *priv)
+{
+	VRT_CTX;
+
+	CAST_OBJ_NOTNULL(ctx, priv, VRT_CTX_MAGIC);
+	VRT_fail(ctx, "thou shalt not rollet back");
+}
+
+VCL_VOID v_matchproto_(td_xyzzy_debug_fail_rollback)
+xyzzy_fail_rollback(VRT_CTX)
+{
+	struct vmod_priv *p;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	p = VRT_priv_task(ctx, (void *)xyzzy_fail_rollback);
+	if (p == NULL) {
+		VRT_fail(ctx, "no priv task - out of ws?");
+		return;
+	}
+
+	if (p->priv != NULL) {
+		assert(p->priv == ctx);
+		assert(p->free == fail_f);
+		return;
+	}
+
+	p->priv = TRUST_ME(ctx);
+	p->free = fail_f;
+}
+
+VCL_VOID v_matchproto_(td_xyzzy_debug_ok_rollback)
+xyzzy_ok_rollback(VRT_CTX)
+{
+	struct vmod_priv *p;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	p = VRT_priv_task(ctx, (void *)xyzzy_fail_rollback);
+	if (p == NULL) {
+		VRT_fail(ctx, "no priv task - out of ws?");
+		return;
+	}
+
+	p->priv = NULL;
+	p->free = NULL;
+}


More information about the varnish-commit mailing list