ShiKaiWi commented on code in PR #286: URL: https://github.com/apache/incubator-horaedb-meta/pull/286#discussion_r1462807835
########## server/coordinator/persist_shard_picker.go: ########## @@ -0,0 +1,86 @@ +/* + * 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 coordinator + +import ( + "context" + + "github.com/apache/incubator-horaedb-meta/server/cluster/metadata" + "github.com/apache/incubator-horaedb-meta/server/storage" +) + +type PersistShardPicker struct { + cluster *metadata.ClusterMetadata + internal ShardPicker +} + +func NewPersistShardPicker(cluster *metadata.ClusterMetadata, internal ShardPicker) *PersistShardPicker { + return &PersistShardPicker{cluster: cluster, internal: internal} +} + +func (p *PersistShardPicker) PickShards(ctx context.Context, snapshot metadata.Snapshot, schemaName string, tableNames []string) (map[string]storage.ShardNode, error) { + result := map[string]storage.ShardNode{} + + shardNodeMap := map[storage.ShardID]storage.ShardNode{} + for _, shardNode := range snapshot.Topology.ClusterView.ShardNodes { + shardNodeMap[shardNode.ID] = shardNode + } + + var missingTables []string + // If table assign has been created, just reuse it. + for i := 0; i < len(tableNames); i++ { + shardID, exists, err := p.cluster.GetTableAssignedShard(ctx, schemaName, tableNames[i]) + if err != nil { + return map[string]storage.ShardNode{}, err + } + if exists { + result[tableNames[i]] = shardNodeMap[shardID] + } else { + missingTables = append(missingTables, tableNames[i]) + } + } + + if len(result) == len(tableNames) { + return result, nil + } + + var tablesNeedToAssignShard []string + if len(missingTables) > 0 { + tablesNeedToAssignShard = missingTables + } else { + tablesNeedToAssignShard = tableNames Review Comment: This branch seems unreachable? ########## server/coordinator/persist_shard_picker.go: ########## @@ -0,0 +1,86 @@ +/* + * 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 coordinator + +import ( + "context" + + "github.com/apache/incubator-horaedb-meta/server/cluster/metadata" + "github.com/apache/incubator-horaedb-meta/server/storage" +) + +type PersistShardPicker struct { + cluster *metadata.ClusterMetadata + internal ShardPicker +} + +func NewPersistShardPicker(cluster *metadata.ClusterMetadata, internal ShardPicker) *PersistShardPicker { + return &PersistShardPicker{cluster: cluster, internal: internal} +} + +func (p *PersistShardPicker) PickShards(ctx context.Context, snapshot metadata.Snapshot, schemaName string, tableNames []string) (map[string]storage.ShardNode, error) { + result := map[string]storage.ShardNode{} + + shardNodeMap := map[storage.ShardID]storage.ShardNode{} Review Comment: Maybe we should pre-allocate the memory. ########## server/coordinator/persist_shard_picker.go: ########## @@ -0,0 +1,86 @@ +/* + * 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 coordinator + +import ( + "context" + + "github.com/apache/incubator-horaedb-meta/server/cluster/metadata" + "github.com/apache/incubator-horaedb-meta/server/storage" +) + +type PersistShardPicker struct { + cluster *metadata.ClusterMetadata + internal ShardPicker +} + +func NewPersistShardPicker(cluster *metadata.ClusterMetadata, internal ShardPicker) *PersistShardPicker { + return &PersistShardPicker{cluster: cluster, internal: internal} +} + +func (p *PersistShardPicker) PickShards(ctx context.Context, snapshot metadata.Snapshot, schemaName string, tableNames []string) (map[string]storage.ShardNode, error) { + result := map[string]storage.ShardNode{} + + shardNodeMap := map[storage.ShardID]storage.ShardNode{} + for _, shardNode := range snapshot.Topology.ClusterView.ShardNodes { + shardNodeMap[shardNode.ID] = shardNode + } + + var missingTables []string + // If table assign has been created, just reuse it. + for i := 0; i < len(tableNames); i++ { + shardID, exists, err := p.cluster.GetTableAssignedShard(ctx, schemaName, tableNames[i]) + if err != nil { + return map[string]storage.ShardNode{}, err + } + if exists { + result[tableNames[i]] = shardNodeMap[shardID] + } else { + missingTables = append(missingTables, tableNames[i]) + } + } + + if len(result) == len(tableNames) { Review Comment: Maybe we should check whether `missingTables` is empty? If the any table name is duplicate in the `tableNames`, this check may won't work. -- 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]
