HolyLow commented on code in PR #3515: URL: https://github.com/apache/celeborn/pull/3515#discussion_r2468067413
########## cpp/celeborn/client/writer/PushState.cpp: ########## @@ -0,0 +1,147 @@ +/* + * 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. + */ + +#include "celeborn/client/writer/PushState.h" + +namespace celeborn { +namespace client { + +PushState::PushState(const conf::CelebornConf& conf) + : waitInflightTimeoutMs_(conf.clientPushLimitInFlightTimeoutMs()), + deltaMs_(conf.clientPushLimitInFlightSleepDeltaMs()), + pushStrategy_(PushStrategy::create(conf)), + maxInFlightReqsTotal_(conf.clientPushMaxReqsInFlightTotal()) {} + +int PushState::nextBatchId() { + return currBatchId_.fetch_add(1); +} + +void PushState::addBatch(int batchId, const std::string& hostAndPushPort) { + auto batchIdSet = inflightBatchesPerAddress_.computeIfAbsent( + hostAndPushPort, + [&]() { return std::make_shared<utils::ConcurrentHashSet<int>>(); }); + batchIdSet->insert(batchId); + totalInflightReqs_.fetch_add(1); +} + +void PushState::onSuccess(const std::string& hostAndPushPort) { + pushStrategy_->onSuccess(hostAndPushPort); +} + +void PushState::onCongestControl(const std::string& hostAndPushPort) { + pushStrategy_->onCongestControl(hostAndPushPort); +} + +void PushState::removeBatch(int batchId, const std::string& hostAndPushPort) { + auto batchIdSetOptional = inflightBatchesPerAddress_.get(hostAndPushPort); + if (batchIdSetOptional.has_value()) { + auto batchIdSet = batchIdSetOptional.value(); + batchIdSet->erase(batchId); + totalInflightReqs_.fetch_sub(1); + } else { + LOG(WARNING) << "BatchIdSet of " << hostAndPushPort << " doesn't exist."; + } +} + +bool PushState::limitMaxInFlight(const std::string& hostAndPushPort) { + throwIfExceptionExists(); + + pushStrategy_->limitPushSpeed(*this, hostAndPushPort); + int currentMaxReqsInFlight = + pushStrategy_->getCurrentMaxReqsInFlight(hostAndPushPort); + + auto batchIdSet = inflightBatchesPerAddress_.computeIfAbsent( + hostAndPushPort, + [&]() { return std::make_shared<utils::ConcurrentHashSet<int>>(); }); + long times = waitInflightTimeoutMs_ / deltaMs_; + for (; times > 0; times--) { + if (totalInflightReqs_ <= maxInFlightReqsTotal_ && + batchIdSet->size() <= currentMaxReqsInFlight) { + break; + } + throwIfExceptionExists(); + std::this_thread::sleep_for(utils::MS(deltaMs_)); + } + + if (times <= 0) { + LOG(WARNING) << "After waiting for " << waitInflightTimeoutMs_ + << " ms, there are still " << batchIdSet->size() + << " batches in flight for hostAndPushPort " << hostAndPushPort + << ", which exceeds the current limit " + << currentMaxReqsInFlight; + } + throwIfExceptionExists(); + return times <= 0; +} + +bool PushState::limitZeroInFlight() { + throwIfExceptionExists(); + + long times = waitInflightTimeoutMs_ / deltaMs_; + for (; times > 0; times--) { + if (totalInflightReqs_ <= 0) { + break; + } + throwIfExceptionExists(); + std::this_thread::sleep_for(utils::MS(deltaMs_)); + } + + if (times <= 0) { + // TODO: log with more detailed address msg. Review Comment: Refactored as suggested. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
