[master] 2676c2e shard director vcc enum overhaul

Nils Goroll nils.goroll at uplex.de
Mon Mar 5 19:26:09 UTC 2018


commit 2676c2e3793fe6fdb691295890b2400391564ed0
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Dec 11 17:52:33 2017 +0100

    shard director vcc enum overhaul

diff --git a/lib/libvmod_directors/Makefile.am b/lib/libvmod_directors/Makefile.am
index d9fce2f..1275938 100644
--- a/lib/libvmod_directors/Makefile.am
+++ b/lib/libvmod_directors/Makefile.am
@@ -12,8 +12,8 @@ libvmod_directors_la_SOURCES = \
 	shard_cfg.h \
 	shard_dir.c \
 	shard_dir.h \
-	shard_parse_vcc_enums.h \
-	shard_parse_vcc_enums.c
+	tbl_by.h \
+	tbl_healthy.h
 
 # Use vmodtool.py generated automake boilerplate
 include $(srcdir)/automake_boilerplate.am
diff --git a/lib/libvmod_directors/flint.lnt b/lib/libvmod_directors/flint.lnt
index e69de29..1b6f088 100644
--- a/lib/libvmod_directors/flint.lnt
+++ b/lib/libvmod_directors/flint.lnt
@@ -0,0 +1 @@
+-efile(451, "tbl_*.h")		// No include guard
diff --git a/lib/libvmod_directors/shard_dir.h b/lib/libvmod_directors/shard_dir.h
index c893c3d..4df4151 100644
--- a/lib/libvmod_directors/shard_dir.h
+++ b/lib/libvmod_directors/shard_dir.h
@@ -27,7 +27,19 @@
  * SUCH DAMAGE.
  */
 
-#include "shard_parse_vcc_enums.h"
+enum by_e {
+	_BY_E_INVALID = 0,
+#define VMODENUM(x) BY_ ## x,
+#include "tbl_by.h"
+	_BY_E_MAX
+};
+
+enum healthy_e {
+	_HEALTHY_E_INVALID = 0,
+#define VMODENUM(x) x,
+#include "tbl_healthy.h"
+	_HEALTHY_E_MAX
+};
 
 struct vbitmap;
 
