Skip to content

Conversation

@moresandeep
Copy link
Contributor

@moresandeep moresandeep commented Oct 5, 2020

What changes were proposed in this pull request?

This PR introduces a way for Knox to load balance backend clients. This is done by introducing 3 parameters in HaProvider

  1. enableStickySession - Enables sticky sessions
  2. stickySessionCookieName - Customize sticky session cookie name, default is KNOX_BACKEND-{serviceName}
  3. noFallback - when this flag is set, Knox will return a 502 (Bad Gateway) if a request comes with a cookie and the corresponding backend is unavailable. This is applicable only when stickySessionCookieName is enabled.
  4. enableLoadBalancing - Enable loadbalancing, roundrobin requests to backend HA urls
     <provider>
            <role>ha</role>
            <name>HaProvider</name>
            <enabled>true</enabled>
            <param>
                <name>WHOAMI</name>
                <value>enabled=true;maxFailoverAttempts=3;failoverSleep=1000;enableStickySession=true;noFallback=true</value>
            </param>
        </provider>

When enabled, knox dispatch will round-robin requests to the URLs listed in the HA configuration unlike previously where knox only chooses other URL after a failover.
The sessions are sticky so same session requests will always end up dispatching to the same backend.

How was this patch tested?

This patch was manually tested using

  1. curl for multiple different sessions
(base) ➜  temp curl -iku admin:admin-password  "https://localhost:8443/gateway/test/whoami/"
HTTP/1.1 200 OK
Date: Mon, 05 Oct 2020 19:20:03 GMT
Set-Cookie: KNOXSESSIONID=node0flaeprbtixj919ozympe23muc2.node0; Path=/gateway/test; Secure; HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: rememberMe=deleteMe; Path=/gateway/test; Max-Age=0; Expires=Sun, 04-Oct-2020 19:20:04 GMT
Set-Cookie: KNOX_BACKEND-WHOAMI=4336ad0a09ba5de5c19d294a6e1be4bd5b0aa62bc0537b2c1951ad50b1aca033; Path=/gateway/test; Secure; HttpOnly
Content-Length: 10

 SERVER 1
(base) ➜  temp curl -iku admin:admin-password  "https://localhost:8443/gateway/test/whoami/"
HTTP/1.1 200 OK
Date: Mon, 05 Oct 2020 19:20:05 GMT
Set-Cookie: KNOXSESSIONID=node01bynsokjpm43t1ag2zogir8wht3.node0; Path=/gateway/test; Secure; HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: rememberMe=deleteMe; Path=/gateway/test; Max-Age=0; Expires=Sun, 04-Oct-2020 19:20:05 GMT
Set-Cookie: KNOX_BACKEND-WHOAMI=d2eea4def3bbb002420943cc4d7983627cf7132b6154a400597c379a1254189b; Path=/gateway/test; Secure; HttpOnly
Content-Length: 10

 SERVER 2
  1. curl for sticky sessions
(base) ➜  temp curl -iku admin:admin-password -c cookies.txt -b cookies.txt "https://localhost:8443/gateway/test/whoami/"

HTTP/1.1 200 OK
Date: Mon, 05 Oct 2020 19:22:05 GMT
Set-Cookie: KNOXSESSIONID=node0117mojjq6uevh1p1nl2v5iumgo5.node0; Path=/gateway/test; Secure; HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: rememberMe=deleteMe; Path=/gateway/test; Max-Age=0; Expires=Sun, 04-Oct-2020 19:22:06 GMT
Set-Cookie: KNOX_BACKEND-WHOAMI=d2eea4def3bbb002420943cc4d7983627cf7132b6154a400597c379a1254189b; Path=/gateway/test; Secure; HttpOnly
Content-Length: 10

 SERVER 2
(base) ➜  temp curl -iku admin:admin-password -c cookies.txt -b cookies.txt "https://localhost:8443/gateway/test/whoami/"

HTTP/1.1 200 OK
Date: Mon, 05 Oct 2020 19:22:06 GMT
Content-Length: 10

 SERVER 2
(base) ➜  temp curl -iku admin:admin-password -c cookies.txt -b cookies.txt "https://localhost:8443/gateway/test/whoami/"

HTTP/1.1 200 OK
Date: Mon, 05 Oct 2020 19:22:07 GMT
Content-Length: 10

 SERVER 2
  1. curl with sticky sessions with fallback off
(base) ➜  temp curl  -iku admin:admin-password -c cookies.txt -b cookies.txt "https://localhost:8443/gateway/test/whoami/"
HTTP/1.1 502 Bad Gateway
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=iso-8859-1
Content-Length: 507

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 502 Service connection error, HA failover disabled</title>
</head>
<body><h2>HTTP ERROR 502 Service connection error, HA failover disabled</h2>
<table>
<tr><th>URI:</th><td>/gateway/test/whoami/</td></tr>
<tr><th>STATUS:</th><td>502</td></tr>
<tr><th>MESSAGE:</th><td>Service connection error, HA failover disabled</td></tr>
<tr><th>SERVLET:</th><td>test-knox-gateway-servlet</td></tr>
</table>

</body>
</html>
  1. Browser

…to enableStickySession and stickySessionCookieName
@moresandeep moresandeep force-pushed the KNOX-843_ClidentSide_HA branch from b9d21c4 to 67836d4 Compare October 22, 2020 19:16
@moresandeep
Copy link
Contributor Author

@lmccay I updated the PR with requested changes go through it and let me know what you think. I had to do some refactoring because I realized that we have a ton of custom HA implementations (e.g. Atlas) which would not get this feature because of how they just extended DefaultHADispatch. I did some refactoring so that custom dispatches should be able to use these flags.

@moresandeep moresandeep merged commit 673bb4b into apache:master Oct 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant