-
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
Merged
Merged
Changes from all 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
7edf235
ElasticSearch instance can now be set to the one responded by th IR
ab2bce3
Included client API code from swagger to connect to IR
bb3d605
Used akka http to call IR instead of outdated spray http
c2c9801
Removed unused swagger code, used logger where println was used
3439480
Crawler can now get elastic search ip from IR
bf368e8
Cleaned up unused dependencies, fixed some minor bugs
ba109f6
Crawler now reads its assigned IP after registration at the Instance …
c67ce57
Made posting matching-result work by storing the matched ElasticSearc…
51fe0b1
Crawler is now deregistering itself from the Instance Registry on shu…
d9111f6
Merge remote-tracking branch 'origin/develop' into feature/instancere…
0d6b1a9
Code style cleanup
18a71a0
Restored setting of Hermes config, fails on Linux and Windows
cc45162
CodeStyle: Replaced .get with .getOrElse
81e8482
Moved default host to val, removed unused resolver/unnecessary condition
10fb5bc
Made class 'Instance' not use Options anymore
1e8ec5e
Fixed shutdown hook not being triggered, fixed port of IR
b2458f0
Better handling of getMatchingInstance returning 404
c2d86be
Adapted IR communication to use new attribute names
3372a89
CodeStyle: Replaced if-else if with match-case
d66bf31
Merge branch 'develop' into feature/instanceregistry
44462af
Fixed merge error (missing comma)
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/main/scala/de/upb/cs/swt/delphi/crawler/instancemanagement/InstanceRegistry.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| // 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 => | ||
| response.status match { | ||
| case StatusCodes.OK => | ||
| Await.result(Unmarshal(response.entity).to[Instance] map {instance => | ||
| val elasticIP = instance.host | ||
| log.info(s"Instance Registry assigned ElasticSearch instance at $elasticIP") | ||
| Success(instance) | ||
| } recover {case ex => | ||
| log.warning(s"Failed to read response from Instance Registry, exception: $ex") | ||
| Failure(ex) | ||
| }, Duration.Inf) | ||
| case StatusCodes.NotFound => | ||
| log.warning(s"No matching instance of type 'ElasticSearch' is present at the instance registry.") | ||
| Failure(new RuntimeException(s"Instance Registry did not contain matching instance, server returned ${StatusCodes.NotFound}")) | ||
| case _ => | ||
| val status = response.status | ||
| log.warning(s"Failed to read matching instance from Instance Registry, server returned $status") | ||
| Failure(new RuntimeException(s"Failed to read matching instance 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("The ElasticSearch instance was not assigned by the Instance Registry, so no matching result will be posted.")) | ||
| } 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, InetAddress.getLocalHost.getHostAddress, controlPort, name, ComponentType.Crawler) | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/main/scala/de/upb/cs/swt/delphi/crawler/io/swagger/client/model/Instance.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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], | ||
| host: String, | ||
| portNumber: Int, | ||
| name: String, | ||
| /* Component Type */ | ||
| componentType: 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") | ||
| } | ||
|
|
||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.