Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6e6a675
Implemented a Preflight check connecting to the Instance Registry
Aug 1, 2018
7edf235
ElasticSearch instance can now be set to the one responded by th IR
Aug 7, 2018
ab2bce3
Included client API code from swagger to connect to IR
Aug 21, 2018
bb3d605
Used akka http to call IR instead of outdated spray http
Aug 28, 2018
c2c9801
Removed unused swagger code, used logger where println was used
Aug 28, 2018
3439480
Crawler can now get elastic search ip from IR
Aug 28, 2018
bf368e8
Cleaned up unused dependencies, fixed some minor bugs
Sep 1, 2018
ba109f6
Crawler now reads its assigned IP after registration at the Instance …
Sep 1, 2018
c67ce57
Made posting matching-result work by storing the matched ElasticSearc…
Sep 2, 2018
51fe0b1
Crawler is now deregistering itself from the Instance Registry on shu…
Sep 4, 2018
d9111f6
Merge remote-tracking branch 'origin/develop' into feature/instancere…
Sep 4, 2018
0d6b1a9
Code style cleanup
Sep 5, 2018
18a71a0
Restored setting of Hermes config, fails on Linux and Windows
Sep 7, 2018
cc45162
CodeStyle: Replaced .get with .getOrElse
Sep 7, 2018
81e8482
Moved default host to val, removed unused resolver/unnecessary condition
Sep 8, 2018
10fb5bc
Made class 'Instance' not use Options anymore
Sep 9, 2018
1e8ec5e
Fixed shutdown hook not being triggered, fixed port of IR
Sep 9, 2018
b2458f0
Better handling of getMatchingInstance returning 404
Sep 12, 2018
c2d86be
Adapted IR communication to use new attribute names
Sep 12, 2018
3372a89
CodeStyle: Replaced if-else if with match-case
Sep 14, 2018
d66bf31
Merge branch 'develop' into feature/instanceregistry
Sep 19, 2018
44462af
Fixed merge error (missing comma)
Sep 19, 2018
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
6 changes: 5 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-stream-testkit" % akkaVersion % Test,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"com.typesafe.akka" %% "akka-http" % "10.1.4"
"com.typesafe.akka" %% "akka-http" % "10.1.4",
"com.typesafe.akka" %% "akka-http-spray-json" % "10.0.8"
)

libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.5.3"

libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3" % Runtime

val elastic4sVersion = "6.3.0"
Expand All @@ -70,6 +73,7 @@ libraryDependencies ++= Seq(
)

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
resolvers ++= Seq(Resolver.mavenLocal)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this local resolver?


