Skip to content

Commit

Permalink
make gRPC work in native when other extensions added
Browse files Browse the repository at this point in the history
  • Loading branch information
michalszynkiewicz authored and gsmet committed Nov 16, 2020
1 parent cf7990d commit 1d37932
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ jobs:
grpc-interceptors
grpc-mutual-auth
grpc-plain-text
grpc-plain-text-mutiny
grpc-proto-v2
grpc-streaming
grpc-tls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ HealthBuildItem addHealthChecks(GrpcServerBuildTimeConfig config,
NativeImageConfigBuildItem nativeImageConfiguration() {
NativeImageConfigBuildItem.Builder builder = NativeImageConfigBuildItem.builder()
.addRuntimeInitializedClass("io.grpc.netty.Utils$ByteBufAllocatorPreferDirectHolder")
.addRuntimeInitializedClass("io.grpc.netty.Utils$ByteBufAllocatorPreferHeapHolder");
.addRuntimeInitializedClass("io.grpc.netty.Utils$ByteBufAllocatorPreferHeapHolder")
// substitutions are runtime-only, Utils have to be substituted until we cannot use EPoll
// in native. NettyServerBuilder and NettyChannelBuilder would "bring in" Utils in build time
// if they were not marked as runtime initialized:
.addRuntimeInitializedClass("io.grpc.netty.Utils")
.addRuntimeInitializedClass("io.grpc.netty.NettyServerBuilder")
.addRuntimeInitializedClass("io.grpc.netty.NettyChannelBuilder");
return builder.build();
}

Expand Down
4 changes: 4 additions & 0 deletions extensions/grpc/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-grpc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ public static SslContextBuilder configure(SslContextBuilder builder, Provider jd

}

@TargetClass(className = "io.grpc.netty.Utils")
final class Target_io_grpc_netty_Utils {

@Substitute
static boolean isEpollAvailable() {
return false;
}

@Substitute
private static Throwable getEpollUnavailabilityCause() {
return null;
}
}

@SuppressWarnings("unused")
class GrpcNettySubstitutions {
}
165 changes: 165 additions & 0 deletions integration-tests/grpc-plain-text-mutiny/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?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">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>quarkus-integration-tests-parent</artifactId>
<groupId>io.quarkus</groupId>
<version>999-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>

<artifactId>quarkus-integration-test-grpc-plain-text-mutiny</artifactId>
<name>Quarkus - Integration Tests - gRPC - Plain Text with Mutiny</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate-code</goal>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<id>native-image</id>
<goals>
<goal>native-image</goal>
</goals>
<configuration>
<cleanupServer>true</cleanupServer>
<enableHttpUrlHandler>true</enableHttpUrlHandler>
<graalvmHome>${graalvmHome}</graalvmHome>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.quarkus.grpc.examples.hello;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import examples.GreeterGrpc;
import examples.HelloReply;
import examples.HelloRequest;
import examples.MutinyGreeterGrpc;
import io.quarkus.grpc.runtime.annotations.GrpcService;
import io.smallrye.mutiny.Uni;

@Path("/hello")
public class HelloWorldEndpoint {

@Inject
@GrpcService("hello")
GreeterGrpc.GreeterBlockingStub blockingHelloService;
@Inject
@GrpcService("hello")
MutinyGreeterGrpc.MutinyGreeterStub mutinyHelloService;

@GET
@Path("/blocking/{name}")
public String helloBlocking(@PathParam("name") String name) {
HelloReply reply = blockingHelloService.sayHello(HelloRequest.newBuilder().setName(name).build());
return generateResponse(reply);

}

@GET
@Path("/mutiny/{name}")
public Uni<String> helloMutiny(@PathParam("name") String name) {
return mutinyHelloService.sayHello(HelloRequest.newBuilder().setName(name).build())
.onItem().transform((reply) -> generateResponse(reply));
}

public String generateResponse(HelloReply reply) {
return String.format("%s! HelloWorldService has been called %d number of times.", reply.getMessage(), reply.getCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.grpc.examples.hello;

import java.util.concurrent.atomic.AtomicInteger;

import javax.inject.Singleton;

import examples.HelloReply;
import examples.HelloRequest;
import examples.MutinyGreeterGrpc;
import io.smallrye.mutiny.Uni;

@Singleton
public class HelloWorldService extends MutinyGreeterGrpc.GreeterImplBase {

AtomicInteger counter = new AtomicInteger();

@Override
public Uni<HelloReply> sayHello(HelloRequest request) {
int count = counter.incrementAndGet();
String name = request.getName();
return Uni.createFrom().item("Hello " + name)
.map(res -> HelloReply.newBuilder().setMessage(res).setCount(count).build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2015, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "examples";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
int32 count = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.grpc.clients.hello.host=localhost
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.grpc.examples.hello;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
class HelloWorldEndpointIT extends HelloWorldEndpointTest {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.grpc.examples.hello;

import static io.restassured.RestAssured.get;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class HelloWorldEndpointTest {

@Test
public void testHelloWorldServiceUsingBlockingStub() {
String response = get("/hello/blocking/neo").asString();
assertThat(response).startsWith("Hello neo");
}

@Test
public void testHelloWorldServiceUsingMutinyStub() {
String response = get("/hello/mutiny/neo-mutiny").asString();
assertThat(response).startsWith("Hello neo-mutiny");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.grpc.examples.hello;

import io.quarkus.test.junit.NativeImageTest;

@NativeImageTest
class HelloWorldServiceIT extends HelloWorldEndpointTest {

}
Loading

0 comments on commit 1d37932

Please sign in to comment.