-
Notifications
You must be signed in to change notification settings - Fork 9
The Crawler now registers itself at the instance registry #24
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
Changes from 14 commits
6e6a675
7edf235
ab2bce3
bb3d605
c2c9801
3439480
bf368e8
ba109f6
c67ce57
51fe0b1
d9111f6
0d6b1a9
18a71a0
cc45162
81e8482
10fb5bc
1e8ec5e
b2458f0
c2d86be
3372a89
d66bf31
44462af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
||
| }else{ | ||
| elasticsearchInstance.iP.getOrElse("elasticsearch://localhost") + ":" + elasticsearchInstance.portnumber.getOrElse(defaultElasticSearchPort) | ||
|
||
| } | ||
| }) | ||
|
|
||
| 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")), | ||
|
||
| 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) | ||
|
|
||
|
|
@@ -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 { | ||
bhermann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case Success(id) => Some(id) | ||
| case Failure(_) => None | ||
| } | ||
|
|
||
| case class Throttle(element : Int, per : FiniteDuration, maxBurst : Int, mode : ThrottleMode) | ||
| } | ||
|
|
||
| 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) | ||
bhermann marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 ( | ||
bhermann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 |
|---|---|---|
|
|
@@ -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._ | ||
|
|
@@ -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) | ||
|
||
| Failure(e) | ||
|
|
||
| }).andThen { | ||
| case _ => client.close() | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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?