val opalVersion = "1.0.0"
libraryDependencies ++= Seq(
Expand Down
24 changes: 24 additions & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
io.swagger.client {

apiRequest {

compression {
enabled: false
size-threshold: 0
}

trust-certificates: true

connection-timeout: 5000ms

default-headers {
"userAgent": "swagger-client_1.0.0"
}

// let you define custom http status code, as in :
// { code: 601, reason: "some custom http status code", success: false }
custom-codes : []
}
}

spray.can.host-connector.max-redirects = 10
40 changes: 38 additions & 2 deletions src/main/scala/de/upb/cs/swt/delphi/crawler/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,36 @@ import java.net.URI

import akka.stream.ThrottleMode
import com.sksamuel.elastic4s.ElasticsearchClientUri
import de.upb.cs.swt.delphi.crawler.instancemanagement.InstanceRegistry
import de.upb.cs.swt.delphi.crawler.io.swagger.client.model.Instance
import de.upb.cs.swt.delphi.crawler.io.swagger.client.model.InstanceEnums.ComponentType

import scala.concurrent.duration._
import scala.util.{Failure, Success}

class Configuration {

lazy val elasticsearchClientUri: ElasticsearchClientUri = ElasticsearchClientUri({
if(elasticsearchInstance.portnumber.isEmpty){
elasticsearchInstance.iP.getOrElse("elasticsearch://localhost:" + defaultElasticSearchPort)
Copy link
Member

Choose a reason for hiding this comment

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

Why is localhost hard coded here?
At first glance this looks a bit strange.

}else{
elasticsearchInstance.iP.getOrElse("elasticsearch://localhost") + ":" + elasticsearchInstance.portnumber.getOrElse(defaultElasticSearchPort)
Copy link
Member

Choose a reason for hiding this comment

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

Here too

}
})

lazy val elasticsearchInstance : Instance = InstanceRegistry.retrieveElasticSearchInstance(this) match {
case Success(instance) => instance
case Failure(_) => Instance(
None,
Some(sys.env.getOrElse("DELPHI_ELASTIC_URI","elasticsearch://localhost")),
Copy link
Member

Choose a reason for hiding this comment

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

In the original code there was a port given. Does it work without giving it the default port?

Some(defaultElasticSearchPort),
Some("Default ElasticSearch instance"),
Some(ComponentType.ElasticSearch))
}

class Configuration {
val elasticsearchClientUri: ElasticsearchClientUri = ElasticsearchClientUri(sys.env.getOrElse("DELPHI_ELASTIC_URI","elasticsearch://localhost:9200"))
val mavenRepoBase: URI = new URI("http://repo1.maven.org/maven2/") // TODO: Create a local demo server "http://localhost:8881/maven2/"
val controlServerPort : Int = 8882
val defaultElasticSearchPort : Int = 9200
val limit : Int = 50
val throttle : Throttle = Throttle(5, 30 second, 5, ThrottleMode.shaping)

Expand All @@ -35,6 +58,19 @@ class Configuration {
val elasticActorPoolSize : Int = 4
val callGraphStreamPoolSize : Int = 4

val instanceName = "MyCrawlerInstance"
val instanceRegistryUri : String = sys.env.getOrElse("DELPHI_IR_URI", "http://localhost:8085")

lazy val usingInstanceRegistry : Boolean = assignedID match {
case Some(_) => true
case None => false
}

lazy val assignedID : Option[Long] = InstanceRegistry.register(this) match {
case Success(id) => Some(id)
case Failure(_) => None
}

case class Throttle(element : Int, per : FiniteDuration, maxBurst : Int, mode : ThrottleMode)
}

3 changes: 3 additions & 0 deletions src/main/scala/de/upb/cs/swt/delphi/crawler/Crawler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.sksamuel.elastic4s.http.ElasticClient
import de.upb.cs.swt.delphi.crawler.control.Server
import de.upb.cs.swt.delphi.crawler.discovery.maven.MavenCrawlActor
import de.upb.cs.swt.delphi.crawler.discovery.maven.MavenCrawlActor.Start
import de.upb.cs.swt.delphi.crawler.instancemanagement.InstanceRegistry
import de.upb.cs.swt.delphi.crawler.preprocessing.PreprocessingDispatchActor
import de.upb.cs.swt.delphi.crawler.processing.{HermesActor, HermesAnalyzer, ProcessingDispatchActor}
import de.upb.cs.swt.delphi.crawler.storage.ElasticActor
Expand All @@ -46,6 +47,7 @@ object Crawler extends App with AppLogging {

sys.addShutdownHook(() => {
log.warning("Received shutdown signal.")
InstanceRegistry.deregister(configuration)
val future = system.terminate()
Await.result(future, 120.seconds)
})
Expand All @@ -55,6 +57,7 @@ object Crawler extends App with AppLogging {
Startup.preflightCheck(configuration) match {
case Success(c) =>
case Failure(e) => {
InstanceRegistry.deregister(configuration)
system.terminate()
sys.exit(1)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Copyright (C) 2018 The Delphi Team.
// See the LICENCE file distributed with this work for additional
// information regarding copyright ownership.
//
// 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 de.upb.cs.swt.delphi.crawler.instancemanagement

import java.net.InetAddress

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import de.upb.cs.swt.delphi.crawler.{AppLogging, Configuration, Crawler}
import de.upb.cs.swt.delphi.crawler.io.swagger.client.model.InstanceEnums.ComponentType
import de.upb.cs.swt.delphi.crawler.io.swagger.client.model.{Instance, JsonSupport}

import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

object InstanceRegistry extends JsonSupport with AppLogging
{

implicit val system : ActorSystem = Crawler.system
implicit val ec : ExecutionContext = system.dispatcher
implicit val materializer : ActorMaterializer = Crawler.materializer


def register(configuration: Configuration) : Try[Long] = {
val instance = createInstance(None,configuration.controlServerPort, configuration.instanceName)

Await.result(postInstance(instance, configuration.instanceRegistryUri + "/register") map {response =>
if(response.status == StatusCodes.OK){
Await.result(Unmarshal(response.entity).to[String] map { assignedID =>
val id = assignedID.toLong
log.info(s"Successfully registered at Instance Registry, got ID $id.")
Success(id)
} recover { case ex =>
log.warning(s"Failed to read assigned ID from Instance Registry, exception: $ex")
Failure(ex)
}, Duration.Inf)
}
else {
val statuscode = response.status
log.warning(s"Failed to register at Instance Registry, server returned $statuscode")
Failure(new RuntimeException(s"Failed to register at Instance Registry, server returned $statuscode"))
}

} recover {case ex =>
log.warning(s"Failed to register at Instance Registry, exception: $ex")
Failure(ex)
}, Duration.Inf)
}

def retrieveElasticSearchInstance(configuration: Configuration) : Try[Instance] = {
if(!configuration.usingInstanceRegistry) {
Failure(new RuntimeException("Cannot get ElasticSearch instance from Instance Registry, no Instance Registry available."))
} else {
val request = HttpRequest(method = HttpMethods.GET, configuration.instanceRegistryUri + "/matchingInstance?ComponentType=ElasticSearch")

Await.result(Http(system).singleRequest(request) map {response =>
val status = response.status
if(status == StatusCodes.OK) {

Await.result(Unmarshal(response.entity).to[Instance] map {instance =>
val elasticIP = instance.iP
log.info(s"Instance Registry assigned ElasticSearch instance at ${elasticIP.getOrElse("None")}")
Success(instance)
} recover {case ex =>
log.warning(s"Failed to read response from Instance Registry, exception: $ex")
Failure(ex)
}, Duration.Inf)
}
else{
log.warning(s"Failed to read response from Instance Registry, server returned $status")
Failure(new RuntimeException(s"Failed to read response from Instance Registry, server returned $status"))
}
} recover { case ex =>
log.warning(s"Failed to request ElasticSearch instance from Instance Registry, exception: $ex ")
Failure(ex)
}, Duration.Inf)
}
}

def sendMatchingResult(isElasticSearchReachable : Boolean, configuration: Configuration) : Try[Unit] = {
if(!configuration.usingInstanceRegistry) {
Failure(new RuntimeException("Cannot post matching result to Instance Registry, no Instance Registry available."))
} else {
if(configuration.elasticsearchInstance.iD.isEmpty) {
Failure(new RuntimeException("Cannot post matching result to Instance Registry, assigned ElasticSearch instance has no ID."))
} else {
val idToPost = configuration.elasticsearchInstance.iD.getOrElse(-1L)
val request = HttpRequest(
method = HttpMethods.POST,
configuration.instanceRegistryUri + s"/matchingResult?Id=$idToPost&MatchingSuccessful=$isElasticSearchReachable")

Await.result(Http(system).singleRequest(request) map {response =>
if(response.status == StatusCodes.OK){
log.info("Successfully posted matching result to Instance Registry.")
Success()
}
else {
val statuscode = response.status
log.warning(s"Failed to post matching result to Instance Registry, server returned $statuscode")
Failure(new RuntimeException(s"Failed to post matching result to Instance Registry, server returned $statuscode"))
}

} recover {case ex =>
log.warning(s"Failed to post matching result to Instance Registry, exception: $ex")
Failure(new RuntimeException(s"Failed to post matching result tot Instance Registry, exception: $ex"))
}, Duration.Inf)
}
}

}

def deregister(configuration: Configuration) : Try[Unit] = {
if(!configuration.usingInstanceRegistry){
Failure(new RuntimeException("Cannot deregister from Instance Registry, no Instance Registry available."))
} else {
val id : Long = configuration.assignedID.getOrElse(-1L)

val request = HttpRequest(method = HttpMethods.POST, configuration.instanceRegistryUri + s"/deregister?Id=$id")

Await.result(Http(system).singleRequest(request) map {response =>
if(response.status == StatusCodes.OK){
log.info("Successfully deregistered from Instance Registry.")
Success()
}
else {
val statuscode = response.status
log.warning(s"Failed to deregister from Instance Registry, server returned $statuscode")
Failure(new RuntimeException(s"Failed to deregister from Instance Registry, server returned $statuscode"))
}

} recover {case ex =>
log.warning(s"Failed to deregister to Instance Registry, exception: $ex")
Failure(ex)
}, Duration.Inf)
}
}

def postInstance(instance : Instance, uri: String) () : Future[HttpResponse] =
Marshal(instance).to[RequestEntity] flatMap { entity =>
val request = HttpRequest(method = HttpMethods.POST, uri = uri, entity = entity)
Http(system).singleRequest(request)
}


private def createInstance(id: Option[Long], controlPort : Int, name : String) : Instance =
Instance(id, Option(InetAddress.getLocalHost.getHostAddress), Option(controlPort), Option(name), Option(ComponentType.Crawler))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* NOTE: This class is auto generated by the akka-scala (beta) swagger code generator program.
* https://github.com/swagger-api/swagger-codegen
* For any issue or feedback, please open a ticket via https://github.com/swagger-api/swagger-codegen/issues/new
*/

package de.upb.cs.swt.delphi.crawler.io.swagger.client.model

import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._

trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val componentTypeFormat = new JsonFormat[InstanceEnums.ComponentType] {
def write(compType : InstanceEnums.ComponentType) = JsString(compType.toString)

def read(value: JsValue) = value match {
case JsString(s) => s match {
case "Crawler" => InstanceEnums.ComponentType.Crawler
case "WebApi" => InstanceEnums.ComponentType.WebApi
case "WebApp" => InstanceEnums.ComponentType.WebApp
case "DelphiManagement" => InstanceEnums.ComponentType.DelphiManagement
case "ElasticSearch" => InstanceEnums.ComponentType.ElasticSearch
case x => throw new RuntimeException(s"Unexpected string value $x for component type.")
}
case y => throw new RuntimeException(s"Unexpected type $y while deserializing component type.")
}
}
implicit val instanceFormat = jsonFormat5(Instance)
}

final case class Instance (
iD: Option[Long],
iP: Option[String],
portnumber: Option[Long],
name: Option[String],
/* Component Type */
componentType: Option[InstanceEnums.ComponentType]
)

object InstanceEnums {

type ComponentType = ComponentType.Value
object ComponentType extends Enumeration {
val Crawler = Value("Crawler")
val WebApi = Value("WebApi")
val WebApp = Value("WebApp")
val DelphiManagement = Value("DelphiManagement")
val ElasticSearch = Value("ElasticSearch")
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package de.upb.cs.swt.delphi.crawler.storage

import akka.actor.ActorSystem
import com.sksamuel.elastic4s.http.ElasticClient
import com.sksamuel.elastic4s.http.ElasticDsl._
import com.sksamuel.elastic4s.http.ElasticClient
import de.upb.cs.swt.delphi.crawler.instancemanagement.InstanceRegistry
import de.upb.cs.swt.delphi.crawler.{Configuration, PreflightCheck}

import scala.concurrent.duration._
Expand All @@ -44,8 +45,14 @@ object ElasticReachablePreflightCheck extends PreflightCheck {

val f = (client.execute {
nodeInfo()
} map { i => Success(configuration)
} recover { case e => Failure(e)
} map { i => {
if(configuration.usingInstanceRegistry) InstanceRegistry.sendMatchingResult(isElasticSearchReachable = true, configuration)
Success(configuration)
}
} recover { case e =>
if(configuration.usingInstanceRegistry) InstanceRegistry.sendMatchingResult(isElasticSearchReachable = false, configuration)
Copy link
Member

Choose a reason for hiding this comment

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

Having the condition at all places where it is used can be dangerous.
Could we always make the call and decide this in the method? (or through inheritance)

Failure(e)

}).andThen {
case _ => client.close()
}
Expand Down