-
Notifications
You must be signed in to change notification settings - Fork 624
[LIVY-11] Enable HA support #212
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
base: master
Are you sure you want to change the base?
Changes from all commits
477b438
1a2bd91
61e06f2
945f5ce
de6f885
048c1a2
31df79a
dd5da8b
c73bdea
b8a4d17
af0cf82
46276f2
5c806fc
19e266b
972ca3d
fdf449d
636ac05
2423e4d
14741eb
de250f5
988c219
ff3fd42
dd47e37
fe79e75
8178eef
eb44f46
9b1f21b
1643f05
6caf0cc
0e2379b
dd87210
112ab27
cbdaaaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| /* | ||
| * 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.livy.server | ||
|
|
||
| import java.io.Closeable | ||
| import java.io.IOException | ||
| import java.net.InetAddress | ||
| import java.util.concurrent.TimeUnit | ||
| import java.util.concurrent.atomic.AtomicInteger | ||
|
|
||
| import org.apache.curator.framework.CuratorFramework | ||
| import org.apache.curator.framework.CuratorFrameworkFactory | ||
| import org.apache.curator.framework.recipes.leader.LeaderLatch | ||
| import org.apache.curator.framework.recipes.leader.LeaderLatchListener | ||
| import org.apache.curator.retry.RetryNTimes | ||
|
|
||
| import org.apache.livy.{LivyConf, Logging} | ||
| import org.apache.livy.LivyConf.Entry | ||
|
|
||
| object CuratorElectorService { | ||
| val HA_KEY_PREFIX_CONF = Entry("livy.server.ha.key-prefix", "livy_ha") | ||
| val HA_RETRY_CONF = Entry("livy.server.ha.retry-policy", "5,100") | ||
| } | ||
|
|
||
| object HAState extends Enumeration{ | ||
| type HAState = Value | ||
| val Active, Standby = Value | ||
| } | ||
|
|
||
|
|
||
| class CuratorElectorService(livyConf : LivyConf, livyServer : LivyServer, | ||
| mockCuratorClient: Option[CuratorFramework] = None, | ||
| mockLeaderLatch: Option[LeaderLatch] = None) | ||
| extends LeaderLatchListener | ||
| with Logging | ||
| { | ||
| import CuratorElectorService._ | ||
| import HAState._ | ||
|
|
||
| val haAddress = livyConf.get(LivyConf.HA_ZOOKEEPER_URL) | ||
| require(!haAddress.isEmpty, s"Please configure ${LivyConf.HA_ZOOKEEPER_URL.key}.") | ||
| val haKeyPrefix = livyConf.get(HA_KEY_PREFIX_CONF) | ||
| val retryValue = livyConf.get(HA_RETRY_CONF) | ||
| // a regex to match patterns like "m, n" where m and n both are integer values | ||
| val retryPattern = """\s*(\d+)\s*,\s*(\d+)\s*""".r | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be easier to have two config values?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The 2 configs should be paired together in the config for clarity I believe. Also we're following the example ins the ZooKeeperStateStore.scala
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good, we can adjust based on whichever PR is merged first. |
||
| val retryPolicy = retryValue match { | ||
| case retryPattern(n, sleepMs) => new RetryNTimes(n.toInt, sleepMs.toInt) | ||
| case _ => throw new IllegalArgumentException( | ||
| s"$HA_KEY_PREFIX_CONF contains bad value: $retryValue. " + | ||
| "Correct format is <max retry count>,<sleep ms between retry>. e.g. 5,100") | ||
| } | ||
|
|
||
| var server : LivyServer = livyServer | ||
|
|
||
| val client: CuratorFramework = mockCuratorClient.getOrElse { | ||
| CuratorFrameworkFactory.newClient(haAddress, retryPolicy) | ||
| } | ||
| val leaderKey = s"/$haKeyPrefix/leader" | ||
|
|
||
| val leaderIds = livyConf.configToSeq(LivyConf.HA_SERVER_IDS) | ||
| val leaderHostnames = livyConf.configToSeq(LivyConf.HA_SERVER_HOSTNAMES) | ||
|
|
||
| var leaderLatch = mockLeaderLatch.getOrElse { | ||
| new LeaderLatch(client, leaderKey, getCurrentId()) | ||
| } | ||
| leaderLatch.addListener(this) | ||
|
|
||
| var currentState = HAState.Standby | ||
| override def isLeader() { | ||
| transitionToActive() | ||
| } | ||
|
|
||
| override def notLeader(){ | ||
| transitionToStandby() | ||
| } | ||
|
|
||
| def getCurrentId(): String = { | ||
| val currentHostname = java.net.InetAddress.getLocalHost().getCanonicalHostName(); | ||
| debug("This server's current hostname is: " + currentHostname) | ||
| debug("This hostnames for valid leaders are: " + leaderHostnames) | ||
| val currentId = leaderIds(leaderHostnames indexOf currentHostname) | ||
| debug("This server's designated ID is: " + currentId) | ||
| currentId | ||
| } | ||
|
|
||
| def getActiveHostname(): String = { | ||
| val activeLeaderId = leaderLatch.getLeader().getId() | ||
| val activeAddress = leaderHostnames(leaderIds indexOf activeLeaderId) | ||
| activeAddress | ||
| } | ||
|
|
||
| def start(): Unit = { | ||
| server.initHa(this) | ||
| transitionToStandby() | ||
|
|
||
| server.start() | ||
| client.start() | ||
| leaderLatch.start() | ||
|
|
||
| try { | ||
| Thread.currentThread.join() | ||
| } finally { | ||
| transitionToStandby() | ||
| } | ||
| } | ||
|
|
||
| def close(): Unit = { | ||
| transitionToStandby() | ||
| leaderLatch.close() | ||
| } | ||
|
|
||
| def transitionToActive(): Unit = { | ||
| info("Transitioning to Active state") | ||
| if(currentState == HAState.Active) { | ||
| info("Already in Active State") | ||
| } | ||
| else { | ||
| server.restart() | ||
| currentState = HAState.Active | ||
| info("Transition complete") | ||
| } | ||
| } | ||
|
|
||
| def transitionToStandby(): Unit = { | ||
| info("Transitioning to Standby state") | ||
| if(currentState == HAState.Standby) { | ||
| info("Already in Standby State") | ||
| } | ||
| else { | ||
| currentState = HAState.Standby | ||
| info("Transition complete") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * 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.livy.server | ||
|
|
||
| import java.io.IOException | ||
| import java.net.URL | ||
| import javax.servlet.Filter | ||
| import javax.servlet.FilterChain | ||
| import javax.servlet.FilterConfig | ||
| import javax.servlet.ServletContext | ||
| import javax.servlet.ServletException | ||
| import javax.servlet.ServletRequest | ||
| import javax.servlet.ServletRequestWrapper | ||
| import javax.servlet.ServletResponse | ||
| import javax.servlet.http.HttpServletRequest | ||
| import javax.servlet.http.HttpServletRequestWrapper | ||
| import javax.servlet.http.HttpServletResponse | ||
|
|
||
| import org.springframework.web.util.UriComponentsBuilder | ||
|
|
||
| import org.apache.livy.{LivyConf, Logging} | ||
|
|
||
| class DomainRedirectionFilter(haService: CuratorElectorService) extends Filter | ||
| with Logging | ||
| { | ||
|
|
||
| val METHODS_TO_IGNORE = Set("GET", "OPTIONS", "HEAD") | ||
|
|
||
| val HEADER_NAME = "X-Requested-By" | ||
|
|
||
| def isLeader(): Boolean = { | ||
| haService.currentState == HAState.Active | ||
| } | ||
|
|
||
| override def init(filterConfig: FilterConfig): Unit = {} | ||
|
|
||
| override def doFilter(request: ServletRequest, | ||
| response: ServletResponse, | ||
| chain: FilterChain): Unit = { | ||
| if (!isLeader()) { | ||
| debug("active leader's hostnames are:" + haService.getActiveHostname()) | ||
| debug("current id:" + haService.getCurrentId()) | ||
| val httpRequest = request.asInstanceOf[HttpServletRequest] | ||
| val queryOpt: Option[String] = Option(httpRequest.getQueryString()) | ||
| val requestURL = httpRequest.getRequestURL().toString() | ||
| debug("requested url: " + requestURL) | ||
|
|
||
| val builder = UriComponentsBuilder.fromHttpUrl(requestURL) | ||
| val activeURL = builder.host(haService.getActiveHostname()).toUriString() | ||
| val redirectURL = if (queryOpt.isEmpty) activeURL else activeURL + "?" + queryOpt.get | ||
| debug("redirected url:" + redirectURL) | ||
|
|
||
| val httpServletResponse = response.asInstanceOf[HttpServletResponse]; | ||
| val redirectMsg = "This is a standby Livy Instance. The redirect url is: " + redirectURL | ||
| val out = httpServletResponse.getWriter() | ||
| // scalastyle:off println | ||
| out.println(redirectMsg) | ||
| // scalastyle:on println | ||
|
|
||
| httpServletResponse.setHeader("Location", redirectURL) | ||
| httpServletResponse.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT) | ||
| } else { | ||
| chain.doFilter(request, response); | ||
| } | ||
| } | ||
|
|
||
| override def destroy(): Unit = {} | ||
| } | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.