Skip to content

Commit 17091bb

Browse files
feat: netty for rest client
1 parent 36f4c40 commit 17091bb

File tree

25 files changed

+1652
-570
lines changed

25 files changed

+1652
-570
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.v3.netty.rest;
23+
24+
import io.netty.channel.ChannelInitializer;
25+
import io.netty.channel.ChannelPipeline;
26+
import io.netty.channel.socket.SocketChannel;
27+
import io.netty.handler.codec.http.FullHttpResponse;
28+
import io.netty.handler.codec.http.HttpClientCodec;
29+
import io.netty.handler.codec.http.HttpObjectAggregator;
30+
import io.netty.handler.ssl.SslContext;
31+
import io.netty.util.concurrent.Promise;
32+
33+
import javax.annotation.Nonnull;
34+
import javax.annotation.Nullable;
35+
36+
public class ClientChannelInitializer extends ChannelInitializer<SocketChannel> {
37+
38+
private final SslContext sslCtx;
39+
40+
private final Promise<FullHttpResponse> promise;
41+
42+
private final String host;
43+
44+
private final Integer port;
45+
46+
public ClientChannelInitializer(@Nonnull String host, @Nonnull Integer port, @Nonnull Promise<FullHttpResponse> promise, @Nullable SslContext sslCtx) {
47+
this.sslCtx = sslCtx;
48+
this.promise = promise;
49+
this.host = host;
50+
this.port = port;
51+
}
52+
53+
@Override
54+
public void initChannel(SocketChannel ch) {
55+
ChannelPipeline p = ch.pipeline();
56+
if (sslCtx != null) {
57+
p.addLast("ssl", sslCtx.newHandler(ch.alloc(), host, port));
58+
}
59+
p.addLast(new HttpClientCodec());
60+
p.addLast(new HttpObjectAggregator(1048576));
61+
p.addLast(new ClientHandler(this.promise));
62+
}
63+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.v3.netty.rest;
23+
24+
import io.netty.channel.ChannelHandlerContext;
25+
import io.netty.channel.SimpleChannelInboundHandler;
26+
import io.netty.handler.codec.http.*;
27+
import io.netty.util.CharsetUtil;
28+
import io.netty.util.concurrent.Promise;
29+
30+
public class ClientHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
31+
32+
private final Promise<FullHttpResponse> promise;
33+
34+
public ClientHandler(Promise<FullHttpResponse> promise) {
35+
this.promise = promise;
36+
}
37+
38+
@Override
39+
public void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) {
40+
// if (msg instanceof HttpResponse) {
41+
// HttpResponse response = (HttpResponse) msg;
42+
// System.err.println("{ START CONTENT");
43+
// }
44+
// if (msg instanceof HttpContent) {
45+
// HttpContent content = (HttpContent) msg;
46+
// System.err.print(content.content().toString(CharsetUtil.UTF_8));
47+
// System.err.flush();
48+
//
49+
// if (content instanceof LastHttpContent) {
50+
// System.err.println("} END OF CONTENT");
51+
// }
52+
// }
53+
// System.out.println(msg);
54+
this.promise.trySuccess(msg.retain());
55+
56+
}
57+
58+
@Override
59+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
60+
this.promise.tryFailure(cause);
61+
cause.printStackTrace();
62+
ctx.close();
63+
}
64+
65+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
package com.influxdb.v3.netty.rest;
23+
24+
import com.influxdb.v3.client.InfluxDBClient;
25+
import com.influxdb.v3.client.Point;
26+
import com.influxdb.v3.client.config.ClientConfig;
27+
import io.netty.handler.codec.http.FullHttpResponse;
28+
import io.netty.handler.codec.http.HttpMethod;
29+
30+
import javax.net.ssl.SSLException;
31+
import java.net.URISyntaxException;
32+
import java.util.UUID;
33+
import java.util.concurrent.ExecutionException;
34+
35+
36+
public class Main {
37+
public static void main(String[] args) throws URISyntaxException, SSLException, ExecutionException, InterruptedException {
38+
ClientConfig clientConfig = configCloud();
39+
40+
var testId = UUID.randomUUID().toString();
41+
// Temporarily use `ClientConfig` as a constructor argument.
42+
try (RestClient restClient = new RestClient(clientConfig)) {
43+
44+
// Get server version.
45+
System.out.println("Server version: " + restClient.getServerVersion());
46+
47+
// Write data
48+
System.out.println("Write data with testId " + testId);
49+
var p = Point.measurement("cpu_sonnh")
50+
.setTag("host", "server1")
51+
.setField("usage_idle", 90.0f)
52+
.setField("testId", testId);
53+
var lineProtocol = p.toLineProtocol();
54+
restClient.write(lineProtocol);
55+
56+
// Read data
57+
System.out.println("Read data with testId " + testId);
58+
String query = String.format("SELECT * FROM \"cpu_sonnh\" WHERE \"testId\" = '%s'", testId);
59+
InfluxDBClient influxDBClient = InfluxDBClient.getInstance(clientConfig);
60+
var stream = influxDBClient.queryPoints(query);
61+
stream.findFirst().ifPresent(pointValues -> System.out.println("Field usage_idle: " + pointValues.getField("usage_idle")));
62+
63+
} catch (Exception e) {
64+
throw new RuntimeException(e);
65+
}
66+
}
67+
68+
public static ClientConfig configCloud() {
69+
String url = System.getenv("TESTING_INFLUXDB_URL");
70+
String token = System.getenv("TESTING_INFLUXDB_TOKEN");
71+
String database = System.getenv("TESTING_INFLUXDB_DATABASE");
72+
return new ClientConfig.Builder()
73+
.host(url)
74+
.token(token.toCharArray())
75+
.database(database)
76+
.build();
77+
}
78+
79+
// This is a docker container ran from scripts/influxdb-setup.sh, get the token and database, url from that script
80+
public static ClientConfig configLocal() {
81+
String url = "localhost";
82+
String token = "apiv3_sMYBS-vRxl6UDMylb7m2u64G6R7g61jlGL76XnUJY3EaN4MD0tZd4DZOBhe6j-dYtoVhrC6PqGgI9Xiv8d3Psw";
83+
String database = System.getenv("bucket0");
84+
return new ClientConfig.Builder()
85+
.host(url)
86+
.token(token.toCharArray())
87+
.database(database)
88+
.build();
89+
}
90+
}

0 commit comments

Comments
 (0)