-
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 7 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,123 @@ | ||
| /* | ||
| * 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.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") | ||
| } | ||
|
|
||
| class CuratorElectorService(livyConf : LivyConf, livyServer : LivyServer) | ||
| extends LeaderLatchListener | ||
| with Logging | ||
| { | ||
|
|
||
| import CuratorElectorService._ | ||
|
|
||
| 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") | ||
| } | ||
|
|
||
| val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, retryPolicy) | ||
| val leaderKey = "/$haKeyPrefix/leader" | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
|
|
||
| var server : LivyServer = livyServer | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
|
|
||
| var leaderLatch = new LeaderLatch(client, leaderKey) | ||
| leaderLatch.addListener(this) | ||
|
|
||
| object HAState extends Enumeration{ | ||
| type HAState = Value | ||
| val Active, Standby = Value | ||
| } | ||
| var currentState = HAState.Standby | ||
|
|
||
| def isLeader() { | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| transitionToActive(); | ||
| } | ||
|
|
||
| def notLeader(){ | ||
|
RogPodge marked this conversation as resolved.
Outdated
RogPodge marked this conversation as resolved.
Outdated
|
||
| transitionToStandby(); | ||
| } | ||
|
|
||
| def start() : Unit = { | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| transitionToStandby() | ||
|
|
||
| client.start() | ||
| leaderLatch.start() | ||
| leaderLatch.await() | ||
|
|
||
| // This instance is now the leader. Joining the webserver to the main thread | ||
| info("starting join") | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| server.join() | ||
| info("join completed?") | ||
| close() | ||
| } | ||
|
|
||
| def close() : Unit = { | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| transitionToStandby(); | ||
| leaderLatch.close(); | ||
| } | ||
|
|
||
| def transitionToActive() : Unit = { | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| info("Transitioning to Active state") | ||
| if(currentState == HAState.Active){ | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| info("Already in Active State") | ||
| } | ||
| else { | ||
| server.start() | ||
| currentState = HAState.Active | ||
| info("Transition complete") | ||
| } | ||
| } | ||
|
|
||
| def transitionToStandby() : Unit = { | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| info("Transitioning to Standby state") | ||
| if(currentState == HAState.Standby){ | ||
|
RogPodge marked this conversation as resolved.
Outdated
|
||
| info("Already in Standby State"); | ||
| } | ||
| else { | ||
| server.stop(); | ||
| currentState = HAState.Standby | ||
| info("Transition complete"); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.