Skip to content

Commit

Permalink
AMBARI-22192. Setup an application server for hosting the AD System M…
Browse files Browse the repository at this point in the history
…anager.
  • Loading branch information
swagle authored and avijayanhwx committed Apr 1, 2018
1 parent 8b8d912 commit f2c342c
Show file tree
Hide file tree
Showing 18 changed files with 855 additions and 210 deletions.
569 changes: 361 additions & 208 deletions ambari-metrics-anomaly-detection-service/pom.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
server:
applicationConnectors:
- type: http
port: 9999
adminConnectors:
- type: http
port: 9990
requestLog:
type: external

logging:
type: external
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.app

import javax.ws.rs.Path
import javax.ws.rs.container.{ContainerRequestFilter, ContainerResponseFilter}

import org.apache.ambari.metrics.adservice.app.GuiceInjector.{withInjector, wrap}
import org.glassfish.jersey.filter.LoggingFilter

import com.codahale.metrics.health.HealthCheck
import com.fasterxml.jackson.databind.{ObjectMapper, SerializationFeature}
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import io.dropwizard.Application
import io.dropwizard.setup.Environment

class AnomalyDetectionApp extends Application[AnomalyDetectionAppConfig] {
override def getName = "anomaly-detection-service"

override def run(t: AnomalyDetectionAppConfig, env: Environment): Unit = {
configure(t, env)
}

def configure(config: AnomalyDetectionAppConfig, env: Environment) {
withInjector(new AnomalyDetectionAppModule(config, env)) { injector =>
injector.instancesWithAnnotation(classOf[Path]).foreach { r => env.jersey().register(r) }
injector.instancesOfType(classOf[HealthCheck]).foreach { h => env.healthChecks.register(h.getClass.getName, h) }
injector.instancesOfType(classOf[ContainerRequestFilter]).foreach { f => env.jersey().register(f) }
injector.instancesOfType(classOf[ContainerResponseFilter]).foreach { f => env.jersey().register(f) }
}
env.jersey.register(jacksonJaxbJsonProvider)
env.jersey.register(new LoggingFilter)
}

private def jacksonJaxbJsonProvider: JacksonJaxbJsonProvider = {
val provider = new JacksonJaxbJsonProvider()
val objectMapper = new ObjectMapper()
objectMapper.registerModule(DefaultScalaModule)
objectMapper.registerModule(new JodaModule)
objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false)
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
objectMapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, true)
provider.setMapper(objectMapper)
provider
}
}


object AnomalyDetectionApp {
def main(args: Array[String]): Unit = new AnomalyDetectionApp().run(args: _*)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.ambari.metrics.adservice.app

import io.dropwizard.Configuration

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
class AnomalyDetectionAppConfig extends Configuration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.app

import org.apache.ambari.metrics.adservice.resource.{AnomalyResource, RootResource}
import org.apache.ambari.metrics.adservice.service.{ADQueryService, ADQueryServiceImpl}

import com.codahale.metrics.health.HealthCheck
import com.google.inject.AbstractModule
import com.google.inject.multibindings.Multibinder
import io.dropwizard.setup.Environment

class AnomalyDetectionAppModule(config: AnomalyDetectionAppConfig, env: Environment) extends AbstractModule {
override def configure() {
bind(classOf[AnomalyDetectionAppConfig]).toInstance(config)
bind(classOf[Environment]).toInstance(env)
val healthCheckBinder = Multibinder.newSetBinder(binder(), classOf[HealthCheck])
healthCheckBinder.addBinding().to(classOf[DefaultHealthCheck])
bind(classOf[AnomalyResource])
bind(classOf[RootResource])
bind(classOf[ADQueryService]).to(classOf[ADQueryServiceImpl])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.app

import com.codahale.metrics.health.HealthCheck
import com.codahale.metrics.health.HealthCheck.Result

class DefaultHealthCheck extends HealthCheck {
override def check(): Result = Result.healthy()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.app

import java.lang.annotation.Annotation

import com.google.inject.{Guice, Injector, Module, TypeLiteral}

import scala.collection.JavaConversions._
import scala.language.implicitConversions
import scala.reflect._

object GuiceInjector {

def withInjector(modules: Module*)(fn: (Injector) => Unit) = {
val injector = Guice.createInjector(modules.toList: _*)
fn(injector)
}

implicit def wrap(injector: Injector): InjectorWrapper = new InjectorWrapper(injector)
}

class InjectorWrapper(injector: Injector) {
def instancesWithAnnotation[T <: Annotation](annotationClass: Class[T]): List[AnyRef] = {
injector.getAllBindings.filter { case (k, v) =>
!k.getTypeLiteral.getRawType.getAnnotationsByType[T](annotationClass).isEmpty
}.map { case (k, v) => injector.getInstance(k).asInstanceOf[AnyRef] }.toList
}

def instancesOfType[T: ClassTag](typeClass: Class[T]): List[T] = {
injector.findBindingsByType(TypeLiteral.get(classTag[T].runtimeClass)).map { b =>
injector.getInstance(b.getKey).asInstanceOf[T]
}.toList
}

def dumpBindings(): Unit = {
injector.getBindings.keySet() foreach { k =>
println(s"bind key = ${k.toString}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object PhoenixQueryConstants {
"METHOD_NAME VARCHAR, " +
"METHOD_TYPE VARCHAR, " +
"PARAMETERS VARCHAR " +
"SNAPSHOT_TIME UNSIGNED LONG NOT NULL "
"SNAPSHOT_TIME UNSIGNED LONG NOT NULL " +
"CONSTRAINT pk PRIMARY KEY (METRIC_UUID, METHOD_NAME)) " +
"DATA_BLOCK_ENCODING='FAST_DIFF', IMMUTABLE_ROWS=true, COMPRESSION='SNAPPY'"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.resource

import javax.ws.rs.{GET, Path, Produces}
import javax.ws.rs.core.Response
import javax.ws.rs.core.MediaType.APPLICATION_JSON

import org.joda.time.DateTime

@Path("/topNAnomalies")
class AnomalyResource {

@GET
@Produces(Array(APPLICATION_JSON))
def default: Response = {
Response.ok.entity(Map("message" -> "Anomaly Detection Service!",
"today" -> DateTime.now.toString("MM-dd-yyyy hh:mm"))).build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.resource

import javax.ws.rs.{GET, Path, Produces}
import javax.ws.rs.core.Response
import javax.ws.rs.core.MediaType.APPLICATION_JSON

import org.joda.time.DateTime

@Path("/")
class RootResource {

@Produces(Array(APPLICATION_JSON))
@GET
def default: Response = {
Response.ok.entity(Map("name" -> "anomaly-detection-service", "today" -> DateTime.now.toString("MM-dd-yyyy hh:mm"))).build()
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.service

trait ADQueryService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ambari.metrics.adservice.service

class ADQueryServiceImpl extends ADQueryService {

}
Loading

0 comments on commit f2c342c

Please sign in to comment.