yupeng9 commented on code in PR #8989: URL: https://github.com/apache/pinot/pull/8989#discussion_r909500588
########## pinot-common/src/main/java/org/apache/pinot/common/utils/config/TableGroupConfigUtils.java: ########## @@ -0,0 +1,59 @@ +/** + * 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.common.utils.config; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.lang.StringUtils; +import org.apache.helix.ZNRecord; +import org.apache.pinot.spi.config.table.TableGroupConfig; +import org.apache.pinot.spi.config.table.assignment.InstanceAssignmentConfig; +import org.apache.pinot.spi.utils.JsonUtils; + + +public class TableGroupConfigUtils { + private TableGroupConfigUtils() { + } + + public static ZNRecord toZNRecord(TableGroupConfig tableGroupConfig) + throws IOException { + Preconditions.checkArgument(tableGroupConfig != null, "Table group config cannot be null"); + Preconditions.checkArgument(StringUtils.isNotBlank(tableGroupConfig.getGroupName()), "Table group name cannot be " + + "blank"); + Preconditions.checkArgument(tableGroupConfig.getInstanceAssignmentConfig() != null, "Instance assignment config " + + "cannot be null for a table-group"); + String groupName = tableGroupConfig.getGroupName(); + ZNRecord znRecord = new ZNRecord(groupName); + Map<String, String> mapFields = new HashMap<>(); + mapFields.put("instanceAssignmentConfig", JsonUtils.objectToString(tableGroupConfig.getInstanceAssignmentConfig())); Review Comment: nit: define `instanceAssignmentConfig ` as constant ########## pinot-common/src/main/java/org/apache/pinot/common/metadata/ZKMetadataProvider.java: ########## @@ -59,6 +59,7 @@ private ZKMetadataProvider() { private static final String PROPERTYSTORE_SCHEMAS_PREFIX = "/SCHEMAS"; private static final String PROPERTYSTORE_INSTANCE_PARTITIONS_PREFIX = "/INSTANCE_PARTITIONS"; private static final String PROPERTYSTORE_TABLE_CONFIGS_PREFIX = "/CONFIGS/TABLE"; + private static final String PROPERTYSTORE_TABLE_GROUP_PREFIX = "/CONFIGS/TABLE_GROUPS"; Review Comment: shall this be `TABLE_GROUP` or `TABLE_GROUPS` ########## pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/ColocatedOfflineSegmentAssignment.java: ########## @@ -0,0 +1,128 @@ +/** + * 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.controller.helix.core.assignment.segment; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import javax.annotation.Nullable; +import org.apache.commons.configuration.Configuration; +import org.apache.helix.HelixManager; +import org.apache.pinot.common.assignment.InstancePartitions; +import org.apache.pinot.common.metadata.ZKMetadataProvider; +import org.apache.pinot.common.metadata.segment.SegmentZKMetadata; +import org.apache.pinot.common.tier.Tier; +import org.apache.pinot.segment.spi.partition.metadata.ColumnPartitionMetadata; +import org.apache.pinot.spi.config.table.ReplicaGroupStrategyConfig; +import org.apache.pinot.spi.config.table.TableConfig; +import org.apache.pinot.spi.config.table.assignment.InstancePartitionsType; +import org.apache.pinot.spi.utils.CommonConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class ColocatedOfflineSegmentAssignment implements SegmentAssignment { + private static final Logger LOGGER = LoggerFactory.getLogger(ColocatedOfflineSegmentAssignment.class); + + private HelixManager _helixManager; + private String _offlineTableName; + private int _replication; + private String _partitionColumn; + + @Override + public void init(HelixManager helixManager, TableConfig tableConfig) { + _helixManager = helixManager; + _offlineTableName = tableConfig.getTableName(); + _replication = tableConfig.getValidationConfig().getReplicationNumber(); + ReplicaGroupStrategyConfig replicaGroupStrategyConfig = + tableConfig.getValidationConfig().getReplicaGroupStrategyConfig(); + _partitionColumn = replicaGroupStrategyConfig != null ? replicaGroupStrategyConfig.getPartitionColumn() : null; + + if (_partitionColumn == null) { + LOGGER.error("Co-located segment assignment works only when a segment partition column is specified"); + } else { + LOGGER.info("Initialized OfflineSegmentAssignment with replication: {} and partition column: {} for table: {}", + _replication, _partitionColumn, _offlineTableName); + } + } + + @Override + public List<String> assignSegment(String segmentName, Map<String, Map<String, String>> currentAssignment, + Map<InstancePartitionsType, InstancePartitions> instancePartitionsMap) { + SegmentZKMetadata segmentZKMetadata = ZKMetadataProvider + .getSegmentZKMetadata(_helixManager.getHelixPropertyStore(), _offlineTableName, segmentName); + Preconditions + .checkState(segmentZKMetadata != null, "Failed to find segment ZK metadata for segment: %s of table: %s", + segmentName, _offlineTableName); + int segmentPartitionId = getPartitionId(segmentZKMetadata); + return SegmentAssignmentUtils.assignSegmentForColocatedTable( + instancePartitionsMap.get(InstancePartitionsType.OFFLINE), segmentPartitionId); + } + + @Override + public Map<String, Map<String, String>> rebalanceTable(Map<String, Map<String, String>> currentAssignment, + Map<InstancePartitionsType, InstancePartitions> instancePartitionsMap, @Nullable List<Tier> sortedTiers, + @Nullable Map<String, InstancePartitions> tierInstancePartitionsMap, Configuration config) { + InstancePartitions instancePartitions = instancePartitionsMap.get(InstancePartitionsType.OFFLINE); Review Comment: can you explain this a bit? why we fetch OFFLINE only, and later update them to ONLINE? ########## pinot-common/src/main/java/org/apache/pinot/common/utils/config/TableGroupConfigUtils.java: ########## @@ -0,0 +1,59 @@ +/** + * 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.common.utils.config; + +import com.google.common.base.Preconditions; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import org.apache.commons.lang.StringUtils; +import org.apache.helix.ZNRecord; +import org.apache.pinot.spi.config.table.TableGroupConfig; +import org.apache.pinot.spi.config.table.assignment.InstanceAssignmentConfig; +import org.apache.pinot.spi.utils.JsonUtils; + + +public class TableGroupConfigUtils { + private TableGroupConfigUtils() { + } + + public static ZNRecord toZNRecord(TableGroupConfig tableGroupConfig) + throws IOException { + Preconditions.checkArgument(tableGroupConfig != null, "Table group config cannot be null"); + Preconditions.checkArgument(StringUtils.isNotBlank(tableGroupConfig.getGroupName()), "Table group name cannot be " + + "blank"); + Preconditions.checkArgument(tableGroupConfig.getInstanceAssignmentConfig() != null, "Instance assignment config " + + "cannot be null for a table-group"); + String groupName = tableGroupConfig.getGroupName(); + ZNRecord znRecord = new ZNRecord(groupName); + Map<String, String> mapFields = new HashMap<>(); + mapFields.put("instanceAssignmentConfig", JsonUtils.objectToString(tableGroupConfig.getInstanceAssignmentConfig())); Review Comment: nit: define `instanceAssignmentConfig ` as constant ########## pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/Constants.java: ########## @@ -35,6 +35,7 @@ private Constants() { public static final String CLUSTER_TAG = "Cluster"; public static final String TABLE_TAG = "Table"; + public static final String TABLE_GROUP_TAG = "Groups"; Review Comment: Group? ########## pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotTableGroupRestletResource.java: ########## @@ -0,0 +1,167 @@ +/** + * 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.controller.api.resources; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import java.io.IOException; +import java.util.List; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.apache.helix.AccessOption; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.ZNRecord; +import org.apache.pinot.common.assignment.InstancePartitions; +import org.apache.pinot.common.assignment.InstancePartitionsUtils; +import org.apache.pinot.common.metadata.ZKMetadataProvider; +import org.apache.pinot.common.utils.config.TableGroupConfigUtils; +import org.apache.pinot.controller.api.exception.ControllerApplicationException; +import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; +import org.apache.pinot.controller.helix.core.assignment.instance.InstanceAssignmentDriver; +import org.apache.pinot.spi.config.table.TableGroupConfig; +import org.apache.pinot.spi.utils.JsonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Api(tags = Constants.TABLE_GROUP_TAG) +@Path("/") +public class PinotTableGroupRestletResource { + public static final Logger LOGGER = LoggerFactory.getLogger(PinotTableGroupRestletResource.class); + + @Inject + PinotHelixResourceManager _pinotHelixResourceManager; + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/groups") + @ApiOperation(value = "Gets list of all table-groups") + public String listTableGroups() { + List<String> groups = ZKMetadataProvider.getAllGroups(_pinotHelixResourceManager.getPropertyStore()); + return JsonUtils.newObjectNode().set("groups", JsonUtils.objectToJsonNode(groups)).toString(); + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + @Path("/groups") + @ApiOperation(value = "Creates a new table group") + public SuccessResponse createTableGroup(String groupConfigStr) { Review Comment: so we are able to create an empty group without any tables in it? ########## pinot-controller/src/main/java/org/apache/pinot/controller/api/resources/PinotTableGroupRestletResource.java: ########## @@ -0,0 +1,167 @@ +/** + * 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.controller.api.resources; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import java.io.IOException; +import java.util.List; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import org.apache.helix.AccessOption; +import org.apache.helix.HelixDataAccessor; +import org.apache.helix.ZNRecord; +import org.apache.pinot.common.assignment.InstancePartitions; +import org.apache.pinot.common.assignment.InstancePartitionsUtils; +import org.apache.pinot.common.metadata.ZKMetadataProvider; +import org.apache.pinot.common.utils.config.TableGroupConfigUtils; +import org.apache.pinot.controller.api.exception.ControllerApplicationException; +import org.apache.pinot.controller.helix.core.PinotHelixResourceManager; +import org.apache.pinot.controller.helix.core.assignment.instance.InstanceAssignmentDriver; +import org.apache.pinot.spi.config.table.TableGroupConfig; +import org.apache.pinot.spi.utils.JsonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Api(tags = Constants.TABLE_GROUP_TAG) +@Path("/") +public class PinotTableGroupRestletResource { + public static final Logger LOGGER = LoggerFactory.getLogger(PinotTableGroupRestletResource.class); + + @Inject + PinotHelixResourceManager _pinotHelixResourceManager; + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/groups") + @ApiOperation(value = "Gets list of all table-groups") + public String listTableGroups() { + List<String> groups = ZKMetadataProvider.getAllGroups(_pinotHelixResourceManager.getPropertyStore()); + return JsonUtils.newObjectNode().set("groups", JsonUtils.objectToJsonNode(groups)).toString(); + } + + @POST + @Produces(MediaType.APPLICATION_JSON) + @Path("/groups") + @ApiOperation(value = "Creates a new table group") + public SuccessResponse createTableGroup(String groupConfigStr) { + try { + TableGroupConfig tableGroupConfig = JsonUtils.stringToObject(groupConfigStr, TableGroupConfig.class); + String groupName = tableGroupConfig.getGroupName(); + HelixDataAccessor helixDataAccessor = _pinotHelixResourceManager.getHelixZkManager().getHelixDataAccessor(); + InstancePartitions instancePartitions = InstanceAssignmentDriver.assignInstancesToGroup(groupName, + helixDataAccessor.getChildValues(helixDataAccessor.keyBuilder().instanceConfigs(), true), + tableGroupConfig.getInstanceAssignmentConfig()); + _pinotHelixResourceManager.addTableGroup(groupName, tableGroupConfig); + InstancePartitionsUtils.persistGroupInstancePartitions(_pinotHelixResourceManager.getPropertyStore(), + groupName, instancePartitions); + return new SuccessResponse(String.format("Group %s successfully created", groupName)); + } catch (Exception e) { + throw new ControllerApplicationException(LOGGER, e.getMessage(), Response.Status.BAD_REQUEST, e); + } + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + @Path("/groups/{groupName}") + @ApiOperation(value = "Gets config for a given table-group") + public String getTableGroup(@ApiParam(value = "name of group") @PathParam("groupName") String groupName) { + try { + TableGroupConfig tableGroupConfig = getTableGroupConfig(groupName); + return JsonUtils.objectToString(tableGroupConfig); + } catch (IOException e) { + throw new ControllerApplicationException(LOGGER, e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR, e); + } + } + + @PUT + @Produces(MediaType.APPLICATION_JSON) + @Path("/groups/{groupName}") + @ApiOperation(value = "Updates the configuration for a table-group") + public SuccessResponse updateTableGroup(String groupConfigStr, Review Comment: how do we add/remove more tables to an existing group? -- 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]
