Skip to content
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

#38 http client and config #73

Merged
merged 5 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ _productMT
_nestedStructs
myTestCheckpoints

**/main/resources/application.properties
benedeki marked this conversation as resolved.
Show resolved Hide resolved
**/main/resources/application.conf
**/main/resources/application.yaml
**/main/resources/application.yml

*.config
*.keytab
Expand Down
18 changes: 18 additions & 0 deletions agent/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2021 ABSA Group Limited
#
# Licensed 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.

# dispatcher to be used (http or console)
atum.dispatcher.type="http"

# The REST API URI of the atum server
#atum.dispatcher.http.url=
cerveada marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 13 additions & 6 deletions agent/src/main/scala/za/co/absa/atum/agent/AtumAgent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,32 @@
*/

package za.co.absa.atum.agent

import com.typesafe.config.{Config, ConfigFactory}
import za.co.absa.atum.agent.dispatcher.{ConsoleDispatcher, HttpDispatcher}
import za.co.absa.atum.agent.AtumContext.AtumPartitions
import za.co.absa.atum.agent.model.MeasureResult

/**
* Place holder for the agent that communicate with the API.
* Place holder for the agent that communicate with the API.
*/
class AtumAgent private() {

val config: Config = ConfigFactory.load()

private val dispatcher = config.getString("atum.dispatcher.type") match {
case "http" => new HttpDispatcher(config.getConfig("atum.dispatcher.http"))
case "console" => new ConsoleDispatcher
case dt => throw new UnsupportedOperationException(s"Unsupported dispatcher type: '$dt''")
}

/**
* Sends a single `MeasureResult` to the AtumService API along with an extra data from a given `AtumContext`.
* @param checkpointKey
* @param atumContext
* @param measureResult
*/
def publish(checkpointKey: String, atumContext: AtumContext, measureResult: MeasureResult): Unit =
println(
s"Enqueued measurement: ${Seq(checkpointKey, atumContext, measureResult).mkString(" || ")}"
)
dispatcher.publish(checkpointKey, atumContext, measureResult)

/**
* Sends a single `MeasureResult` to the AtumService API. It doesn't involve AtumContext.
Expand All @@ -42,7 +49,7 @@ class AtumAgent private() {
* @param measureResult
*/
def measurePublish(checkpointKey: String, measureResult: MeasureResult): Unit =
println(s"Enqueued measurement: $checkpointKey || $measureResult")
dispatcher.publish(checkpointKey, measureResult)

/**
* Provides an AtumContext given a `AtumPartitions` instance. Retrieves the data from AtumService API.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed 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 za.co.absa.atum.agent.dispatcher

import org.apache.spark.internal.Logging
import za.co.absa.atum.agent.AtumContext
import za.co.absa.atum.agent.model.MeasureResult

/**
* dispatcher useful for development, testing and debugging
*/
class ConsoleDispatcher extends Dispatcher with Logging {

logInfo("using console dispatcher")

override def publish(checkpointKey: String, measureResult: MeasureResult): Unit =
println(s"Publishing $checkpointKey $measureResult")

override def publish(checkpointKey: String, context: AtumContext, measureResult: MeasureResult): Unit =
println(s"Publishing $checkpointKey $context $measureResult")

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed 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 za.co.absa.atum.agent.dispatcher

import za.co.absa.atum.agent.AtumContext
import za.co.absa.atum.agent.model.MeasureResult

trait Dispatcher {
def publish(checkpointKey: String, measureResult: MeasureResult): Unit

def publish(checkpointKey: String, context: AtumContext, measureResult: MeasureResult): Unit
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021 ABSA Group Limited
*
* Licensed 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 za.co.absa.atum.agent.dispatcher

import com.typesafe.config.Config
import org.apache.spark.internal.Logging
import za.co.absa.atum.agent.model.MeasureResult
import sttp.client3._
import sttp.model.Uri
import za.co.absa.atum.agent.AtumContext

class HttpDispatcher(config: Config) extends Dispatcher with Logging {

private val serverUri = Uri.unsafeParse(config.getString("url"))
private val backend = HttpURLConnectionBackend()

logInfo("using http dispatcher")
logInfo(s"serverUri $serverUri")

override def publish(checkpointKey: String, measureResult: MeasureResult): Unit = {
basicRequest
.body(s"$checkpointKey $measureResult")
.post(serverUri)
.send(backend)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would retries work? I never used sttp library but I quickly checked it and it looks awesome!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no direct retry support, but it could be implemented using: https://github.com/softwaremill/retry or https://github.com/hipjim/scala-retry

}

override def publish(checkpointKey: String, context: AtumContext, measureResult: MeasureResult): Unit = {
basicRequest
.body(s"$checkpointKey $context $measureResult")
.post(serverUri)
.send(backend)
}
}
14 changes: 14 additions & 0 deletions agent/src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2021 ABSA Group Limited
#
# Licensed 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.

atum.dispatcher.type="console"
25 changes: 11 additions & 14 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ object Dependencies {
springBootTest % Test,
springBootWeb,
springBootConfiguration,
springBootTomcat /*% Provided*/,
servletApi /*% Provided*/,
springBootTomcat /*% Provided*/ ,
servletApi /*% Provided*/ ,
springFoxSwagger,
springFoxSwaggerUI,
springFoxBoot,
Expand All @@ -72,22 +72,19 @@ object Dependencies {

def agentDependencies(sparkVersion: String): Seq[ModuleID] = {

val typesafeVersion = "1.4.2"
val typesafeVersion = "1.4.2"
val sparkCommonsVersion = "0.6.0"
val sparkMinorVersion = getVersionUpToMinor(sparkVersion)
val specs2CoreVersion = "4.19.2"
val sparkMinorVersion = getVersionUpToMinor(sparkVersion)
val specs2CoreVersion = "4.19.2"

Seq(
"org.apache.spark" %% "spark-core" % sparkVersion % Provided,
"org.apache.spark" %% "spark-sql" % sparkVersion % Provided,
"com.typesafe" % "config" % typesafeVersion,
"za.co.absa" %% s"spark-commons-spark${sparkMinorVersion}" % sparkCommonsVersion,
"za.co.absa" %% "spark-commons-test" % sparkCommonsVersion % Test,
"org.apache.spark" %% "spark-core" % sparkVersion % Provided,
"org.apache.spark" %% "spark-sql" % sparkVersion % Provided,
"com.typesafe" % "config" % typesafeVersion,
"za.co.absa" %% s"spark-commons-spark${sparkMinorVersion}" % sparkCommonsVersion,
"com.softwaremill.sttp.client3" %% "core" % "3.5.2",
"za.co.absa" %% "spark-commons-test" % sparkCommonsVersion % Test
)

}

}



Loading