Skip to content

Commit

Permalink
Switches from Netty to our gRPC client.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Jan 22, 2025
1 parent c3a526b commit 17a1ebc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 44 deletions.
25 changes: 4 additions & 21 deletions config/etcd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-media-type</artifactId>
</dependency>

<dependency>
<groupId>io.helidon.webclient</groupId>
<artifactId>helidon-webclient-grpc</artifactId>
</dependency>
<!-- etcd v2 -->
<dependency>
<groupId>org.mousio</groupId>
Expand All @@ -60,11 +63,6 @@
<groupId>io.grpc</groupId>
<artifactId>grpc-api</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
Expand Down Expand Up @@ -136,21 +134,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<property>
<!-- on big machines (e.g. 52 cores hyperthreaded)
this failed with too many open files
-->
<name>io.netty.eventLoopThreads</name>
<value>2</value>
</property>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2024 Oracle and/or its affiliates.
* Copyright (c) 2017, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,8 +22,8 @@
import java.util.concurrent.Executor;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;
import java.util.concurrent.TimeUnit;

import io.helidon.common.tls.Tls;
import io.helidon.config.etcd.internal.client.EtcdClient;
import io.helidon.config.etcd.internal.client.EtcdClientException;
import io.helidon.config.etcd.internal.client.proto.KVGrpc;
Expand All @@ -34,23 +34,20 @@
import io.helidon.config.etcd.internal.client.proto.WatchGrpc;
import io.helidon.config.etcd.internal.client.proto.WatchRequest;
import io.helidon.config.etcd.internal.client.proto.WatchResponse;
import io.helidon.webclient.grpc.GrpcClient;

import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;

/**
* Etcd API v3 client.
*/
public class EtcdV3Client implements EtcdClient {

private static final System.Logger LOGGER = System.getLogger(EtcdV3Client.class.getName());
private static final Tls DISABLE_TLS = Tls.builder().enabled(false).build();

private final Map<String, SubmissionPublisher<Long>> publishers = new ConcurrentHashMap<>();

private final ManagedChannel channel;
private final KVGrpc.KVBlockingStub kvStub;
private final WatchGrpc.WatchStub watchStub;

Expand All @@ -64,11 +61,12 @@ public EtcdV3Client(URI... uris) {
throw new IllegalArgumentException("EtcdV3Client only supports a single URI");
}
URI uri = uris[0];
ManagedChannelBuilder mcb = ManagedChannelBuilder.forAddress(uri.getHost(), uri.getPort());
this.channel = mcb.usePlaintext().build();

kvStub = KVGrpc.newBlockingStub(channel);
watchStub = WatchGrpc.newStub(channel);
GrpcClient grpcClient = GrpcClient.builder()
.baseUri(uri)
.tls(DISABLE_TLS) // must explicitly disable it
.build();
kvStub = KVGrpc.newBlockingStub(grpcClient.channel());
watchStub = WatchGrpc.newStub(grpcClient.channel());
}

@Override
Expand Down Expand Up @@ -140,16 +138,7 @@ public Flow.Publisher<Long> watch(String key) throws EtcdClientException {
}

@Override
public void close() throws EtcdClientException {
public void close() {
publishers.values().forEach(SubmissionPublisher::close);
if (!channel.isShutdown() && !channel.isTerminated()) {
try {
channel.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.log(System.Logger.Level.INFO, "Error closing gRPC channel, reason: " + e.getLocalizedMessage(), e);
} finally {
channel.shutdown();
}
}
}
}
3 changes: 2 additions & 1 deletion config/etcd/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2024 Oracle and/or its affiliates.
* Copyright (c) 2017, 2025 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,7 @@
requires io.grpc.stub;
requires io.helidon.common.media.type;
requires io.helidon.common;
requires io.helidon.webclient.grpc;

requires static java.annotation;

Expand Down

0 comments on commit 17a1ebc

Please sign in to comment.