1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical.healthcheck
21 
22 import com.typesafe.config.Config
23 import org.apache.curator.framework.{CuratorFramework, CuratorFrameworkFactory}
24 import org.apache.curator.retry.RetryNTimes
25 import org.apache.curator.utils.CloseableUtils
26 import org.apache.zookeeper.CreateMode
27 import org.make.api.technical.healthcheck.HealthCheck.Status
28 
29 import scala.concurrent.{ExecutionContext, Future}
30 
31 class ZookeeperHealthCheck(config: Config) extends HealthCheck {
32 
33   override val techno: String = "zookeeper"
34 
35   val connectString: String = config.getString("make-api.zookeeper.url")
36 
37   override def healthCheck()(implicit ctx: ExecutionContext): Future[Status] = {
38     Future {
39       val client: CuratorFramework = CuratorFrameworkFactory.newClient(connectString, new RetryNTimes(3, 500))
40       val path: String = "/ephemeral_path"
41       val data: String = System.currentTimeMillis.toString
42 
43       client.start()
44       val realPath = client.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, data.getBytes("utf-8"))
45       val result = new String(client.getData.forPath(realPath), "utf-8")
46       client.delete().forPath(realPath)
47 
48       CloseableUtils.closeQuietly(client)
49       if (result != data) {
50         Status.NOK(Some(s"""Unexpected result in zookeeper health check: expected "$result" but got "$data""""))
51       } else {
52         Status.OK
53       }
54     }
55   }
56 }
Line Stmt Id Pos Tree Symbol Tests Code
33 39990 1245 - 1256 Literal <nosymbol> org.scalatest.testsuite "zookeeper"
35 32391 1288 - 1330 Apply com.typesafe.config.Config.getString org.scalatest.testsuite ZookeeperHealthCheck.this.config.getString("make-api.zookeeper.url")
38 49199 1417 - 2121 ApplyToImplicitArgs scala.concurrent.Future.apply org.scalatest.testsuite scala.concurrent.Future.apply[org.make.api.technical.healthcheck.HealthCheck.Status]({ val client: org.apache.curator.framework.CuratorFramework = org.apache.curator.framework.CuratorFrameworkFactory.newClient(ZookeeperHealthCheck.this.connectString, new org.apache.curator.retry.RetryNTimes(3, 500)); val path: String = "/ephemeral_path"; val data: String = java.lang.System.currentTimeMillis().toString(); client.start(); val realPath: String = client.create().withMode(EPHEMERAL_SEQUENTIAL).forPath(path, data.getBytes("utf-8")); val result: String = new scala.Predef.String(client.getData().forPath(realPath), "utf-8"); client.delete().forPath(realPath); org.apache.curator.utils.CloseableUtils.closeQuietly(client); if (result.!=(data)) org.make.api.technical.healthcheck.HealthCheck.Status.NOK.apply(scala.Some.apply[String](("Unexpected result in zookeeper health check: expected \"".+(result).+("\" but got \"").+(data).+("\""): String))) else org.make.api.technical.healthcheck.HealthCheck.Status.OK })(ctx)