vvivekiyer commented on code in PR #15798:
URL: https://github.com/apache/pinot/pull/15798#discussion_r2114792832


##########
pinot-core/src/main/java/org/apache/pinot/core/accounting/WorkloadBudgetManager.java:
##########
@@ -0,0 +1,229 @@
+/**
+ * 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.core.accounting;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import javax.annotation.Nullable;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class WorkloadBudgetManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(WorkloadBudgetManager.class);
+
+  private static final AtomicReference<WorkloadBudgetManager> INSTANCE = new 
AtomicReference<>();
+
+  private long _enforcementWindowMs;
+  private final ConcurrentHashMap<String, Budget> _workloadBudgets = new 
ConcurrentHashMap<>();
+  private final ScheduledExecutorService _resetScheduler = 
Executors.newSingleThreadScheduledExecutor();
+  private volatile boolean _isEnabled;
+
+  public static void init(PinotConfiguration config) {
+    if (INSTANCE.compareAndSet(null, new WorkloadBudgetManager(config))) {
+      LOGGER.info("Initialized WorkloadBudgetManager");
+    } else {
+      LOGGER.error("WorkloadBudgetManager is already initialized, not 
initializing it again");
+    }
+  }
+
+  public void shutdown() {
+    if (!_isEnabled) {
+      return;
+    }
+
+    _isEnabled = false;
+    _resetScheduler.shutdownNow();
+    try {
+      if (!_resetScheduler.awaitTermination(5, TimeUnit.SECONDS)) {
+        LOGGER.warn("Reset scheduler did not terminate in time");
+      }
+    } catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+    }
+
+    INSTANCE.set(null);
+    LOGGER.info("WorkloadBudgetManager has been shut down.");
+  }
+
+  @Nullable
+  public static WorkloadBudgetManager getInstance() {
+    return INSTANCE.get();
+  }
+
+  private WorkloadBudgetManager(PinotConfiguration config) {
+    _isEnabled = 
config.getProperty(CommonConstants.Accounting.CONFIG_OF_WORKLOAD_ENABLE_COST_COLLECTION,

Review Comment:
   We need a config to not enable the BudgetResetTask. This 
WorkloadBudgetManager will be used in other areas of our code like Scheduler 
when we build a scheduler based on workload budgets. 



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to