|
| 1 | +// Copyright (C) 2018 The Delphi Team. |
| 2 | +// See the LICENCE file distributed with this work for additional |
| 3 | +// information regarding copyright ownership. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package de.upb.cs.swt.delphi.crawler.instancemanagement |
| 18 | + |
| 19 | +import java.net.InetAddress |
| 20 | + |
| 21 | +import akka.actor.ActorSystem |
| 22 | +import akka.http.scaladsl.Http |
| 23 | +import akka.http.scaladsl.marshalling.Marshal |
| 24 | +import akka.http.scaladsl.model._ |
| 25 | +import akka.http.scaladsl.unmarshalling.Unmarshal |
| 26 | +import akka.stream.ActorMaterializer |
| 27 | +import de.upb.cs.swt.delphi.crawler.{AppLogging, Configuration, Crawler} |
| 28 | +import de.upb.cs.swt.delphi.crawler.io.swagger.client.model.InstanceEnums.ComponentType |
| 29 | +import de.upb.cs.swt.delphi.crawler.io.swagger.client.model.{Instance, JsonSupport} |
| 30 | + |
| 31 | +import scala.concurrent.duration.Duration |
| 32 | +import scala.concurrent.{Await, ExecutionContext, Future} |
| 33 | +import scala.util.{Failure, Success, Try} |
| 34 | + |
| 35 | +object InstanceRegistry extends JsonSupport with AppLogging |
| 36 | +{ |
| 37 | + |
| 38 | + implicit val system : ActorSystem = Crawler.system |
| 39 | + implicit val ec : ExecutionContext = system.dispatcher |
| 40 | + implicit val materializer : ActorMaterializer = Crawler.materializer |
| 41 | + |
| 42 | + |
| 43 | + def register(configuration: Configuration) : Try[Long] = { |
| 44 | + val instance = createInstance(None,configuration.controlServerPort, configuration.instanceName) |
| 45 | + |
| 46 | + Await.result(postInstance(instance, configuration.instanceRegistryUri + "/register") map {response => |
| 47 | + if(response.status == StatusCodes.OK){ |
| 48 | + Await.result(Unmarshal(response.entity).to[String] map { assignedID => |
| 49 | + val id = assignedID.toLong |
| 50 | + log.info(s"Successfully registered at Instance Registry, got ID $id.") |
| 51 | + Success(id) |
| 52 | + } recover { case ex => |
| 53 | + log.warning(s"Failed to read assigned ID from Instance Registry, exception: $ex") |
| 54 | + Failure(ex) |
| 55 | + }, Duration.Inf) |
| 56 | + } |
| 57 | + else { |
| 58 | + val statuscode = response.status |
| 59 | + log.warning(s"Failed to register at Instance Registry, server returned $statuscode") |
| 60 | + Failure(new RuntimeException(s"Failed to register at Instance Registry, server returned $statuscode")) |
| 61 | + } |
| 62 | + |
| 63 | + } recover {case ex => |
| 64 | + log.warning(s"Failed to register at Instance Registry, exception: $ex") |
| 65 | + Failure(ex) |
| 66 | + }, Duration.Inf) |
| 67 | + } |
| 68 | + |
| 69 | + def retrieveElasticSearchInstance(configuration: Configuration) : Try[Instance] = { |
| 70 | + if(!configuration.usingInstanceRegistry) { |
| 71 | + Failure(new RuntimeException("Cannot get ElasticSearch instance from Instance Registry, no Instance Registry available.")) |
| 72 | + } else { |
| 73 | + val request = HttpRequest(method = HttpMethods.GET, configuration.instanceRegistryUri + "/matchingInstance?ComponentType=ElasticSearch") |
| 74 | + |
| 75 | + Await.result(Http(system).singleRequest(request) map {response => |
| 76 | + response.status match { |
| 77 | + case StatusCodes.OK => |
| 78 | + Await.result(Unmarshal(response.entity).to[Instance] map {instance => |
| 79 | + val elasticIP = instance.host |
| 80 | + log.info(s"Instance Registry assigned ElasticSearch instance at $elasticIP") |
| 81 | + Success(instance) |
| 82 | + } recover {case ex => |
| 83 | + log.warning(s"Failed to read response from Instance Registry, exception: $ex") |
| 84 | + Failure(ex) |
| 85 | + }, Duration.Inf) |
| 86 | + case StatusCodes.NotFound => |
| 87 | + log.warning(s"No matching instance of type 'ElasticSearch' is present at the instance registry.") |
| 88 | + Failure(new RuntimeException(s"Instance Registry did not contain matching instance, server returned ${StatusCodes.NotFound}")) |
| 89 | + case _ => |
| 90 | + val status = response.status |
| 91 | + log.warning(s"Failed to read matching instance from Instance Registry, server returned $status") |
| 92 | + Failure(new RuntimeException(s"Failed to read matching instance from Instance Registry, server returned $status")) |
| 93 | + } |
| 94 | + } recover { case ex => |
| 95 | + log.warning(s"Failed to request ElasticSearch instance from Instance Registry, exception: $ex ") |
| 96 | + Failure(ex) |
| 97 | + }, Duration.Inf) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + def sendMatchingResult(isElasticSearchReachable : Boolean, configuration: Configuration) : Try[Unit] = { |
| 102 | + if(!configuration.usingInstanceRegistry) { |
| 103 | + Failure(new RuntimeException("Cannot post matching result to Instance Registry, no Instance Registry available.")) |
| 104 | + } else { |
| 105 | + if(configuration.elasticsearchInstance.id.isEmpty) { |
| 106 | + Failure(new RuntimeException("The ElasticSearch instance was not assigned by the Instance Registry, so no matching result will be posted.")) |
| 107 | + } else { |
| 108 | + val idToPost = configuration.elasticsearchInstance.id.getOrElse(-1L) |
| 109 | + val request = HttpRequest( |
| 110 | + method = HttpMethods.POST, |
| 111 | + configuration.instanceRegistryUri + s"/matchingResult?Id=$idToPost&MatchingSuccessful=$isElasticSearchReachable") |
| 112 | + |
| 113 | + Await.result(Http(system).singleRequest(request) map {response => |
| 114 | + if(response.status == StatusCodes.OK){ |
| 115 | + log.info("Successfully posted matching result to Instance Registry.") |
| 116 | + Success() |
| 117 | + } |
| 118 | + else { |
| 119 | + val statuscode = response.status |
| 120 | + log.warning(s"Failed to post matching result to Instance Registry, server returned $statuscode") |
| 121 | + Failure(new RuntimeException(s"Failed to post matching result to Instance Registry, server returned $statuscode")) |
| 122 | + } |
| 123 | + |
| 124 | + } recover {case ex => |
| 125 | + log.warning(s"Failed to post matching result to Instance Registry, exception: $ex") |
| 126 | + Failure(new RuntimeException(s"Failed to post matching result tot Instance Registry, exception: $ex")) |
| 127 | + }, Duration.Inf) |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + def deregister(configuration: Configuration) : Try[Unit] = { |
| 134 | + if(!configuration.usingInstanceRegistry){ |
| 135 | + Failure(new RuntimeException("Cannot deregister from Instance Registry, no Instance Registry available.")) |
| 136 | + } else { |
| 137 | + val id : Long = configuration.assignedID.getOrElse(-1L) |
| 138 | + |
| 139 | + val request = HttpRequest(method = HttpMethods.POST, configuration.instanceRegistryUri + s"/deregister?Id=$id") |
| 140 | + |
| 141 | + Await.result(Http(system).singleRequest(request) map {response => |
| 142 | + if(response.status == StatusCodes.OK){ |
| 143 | + log.info("Successfully deregistered from Instance Registry.") |
| 144 | + Success() |
| 145 | + } |
| 146 | + else { |
| 147 | + val statuscode = response.status |
| 148 | + log.warning(s"Failed to deregister from Instance Registry, server returned $statuscode") |
| 149 | + Failure(new RuntimeException(s"Failed to deregister from Instance Registry, server returned $statuscode")) |
| 150 | + } |
| 151 | + |
| 152 | + } recover {case ex => |
| 153 | + log.warning(s"Failed to deregister to Instance Registry, exception: $ex") |
| 154 | + Failure(ex) |
| 155 | + }, Duration.Inf) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + def postInstance(instance : Instance, uri: String) () : Future[HttpResponse] = |
| 160 | + Marshal(instance).to[RequestEntity] flatMap { entity => |
| 161 | + val request = HttpRequest(method = HttpMethods.POST, uri = uri, entity = entity) |
| 162 | + Http(system).singleRequest(request) |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | + private def createInstance(id: Option[Long], controlPort : Int, name : String) : Instance = |
| 167 | + Instance(id, InetAddress.getLocalHost.getHostAddress, controlPort, name, ComponentType.Crawler) |
| 168 | +} |
0 commit comments