keith-turner commented on a change in pull request #1891: URL: https://github.com/apache/accumulo/pull/1891#discussion_r567979429
########## File path: core/src/main/java/org/apache/accumulo/core/manager/balancer/TabletServerIdImpl.java ########## @@ -0,0 +1,96 @@ +/* + * 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.manager.balancer; + +import static java.util.Objects.requireNonNull; + +import org.apache.accumulo.core.metadata.TServerInstance; +import org.apache.accumulo.core.spi.balancer.data.TabletServerId; +import org.apache.accumulo.core.util.HostAndPort; + +/** + * @since 2.1.0 + */ +public class TabletServerIdImpl implements TabletServerId { + private final TServerInstance tServerInstance; + + public static TabletServerIdImpl fromThrift(TServerInstance tsi) { + return (tsi == null) ? null : new TabletServerIdImpl(tsi); + } + + public TabletServerIdImpl(String host, int port, String session) { + requireNonNull(host); + this.tServerInstance = new TServerInstance(HostAndPort.fromParts(host, port), session); + } + + public TabletServerIdImpl(TServerInstance tServerInstance) { + this.tServerInstance = requireNonNull(tServerInstance); + } + + public String getHost() { + return tServerInstance.getHostAndPort().getHost(); + } + + public int getPort() { + return tServerInstance.getHostAndPort().getPort(); + } + + public String getSession() { Review comment: ```suggestion @Override public String getSession() { ``` ########## File path: server/manager/src/main/java/org/apache/accumulo/master/Master.java ########## @@ -1686,4 +1725,20 @@ public boolean isActiveService() { return masterInitialized.get(); } + void initializeBalancer() { + tabletBalancer = Property.createInstanceFromPropertyName(getConfiguration(), + Property.MANAGER_TABLET_BALANCER, TabletBalancer.class, new DefaultLoadBalancer()); + tabletBalancer.init(balancerEnvironment); Review comment: Wondering if there is a threading problem and the balancer should only be set on the instance var after init(). Also if init throws an exception, may not want it set. Wondering if tabletBalancer should be volatile. ```suggestion var localTabletBalancer = Property.createInstanceFromPropertyName(getConfiguration(), Property.MANAGER_TABLET_BALANCER, TabletBalancer.class, new DefaultLoadBalancer()); localTabletBalancer.init(balancerEnvironment); tabletBalancer = localTabletBalancer; ``` ########## File path: core/src/main/java/org/apache/accumulo/core/manager/balancer/TabletServerIdImpl.java ########## @@ -0,0 +1,96 @@ +/* + * 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.manager.balancer; + +import static java.util.Objects.requireNonNull; + +import org.apache.accumulo.core.metadata.TServerInstance; +import org.apache.accumulo.core.spi.balancer.data.TabletServerId; +import org.apache.accumulo.core.util.HostAndPort; + +/** + * @since 2.1.0 + */ +public class TabletServerIdImpl implements TabletServerId { + private final TServerInstance tServerInstance; + + public static TabletServerIdImpl fromThrift(TServerInstance tsi) { + return (tsi == null) ? null : new TabletServerIdImpl(tsi); + } + + public TabletServerIdImpl(String host, int port, String session) { + requireNonNull(host); + this.tServerInstance = new TServerInstance(HostAndPort.fromParts(host, port), session); + } + + public TabletServerIdImpl(TServerInstance tServerInstance) { + this.tServerInstance = requireNonNull(tServerInstance); + } + + public String getHost() { Review comment: ```suggestion @Override public String getHost() { ``` ########## File path: core/src/main/java/org/apache/accumulo/core/manager/balancer/TabletServerIdImpl.java ########## @@ -0,0 +1,96 @@ +/* + * 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.manager.balancer; + +import static java.util.Objects.requireNonNull; + +import org.apache.accumulo.core.metadata.TServerInstance; +import org.apache.accumulo.core.spi.balancer.data.TabletServerId; +import org.apache.accumulo.core.util.HostAndPort; + +/** + * @since 2.1.0 + */ +public class TabletServerIdImpl implements TabletServerId { + private final TServerInstance tServerInstance; + + public static TabletServerIdImpl fromThrift(TServerInstance tsi) { + return (tsi == null) ? null : new TabletServerIdImpl(tsi); + } + + public TabletServerIdImpl(String host, int port, String session) { + requireNonNull(host); + this.tServerInstance = new TServerInstance(HostAndPort.fromParts(host, port), session); + } + + public TabletServerIdImpl(TServerInstance tServerInstance) { + this.tServerInstance = requireNonNull(tServerInstance); + } + + public String getHost() { + return tServerInstance.getHostAndPort().getHost(); + } + + public int getPort() { Review comment: ```suggestion @Override public int getPort() { ``` ########## File path: core/src/main/java/org/apache/accumulo/core/spi/balancer/BalancerEnvironment.java ########## @@ -0,0 +1,88 @@ +/* + * 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 org.apache.accumulo.core.client.AccumuloException; +import org.apache.accumulo.core.client.AccumuloSecurityException; +import org.apache.accumulo.core.data.TableId; +import org.apache.accumulo.core.data.TabletId; +import org.apache.accumulo.core.spi.balancer.data.TabletServerId; +import org.apache.accumulo.core.spi.balancer.data.TabletStatistics; +import org.apache.accumulo.core.spi.common.ServiceEnvironment; + +/** + * This interface is an extension of {@link ServiceEnvironment} that exposes system level + * information that is specific to tablet balancing. + * + * @since 2.1.0 + */ +public interface BalancerEnvironment extends ServiceEnvironment { + /** + * Many Accumulo plugins are given table IDs as this is what Accumulo uses internally to identify + * tables. This provides a mapping of table names to table IDs for the purposes of translating + * and/or enumerating the existing tables. + */ + Map<String,TableId> getTableIdMap(); + + /** + * Accumulo plugins working with a table may need to know if the table is online or not before + * operating on it. + * + * @param tableId + * The id of the table to check. + * @return {@code true} if the table is online and {@code false} if not + */ + boolean isTableOnline(TableId tableId); + + /** + * Fetch the locations for each of {@code tableId}'s tablets from the metadata table. Review comment: Would be nice to document what the behavior is for tablets w/o a location. ########## File path: server/base/src/main/java/org/apache/accumulo/server/manager/balancer/BalancerEnvironmentImpl.java ########## @@ -0,0 +1,114 @@ +/* + * 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.stream.Collectors; + +import org.apache.accumulo.core.classloader.ClassLoaderUtil; +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.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(); Review comment: This may return null if a tablet has no location. ---------------------------------------------------------------- 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]
