-
Notifications
You must be signed in to change notification settings - Fork 276
KNOX-2253 - Add support to handle Livy server redirects #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cfb6b0b
added support for Livy HA with knox gateway
RogPodge fc9c564
removed unneccessary additions to the xml files
RogPodge 1cbb708
updated the HA dispatch to extend the DefaultHaDispatch
RogPodge eb6e8c1
renamed messages to livymessages, removed unneeded edits
RogPodge d2c9f20
fixed checkstyle violation
RogPodge a98125c
explicitly added dependencies
RogPodge 3014ee2
fixed pmd error
RogPodge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
gateway-service-livy/src/main/java/org/apache/knox/gateway/livy/LivyHaDispatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with this | ||
| * work for additional information regarding copyright ownership. The ASF | ||
| * licenses this file to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| * License for the specific language governing permissions and limitations under | ||
| * the License. | ||
| */ | ||
| package org.apache.knox.gateway.livy; | ||
|
|
||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.client.methods.HttpRequestBase; | ||
| import org.apache.http.client.methods.HttpUriRequest; | ||
| import org.apache.http.entity.BufferedHttpEntity; | ||
| import org.apache.knox.gateway.ha.dispatch.DefaultHaDispatch; | ||
| import org.apache.knox.gateway.filter.AbstractGatewayFilter; | ||
| import org.apache.knox.gateway.i18n.messages.MessagesFactory; | ||
| import org.apache.knox.gateway.livy.i18n.RMMessages; | ||
|
|
||
| import javax.servlet.http.HttpServletRequest; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| public class LivyHaDispatch extends DefaultHaDispatch { | ||
| private static final String FAILOVER_COUNTER_ATTRIBUTE = "dispatch.ha.failover.counter"; | ||
| private static final String LOCATION = "Location"; | ||
| private static final RMMessages LOG = MessagesFactory.get(RMMessages.class); | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| private HttpResponse inboundResponse = null; | ||
|
|
||
|
|
||
| public LivyHaDispatch() { | ||
| setServiceRole("LIVYSERVER"); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @return HttpReponse used for unit testing so we | ||
| * can inject inboundResponse before calling executeRequest method | ||
| */ | ||
| private HttpResponse getInboundResponse() { | ||
| HttpResponse response = this.inboundResponse; | ||
| this.setInboundResponse(null); | ||
| return response; | ||
| } | ||
|
|
||
| void setInboundResponse(HttpResponse inboundResponse) { | ||
| this.inboundResponse = inboundResponse; | ||
| } | ||
|
|
||
| @Override | ||
| protected void executeRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse) throws IOException { | ||
| HttpResponse inboundResponse = this.getInboundResponse(); | ||
| try { | ||
| if( this.getInboundResponse() == null ) { | ||
| inboundResponse = executeOutboundRequest(outboundRequest); | ||
| } | ||
| writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse); | ||
| } catch (StandbyException e) { | ||
| LOG.errorReceivedFromStandbyNode(e); | ||
| failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e); | ||
| } catch (IOException e) { | ||
| LOG.errorConnectingToServer(outboundRequest.getURI().toString(), e); | ||
| failoverRequest(outboundRequest, inboundRequest, outboundResponse, inboundResponse, e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks for specific outbound response codes/content to trigger a retry or failover | ||
| */ | ||
| @Override | ||
| protected void writeOutboundResponse(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse, HttpResponse inboundResponse) throws IOException { | ||
| int status = inboundResponse.getStatusLine().getStatusCode(); | ||
| if ( status == 403 || status == 307) { | ||
| BufferedHttpEntity entity = new BufferedHttpEntity(inboundResponse.getEntity()); | ||
| inboundResponse.setEntity(entity); | ||
| ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
| inboundResponse.getEntity().writeTo(outputStream); | ||
| String body = new String(outputStream.toByteArray(), StandardCharsets.UTF_8); | ||
| if (body.contains("This is a standby Livy Instance")) { | ||
| throw new StandbyException(); | ||
| } | ||
| } | ||
| super.writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse); | ||
| } | ||
|
|
||
| @Override | ||
| protected void failoverRequest(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest, HttpServletResponse outboundResponse, HttpResponse inboundResponse, Exception exception) throws IOException { | ||
| LOG.failingOverRequest(outboundRequest.getURI().toString()); | ||
| URI uri; | ||
| String outboundURIs; | ||
| AtomicInteger counter = (AtomicInteger) inboundRequest.getAttribute(FAILOVER_COUNTER_ATTRIBUTE); | ||
| if (counter == null) { | ||
| counter = new AtomicInteger(0); | ||
| } | ||
| inboundRequest.setAttribute(FAILOVER_COUNTER_ATTRIBUTE, counter); | ||
| outboundURIs = outboundRequest.getURI().toString(); | ||
|
|
||
| if (counter.incrementAndGet() <= getMaxFailoverAttempts()) { | ||
| //null out target url so that rewriters run again | ||
| inboundRequest.setAttribute(AbstractGatewayFilter.TARGET_REQUEST_URL_ATTRIBUTE_NAME, null); | ||
|
|
||
| uri = getUriFromInbound(inboundRequest, inboundResponse, outboundURIs); | ||
| ((HttpRequestBase) outboundRequest).setURI(uri); | ||
| if (getFailoverSleep() > 0) { | ||
| try { | ||
| Thread.sleep(getFailoverSleep()); | ||
| } catch (InterruptedException e) { | ||
| LOG.failoverSleepFailed(getServiceRole(), e); | ||
| } | ||
| } | ||
| executeRequest(outboundRequest, inboundRequest, outboundResponse); | ||
| } else { | ||
| LOG.maxFailoverAttemptsReached(getMaxFailoverAttempts(), getServiceRole()); | ||
| if (inboundResponse != null) { | ||
| writeOutboundResponse(outboundRequest, inboundRequest, outboundResponse, inboundResponse); | ||
| } else { | ||
| throw new IOException(exception); | ||
| } | ||
| } | ||
| } | ||
|
RogPodge marked this conversation as resolved.
|
||
|
|
||
| URI getUriFromInbound(HttpServletRequest inboundRequest, HttpResponse inboundResponse, String outboundURIs) { | ||
| URI uri; | ||
| if( outboundURIs != null ) { | ||
| getHaProvider().markFailedURL(getServiceRole(), outboundURIs); | ||
| } | ||
| try { | ||
| if( inboundResponse != null ) { | ||
| // We get redirection URI, failover condition we don't | ||
| // need to consult list of host. | ||
| String host = inboundResponse.getFirstHeader(LOCATION).getValue(); | ||
| LOG.failoverRedirect(host); | ||
| uri = URI.create(host); | ||
| } else { // inboundRequest was null previous active node is down | ||
| // get next URI in list to try. | ||
| uri = getDispatchUrl(inboundRequest); | ||
| } | ||
| } catch(Exception ex ) { | ||
| uri = getDispatchUrl(inboundRequest); | ||
| } | ||
| getHaProvider().setActiveURL(getServiceRole(), uri.toString()); | ||
| return uri; | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
gateway-service-livy/src/main/java/org/apache/knox/gateway/livy/StandbyException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * <p/> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p/> | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.knox.gateway.livy; | ||
|
|
||
| class StandbyException extends RuntimeException { | ||
| } |
34 changes: 34 additions & 0 deletions
34
gateway-service-livy/src/main/java/org/apache/knox/gateway/livy/i18n/RMMessages.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.knox.gateway.livy.i18n; | ||
|
|
||
| import org.apache.knox.gateway.ha.dispatch.i18n.HaDispatchMessages; | ||
| import org.apache.knox.gateway.i18n.messages.Message; | ||
| import org.apache.knox.gateway.i18n.messages.MessageLevel; | ||
| import org.apache.knox.gateway.i18n.messages.Messages; | ||
| import org.apache.knox.gateway.i18n.messages.StackTrace; | ||
|
|
||
| @Messages(logger = "org.apache.knox.gateway") | ||
| public interface RMMessages extends HaDispatchMessages { | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
|
|
||
| @Message(level = MessageLevel.WARN, text = "Received an error from a Livy Server in Standby: {0}") | ||
| void errorReceivedFromStandbyNode(@StackTrace(level = MessageLevel.DEBUG) Exception e); | ||
|
|
||
| @Message(level = MessageLevel.WARN, text = "Redirect to {0} because of accessing standby Livy Server.") | ||
| void failoverRedirect(String redirect); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.