|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.transport.grpc; |
| 10 | + |
| 11 | +import org.opensearch.action.index.IndexResponse; |
| 12 | +import org.opensearch.common.network.NetworkAddress; |
| 13 | +import org.opensearch.common.settings.Settings; |
| 14 | +import org.opensearch.common.xcontent.XContentType; |
| 15 | +import org.opensearch.core.common.transport.TransportAddress; |
| 16 | +import org.opensearch.plugin.transport.grpc.ssl.NettyGrpcClient; |
| 17 | +import org.opensearch.plugins.Plugin; |
| 18 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 19 | + |
| 20 | +import java.net.InetSocketAddress; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | + |
| 27 | +import io.grpc.health.v1.HealthCheckResponse; |
| 28 | + |
| 29 | +import static org.opensearch.plugin.transport.grpc.Netty4GrpcServerTransport.GRPC_TRANSPORT_SETTING_KEY; |
| 30 | +import static org.opensearch.plugins.NetworkPlugin.AuxTransport.AUX_TRANSPORT_TYPES_KEY; |
| 31 | + |
| 32 | +/** |
| 33 | + * Base test class for gRPC transport integration tests. |
| 34 | + */ |
| 35 | +public abstract class GrpcTransportBaseIT extends OpenSearchIntegTestCase { |
| 36 | + |
| 37 | + // Common constants |
| 38 | + protected static final String DEFAULT_INDEX_NAME = "test-grpc-index"; |
| 39 | + protected static final String DEFAULT_DOCUMENT_SOURCE = "{\"field1\":\"value1\",\"field2\":42}"; |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets a random gRPC transport address from the cluster. |
| 43 | + * |
| 44 | + * @return A random transport address |
| 45 | + */ |
| 46 | + protected TransportAddress randomNetty4GrpcServerTransportAddr() { |
| 47 | + List<TransportAddress> addresses = new ArrayList<>(); |
| 48 | + for (Netty4GrpcServerTransport transport : internalCluster().getInstances(Netty4GrpcServerTransport.class)) { |
| 49 | + TransportAddress tAddr = new TransportAddress(transport.getBoundAddress().publishAddress().address()); |
| 50 | + addresses.add(tAddr); |
| 51 | + } |
| 52 | + return randomFrom(addresses); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Configures node settings for gRPC transport. |
| 57 | + */ |
| 58 | + @Override |
| 59 | + protected Settings nodeSettings(int nodeOrdinal) { |
| 60 | + return Settings.builder().put(super.nodeSettings(nodeOrdinal)).put(AUX_TRANSPORT_TYPES_KEY, GRPC_TRANSPORT_SETTING_KEY).build(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Configures plugins for gRPC transport. |
| 65 | + */ |
| 66 | + @Override |
| 67 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 68 | + return Collections.singleton(GrpcPlugin.class); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Verifies that the gRPC transport is started on all nodes. |
| 73 | + */ |
| 74 | + protected void verifyGrpcTransportStarted() { |
| 75 | + for (String nodeName : internalCluster().getNodeNames()) { |
| 76 | + Netty4GrpcServerTransport transport = internalCluster().getInstance(Netty4GrpcServerTransport.class, nodeName); |
| 77 | + assertNotNull("gRPC transport should be started on node " + nodeName, transport); |
| 78 | + |
| 79 | + // Verify that the transport is bound to an address |
| 80 | + TransportAddress[] boundAddresses = transport.getBoundAddress().boundAddresses(); |
| 81 | + assertTrue("gRPC transport should be bound to at least one address", boundAddresses.length > 0); |
| 82 | + |
| 83 | + // Log the bound addresses for debugging |
| 84 | + for (TransportAddress address : boundAddresses) { |
| 85 | + InetSocketAddress inetAddress = address.address(); |
| 86 | + logger.info("Node {} gRPC transport bound to {}", nodeName, NetworkAddress.format(inetAddress)); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates and ensures a test index is green. |
| 93 | + * |
| 94 | + * @param indexName The name of the index to create |
| 95 | + */ |
| 96 | + protected void createTestIndex(String indexName) { |
| 97 | + createIndex(indexName); |
| 98 | + ensureGreen(indexName); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Creates a gRPC client connected to a random node. |
| 103 | + * |
| 104 | + * @return A new NettyGrpcClient instance |
| 105 | + * @throws javax.net.ssl.SSLException if there's an SSL error |
| 106 | + */ |
| 107 | + protected NettyGrpcClient createGrpcClient() throws javax.net.ssl.SSLException { |
| 108 | + return new NettyGrpcClient.Builder().setAddress(randomNetty4GrpcServerTransportAddr()).build(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Indexes a test document and refreshes the index. |
| 113 | + * |
| 114 | + * @param indexName The name of the index |
| 115 | + * @param id The document ID |
| 116 | + * @param source The document source |
| 117 | + * @return The IndexResponse |
| 118 | + */ |
| 119 | + protected IndexResponse indexTestDocument(String indexName, String id, String source) { |
| 120 | + IndexResponse response = client().prepareIndex(indexName).setId(id).setSource(source, XContentType.JSON).get(); |
| 121 | + |
| 122 | + assertTrue( |
| 123 | + "Document should be indexed without shard failures", |
| 124 | + response.getShardInfo().getSuccessful() > 0 && response.getShardInfo().getFailed() == 0 |
| 125 | + ); |
| 126 | + |
| 127 | + // Refresh to make document searchable |
| 128 | + client().admin().indices().prepareRefresh(indexName).get(); |
| 129 | + |
| 130 | + return response; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Waits for a document to become searchable. |
| 135 | + * |
| 136 | + * @param indexName The name of the index |
| 137 | + * @param id The document ID |
| 138 | + * @throws Exception If the document doesn't become searchable within the timeout |
| 139 | + */ |
| 140 | + protected void waitForSearchableDoc(String indexName, String id) throws Exception { |
| 141 | + assertBusy(() -> { |
| 142 | + refresh(indexName); |
| 143 | + assertTrue("Document should be searchable", client().prepareGet(indexName, id).get().isExists()); |
| 144 | + }, 30, TimeUnit.SECONDS); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Checks the health of the gRPC transport service. |
| 149 | + * |
| 150 | + * @throws Exception If the health check fails |
| 151 | + */ |
| 152 | + protected void checkGrpcTransportHealth() throws Exception { |
| 153 | + try (NettyGrpcClient client = createGrpcClient()) { |
| 154 | + assertEquals(client.checkHealth(), HealthCheckResponse.ServingStatus.SERVING); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments