keith-turner commented on code in PR #3904:
URL: https://github.com/apache/accumulo/pull/3904#discussion_r1376720158


##########
server/base/src/main/java/org/apache/accumulo/server/manager/state/TabletGoalState.java:
##########
@@ -0,0 +1,175 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.server.manager.state;
+
+import org.apache.accumulo.core.data.TabletId;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.dataImpl.TabletIdImpl;
+import org.apache.accumulo.core.manager.balancer.TabletServerIdImpl;
+import org.apache.accumulo.core.metadata.TServerInstance;
+import org.apache.accumulo.core.metadata.TabletState;
+import org.apache.accumulo.core.metadata.schema.Ample;
+import org.apache.accumulo.core.metadata.schema.TabletMetadata;
+import org.apache.accumulo.core.spi.balancer.TabletBalancer;
+import org.apache.accumulo.core.spi.balancer.data.TabletServerId;
+import org.apache.accumulo.core.tablet.thrift.TUnloadTabletGoal;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Preconditions;
+
+public enum TabletGoalState {
+
+  HOSTED(TUnloadTabletGoal.UNKNOWN),
+  UNASSIGNED(TUnloadTabletGoal.UNASSIGNED),
+  SUSPENDED(TUnloadTabletGoal.SUSPENDED);
+
+  private final TUnloadTabletGoal unloadGoal;
+
+  TabletGoalState(TUnloadTabletGoal unloadGoal) {
+    this.unloadGoal = unloadGoal;
+  }
+
+  /** The purpose of unloading this tablet. */
+  public TUnloadTabletGoal howUnload() {
+    return unloadGoal;
+  }
+
+  private static final Logger log = 
LoggerFactory.getLogger(TabletGoalState.class);
+
+  public static TabletGoalState compute(TabletMetadata tm, TabletState 
currentState,
+      TabletBalancer balancer, TabletManagementParameters params) {
+    Preconditions.checkArgument(Ample.DataLevel.of(tm.getTableId()) == 
params.getLevel(),
+        "Tablet %s not in expected level %s", tm.getExtent(), 
params.getLevel());
+
+    // Always follow through with assignments
+    if (currentState == TabletState.ASSIGNED) {
+      return HOSTED;
+    }
+
+    KeyExtent extent = tm.getExtent();
+    // Shutting down?
+    TabletGoalState state = getSystemGoalState(tm, params);
+
+    if (state == TabletGoalState.HOSTED) {
+      if (!params.isParentLevelUpgraded()) {
+        // The place where this tablet stores its metadata was not upgraded, 
so do not assign this
+        // tablet yet.
+        return TabletGoalState.UNASSIGNED;
+      }
+
+      if (tm.getOperationId() != null) {
+        return TabletGoalState.UNASSIGNED;
+      }
+
+      if (!params.isTableOnline(tm.getTableId())) {
+        return TabletGoalState.UNASSIGNED;
+      }
+
+      switch (tm.getHostingGoal()) {
+        case NEVER:
+          return TabletGoalState.UNASSIGNED;
+        case ONDEMAND:
+          if (!tm.getHostingRequested()) {
+            return TabletGoalState.UNASSIGNED;
+          }
+      }
+
+      TServerInstance dest = params.getMigrations().get(extent);
+      if (dest != null && tm.hasCurrent() && 
!dest.equals(tm.getLocation().getServerInstance())) {
+        return TabletGoalState.UNASSIGNED;
+      }
+
+      if (currentState == TabletState.HOSTED && balancer != null) {
+        // see if the balancer thinks this tablet needs to be unassigned
+
+        Preconditions.checkArgument(
+            tm.getLocation().getType() == TabletMetadata.LocationType.CURRENT,
+            "Expected current tablet location %s %s", tm.getExtent(), 
tm.getLocation());
+        var tsii = new 
TabletServerIdImpl(tm.getLocation().getServerInstance());
+
+        var resourceGroup = 
params.getResourceGroup(tm.getLocation().getServerInstance());
+
+        if (resourceGroup != null) {
+          var reassign = balancer.needsReassignment(new 
TabletBalancer.CurrentAssignment() {
+            @Override
+            public TabletId getTablet() {
+              return new TabletIdImpl(tm.getExtent());
+            }
+
+            @Override
+            public TabletServerId getTabletServer() {
+              return tsii;
+            }
+
+            @Override
+            public String getResourceGroup() {
+              return resourceGroup;
+            }
+          });
+
+          if (reassign) {
+            return UNASSIGNED;
+          }
+        } else {
+          // A tablet server should always have a resource group, however 
there is a race
+          // conditions where the resource group map was read before a tablet 
server came into
+          // existence. Another possible cause for an absent resource group is 
a bug in accumulo.
+          // In either case do not call the balancer for now with the 
assumption that the resource
+          // group will be available later. Log a message in case it is a bug.
+          log.trace(
+              "Could not find resource group for tserver {}, so did not 
consult balancer.  Assuming this is a temporary race condition.",
+              tm.getLocation().getServerInstance());
+        }
+      }
+
+      if (tm.hasCurrent()
+          && 
params.getServersToShutdown().contains(tm.getLocation().getServerInstance())) {
+        return TabletGoalState.SUSPENDED;

Review Comment:
   done in 1f10135



##########
server/base/src/main/java/org/apache/accumulo/server/manager/state/TabletManagementParameters.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.server.manager.state;
+
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toMap;
+import static java.util.stream.Collectors.toSet;
+import static java.util.stream.Collectors.toUnmodifiableMap;
+import static java.util.stream.Collectors.toUnmodifiableSet;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+
+import org.apache.accumulo.core.data.AbstractId;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.dataImpl.KeyExtent;
+import org.apache.accumulo.core.manager.balancer.TabletServerIdImpl;
+import org.apache.accumulo.core.manager.thrift.ManagerState;
+import org.apache.accumulo.core.metadata.TServerInstance;
+import org.apache.accumulo.core.metadata.schema.Ample;
+import org.apache.accumulo.core.spi.balancer.data.TabletServerId;
+import org.apache.hadoop.io.DataInputBuffer;
+import org.apache.hadoop.io.DataOutputBuffer;
+
+import com.google.common.base.Suppliers;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+/**
+ * An immutable snapshot of the information needed by the TabletGroupWatcher 
and the
+ * {@link TabletManagementIterator} to make decisions about tablets.
+ */
+public class TabletManagementParameters {
+
+  private final ManagerState managerState;
+  private final Map<Ample.DataLevel,Boolean> parentUpgradeMap;
+  private final Set<TableId> onlineTables;
+  private final Set<TServerInstance> serversToShutdown;
+  private final Map<KeyExtent,TServerInstance> migrations;
+
+  private final Ample.DataLevel level;
+
+  private final Supplier<Map<TServerInstance,String>> resourceGroups;
+  private final Map<String,Set<TServerInstance>> tserverGroups;
+  private final Map<Long,Map<String,String>> compactionHints;
+  private final Set<TServerInstance> onlineTservers;
+
+  public TabletManagementParameters(ManagerState managerState,
+      Map<Ample.DataLevel,Boolean> parentUpgradeMap, Set<TableId> onlineTables,
+      Set<TServerInstance> onlineTservers, Set<TServerInstance> 
serversToShutdown,
+      Map<KeyExtent,TServerInstance> migrations, 
Map<String,Set<TServerInstance>> tserverGroups,
+      Ample.DataLevel level, Map<Long,Map<String,String>> compactionHints) {
+    this.managerState = managerState;
+    this.parentUpgradeMap = Map.copyOf(parentUpgradeMap);
+    // TODO could filter by level
+    this.onlineTables = Set.copyOf(onlineTables);
+    this.onlineTservers = Set.copyOf(onlineTservers);
+    this.serversToShutdown = Set.copyOf(serversToShutdown);
+    // TODO could filter by level
+    this.migrations = Map.copyOf(migrations);
+    this.level = level;
+    this.tserverGroups = tserverGroups.entrySet().stream()
+        .collect(toUnmodifiableMap(Map.Entry::getKey, entry -> 
Set.copyOf(entry.getValue())));
+    this.compactionHints = makeImmutable(compactionHints);
+    this.resourceGroups = Suppliers.memoize(() -> {
+      Map<TServerInstance,String> resourceGroups = new HashMap<>();
+      TabletManagementParameters.this.tserverGroups.forEach((resourceGroup, 
tservers) -> tservers
+          .forEach(tserver -> resourceGroups.put(tserver, resourceGroup)));
+      return Map.copyOf(resourceGroups);
+    });
+  }
+
+  private TabletManagementParameters(JsonData jdata) {
+    this.managerState = jdata.managerState;
+    this.parentUpgradeMap = Map.copyOf(jdata.parentUpgradeMap);
+    this.onlineTables = 
jdata.onlineTables.stream().map(TableId::of).collect(toUnmodifiableSet());
+    this.onlineTservers =
+        
jdata.onlineTservers.stream().map(TServerInstance::new).collect(toUnmodifiableSet());
+    this.serversToShutdown =
+        
jdata.serversToShutdown.stream().map(TServerInstance::new).collect(toUnmodifiableSet());
+    this.migrations = jdata.migrations.entrySet().stream()
+        .collect(toUnmodifiableMap(entry -> 
JsonData.strToExtent(entry.getKey()),
+            entry -> new TServerInstance(entry.getValue())));
+    this.level = jdata.level;
+    this.compactionHints = makeImmutable(jdata.compactionHints);
+    this.tserverGroups = 
jdata.tserverGroups.entrySet().stream().collect(toUnmodifiableMap(
+        Map.Entry::getKey,
+        entry -> 
entry.getValue().stream().map(TServerInstance::new).collect(toUnmodifiableSet())));
+    this.resourceGroups = Suppliers.memoize(() -> {
+      Map<TServerInstance,String> resourceGroups = new HashMap<>();
+      TabletManagementParameters.this.tserverGroups.forEach((resourceGroup, 
tservers) -> tservers
+          .forEach(tserver -> resourceGroups.put(tserver, resourceGroup)));
+      return Map.copyOf(resourceGroups);
+    });
+  }
+
+  public ManagerState getManagerState() {
+    return managerState;
+  }
+
+  public boolean isParentLevelUpgraded() {
+    return parentUpgradeMap.get(level);
+  }
+
+  public Set<TServerInstance> getOnlineTsevers() {

Review Comment:
   done in 1f10135



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