walterddr commented on code in PR #9773: URL: https://github.com/apache/pinot/pull/9773#discussion_r1022129096
########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java: ########## @@ -49,57 +47,67 @@ */ public class MailboxSendOperator extends BaseOperator<TransferableBlock> { private static final Logger LOGGER = LoggerFactory.getLogger(MailboxSendOperator.class); + private static final String EXPLAIN_NAME = "MAILBOX_SEND"; private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE = ImmutableSet.of(RelDistribution.Type.SINGLETON, RelDistribution.Type.RANDOM_DISTRIBUTED, RelDistribution.Type.BROADCAST_DISTRIBUTED, RelDistribution.Type.HASH_DISTRIBUTED); - private static final Random RANDOM = new Random(); - // TODO: Deduct this value via grpc config maximum byte size; and make it configurable with override. - // TODO: Max block size is a soft limit. only counts fixedSize datatable byte buffer - private static final int MAX_MAILBOX_CONTENT_SIZE_BYTES = 4 * 1024 * 1024; - - private final List<ServerInstance> _receivingStageInstances; - private final RelDistribution.Type _exchangeType; - private final KeySelector<Object[], Object[]> _keySelector; - private final String _serverHostName; - private final int _serverPort; - private final long _jobId; - private final int _stageId; - private final MailboxService<TransferableBlock> _mailboxService; - private final DataSchema _dataSchema; - private final boolean _isLeafStageSender; - private Operator<TransferableBlock> _dataTableBlockBaseOperator; - - public MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, DataSchema dataSchema, + + private final Operator<TransferableBlock> _dataTableBlockBaseOperator; + private final BlockExchange _exchange; + + @VisibleForTesting + interface BlockExchangeFactory { + BlockExchange build(MailboxService<TransferableBlock> mailboxService, List<MailboxIdentifier> destinations, + RelDistribution.Type exchange, KeySelector<Object[], Object[]> selector, BlockSplitter splitter); + } + + @VisibleForTesting + interface MailboxIdGenerator { + MailboxIdentifier generate(ServerInstance server); + } Review Comment: great idea! ########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java: ########## @@ -49,57 +47,67 @@ */ public class MailboxSendOperator extends BaseOperator<TransferableBlock> { private static final Logger LOGGER = LoggerFactory.getLogger(MailboxSendOperator.class); + private static final String EXPLAIN_NAME = "MAILBOX_SEND"; private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE = ImmutableSet.of(RelDistribution.Type.SINGLETON, RelDistribution.Type.RANDOM_DISTRIBUTED, RelDistribution.Type.BROADCAST_DISTRIBUTED, RelDistribution.Type.HASH_DISTRIBUTED); - private static final Random RANDOM = new Random(); - // TODO: Deduct this value via grpc config maximum byte size; and make it configurable with override. - // TODO: Max block size is a soft limit. only counts fixedSize datatable byte buffer - private static final int MAX_MAILBOX_CONTENT_SIZE_BYTES = 4 * 1024 * 1024; - - private final List<ServerInstance> _receivingStageInstances; - private final RelDistribution.Type _exchangeType; - private final KeySelector<Object[], Object[]> _keySelector; - private final String _serverHostName; - private final int _serverPort; - private final long _jobId; - private final int _stageId; - private final MailboxService<TransferableBlock> _mailboxService; - private final DataSchema _dataSchema; - private final boolean _isLeafStageSender; - private Operator<TransferableBlock> _dataTableBlockBaseOperator; - - public MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, DataSchema dataSchema, + + private final Operator<TransferableBlock> _dataTableBlockBaseOperator; + private final BlockExchange _exchange; + + @VisibleForTesting + interface BlockExchangeFactory { + BlockExchange build(MailboxService<TransferableBlock> mailboxService, List<MailboxIdentifier> destinations, + RelDistribution.Type exchange, KeySelector<Object[], Object[]> selector, BlockSplitter splitter); + } + + @VisibleForTesting + interface MailboxIdGenerator { + MailboxIdentifier generate(ServerInstance server); + } + + public MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, Operator<TransferableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances, RelDistribution.Type exchangeType, KeySelector<Object[], Object[]> keySelector, String hostName, int port, long jobId, int stageId, boolean isLeafStageSender) { - _dataSchema = dataSchema; - _mailboxService = mailboxService; + this(mailboxService, dataTableBlockBaseOperator, receivingStageInstances, exchangeType, keySelector, + isLeafStageSender, server -> toMailboxId(server, jobId, stageId, hostName, port), BlockExchange::getExchange); + } + + @VisibleForTesting + MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, + Operator<TransferableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances, + RelDistribution.Type exchangeType, KeySelector<Object[], Object[]> keySelector, + boolean isLeafStageSender, MailboxIdGenerator mailboxIdGenerator, BlockExchangeFactory blockExchangeFactory) { _dataTableBlockBaseOperator = dataTableBlockBaseOperator; - _exchangeType = exchangeType; - if (_exchangeType == RelDistribution.Type.SINGLETON) { + + List<MailboxIdentifier> receivingMailboxes; + if (exchangeType == RelDistribution.Type.SINGLETON) { + // TODO: this logic should be moved into SingletonExchange ServerInstance singletonInstance = null; for (ServerInstance serverInstance : receivingStageInstances) { - if (serverInstance.getHostname().equals(_mailboxService.getHostname()) - && serverInstance.getQueryMailboxPort() == _mailboxService.getMailboxPort()) { + if (serverInstance.getHostname().equals(mailboxService.getHostname()) + && serverInstance.getQueryMailboxPort() == mailboxService.getMailboxPort()) { Preconditions.checkState(singletonInstance == null, "multiple instance found for singleton exchange type!"); singletonInstance = serverInstance; } } Preconditions.checkNotNull(singletonInstance, "Unable to find receiving instance for singleton exchange"); - _receivingStageInstances = Collections.singletonList(singletonInstance); + receivingMailboxes = Collections.singletonList(mailboxIdGenerator.generate(singletonInstance)); } else { - _receivingStageInstances = receivingStageInstances; + receivingMailboxes = receivingStageInstances + .stream() + .map(mailboxIdGenerator::generate) + .collect(Collectors.toList()); } - _keySelector = keySelector; - _serverHostName = hostName; - _serverPort = port; - _jobId = jobId; - _stageId = stageId; - Preconditions.checkState(SUPPORTED_EXCHANGE_TYPE.contains(_exchangeType), - String.format("Exchange type '%s' is not supported yet", _exchangeType)); - _isLeafStageSender = isLeafStageSender; + + BlockSplitter splitter = (block, type, size) + -> TransferableBlockUtils.splitBlock(block, type, size, isLeafStageSender); + _exchange = blockExchangeFactory.build(mailboxService, receivingMailboxes, exchangeType, keySelector, splitter); + + Preconditions.checkState(SUPPORTED_EXCHANGE_TYPE.contains(exchangeType), + String.format("Exchange type '%s' is not supported yet", exchangeType)); Review Comment: why are we checking here instead of at the beginning? ########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/blocks/BlockSplitter.java: ########## @@ -0,0 +1,36 @@ +/** + * 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.pinot.query.runtime.blocks; + +import java.util.Iterator; +import org.apache.pinot.common.datablock.BaseDataBlock; + + +/** + * Interface for splitting transferable blocks. This is used for ensuring + * that the blocks that are sent along the wire play nicely with the + * underlying transport. + */ +public interface BlockSplitter { Review Comment: seems like we can also make this an inner interface in BlockExchange.java? any chance we will use this other than test purpose? ########## pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/MailboxSendOperator.java: ########## @@ -49,57 +47,67 @@ */ public class MailboxSendOperator extends BaseOperator<TransferableBlock> { private static final Logger LOGGER = LoggerFactory.getLogger(MailboxSendOperator.class); + private static final String EXPLAIN_NAME = "MAILBOX_SEND"; private static final Set<RelDistribution.Type> SUPPORTED_EXCHANGE_TYPE = ImmutableSet.of(RelDistribution.Type.SINGLETON, RelDistribution.Type.RANDOM_DISTRIBUTED, RelDistribution.Type.BROADCAST_DISTRIBUTED, RelDistribution.Type.HASH_DISTRIBUTED); - private static final Random RANDOM = new Random(); - // TODO: Deduct this value via grpc config maximum byte size; and make it configurable with override. - // TODO: Max block size is a soft limit. only counts fixedSize datatable byte buffer - private static final int MAX_MAILBOX_CONTENT_SIZE_BYTES = 4 * 1024 * 1024; - - private final List<ServerInstance> _receivingStageInstances; - private final RelDistribution.Type _exchangeType; - private final KeySelector<Object[], Object[]> _keySelector; - private final String _serverHostName; - private final int _serverPort; - private final long _jobId; - private final int _stageId; - private final MailboxService<TransferableBlock> _mailboxService; - private final DataSchema _dataSchema; - private final boolean _isLeafStageSender; - private Operator<TransferableBlock> _dataTableBlockBaseOperator; - - public MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, DataSchema dataSchema, + + private final Operator<TransferableBlock> _dataTableBlockBaseOperator; + private final BlockExchange _exchange; + + @VisibleForTesting + interface BlockExchangeFactory { + BlockExchange build(MailboxService<TransferableBlock> mailboxService, List<MailboxIdentifier> destinations, + RelDistribution.Type exchange, KeySelector<Object[], Object[]> selector, BlockSplitter splitter); + } + + @VisibleForTesting + interface MailboxIdGenerator { + MailboxIdentifier generate(ServerInstance server); + } + + public MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, Operator<TransferableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances, RelDistribution.Type exchangeType, KeySelector<Object[], Object[]> keySelector, String hostName, int port, long jobId, int stageId, boolean isLeafStageSender) { - _dataSchema = dataSchema; - _mailboxService = mailboxService; + this(mailboxService, dataTableBlockBaseOperator, receivingStageInstances, exchangeType, keySelector, + isLeafStageSender, server -> toMailboxId(server, jobId, stageId, hostName, port), BlockExchange::getExchange); + } + + @VisibleForTesting + MailboxSendOperator(MailboxService<TransferableBlock> mailboxService, + Operator<TransferableBlock> dataTableBlockBaseOperator, List<ServerInstance> receivingStageInstances, + RelDistribution.Type exchangeType, KeySelector<Object[], Object[]> keySelector, + boolean isLeafStageSender, MailboxIdGenerator mailboxIdGenerator, BlockExchangeFactory blockExchangeFactory) { _dataTableBlockBaseOperator = dataTableBlockBaseOperator; - _exchangeType = exchangeType; - if (_exchangeType == RelDistribution.Type.SINGLETON) { + + List<MailboxIdentifier> receivingMailboxes; + if (exchangeType == RelDistribution.Type.SINGLETON) { + // TODO: this logic should be moved into SingletonExchange Review Comment: any reason we can't create a SingletonExchange? in this PR? i won't imagine too much code change right? -- 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. To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org