diff --git a/lib/libvmod_directors/shard_parse_vcc_enums.c b/lib/libvmod_directors/shard_parse_vcc_enums.c
deleted file mode 100644
index 7061ced..0000000
--- a/lib/libvmod_directors/shard_parse_vcc_enums.c
+++ /dev/null
@@ -1,132 +0,0 @@
-/*
- * for the time being, this code is auto-generated outside the varnishd source
- * tree, see
- * https://code.uplex.de/uplex-varnish/libvmod-vslp/blob/shard/src/gen_enum_parse.pl
- *
- * TODO: integrate in vmodtool.py or replace with something else
- */
-
-/*lint -e801 */
-
-#include "shard_parse_vcc_enums.h"
-#define term(c) ((c) == '\0')
-
-
-
-enum alg_e parse_alg_e (const char *m) {
-	enum alg_e r;
-
-	switch (m[0]) {
-	case 'C':	goto _0C;	// CRC32
-	case 'R':	goto _0R;	// RS
-	case 'S':	goto _0S;	// SHA256
-	default:	goto invalid;
-	}
-	 _0C:
-	//CRC32
-	if ((m[1] == 'R') && (m[2] == 'C') && (m[3] == '3') && (m[4] == '2') && (term(m[5]))) {
-	    r = CRC32;
-	    goto ok;
-	}
-	goto invalid;
-	 _0R:
-	//RS
-	if ((m[1] == 'S') && (term(m[2]))) {
-	    r = RS;
-	    goto ok;
-	}
-	goto invalid;
-	 _0S:
-	//SHA256
-	if ((m[1] == 'H') && (m[2] == 'A') && (m[3] == '2') && (m[4] == '5') && (m[5] == '6') && (term(m[6]))) {
-	    r = SHA256;
-	    goto ok;
-	}
-	goto invalid;
-  ok:
-	return r;
-  invalid:
-    return _ALG_E_INVALID;
-}
-
-
-enum by_e parse_by_e (const char *m) {
-	enum by_e r;
-
-	switch (m[0]) {
-	case 'B':	goto _0B;	// BLOB
-	case 'H':	goto _0H;	// HASH
-	case 'K':	goto _0K;	// KEY
-	case 'U':	goto _0U;	// URL
-	default:	goto invalid;
-	}
-	 _0B:
-	//BLOB
-	if ((m[1] == 'L') && (m[2] == 'O') && (m[3] == 'B') && (term(m[4]))) {
-	    r = BY_BLOB;
-	    goto ok;
-	}
-	goto invalid;
-	 _0H:
-	//HASH
-	if ((m[1] == 'A') && (m[2] == 'S') && (m[3] == 'H') && (term(m[4]))) {
-	    r = BY_HASH;
-	    goto ok;
-	}
-	goto invalid;
-	 _0K:
-	//KEY
-	if ((m[1] == 'E') && (m[2] == 'Y') && (term(m[3]))) {
-	    r = BY_KEY;
-	    goto ok;
-	}
-	goto invalid;
-	 _0U:
-	//URL
-	if ((m[1] == 'R') && (m[2] == 'L') && (term(m[3]))) {
-	    r = BY_URL;
-	    goto ok;
-	}
-	goto invalid;
-  ok:
-	return r;
-  invalid:
-    return _BY_E_INVALID;
-}
-
-
-enum healthy_e parse_healthy_e (const char *m) {
-	enum healthy_e r;
-
-	switch (m[0]) {
-	case 'A':	goto _0A;	// ALL
-	case 'C':	goto _0C;	// CHOSEN
-	case 'I':	goto _0I;	// IGNORE
-	default:	goto invalid;
-	}
-	 _0A:
-	//ALL
-	if ((m[1] == 'L') && (m[2] == 'L') && (term(m[3]))) {
-	    r = ALL;
-	    goto ok;
-	}
-	goto invalid;
-	 _0C:
-	//CHOSEN
-	if ((m[1] == 'H') && (m[2] == 'O') && (m[3] == 'S') && (m[4] == 'E') && (m[5] == 'N') && (term(m[6]))) {
-	    r = CHOSEN;
-	    goto ok;
-	}
-	goto invalid;
-	 _0I:
-	//IGNORE
-	if ((m[1] == 'G') && (m[2] == 'N') && (m[3] == 'O') && (m[4] == 'R') && (m[5] == 'E') && (term(m[6]))) {
-	    r = IGNORE;
-	    goto ok;
-	}
-	goto invalid;
-  ok:
-	return r;
-  invalid:
-    return _HEALTHY_E_INVALID;
-}
diff --git a/lib/libvmod_directors/shard_parse_vcc_enums.h b/lib/libvmod_directors/shard_parse_vcc_enums.h
deleted file mode 100644
index afa0c47..0000000
--- a/lib/libvmod_directors/shard_parse_vcc_enums.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * for the time being, this code is auto-generated outside the varnishd source
- * tree, see
- * https://code.uplex.de/uplex-varnish/libvmod-vslp/blob/shard/src/gen_enum_parse.pl
- *
- * TODO: integrate in vmodtool.py or replace with something else
- */
-
-enum alg_e {
-	_ALG_E_INVALID = 0,
-	CRC32,
-	SHA256,
-	RS,
-	_ALG_E_MAX
-};
-
-
-enum alg_e parse_alg_e (const char *);
-
-/*lint -esym(769,  by_e::_BY_E_MAX) */
-
-enum by_e {
-	_BY_E_INVALID = 0,
-	BY_HASH,
-	BY_URL,
-	BY_KEY,
-	BY_BLOB,
-	_BY_E_MAX
-};
-
-enum by_e parse_by_e (const char *);
-
-/*lint -esym(769,  healthy_e::_HEALTHY_E_MAX) */
-
-enum healthy_e {
-	_HEALTHY_E_INVALID = 0,
-	CHOSEN,
-	IGNORE,
-	ALL,
-	_HEALTHY_E_MAX
-};
-
-
-enum healthy_e parse_healthy_e (const char *);
-
diff --git a/lib/libvmod_directors/tbl_by.h b/lib/libvmod_directors/tbl_by.h
new file mode 100644
index 0000000..97cb828
--- /dev/null
+++ b/lib/libvmod_directors/tbl_by.h
@@ -0,0 +1,5 @@
+VMODENUM(HASH)
+VMODENUM(URL)
+VMODENUM(KEY)
+VMODENUM(BLOB)
+#undef VMODENUM
diff --git a/lib/libvmod_directors/tbl_healthy.h b/lib/libvmod_directors/tbl_healthy.h
new file mode 100644
index 0000000..7be0e44
--- /dev/null
+++ b/lib/libvmod_directors/tbl_healthy.h
@@ -0,0 +1,4 @@
+VMODENUM(CHOSEN)
+VMODENUM(IGNORE)
+VMODENUM(ALL)
+#undef VMODENUM
diff --git a/lib/libvmod_directors/vmod.vcc b/lib/libvmod_directors/vmod.vcc
index 4737284..ef2fd75 100644
--- a/lib/libvmod_directors/vmod.vcc
+++ b/lib/libvmod_directors/vmod.vcc
@@ -409,13 +409,13 @@ To generate sharding keys using other hashes, use a custom vmod like
 .. _vmod blobdigest: https://code.uplex.de/uplex-varnish/libvmod-blobdigest/blob/master/README.rst
 
 $Method BACKEND .backend(
-	ENUM {HASH, URL, KEY, BLOB} by="HASH",
+	ENUM {HASH, URL, KEY, BLOB} by=HASH,
 	INT key=0,
 	BLOB key_blob=0,
 	INT alt=0,
 	REAL warmup=-1,
 	BOOL rampup=1,
-	ENUM {CHOSEN, IGNORE, ALL} healthy="CHOSEN")
+	ENUM {CHOSEN, IGNORE, ALL} healthy=CHOSEN)
 
 
 Lookup a backend on the consistent hashing ring.
diff --git a/lib/libvmod_directors/vmod_shard.c b/lib/libvmod_directors/vmod_shard.c
index 6a8660a..655e2ad 100644
--- a/lib/libvmod_directors/vmod_shard.c
+++ b/lib/libvmod_directors/vmod_shard.c
@@ -218,6 +218,22 @@ get_key(VRT_CTX, enum by_e by, VCL_INT key_int, VCL_BLOB key_blob)
 	}
 }
 
+static enum by_e
+parse_by_e(VCL_ENUM e)
+{
+#define VMODENUM(n) if (e == vmod_enum_ ## n) return(BY_ ## n);
+#include "tbl_by.h"
+       WRONG("illegal by enum");
+}
+
+static enum healthy_e
+parse_healthy_e(VCL_ENUM e)
+{
+#define VMODENUM(n) if (e == vmod_enum_ ## n) return(n);
+#include "tbl_healthy.h"
+       WRONG("illegal healthy enum");
+}
+
 VCL_BACKEND v_matchproto_(td_directors_shard_backend)
 vmod_shard_backend(VRT_CTX, struct vmod_directors_shard *vshard,
     VCL_ENUM by_s, VCL_INT key_int, VCL_BLOB key_blob, VCL_INT alt,


More information about the varnish-commit mailing list