migrate --docker-metrics to okhttp

This commit is contained in:
Jonathan Shook 2020-03-23 18:30:35 -05:00
parent 4b434f871f
commit f9c9beb4c9
3 changed files with 255 additions and 216 deletions

View File

@ -1,125 +1,137 @@
<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">
<modelVersion>4.0.0</modelVersion>
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>mvn-defaults</artifactId>
<groupId>io.nosqlbench</groupId>
<version>3.12.48-SNAPSHOT</version>
<relativePath>../mvn-defaults</relativePath>
</parent>
<parent>
<artifactId>mvn-defaults</artifactId>
<groupId>io.nosqlbench</groupId>
<version>3.12.48-SNAPSHOT</version>
<relativePath>../mvn-defaults</relativePath>
</parent>
<artifactId>engine-docker</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>java-docker module for nosqlbench</description>
<artifactId>engine-docker</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>java-docker module for nosqlbench</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javadoc.name>nosqlbench Java Docker Module</javadoc.name>
</properties>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javadoc.name>nosqlbench Java Docker Module</javadoc.name>
</properties>
<dependencies>
<dependencies>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport-native-epoll</artifactId>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-api</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-api</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-jersey</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-transport-okhttp</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-transport-netty</artifactId>-->
<!-- <version>3.2.0-rc2</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-transport-jersey</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<exclusions>
<exclusion>
<artifactId>docker-java-transport-netty</artifactId>
<groupId>com.github.docker-java</groupId>
</exclusion>
</exclusions>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>docker-java-transport-jersey</artifactId>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>io.nosqlbench</groupId>
<artifactId>engine-api</artifactId>
<version>3.12.48-SNAPSHOT</version>
</dependency>
</dependencies>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-transport-netty</artifactId>-->
<!-- <version>3.2.0-rc2</version>-->
<!-- </dependency>-->
<profiles>
<profile>
<id>shade</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<executions>
<execution>
<goals><goal>shade</goal></goals>
<phase>package</phase>
</execution>
</executions>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java</artifactId>
<exclusions>
<exclusion>
<artifactId>docker-java-transport-netty</artifactId>
<groupId>com.github.docker-java</groupId>
</exclusion>
<exclusion>
<artifactId>docker-java-transport-jersey</artifactId>
<groupId>com.github.docker-java</groupId>
</exclusion>
</exclusions>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>docker-java-transport-jersey</artifactId>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>io.nosqlbench</groupId>
<artifactId>engine-api</artifactId>
<version>3.12.48-SNAPSHOT</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>shade</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>shaded</shadedClassifierName>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -19,17 +19,19 @@ import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.async.ResultCallbackTemplate;
import com.github.dockerjava.core.command.LogContainerResultCallback;
import com.github.dockerjava.core.command.PullImageResultCallback;
import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;
//import com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory;
import com.github.dockerjava.okhttp.OkHttpDockerCmdExecFactory;
import com.sun.security.auth.module.UnixSystem;
import io.nosqlbench.engine.api.util.NosqlBenchFiles;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.client.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.*;
import java.net.Authenticator;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -45,7 +47,8 @@ public class DockerMetricsHelper {
private static final String DOCKER_HOST = "DOCKER_HOST";
private static final String DOCKER_HOST_ADDR = "unix:///var/run/docker.sock";
String userHome = System.getProperty("user.home");
private Client rsClient = ClientBuilder.newClient();
// private Client rsClient = ClientBuilder.newClient();
private DockerClientConfig config;
private DockerClient dockerClient;
private Logger logger = LoggerFactory.getLogger(DockerMetricsHelper.class);
@ -53,9 +56,13 @@ public class DockerMetricsHelper {
public DockerMetricsHelper() {
System.getProperties().setProperty(DOCKER_HOST, DOCKER_HOST_ADDR);
this.config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost(DOCKER_HOST_ADDR).build();
DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
.withReadTimeout(1000)
.withConnectTimeout(1000);
DockerCmdExecFactory dockerCmdExecFactory = new OkHttpDockerCmdExecFactory()
.withReadTimeout(1000)
.withConnectTimeout(1000);
// DockerCmdExecFactory dockerCmdExecFactory = new JerseyDockerCmdExecFactory()
// .withReadTimeout(1000)
// .withConnectTimeout(1000);
this.dockerClient = DockerClientBuilder.getInstance(config)
.withDockerCmdExecFactory(dockerCmdExecFactory)
@ -240,36 +247,41 @@ public class DockerMetricsHelper {
private void configureGrafana() {
Response response = post("http://localhost:3000/api/dashboards/db", "docker/dashboards/analysis.json", true);
response = post("http://localhost:3000/api/datasources", "docker/datasources/prometheus-datasource.yaml", true);
post("http://localhost:3000/api/dashboards/db", "docker/dashboards/analysis.json", true);
post("http://localhost:3000/api/datasources", "docker/datasources/prometheus-datasource.yaml", true);
}
private Response post(String url, String path, boolean auth) {
private static String basicAuth(String username, String password) {
return "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
}
private HttpResponse<String> post(String url, String path, boolean auth) {
HttpClient.Builder clientBuilder = HttpClient.newBuilder();
HttpClient httpClient = clientBuilder.build();
HttpRequest.Builder builder = HttpRequest.newBuilder();
builder = builder.uri(URI.create(url));
if (auth) {
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("admin", "admin");
rsClient.register(feature);
builder = builder.header("Authorization", basicAuth("username", "password"));
}
WebTarget webTarget
= rsClient.target(url);
Invocation.Builder invocationBuilder
= webTarget.request(MediaType.APPLICATION_JSON);
Response response;
if (path != null) {
if (path !=null) {
String dashboard = NosqlBenchFiles.readFile(path);
response
= invocationBuilder
.post(Entity.entity(dashboard, MediaType.APPLICATION_JSON));
builder = builder.POST(HttpRequest.BodyPublishers.ofString(dashboard));
} else {
response
= invocationBuilder
.post(Entity.entity(null, MediaType.APPLICATION_JSON));
builder = builder.GET();
}
return response;
HttpRequest request = builder.build();
try {
HttpResponse<String> resp = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
logger.debug("http response for configuring grafana:\n" + resp);
return resp;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

View File

@ -1,4 +1,5 @@
<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">
<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">
<modelVersion>4.0.0</modelVersion>
@ -83,10 +84,10 @@
<!-- <dependency>-->
<!-- <groupId>io.nosqlbench</groupId>-->
<!-- <artifactId>nb-runtime</artifactId>-->
<!-- <version>2.12.51-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- <groupId>io.nosqlbench</groupId>-->
<!-- <artifactId>nb-runtime</artifactId>-->
<!-- <version>2.12.51-SNAPSHOT</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
@ -122,15 +123,15 @@
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-api</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-api</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.docker-java</groupId>
<artifactId>docker-java-core</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.docker-java</groupId>-->
<!-- <artifactId>docker-java-core</artifactId>-->
<!-- </dependency>-->
</dependencies>
@ -149,23 +150,26 @@
<execution>
<id>shade-nb-jar</id>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- <shadedArtifactAttached>true</shadedArtifactAttached>-->
<!-- <shadedClassifierName>jarapp</shadedClassifierName>-->
<!-- <shadedArtifactId>${project.artifactId}</shadedArtifactId>-->
<!-- <shadedArtifactAttached>true</shadedArtifactAttached>-->
<!-- <shadedClassifierName>jarapp</shadedClassifierName>-->
<!-- <shadedArtifactId>${project.artifactId}</shadedArtifactId>-->
<transformers combine.children="append">
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>topics.md</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.nosqlbench.engine.cli.NBCLI</mainClass>
</transformer>
</transformers>
<!-- <finalName>${project.artifactId}</finalName>-->
<!-- <finalName>${project.artifactId}</finalName>-->
<filters>
<filter>
@ -180,66 +184,73 @@
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>link-nb-jar-name</id>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<configuration>
<environmentVariables>
<NBJAR_VERSION>${project.version}</NBJAR_VERSION>
</environmentVariables>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/nb-scripts/link-jar-name.sh</executable>
</configuration>
</execution>
<execution>
<id>build-static-guidebook</id>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<configuration>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/gendocs.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>link-nb-jar-name</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<environmentVariables>
<NBJAR_VERSION>${project.version}</NBJAR_VERSION>
</environmentVariables>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/nb-scripts/link-jar-name.sh</executable>
</configuration>
</execution>
<execution>
<id>build-static-guidebook</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/gendocs.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<activation>
<os>
<family>unix</family>
<name>linux</name>
<arch>amd64</arch>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>build-nb-appimage</id>
<phase>package</phase>
<goals><goal>exec</goal></goals>
<configuration>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/build-bin.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<profile>
<id>build-nb-appimage</id>
<activation>
<os>
<family>unix</family>
<name>linux</name>
<arch>amd64</arch>
</os>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>build-nb-appimage</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<basedir>${project.basedir}</basedir>
<executable>${project.basedir}/build-bin.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>enforce</id>
<activation>
<activeByDefault>false</activeByDefault>
@ -249,13 +260,17 @@
<plugin>
<executions>
<execution>
<goals><goal>enforce</goal></goals>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<configuration>
<rules><dependencyConvergence /></rules>
<rules>
<dependencyConvergence/>
</rules>
</configuration>
</plugin>
</plugins>