keith-turner commented on a change in pull request #1891:
URL: https://github.com/apache/accumulo/pull/1891#discussion_r567306759



##########
File path: 
server/base/src/main/java/org/apache/accumulo/server/manager/balancer/BalancerEnvironmentImpl.java
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.accumulo.server.manager.balancer;
+
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.LOCATION;
+import static 
org.apache.accumulo.core.metadata.schema.TabletMetadata.ColumnType.PREV_ROW;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.apache.accumulo.core.client.AccumuloException;
+import org.apache.accumulo.core.client.AccumuloSecurityException;
+import org.apache.accumulo.core.clientImpl.Tables;
+import org.apache.accumulo.core.clientImpl.thrift.ThriftSecurityException;
+import org.apache.accumulo.core.data.TableId;
+import org.apache.accumulo.core.data.TabletId;
+import org.apache.accumulo.core.dataImpl.TabletIdImpl;
+import org.apache.accumulo.core.manager.balancer.TabletServerIdImpl;
+import org.apache.accumulo.core.manager.balancer.TabletStatisticsImpl;
+import org.apache.accumulo.core.master.state.tables.TableState;
+import org.apache.accumulo.core.metadata.TServerInstance;
+import org.apache.accumulo.core.metadata.schema.TabletsMetadata;
+import org.apache.accumulo.core.rpc.ThriftUtil;
+import org.apache.accumulo.core.spi.balancer.BalancerEnvironment;
+import org.apache.accumulo.core.spi.balancer.data.TabletMigration;
+import org.apache.accumulo.core.spi.balancer.data.TabletServerId;
+import org.apache.accumulo.core.spi.balancer.data.TabletStatistics;
+import org.apache.accumulo.core.tabletserver.thrift.TabletClientService;
+import org.apache.accumulo.core.trace.TraceUtil;
+import org.apache.accumulo.core.util.HostAndPort;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.server.ServiceEnvironmentImpl;
+import org.apache.thrift.TException;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class BalancerEnvironmentImpl extends ServiceEnvironmentImpl implements 
BalancerEnvironment {
+  private static final Logger log = 
LoggerFactory.getLogger(BalancerEnvironmentImpl.class);
+
+  public BalancerEnvironmentImpl(ServerContext ctx) {
+    super(ctx);
+  }
+
+  @Override
+  public Map<String,TableId> getTableIdMap() {
+    return Tables.getNameToIdMap(getContext());
+  }
+
+  @Override
+  public boolean isTableOnline(TableId tableId) {
+    return TableState.ONLINE.equals(Tables.getTableState(getContext(), 
tableId));
+  }
+
+  @Override
+  public Map<TabletId,TabletServerId> listTabletLocations(TableId tableId) {
+    Map<TabletId,TabletServerId> tablets = new LinkedHashMap<>();
+    for (var tm : TabletsMetadata.builder().forTable(tableId).fetch(LOCATION, 
PREV_ROW)
+        .build(getContext())) {
+      TServerInstance inst = tm.getLocation();
+      tablets.put(new TabletIdImpl(tm.getExtent()), new 
TabletServerIdImpl(inst.getHost(),
+          inst.getHostAndPort().getPort(), inst.getSession()));
+    }
+    return tablets;
+  }
+
+  @Override
+  public List<TabletStatistics> listOnlineTabletsForTable(TabletServerId 
tabletServerId,
+      TableId tableId) throws AccumuloException, AccumuloSecurityException {
+    log.debug("Scanning tablet server {} for table {}", tabletServerId, 
tableId);
+    try {
+      TabletClientService.Client client = ThriftUtil.getClient(
+          new TabletClientService.Client.Factory(),
+          HostAndPort.fromParts(tabletServerId.getHost(), 
tabletServerId.getPort()), getContext());
+      try {
+        return client
+            .getTabletStats(TraceUtil.traceInfo(), getContext().rpcCreds(), 
tableId.canonical())
+            
.stream().map(TabletStatisticsImpl::new).collect(Collectors.toList());
+      } catch (TTransportException e) {
+        log.error("Unable to connect to {}: ", tabletServerId, e);
+      } finally {
+        ThriftUtil.returnClient(client);
+      }
+    } catch (ThriftSecurityException e) {
+      throw new AccumuloSecurityException(e);
+    } catch (TException e) {
+      throw new AccumuloException(e);
+    }
+    return null;
+  }
+
+  @Override
+  public List<TabletMigration> checkMigrationSanity(Set<TabletServerId> 
current,

Review comment:
       Although this method is public in the old Balancer, I don't think it was 
intended for user user.  I think it was just meant to be internal sanity check 
code.  So maybe it does not need to exists in the new SPI interfaces.  It could 
be placed somewhere outside SPI for internal use.

##########
File path: 
core/src/main/java/org/apache/accumulo/core/spi/balancer/TabletBalancer.java
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.accumulo.core.spi.balancer;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.SortedMap;
+
+import org.apache.accumulo.core.conf.Property;
+import org.apache.accumulo.core.data.TabletId;
+import org.apache.accumulo.core.spi.balancer.data.TServerStatus;
+import org.apache.accumulo.core.spi.balancer.data.TabletMigration;
+import org.apache.accumulo.core.spi.balancer.data.TabletServerId;
+
+/**
+ * This class is responsible for managing the distribution of tablets 
throughout an Accumulo
+ * cluster. In most cases, users will want a balancer implementation which 
ensures a uniform
+ * distribution of tablets, so that no individual tablet server is handling 
significantly more work
+ * than any other.
+ *
+ * <p>
+ * Implementations may wish to store configuration in Accumulo's system 
configuration using the
+ * {@link Property#GENERAL_ARBITRARY_PROP_PREFIX}. They may also benefit from 
using per-table
+ * configuration using {@link Property#TABLE_ARBITRARY_PROP_PREFIX}.
+ *
+ * @since 2.1.0
+ */
+public interface TabletBalancer {
+
+  /**
+   * An interface for grouping parameters required for the balancer to assign 
unassigned tablets.
+   * This interface allows for evolution of the parameter set without changing 
the balancer's method
+   * signature.
+   *
+   * @since 2.1.0
+   */
+  interface AssignmentParameters {
+    /**
+     * @return the current status for all tablet servers (read-only)
+     */
+    SortedMap<TabletServerId,TServerStatus> currentStatus();
+
+    /**
+     * @return the tablets that need to be assigned, mapped to their previous 
known location
+     *         (read-only)
+     */
+    Map<TabletId,TabletServerId> unassignedTablets();
+
+    /**
+     * @return a write-only map for storing new assignments
+     */
+    Map<TabletId,TabletServerId> assignmentsOut();
+  }
+
+  /**
+   * An interface for grouping parameters required for the balancer to balance 
tablets. This
+   * interface allows for evolution of the parameter set without changing the 
balancer's method
+   * signature.
+   *
+   * @since 2.1.0
+   */
+  interface BalanceParameters {
+    /**
+     * @return the current status for all tablet servers (read-only)
+     */
+    SortedMap<TabletServerId,TServerStatus> currentStatus();
+
+    /**
+     * @return the migrations that are currently in progress (read-only)
+     */
+    Set<TabletId> currentMigrations();
+
+    /**
+     * @return a write-only map for storing new assignments made by the 
balancer. It is important
+     *         that any tablets found in {@link #currentMigrations()} are not 
included in the output
+     *         migrations.
+     */
+    List<TabletMigration> migrationsOut();

Review comment:
       Could possibly do the following instead. Maybe its better to just make 
the new SPI interfaces follow the patterns of the old ones for this PR and then 
have a second PR that focuses on improving the new balancer SPI.
   
   ```suggestion
       addMigration(TabletMigration);
   ```

##########
File path: server/manager/src/main/java/org/apache/accumulo/master/Master.java
##########
@@ -1686,4 +1741,56 @@ public boolean isActiveService() {
     return masterInitialized.get();
   }
 
+  @SuppressWarnings("deprecation")
+  void initializeBalancer() {
+
+    // Try to initialize the defined balancer as the updated TabletBalancer 
class first. If that
+    // fails with a ClassCastException, it means the property is specified as 
the deprecated
+    // balancer type, so initialize it instead. If we still end up with no 
balancer, then use
+    // DefaultLoadBalancer.
+    try {
+      tabletBalancer = 
Property.createInstanceFromPropertyName(getConfiguration(),
+          Property.TABLE_LOAD_BALANCER, TabletBalancer.class, null);
+    } catch (ClassCastException e) {
+      // ignore -- this means that the deprecated balancer type was used
+      deprecatedTabletBalancer = 
Property.createInstanceFromPropertyName(getConfiguration(),
+          Property.MANAGER_TABLET_BALANCER,
+          org.apache.accumulo.server.master.balancer.TabletBalancer.class, 
null);
+    }

Review comment:
       I was curious about this and tried experimenting with it locally with 
the following approach.  Assuming we can make assumptions about the concrete 
types within Accumulo code and add needed methods to the concrete types.
   
   ```java
   @Deprecated(since = "2.1.0")
   public abstract class TabletBalancer implements 
org.apache.accumulo.core.spi.balancer.TabletBalancer {
   
     @Override
     public void init(BalancerEnvironment balancerEnvironment) {
       var bei = (BalancerEnvironmentImpl)balancerEnvironment;
       init(bei.getContext());
     }
   
     @Override
     public void getAssignments(AssignmentParameters params) {
       var api = (AssignmentParamsImpl)params; 
       Map<KeyExtent,TServerInstance> assignments = new HashMap<>();
       getAssignments(api.currentStatusOld(), api.unassignedTabletsOld(), 
assignments);
       assignments.forEach((ke, tsi) -> {
         params.assignmentsOut().put(new TabletIdImpl(ke), 
TabletServerIdImpl.fromThrift(tsi));
       });
     }
   
     @Override
     public long balance(BalanceParameters params) {
       var bpi = (BalanceParamsImpl) params;
       List<TabletMigration> migrationsOut = new ArrayList<>();
       balance(bpi.currentStatusOld(), bpi.currentMigrationsOld(), 
migrationsOut);
       migrationsOut.forEach(mo -> {
         params.migrationsOut()
             .add(new 
org.apache.accumulo.core.spi.balancer.data.TabletMigration(
                 new TabletIdImpl(mo.tablet), 
TabletServerIdImpl.fromThrift(mo.oldServer),
                 TabletServerIdImpl.fromThrift(mo.newServer)));
       });
     }
   ```

##########
File path: server/manager/src/main/java/org/apache/accumulo/master/Master.java
##########
@@ -1686,4 +1741,56 @@ public boolean isActiveService() {
     return masterInitialized.get();
   }
 
+  @SuppressWarnings("deprecation")
+  void initializeBalancer() {
+
+    // Try to initialize the defined balancer as the updated TabletBalancer 
class first. If that
+    // fails with a ClassCastException, it means the property is specified as 
the deprecated
+    // balancer type, so initialize it instead. If we still end up with no 
balancer, then use
+    // DefaultLoadBalancer.
+    try {
+      tabletBalancer = 
Property.createInstanceFromPropertyName(getConfiguration(),
+          Property.TABLE_LOAD_BALANCER, TabletBalancer.class, null);
+    } catch (ClassCastException e) {
+      // ignore -- this means that the deprecated balancer type was used

Review comment:
       It would be nice to log the exception, maybe at debug.  Could help a 
user figure out a problem.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to