+ All Categories
Home > Documents > Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop...

Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop...

Date post: 17-Oct-2020
Category:
Upload: others
View: 2 times
Download: 0 times
Share this document with a friend
86
Building Business Systems with DSLs atop OpenResty [email protected]Yichun Zhang (@agentzh) 2016.9
Transcript
Page 1: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Building Business Systems

with DSLsatop OpenResty

[email protected] Zhang (@agentzh)

2016.9

Page 2: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 3: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 4: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 5: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

NGINX + LuaJIT

Page 6: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The all-inclusive philosophy

Page 7: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

SimpleSmallFast

Flexible

Page 8: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Synchronously nonblocking

Page 9: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Light threads & semaphores

Page 10: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Cosockets

Page 11: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Timers and Sleeping

Page 12: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Shm-based dictionaries and queues

Page 13: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Dynamic SSL Handshakes

Page 14: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 15: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Dynamic Load Balancers

Page 16: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

upstream server 0.0.0.1; # just a place holder balancer_by_lua_block local balancer = require "ngx.balancer" local host, port = my_load_backend_addr() assert(balancer.set_cur_peer(host, port))

Page 17: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

ngx_stream_lua_module (TCP & UDP)

Page 18: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Advanced debugging & profiling toolsbased on gdb & systemtap

Page 19: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

C2000K

Page 20: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Web API and Microservices (real-time stock market info/weather)

Page 21: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Web Gateways (HTTP/HTTPS/TCP/UDP, CDN)

Page 22: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Web Applications

(e-commerce/ad platforms/push systems/web sites)

Page 23: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 24: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 25: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Distributed Storage Systems

Page 26: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Datanet is coming Find @jaksprats on twitter

Page 27: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Conflict-free Replicated Data TypesCRDT

Page 28: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 29: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The sregex regex engine

Page 30: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 31: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 32: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 33: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 34: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 35: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 36: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 37: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 38: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

A web platform as a virtual machine

Page 39: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

lz.taobao.com

Page 40: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 41: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 42: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 43: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 44: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 45: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

I invented the LZSQL languagefor the core business system.

Page 46: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

­­ /=/view/itemdailyflow/type/trend int $uid; text $begin, $end, $today, $url_index; symbol $db; location $lz_report; @hist := select ... from LZDB.dpunit_purl_result($db, $begin, $end, $uid) as a ... at $lz_report; @rt := select name, count(name) from LZRTI.getPurl($end as day, $uid) group by name ... return select ... from @hist union all @rt ...

Page 47: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 48: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

