Skip to content

Commit 7b4f7ab

Browse files
authored
don't use deprecated influxdb methods (#10)
* don't use deprecated influxdb methods * minor import cleanup * minor cleanup, use Either in influxdbclient to store error message
1 parent 9ee73f2 commit 7b4f7ab

File tree

3 files changed

+87
-39
lines changed

3 files changed

+87
-39
lines changed

src/main/scala/com/workday/warp/collectors/ResponseTimeCollector.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ResponseTimeCollector(testId: String) extends AbstractMeasurementCollector
2828
* Called prior to starting an individual test invocation.
2929
*/
3030
override def startMeasurement(): Unit = {
31-
if (InfluxDBClient.client.isEmpty) {
31+
if (InfluxDBClient.maybeClient.isLeft) {
3232
this.isEnabled = false
3333
}
3434
}

src/main/scala/com/workday/warp/persistence/influxdb/InfluxDBClient.scala

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.workday.warp.common.utils.StackTraceFilter
1212
import com.workday.warp.persistence.CorePersistenceAware
1313
import com.workday.warp.persistence.TablesLike._
1414
import com.workday.warp.persistence.Tables._
15-
import org.influxdb.dto.{BatchPoints, Point, Pong}
15+
import org.influxdb.dto.{BatchPoints, Point, Pong, Query, QueryResult}
1616
import org.influxdb.{InfluxDB, InfluxDBFactory}
1717
import org.pmw.tinylog.Logger
1818

@@ -34,12 +34,12 @@ trait InfluxDBClient extends StackTraceFilter with CorePersistenceAware {
3434
* @param seriesName the series (also referred to as a measurement) to use for persistence.
3535
*/
3636
def persistHeapHistogram(histo: HeapHistogram, dbName: String, seriesName: String, warpTestName: String): Try[Unit] = {
37-
InfluxDBClient.client match {
38-
case None => Failure(new RuntimeException("unable to connect to influxdb"))
39-
case Some(client) =>
37+
InfluxDBClient.maybeClient match {
38+
case Left(error) => Failure(new WarpConfigurationException(error))
39+
case Right(client) =>
4040
// create the database if necessary
4141
if (!this.databaseExists(dbName).getOrElse(false)) {
42-
client.createDatabase(dbName)
42+
this.createDatabase(dbName)
4343
}
4444

4545
// timestamp to use for all points in this batch
@@ -120,12 +120,12 @@ trait InfluxDBClient extends StackTraceFilter with CorePersistenceAware {
120120
testExecutions: Iterable[T],
121121
threshold: Option[Duration] = None): Try[Unit] = {
122122

123-
InfluxDBClient.client match {
124-
case None => Failure(new RuntimeException("unable to connect to influxdb"))
125-
case Some(client) =>
123+
InfluxDBClient.maybeClient match {
124+
case Left(error) => Failure(new WarpConfigurationException(error))
125+
case Right(client) =>
126126
// create the database if necessary
127127
if (!this.databaseExists(dbName).getOrElse(false)) {
128-
client.createDatabase(dbName)
128+
this.createDatabase(dbName)
129129
}
130130

131131
val points = addPoints(dbName, seriesName, testExecutions, threshold)
@@ -157,9 +157,20 @@ trait InfluxDBClient extends StackTraceFilter with CorePersistenceAware {
157157
* @return true iff databaseName exists as a database in InfluxDB and we have a successful connection.
158158
*/
159159
def databaseExists(databaseName: String): Try[Boolean] = {
160-
InfluxDBClient.client match {
161-
case None => Failure(new WarpConfigurationException(InfluxDBClient.error))
162-
case Some(client) => Try(client.describeDatabases.asScala.exists(_.equals(databaseName)))
160+
val showQuery: Query = new Query("SHOW DATABASES", databaseName)
161+
InfluxDBClient.maybeClient match {
162+
case Left(error) => Failure(new WarpConfigurationException(error))
163+
case Right(client) => Try {
164+
val results: Seq[QueryResult.Result] = client.query(showQuery).getResults.asScala
165+
val databaseNames: Seq[String] = for {
166+
res <- results
167+
serie <- res.getSeries.asScala
168+
value <- serie.getValues.asScala
169+
name <- value.asScala
170+
} yield name.toString
171+
172+
databaseNames.exists(_.equals(databaseName))
173+
}
163174
}
164175
}
165176

@@ -169,10 +180,27 @@ trait InfluxDBClient extends StackTraceFilter with CorePersistenceAware {
169180
*
170181
* @param database name of the database to delete
171182
*/
172-
def deleteDatabase(database: String): Try[Unit] = {
173-
InfluxDBClient.client match {
174-
case None => Failure(new WarpConfigurationException(InfluxDBClient.error))
175-
case Some(client) => Try(client.deleteDatabase(database))
183+
def dropDatabase(database: String): Try[Unit] = {
184+
val dropQuery: Query = new Query(s"""DROP DATABASE "$database"""", database)
185+
186+
InfluxDBClient.maybeClient match {
187+
case Left(error) => Failure(new WarpConfigurationException(error))
188+
case Right(client) => Try(client.query(dropQuery))
189+
}
190+
}
191+
192+
193+
/**
194+
*
195+
* @param database
196+
* @return
197+
*/
198+
def createDatabase(database: String): Try[Unit] = {
199+
val createQuery: Query = new Query(s"""CREATE DATABASE "$database"""", database)
200+
201+
InfluxDBClient.maybeClient match {
202+
case Left(error) => Failure(new WarpConfigurationException(error))
203+
case Right(client) => Try(client.query(createQuery))
176204
}
177205
}
178206

@@ -181,9 +209,9 @@ trait InfluxDBClient extends StackTraceFilter with CorePersistenceAware {
181209
* @return a Pong object describing the deployed influxdb server
182210
*/
183211
def ping: Try[Pong] = {
184-
InfluxDBClient.client match {
185-
case None => Failure(new WarpConfigurationException(InfluxDBClient.error))
186-
case Some(client) => Try(client.ping)
212+
InfluxDBClient.maybeClient match {
213+
case Left(error) => Failure(new WarpConfigurationException(error))
214+
case Right(client) => Try(client.ping)
187215
}
188216
}
189217
}
@@ -196,15 +224,8 @@ object InfluxDBClient {
196224
private val user: String = WARP_INFLUXDB_USER.value
197225
private val password: String = WARP_INFLUXDB_PASSWORD.value
198226

199-
/** [[Option]] containing an [[InfluxDB]]. Use this to write datapoints to influxdb. */
200-
val client: Option[InfluxDB] = this.connect
201-
202-
203-
/** a simple error message containing url, user, password we attempted to connect with. */
204-
lazy val error: String = {
205-
s"unable to connect to influxdb at ${this.url} using credentials (user = ${this.user}, password = ${this.password})"
206-
}
207-
227+
/** [[Either]] containing an error message, or an [[InfluxDB]]. Use this to write datapoints to influxdb. */
228+
val maybeClient: Either[String, InfluxDB] = this.connect(this.url, this.user, this.password)
208229

209230
/**
210231
* Constructs a [[BatchPoints]].
@@ -222,14 +243,17 @@ object InfluxDBClient {
222243

223244

224245
/** @return an InfluxDB connection based on the values set in WarpProperty. */
225-
private[this] def connect: Option[InfluxDB] = {
226-
val influx: InfluxDB = InfluxDBFactory.connect(this.url, this.user, this.password)
246+
protected[influxdb] def connect(url: String, user: String, password: String): Either[String, InfluxDB] = {
247+
val influx: InfluxDB = InfluxDBFactory.connect(url, user, password)
227248

228249
Try(influx.ping) match {
229250
case Failure(exception) =>
230-
Logger.warn(this.error, exception.getMessage)
231-
None
232-
case Success(_) => Option(influx)
251+
val error: String =
252+
s"unable to connect to influxdb at $url using credentials (user = $user, password = $password)"
253+
Logger.warn(error, exception.getMessage)
254+
Left(error)
255+
case Success(_) =>
256+
Right(influx)
233257
}
234258
}
235259
}

src/test/scala/com/workday/warp/persistence/influxdb/InfluxDBClientSpec.scala

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.workday.warp.persistence.influxdb
22

3-
import java.util.Date
3+
import java.util.{Date, UUID}
44

55
import com.workday.warp.common.category.IntegrationTest
66
import com.workday.warp.common.heaphistogram.{HeapHistogram, HeapHistogramEntry}
77
import com.workday.warp.common.spec.WarpJUnitSpec
88
import com.workday.warp.persistence.{Connection, CorePersistenceAware}
99
import com.workday.warp.persistence.TablesLike.TestExecutionRowLike
1010
import com.workday.warp.persistence.TablesLike.RowTypeClasses._
11+
import org.influxdb.InfluxDB
1112
import org.influxdb.dto.Pong
1213
import org.junit.Test
1314
import org.junit.experimental.categories.Category
@@ -20,15 +21,21 @@ import scala.util.Try
2021
class InfluxDBClientSpec extends WarpJUnitSpec with CorePersistenceAware with InfluxDBClient {
2122

2223

24+
@Test
25+
@Category(Array(classOf[IntegrationTest]))
26+
def failedConnection(): Unit = {
27+
val maybeClient: Either[String, InfluxDB] = InfluxDBClient.connect("http://localhost:1234/bogus/", "dsjak", "sjk")
28+
maybeClient.isLeft should be (true)
29+
}
30+
31+
2332
/** Checks that we can establish a connection to influxdb. */
2433
@Test
2534
@Category(Array(classOf[IntegrationTest]))
2635
def testPing(): Unit = {
2736
val ping: Try[Pong] = this.ping
2837
ping.isSuccess should be (true)
2938
ping.get.getVersion should not be "unknown"
30-
31-
InfluxDBClient.error should startWith ("unable to connect to influxdb at")
3239
}
3340

3441

@@ -44,7 +51,7 @@ class InfluxDBClientSpec extends WarpJUnitSpec with CorePersistenceAware with In
4451
val histo: HeapHistogram = new HeapHistogram(List(e1, e2, e3, e4))
4552

4653
this.persistHeapHistogram(histo, "testHeapHistograms", "testSeries", "com.workday.warp.test").get
47-
this.deleteDatabase("testHeapHistograms").get
54+
this.dropDatabase("testHeapHistograms").get
4855
}
4956

5057

@@ -55,6 +62,23 @@ class InfluxDBClientSpec extends WarpJUnitSpec with CorePersistenceAware with In
5562
Connection.refresh()
5663
val testExecution: TestExecutionRowLike = this.persistenceUtils.createTestExecution(this.getTestId, new Date, 1.0, 1.5)
5764
this.persistThreshold("testResponseTimes", "testResponseTimes", testExecution).get
58-
this.deleteDatabase("testResponseTimes").get
65+
this.dropDatabase("testResponseTimes").get
66+
}
67+
68+
69+
/** Checks that we can persist response times and thresholds. */
70+
@Test
71+
@Category(Array(classOf[IntegrationTest]))
72+
def createDatabase(): Unit = {
73+
val dbName: String = s"schema-${UUID.randomUUID().toString}"
74+
75+
val exists: Boolean = this.databaseExists(dbName).get
76+
if (exists) {
77+
this.dropDatabase(dbName).get
78+
}
79+
80+
this.createDatabase(dbName).get
81+
this.databaseExists(dbName).get should be (true)
82+
this.dropDatabase(dbName).get
5983
}
6084
}

0 commit comments

Comments
 (0)