Initial working ExporingService, plugin + an ESExport implementation

Original commit: elastic/x-pack-elasticsearch@982144e308
This commit is contained in:
Boaz Leskes 2013-05-22 11:12:09 +02:00
parent 5ee82e4ae7
commit 6c56fe1110
8 changed files with 442 additions and 0 deletions

5
NOTICE.txt Normal file
View File

@ -0,0 +1,5 @@
ElasticSearch
Copyright 2009-2013 ElasticSearch
This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).

0
README.md Normal file
View File

141
pom.xml Normal file
View File

@ -0,0 +1,141 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<name>elasticsearch-dash</name>
<modelVersion>4.0.0</modelVersion>
<groupId>com.elasticsearch</groupId>
<artifactId>elasticsearch-dash</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<description>Management &amp; monitoring plugin for ElasticSearch</description>
<inceptionYear>2013</inceptionYear>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:git:git@github.com:elasticsearch/elasticsearch-m-m.git</connection>
<developerConnection>scm:git:git@github.com:elasticsearch/elasticsearch-m-m.git
</developerConnection>
<url>http://github.com/elasticsearch/elasticsearch-m-m</url>
</scm>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
<properties>
<elasticsearch.version>1.0.0.Beta1-SNAPSHOT</elasticsearch.version>
</properties>
<repositories>
<repository>
<id>sonatype</id>
<url>http://oss.sonatype.org/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<outputDirectory>${project.build.directory}/releases/</outputDirectory>
<descriptors>
<descriptor>${basedir}/src/main/assemblies/plugin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package com.elasticsearch.dash;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.common.component.CloseableComponent;
import org.elasticsearch.common.component.LifecycleComponent;
public interface Exporter<T> extends LifecycleComponent<T> {
String name();
void exportNodeStats(NodeStats nodeStats);
}

View File

@ -0,0 +1,101 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package com.elasticsearch.dash;/*
import com.elasticsearch.dash.exporters.ESExporter;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.cluster.ClusterName;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.node.service.NodeService;
import org.elasticsearch.threadpool.ThreadPool;
import java.util.Collection;
public class ExportersService extends AbstractLifecycleComponent<ExportersService> {
private final IndicesService indicesService;
private final NodeService nodeService;
private volatile ExportingWorker exp;
private volatile Thread thread;
private final TimeValue interval;
private Collection<Exporter> exporters;
@Inject
public ExportersService(Settings settings, IndicesService indicesService,ClusterName clusterName, NodeService nodeService) {
super(settings);
this.indicesService = indicesService;
this.nodeService = nodeService;
this.interval = componentSettings.getAsTime("interval", TimeValue.timeValueSeconds(5));
Exporter esExporter = new ESExporter(settings.getComponentSettings(ESExporter.class), clusterName );
this.exporters = ImmutableSet.of(esExporter);
}
@Override
protected void doStart() throws ElasticSearchException {
for (Exporter e: exporters)
e.start();
this.exp = new ExportingWorker();
this.thread = new Thread(exp, EsExecutors.threadName(settings, "dash"));
this.thread.setDaemon(true);
this.thread.start();
}
@Override
protected void doStop() throws ElasticSearchException {
this.exp.closed = true;
this.thread.interrupt();
for (Exporter e: exporters)
e.stop();
}
@Override
protected void doClose() throws ElasticSearchException {
for (Exporter e: exporters)
e.close();
}
class ExportingWorker implements Runnable {
volatile boolean closed;
@Override
public void run() {
while (!closed) {
// do the actual export..., go over the actual exporters list and...
try {
logger.debug("Collecting node stats");
NodeStats nodeStats = nodeService.stats();
logger.debug("Exporting node stats");
for (Exporter e: exporters) {
try {
e.exportNodeStats(nodeStats);
}
catch (Throwable t){
logger.error("Exporter {} has thrown an exception:", t, e.name());
}
}
Thread.sleep(interval.millis());
} catch (InterruptedException e) {
// ignore, if closed, good....
} catch (Throwable t) {
logger.error("Background thread had an uncaught exception:", t);
}
}
}
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package com.elasticsearch.dash;
import com.google.common.collect.ImmutableList;
import org.elasticsearch.common.component.LifecycleComponent;
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.Module;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.plugins.AbstractPlugin;
import java.util.ArrayList;
import java.util.Collection;
public class Plugin extends AbstractPlugin {
public Plugin() {
}
@Override
public String name() {
return "Dash";
}
@Override
public String description() {
return "Monitoring with an elastic sauce";
}
@Override
public Collection<Module> modules(Settings settings) {
Module m = new AbstractModule() {
@Override
protected void configure() {
bind(ExportersService.class).asEagerSingleton();
}
};
return ImmutableList.of(m);
}
@Override
public Collection<Class<? extends LifecycleComponent>> services() {
Collection<Class<? extends LifecycleComponent>> l = new ArrayList<Class<? extends LifecycleComponent>>();
l.add(ExportersService.class);
return l;
}
}

View File

@ -0,0 +1,125 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package com.elasticsearch.dash.exporters;
import com.elasticsearch.dash.Exporter;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.action.admin.cluster.node.stats.NodeStats;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.common.component.AbstractLifecycleComponent;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
public class ESExporter extends AbstractLifecycleComponent<ESExporter> implements Exporter<ESExporter> {
final String targetHost;
final int targetPort;
final String targetPathPrefix;
final ClusterName clusterName;
final ESLogger logger = ESLoggerFactory.getLogger(ESExporter.class.getName());
public ESExporter(Settings settings, ClusterName clusterName) {
super(settings);
this.clusterName = clusterName;
// TODO: move to a single settings.
targetHost = settings.get("target.host", "localhost");
targetPort = settings.getAsInt("target.post", 9200);
String targetIndexPrefix = settings.get("target.index.prefix", "dash");
try {
targetPathPrefix = String.format("/%s_%s_",
URLEncoder.encode(targetIndexPrefix,"UTF-8"),
URLEncoder.encode(clusterName.value(),"UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new ElasticSearchException("Can't encode target url", e);
}
logger.info("ESExporter initialized. Target: {}:{} Index prefix set to {}", targetHost, targetPort, targetIndexPrefix );
// explode early on broken settings
getTargetURL("test");
}
private URL getTargetURL(String type) {
try {
String path = String.format("%1$s%2$tY.%2$tm.%2$td/%3$s", targetPathPrefix, new Date(), type);
return new URL("http", targetHost, targetPort, path);
} catch (MalformedURLException e) {
throw new ElasticSearchIllegalArgumentException("Target settings result in a malformed url");
}
}
@Override
public String name() {
return "ESExporter";
}
@Override
public void exportNodeStats(NodeStats nodeStats) {
URL url = getTargetURL("nodestats");
logger.debug("Exporting node stats to {}", url);
HttpURLConnection conn;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", XContentType.SMILE.restContentType());
OutputStream os = conn.getOutputStream();
XContentBuilder builder = XContentFactory.smileBuilder(os);
builder.startObject();
nodeStats.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
builder.close();
if (conn.getResponseCode() != 201) {
logger.error("Remote target didn't respond with 201 Created");
}
} catch (IOException e) {
logger.error("Error connecting to target", e);
return;
}
}
@Override
protected void doStart() throws ElasticSearchException {
}
@Override
protected void doStop() throws ElasticSearchException {
}
@Override
protected void doClose() throws ElasticSearchException {
}
}

View File

@ -0,0 +1 @@
plugin=com.elasticsearch.dash.Plugin