moresandeep commented on code in PR #1049:
URL: https://github.com/apache/knox/pull/1049#discussion_r2283438950


##########
gateway-provider-ha/src/main/java/org/apache/knox/gateway/ha/dispatch/CommonHaDispatch.java:
##########
@@ -225,19 +223,88 @@ default void shiftActiveURL(boolean userAgentDisabled, 
Optional<URI> backendURI)
          *    needs to be loadbalanced. If a request has BACKEND coookie and 
Loadbalance=on then
          *    there should be no loadbalancing.
          */
-        if (isLoadBalancingEnabled() && !userAgentDisabled) {
+        if (getHaConfigurations().isLoadBalancingEnabled() && 
!userAgentDisabled) {
             /* check sticky session enabled */
-            if (isStickySessionEnabled()) {
+            if (getHaConfigurations().isStickySessionEnabled()) {
                 /* loadbalance only when sticky session enabled and no backend 
url cookie */
                 if (!backendURI.isPresent()) {
-                    
getHaProvider().makeNextActiveURLAvailable(getServiceRole());
+                    
getHaConfigurations().getHaProvider().makeNextActiveURLAvailable(getServiceRole());
                 } else {
                     /* sticky session enabled and backend url cookie is valid 
no need to loadbalance */
                     /* do nothing */
                 }
             } else {
-                getHaProvider().makeNextActiveURLAvailable(getServiceRole());
+                
getHaConfigurations().getHaProvider().makeNextActiveURLAvailable(getServiceRole());
+            }
+        }
+    }
+
+    /**
+     * A helper method that marks an endpoint failed.
+     * Changes HA Provider state.
+     * Changes ActiveUrl state.
+     * Changes for inbound urls should be handled by calling functions.
+     *
+     * @param outboundRequest
+     * @param inboundRequest
+     * @return current failover counter
+     */
+    default AtomicInteger markEndpointFailed(final HttpUriRequest 
outboundRequest, final HttpServletRequest inboundRequest) {
+        synchronized (this) {
+            
getHaConfigurations().getHaProvider().markFailedURL(getServiceRole(), 
outboundRequest.getURI().toString());
+            AtomicInteger counter = (AtomicInteger) 
inboundRequest.getAttribute(FAILOVER_COUNTER_ATTRIBUTE);
+            if (counter == null) {
+                counter = new AtomicInteger(0);
+            }
+
+            if (counter.incrementAndGet() <= 
getHaConfigurations().getMaxFailoverAttempts()) {
+                setupUrlHashLookup(); // refresh the url hash after failing a 
url
+                /* in case of failover update the activeURL variable */
+                getActiveURL().set(outboundRequest.getURI().toString());
+            }
+            return counter;
+        }
+    }
+
+    default HttpServletRequest prepareForFailover(HttpUriRequest 
outboundRequest, HttpServletRequest inboundRequest) {
+        //null out target url so that rewriters run again
+        
inboundRequest.setAttribute(AbstractGatewayFilter.TARGET_REQUEST_URL_ATTRIBUTE_NAME,
 null);
+        // Make sure to remove the ha cookie from the request
+        inboundRequest = new 
StickySessionCookieRemovedRequest(getHaConfigurations().getStickySessionCookieName(),
 inboundRequest);

Review Comment:
   Could there be a case where there is an HA request but no cookie? i.e. no 
sticky session, will it blow up here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@knox.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to