[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415457
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
 
 Review comment:
   space before `{`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415651
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
+transitionToStandby();
+  }
+
+  def start() : Unit = {
+transitionToStandby()
+
+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.start()
+  currentState = HAState.Active
+  info("Transition complete")
+}
+  }
+
+  def transitionToStandby() : Unit = {
 
 Review comment:
   no space before `:`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415549
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
+transitionToStandby();
+  }
+
+  def start() : Unit = {
+transitionToStandby()
+
+client.start()
+leaderLatch.start()
+
+try {
+  Thread.currentThread.join()
+} finally {
+  transitionToStandby()
+}
+  }
+
+  def close() : Unit = {
 
 Review comment:
   no space before `:`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415827
 
 

 ##
 File path: server/src/main/scala/org/apache/livy/server/LivyServer.scala
 ##
 @@ -394,16 +395,29 @@ class LivyServer extends Logging {
   }
 }
 
+object HighAvailabilitySettings {
+  val HA_ON = "on"
+  val HA_OFF = "off"
+}
+
 object LivyServer {
 
   def main(args: Array[String]): Unit = {
 val server = new LivyServer()
-try {
-  server.start()
-  server.join()
-} finally {
-  server.stop()
+val livyConf = new LivyConf().loadFromFile("livy.conf")
+
+if(livyConf.get(LivyConf.HA_MODE) == HighAvailabilitySettings.HA_ON){
 
 Review comment:
   space before `{`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415571
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
+transitionToStandby();
+  }
+
+  def start() : Unit = {
+transitionToStandby()
+
+client.start()
+leaderLatch.start()
+
+try {
+  Thread.currentThread.join()
+} finally {
+  transitionToStandby()
+}
+  }
+
+  def close() : Unit = {
+transitionToStandby();
+leaderLatch.close();
+  }
+
+  def transitionToActive() : Unit = {
 
 Review comment:
   no space before `:`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415620
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
+transitionToStandby();
+  }
+
+  def start() : Unit = {
+transitionToStandby()
+
+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){
 
 Review comment:
   space before `{`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415370
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
 
 Review comment:
   no space before `:`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415504
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
+transitionToStandby();
+  }
+
+  def start() : Unit = {
 
 Review comment:
   no space before `:`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325415712
 
 

 ##
 File path: 
server/src/main/scala/org/apache/livy/server/CuratorElectorService.scala
 ##
 @@ -0,0 +1,122 @@
+/*
+ * 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
+  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 ,. e.g. 
5,100")
+  }
+
+  val client: CuratorFramework = CuratorFrameworkFactory.newClient(haAddress, 
retryPolicy)
+  val leaderKey = s"/$haKeyPrefix/leader"
+
+  var server : LivyServer = livyServer
+
+  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() {
+transitionToActive();
+  }
+
+  def notLeader(){
+transitionToStandby();
+  }
+
+  def start() : Unit = {
+transitionToStandby()
+
+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.start()
+  currentState = HAState.Active
+  info("Transition complete")
+}
+  }
+
+  def transitionToStandby() : Unit = {
+info("Transitioning to Standby state")
+if(currentState == HAState.Standby){
 
 Review comment:
   space before `{`


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-livy] ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA support

2019-09-17 Thread GitBox
ajbozarth commented on a change in pull request #212: [LIVY-11] Enable HA 
support
URL: https://github.com/apache/incubator-livy/pull/212#discussion_r325412516
 
 

 ##
 File path: conf/livy.conf.template
 ##
 @@ -100,6 +100,16 @@
 # on user request and then livy server classpath automatically.
 # livy.repl.enable-hive-context =
 
+# High Availability mode of Livy. Possible values:
+# off: Default. Turn off High Availability. 
+# on: Livy uses Zookeeper as a state store to ensure a livy server is always 
available with the
+# correct state.
+# Must set livy.server.ha.zookeeper-url to configure HA
+# livy.server.ha.mode = off
 
 Review comment:
   is `off`/`on` how HA configs usually work? I feel like 
`livy.server.ha.mode.enabled = true|false` would be more consistent with the 
current configs. This isn't an issue, just an observation


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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services