$ lzsql­compile ­c ­O2 ­n src/*.lzsql $ lzsql­link ­m lightface.core \ ­o lightface/core.lua src/*.oul

Page 49: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 50: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 51: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The LZSQL compiler was written injust 4000 lines of Perl.

Page 52: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

LZSQL Parser

LZSQL AST

LZSQL Optimizers

LZSQL Code Emitter

Page 53: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

We can generate Lua code,so why not C?

Page 54: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The ngx_lz_st module was generatedautomatically from the real-time statsengine's TCP protocol documentation.

Page 55: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The documentation had been originallywritten in wiki format

and then ported over to a declarative little languagenamed Ticpy designed by myself.

Page 56: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

// File lzrti.tcp

...

// fid=7

// @desc: obtain info for a specific shop and a specific visitor

// @param uid: unit_id

// @param uv: visitor cookie

// @param all_sessions: whether return p4p hits: 1 is yes; 0 otherwise

getUv(uid:4, uv:s, all_sessions:4): (

login_time:8, // session start time

login_url:s, // session entry page

login_title:s, // session entry page title

stay_time:4, // current session elapsed time

page_deep:4, // current session's access depth

im:s, // user's IM account

ip:s, // ip address

location_id:4, // address id

...

)

Page 57: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 58: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

$ wc ­l lzrti.tcp protocol.c.tt ngx_http_lz_st.c 345 lzrti.tcp 464 protocol.c.tt 12478 ngx_http_lz_st.c13287 total

Page 59: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

So...I'd rather writeprograms to write programs

to write programs...

Page 60: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Test our nginx C modules byour open source Perl module Test::Nginx!

http://search.cpan.org/perldoc?Test::Nginx

Page 61: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

use Test::Nginx::Socket 'no_plan'; run_tests(); __DATA__ === TEST 1: sanity ­­­ config location /main echo_subrequest GET /sub; location /sub echo hello; ­­­ request GET /main ­­­ response_body hello

Page 62: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Test the application web serives by Perl.

Page 63: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

use t::LZ; plan tests => 1 * blocks(); run_tests(); __DATA__ === TEST 1: lzers ­­­ url /=/view/lzers/~/~ ­­­ res [ "cnt" : 911619 ]

Page 64: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

=== TEST 5: sellerbasic ­­­ uid: 1509 ­­­ url /=/view/sellerbasic/~/~ ­­­ res [ "uv_times" : 257 ,

"login_title" : "店内搜索页", "login_url" : "http://xiaowangpu...", ...

Page 65: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Generate test databases from rulesby my open source Cheater utility

written by Perl.

http://search.cpan.org/perldoc?Cheater

Page 66: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9
Page 67: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The OpenResty Model Language

Page 68: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The OpenResty View Language

Page 69: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Jemplate & Lemplate

Page 70: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The OpenResty Controller Language

Page 71: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

uri­prefix("/foo/"), !uri­arg("_t") => redirect("/bar/"), done; uri­suffix(".jpg", ".gif", ".png") => exit(403);

Page 72: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

resp­filter # skip C/C++ string literals: rx ' (?: \ [\n] | ['\n] )* ' ­> $0; rx " (?: \ [\n] | ["\n] )* " ­> $0; # remove all those ugly C/C++ comments: rx /\* .*? \*/ | // [\n]* ­> '';

Page 73: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

WAF on the Controller Language.

Page 74: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

ModSecurity is a horrible DSL.

Page 75: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

SecRule ARGS "(?i)([\s\"';\/0­9\=]+on\w+\s*=)"

"id:'973337',phase:2,t:none,rev:'1',ver:'OWASP_CRS/2.2.9',

maturity:'1',accuracy:'8',t:urlDecodeUni,t:htmlEntityDecode,

t:jsDecode,t:cssDecode,log,capture,

msg:'XSS Filter ­ Category 2: Event Handler Vector',

tag:'OWASP_CRS/WEB_ATTACK/XSS',tag:'WASCTC/WASC­8',

tag:'WASCTC/WASC­22',tag:'OWASP_TOP_10/A2',

tag:'OWASP_AppSensor/IE1',tag:'PCI/6.5.1',

logdata:'Matched Data: %TX.0 found within %MATCHED_VAR_NAME: %MATCHED_VAR',

severity:'2',setvar:'tx.msg=%rule.msg',

setvar:tx.xss_score=+%tx.critical_anomaly_score,

setvar:tx.anomaly_score=+%tx.critical_anomaly_score,

setvar:tx.%rule.id­OWASP_CRS/WEB_ATTACK/XSS­%matched_var_name=%tx.0"

Page 76: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Model (Data analytics, Microservices) View (Web pages) Controller (CDN, WAF, Microservices)

Page 77: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

SportLang

Page 78: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The Y Language

gdb/pythonlldb/pythonsystemtapbcc/ebpfluajit/ebpf

Page 79: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

CoffeeScript

Page 80: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The meta domain specific language A DSL for creating all the DSLs (including itself).

Page 81: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Clean separation betweenbusiness representation and business implementation

Page 82: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Compiling-style Web Frameworks

Page 83: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

The best language is the business language.

Page 84: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Machine truly understands your business logic.

Page 85: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Any questions?

Page 86: Building Business Systems with DSLs atop OpenResty · Building Business Systems with DSLs atop OpenResty ☺agentzh@openresty.org☺ Yichun Zhang (@agentzh) 2016.9

Recommended