dimas-b commented on code in PR #2728:
URL: https://github.com/apache/polaris/pull/2728#discussion_r2399578007


##########
persistence/nosql/nodes/impl/src/main/java/org/apache/polaris/nodes/impl/NodeManagementImpl.java:
##########
@@ -0,0 +1,630 @@
+/*
+ * 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.polaris.nodes.impl;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.lang.Integer.bitCount;
+
+import com.google.common.annotations.VisibleForTesting;
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.PreDestroy;
+import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.inject.Inject;
+import java.net.InterfaceAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.IntSupplier;
+import org.apache.polaris.ids.api.IdGenerator;
+import org.apache.polaris.ids.api.IdGeneratorSpec;
+import org.apache.polaris.ids.api.ImmutableIdGeneratorSpec;
+import org.apache.polaris.ids.api.MonotonicClock;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+import org.apache.polaris.nodes.api.ImmutableNode;
+import org.apache.polaris.nodes.api.Node;
+import org.apache.polaris.nodes.api.NodeLease;
+import org.apache.polaris.nodes.api.NodeManagement;
+import org.apache.polaris.nodes.api.NodeManagementConfig;
+import org.apache.polaris.nodes.spi.ImmutableBuildableNodeManagementState;
+import org.apache.polaris.nodes.spi.ImmutableNodeState;
+import org.apache.polaris.nodes.spi.NodeState;
+import org.apache.polaris.nodes.spi.NodeStore;
+import org.apache.polaris.nodes.spi.NodeStoreFactory;
+import org.apache.polaris.nosql.async.AsyncExec;
+import org.apache.polaris.nosql.async.Cancelable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@ApplicationScoped
+class NodeManagementImpl implements NodeManagement {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(NodeManagementImpl.class);
+
+  // should be a power of 2
+  private static final int CHECK_BATCH_SIZE = 16;
+  static final Duration RESCHEDULE_AFTER_FAILURE = Duration.ofSeconds(10);
+  static final Duration RESCHEDULE_UNTIL_EXPIRATION = Duration.ofMinutes(1);
+  static final Duration RENEWAL_MIN_LEFT_FOR_RENEWAL = Duration.ofSeconds(30);
+  private final NodeStore nodeStore;
+  private final NodeManagementConfig config;
+  private final MonotonicClock clock;
+  private final int numNodeIds;
+  private final IdGeneratorFactory<?> idGenFactory;
+  private final IdGeneratorSpec idGenSpec;
+
+  private final Set<NodeLeaseImpl> registeredLeases = 
ConcurrentHashMap.newKeySet();
+  private final IdGenerator systemIdGen;
+  private final AsyncExec scheduler;
+
+  private volatile boolean closed;
+
+  @SuppressWarnings("CdiInjectionPointsInspection")
+  @Inject
+  NodeManagementImpl(
+      NodeManagementConfig config,
+      MonotonicClock clock,
+      NodeStoreFactory nodeStoreFactory,
+      AsyncExec scheduler) {
+    var activePeriod = config.leaseDuration().minus(config.renewalPeriod());
+    this.numNodeIds = config.numNodes();
+    checkArgs(
+        () ->
+            checkArgument(
+                config.leaseDuration().compareTo(Duration.ofMinutes(15)) > 0,
+                "leaseDuration must be at least 15 minutes"),
+        () ->
+            checkArgument(
+                activePeriod.isPositive(), "leaseDuration must be greater than 
renewalPeriod"),
+        () ->
+            checkArgument(
+                activePeriod.compareTo(Duration.ofMinutes(15)) > 0,
+                "active period (leaseDuration - renewalPeriod) must be at 
least 15 minutes"),
+        () ->
+            checkArgument(
+                numNodeIds >= CHECK_BATCH_SIZE && bitCount(numNodeIds) == 1,
+                "numNodeIds %s must not be smaller than %s and a power of 2",
+                numNodeIds,
+                CHECK_BATCH_SIZE));
+    this.config = config;
+    this.clock = clock;
+    this.scheduler = scheduler;
+
+    var idGenSpec =
+        (IdGeneratorSpec) 
ImmutableIdGeneratorSpec.builder().from(config.idGeneratorSpec()).build();
+    var validationIdGeneratorSource =
+        new IdGeneratorSource() {
+          @Override
+          public long currentTimeMillis() {
+            return clock.currentTimeMillis();
+          }
+
+          @Override
+          public int nodeId() {
+            return 0;
+          }
+        };
+    while (true) {

Review Comment:
   thx :+1: 



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

Reply via email to