emkornfield commented on a change in pull request #8265: URL: https://github.com/apache/arrow/pull/8265#discussion_r513165104
########## File path: java/flight/flight-grpc/src/main/java/org/apache/arrow/flight/FlightHandlerRegistry.java ########## @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.arrow.flight; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicInteger; + +import org.apache.arrow.flight.impl.Flight; +import org.apache.arrow.memory.BufferAllocator; + +import io.grpc.Context; +import io.grpc.HandlerRegistry; +import io.grpc.Metadata; +import io.grpc.MethodDescriptor; +import io.grpc.ServerCall; +import io.grpc.ServerCallHandler; +import io.grpc.ServerMethodDefinition; +import io.grpc.ServerServiceDefinition; +import io.grpc.Status; + +/** + * A gRPC HandlerRegistry that creates a new Arrow allocator for each call to a Flight "data" method + * (DoGet, DoPut, or DoExchange). + */ +public final class FlightHandlerRegistry extends HandlerRegistry { + private final BufferAllocator allocator; + private final ServerServiceDefinition delegate; + private final ServerMethodDefinition<Flight.Ticket, ArrowMessage> doGetMethod; + private final ServerMethodDefinition<ArrowMessage, Flight.PutResult> doPutMethod; + private final ServerMethodDefinition<ArrowMessage, ArrowMessage> doExchangeMethod; + private final AtomicInteger counter; + + @SuppressWarnings("unchecked") + FlightHandlerRegistry(BufferAllocator allocator, ServerServiceDefinition delegate) { + this.allocator = allocator; + this.delegate = delegate; + // Unchecked cast + this.doGetMethod = (ServerMethodDefinition<Flight.Ticket, ArrowMessage>) + Objects.requireNonNull(delegate.getMethod(FlightBindingService.DO_GET)); + this.doPutMethod = (ServerMethodDefinition<ArrowMessage, Flight.PutResult>) + Objects.requireNonNull(delegate.getMethod(FlightBindingService.DO_PUT)); + this.doExchangeMethod = (ServerMethodDefinition<ArrowMessage, ArrowMessage>) + Objects.requireNonNull(delegate.getMethod(FlightBindingService.DO_EXCHANGE)); + this.counter = new AtomicInteger(0); + } + + @Override + public ServerMethodDefinition<?, ?> lookupMethod(String methodName, String authority) { + if (FlightBindingService.DO_GET.equals(methodName)) { + final String allocatorName = allocator.getName() + "-DoGet-" + counter.getAndIncrement(); + final BufferAllocator childAllocator = allocator.newChildAllocator(allocatorName, 0, allocator.getLimit()); Review comment: i don't know if this is a concern, but does this potentially over reserve space on the parent allocator? I suppose this doesn't really change existing semantics? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
