|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.arrow.flight; |
| 18 | + |
| 19 | +import static org.apache.arrow.flight.FlightTestUtil.LOCALHOST; |
| 20 | +import static org.apache.arrow.flight.Location.forGrpcInsecure; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | + |
| 25 | +import com.google.common.io.ByteStreams; |
| 26 | +import io.grpc.Drainable; |
| 27 | +import java.io.ByteArrayOutputStream; |
| 28 | +import java.io.InputStream; |
| 29 | +import org.apache.arrow.memory.BufferAllocator; |
| 30 | +import org.apache.arrow.memory.RootAllocator; |
| 31 | +import org.apache.arrow.vector.IntVector; |
| 32 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 33 | +import org.apache.arrow.vector.VectorUnloader; |
| 34 | +import org.apache.arrow.vector.ipc.message.IpcOption; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * Proves the zero-copy read path is taken over a real gRPC transport, not only against a mock. |
| 39 | + * |
| 40 | + * <p>The oracle is allocator accounting. When the client takes ownership of gRPC's buffer, it is |
| 41 | + * charged for the whole wire frame (header, tags and body). When it copies, it is charged for a |
| 42 | + * buffer of the body's size only. The two are never equal, so the number distinguishes the paths. |
| 43 | + */ |
| 44 | +public class TestFlightZeroCopyRead { |
| 45 | + |
| 46 | + private static final int ROWS = 1000; |
| 47 | + |
| 48 | + @Test |
| 49 | + public void recordBatchIsReadWithoutCopyingOverRealTransport() throws Exception { |
| 50 | + assertTrue(ArrowMessage.ENABLE_ZERO_COPY_READ, "zero-copy reads must be enabled for this test"); |
| 51 | + try (BufferAllocator serverAllocator = new RootAllocator(Long.MAX_VALUE); |
| 52 | + BufferAllocator clientAllocator = new RootAllocator(Long.MAX_VALUE); |
| 53 | + FlightServer server = |
| 54 | + FlightServer.builder( |
| 55 | + serverAllocator, |
| 56 | + forGrpcInsecure(LOCALHOST, 0), |
| 57 | + new OneBatchProducer(serverAllocator)) |
| 58 | + .build() |
| 59 | + .start(); |
| 60 | + FlightClient client = FlightClient.builder(clientAllocator, server.getLocation()).build()) { |
| 61 | + final long wireSize = wireSizeOfTheBatch(clientAllocator); |
| 62 | + assertEquals(0, clientAllocator.getAllocatedMemory()); |
| 63 | + |
| 64 | + try (FlightStream stream = client.getStream(new Ticket(new byte[0]))) { |
| 65 | + assertTrue(stream.next()); |
| 66 | + final VectorSchemaRoot root = stream.getRoot(); |
| 67 | + final IntVector values = (IntVector) root.getVector("c1"); |
| 68 | + assertEquals(ROWS, root.getRowCount()); |
| 69 | + for (int i = 0; i < ROWS; i++) { |
| 70 | + assertEquals(i * 3, values.get(i)); |
| 71 | + } |
| 72 | + assertEquals( |
| 73 | + wireSize, |
| 74 | + clientAllocator.getAllocatedMemory(), |
| 75 | + "the client should be holding gRPC's whole frame, not a copy of the body"); |
| 76 | + assertFalse(stream.next()); |
| 77 | + } |
| 78 | + assertEquals(0, clientAllocator.getAllocatedMemory(), "gRPC's buffer must be released"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** The exact number of bytes the server puts on the wire for the batch the producer sends. */ |
| 83 | + private static long wireSizeOfTheBatch(BufferAllocator allocator) throws Exception { |
| 84 | + final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
| 85 | + // Same ownership convention as the server: the batch's buffer references belong to the |
| 86 | + // message, which is closed once the wire stream is done with them. |
| 87 | + try (VectorSchemaRoot root = theBatch(allocator); |
| 88 | + ArrowMessage message = |
| 89 | + new ArrowMessage( |
| 90 | + new VectorUnloader(root).getRecordBatch(), null, false, IpcOption.DEFAULT); |
| 91 | + InputStream wire = ArrowMessage.createMarshaller(allocator).stream(message)) { |
| 92 | + if (wire instanceof Drainable) { |
| 93 | + ((Drainable) wire).drainTo(bytes); |
| 94 | + } else { |
| 95 | + ByteStreams.copy(wire, bytes); |
| 96 | + } |
| 97 | + } |
| 98 | + return bytes.size(); |
| 99 | + } |
| 100 | + |
| 101 | + private static VectorSchemaRoot theBatch(BufferAllocator allocator) { |
| 102 | + final IntVector values = new IntVector("c1", allocator); |
| 103 | + final VectorSchemaRoot root = VectorSchemaRoot.of(values); |
| 104 | + root.allocateNew(); |
| 105 | + for (int i = 0; i < ROWS; i++) { |
| 106 | + values.set(i, i * 3); |
| 107 | + } |
| 108 | + values.setValueCount(ROWS); |
| 109 | + root.setRowCount(ROWS); |
| 110 | + return root; |
| 111 | + } |
| 112 | + |
| 113 | + private static final class OneBatchProducer extends NoOpFlightProducer { |
| 114 | + private final BufferAllocator allocator; |
| 115 | + |
| 116 | + OneBatchProducer(BufferAllocator allocator) { |
| 117 | + this.allocator = allocator; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void getStream(CallContext context, Ticket ticket, ServerStreamListener listener) { |
| 122 | + try (VectorSchemaRoot root = theBatch(allocator)) { |
| 123 | + listener.start(root); |
| 124 | + listener.putNext(); |
| 125 | + listener.completed(); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments