Malarg commented on code in PR #25610: URL: https://github.com/apache/beam/pull/25610#discussion_r1122611303
########## playground/frontend/playground_components/lib/src/controllers/build_metadata.dart: ########## @@ -0,0 +1,74 @@ +/* + * 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. + */ + +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:get_it/get_it.dart'; + +import '../api/v1/api.pbgrpc.dart' show GetMetadataResponse; +import '../models/component_version.dart'; +import '../models/sdk.dart'; +import '../repositories/code_repository.dart'; +import '../repositories/example_repository.dart'; +import '../repositories/get_metadata_response_grpc_extension.dart'; + +/// Obtains versions from the backend. +class BuildMetadataController extends ChangeNotifier { + ComponentVersion? _routerVersion; + Future<GetMetadataResponse>? _routerVersionFuture; + + final _runnerVersions = <Sdk, ComponentVersion>{}; + final _runnerVersionFutures = <Sdk, Future<GetMetadataResponse>>{}; + + /// Returns the router version and starts loading if it is not started yet. + ComponentVersion? get routerVersion { Review Comment: such way don't assure that it returns version and client won't know when it will be available. Suggest make it future and return cached data if the have been loaded ########## playground/frontend/playground_components/lib/src/repositories/backend_urls.dart: ########## @@ -0,0 +1,112 @@ +/* + * 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. + */ + +// ignore_for_file: avoid_print + +import '../../playground_components.dart'; +import '../constants/backend_urls.dart'; + +const _routerNode = 'router'; + +Future<Uri> getRouterUrl() => _getBackendUrl(_routerNode); +Future<Uri> getRunnerUrl(Sdk sdk) => _getBackendUrl(sdk.id); + +/// Returns options for backend URLs for [node]. +/// +/// If an override is given in [backendUrlOverrides], it is the only option. +/// This can be used for development if the production backend +/// must not be used. +/// +/// Otherwise there are 2 options in the following order: +/// 1. [node] is prepended to the host of the current URL. +/// 2. [node] is inserted into [defaultBackendUrlTemplate] (the production URL). +/// If the two are the same, only one is returned. +/// This results in the following when looking up "node": +/// +/// For production Playground: +/// - node.play.beam.apache.org (the production URL). +/// +/// For production Tour of Beam: +/// - node.play.beam.apache.org (as node.tour.beam.apache.org is blacklisted +/// via [skipBackendUrls]). +/// +/// For any other stage of Playground or Tour of Beam (my-stage.com): +/// - node.my-stage.com +/// - node.play.beam.apache.org +/// This means that if the stage runs its own container for the given +/// backend node, it is used. Otherwise the production backend is used. +List<Uri> _getBackendUrlOptions(String node) { Review Comment: place this method under `_getBackendUrl` ########## playground/frontend/playground_components/lib/src/repositories/code_repository.dart: ########## @@ -35,9 +35,11 @@ const kProcessingStartedText = 'The processing has started\n'; // TODO(alexeyinkin): Rename. This is not a repository but a higher level client. class CodeRepository { - final CodeClient _client; + final CodeClient client; Review Comment: Client changed to public here just because of one method in `build_metadata.dart`. I suggest to keep this field incapsulated and turn back to private ########## playground/frontend/playground_components/lib/src/repositories/backend_urls.dart: ########## @@ -0,0 +1,112 @@ +/* + * 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. + */ + +// ignore_for_file: avoid_print + +import '../../playground_components.dart'; +import '../constants/backend_urls.dart'; + +const _routerNode = 'router'; + +Future<Uri> getRouterUrl() => _getBackendUrl(_routerNode); +Future<Uri> getRunnerUrl(Sdk sdk) => _getBackendUrl(sdk.id); + +/// Returns options for backend URLs for [node]. +/// +/// If an override is given in [backendUrlOverrides], it is the only option. +/// This can be used for development if the production backend +/// must not be used. +/// +/// Otherwise there are 2 options in the following order: +/// 1. [node] is prepended to the host of the current URL. +/// 2. [node] is inserted into [defaultBackendUrlTemplate] (the production URL). +/// If the two are the same, only one is returned. +/// This results in the following when looking up "node": +/// +/// For production Playground: +/// - node.play.beam.apache.org (the production URL). +/// +/// For production Tour of Beam: +/// - node.play.beam.apache.org (as node.tour.beam.apache.org is blacklisted +/// via [skipBackendUrls]). +/// +/// For any other stage of Playground or Tour of Beam (my-stage.com): +/// - node.my-stage.com +/// - node.play.beam.apache.org +/// This means that if the stage runs its own container for the given +/// backend node, it is used. Otherwise the production backend is used. +List<Uri> _getBackendUrlOptions(String node) { + final override = backendUrlOverrides[node]; + if (override != null) { + return [Uri.parse(override)]; + } + + final currentHost = Uri.base.host; + final nodeUriFromCurrentHost = Uri.base.replace( + host: '$node.$currentHost', + path: '', + queryParameters: {}, + ).removeFragment(); + + return { + nodeUriFromCurrentHost.toString(), + defaultBackendUrlTemplate.replaceAll('{node}', node), + }.where(_shouldAttemptUrl).map(Uri.parse).toList(); +} + +/// Whether [url] does not match any pattern in [skipBackendUrls]. +bool _shouldAttemptUrl(String url) { + return !skipBackendUrls.any((pattern) => pattern.allMatches(url).isNotEmpty); +} + +/// Returns the URL for the backend [node]. +/// +/// Calls [_getBackendUrlOptions] for options. +/// +/// If only one option exists, returns it. +/// This ensures fast initialization in production. +/// +/// If multiple options exist, each of them except the last one is probed +/// with getMetadata() call. This results in slower initialization for +/// custom stages but keeps the configuration simple. +Future<Uri> _getBackendUrl(String node) async { + final urls = _getBackendUrlOptions(node); + + if (urls.length == 1) { + return urls.first; + } + + print('Probing multiple options for $node backend:'); Review Comment: all of these `print`s are required? ########## playground/frontend/playground_components/lib/src/controllers/build_metadata.dart: ########## @@ -0,0 +1,74 @@ +/* + * 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. + */ + +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:get_it/get_it.dart'; + +import '../api/v1/api.pbgrpc.dart' show GetMetadataResponse; +import '../models/component_version.dart'; +import '../models/sdk.dart'; +import '../repositories/code_repository.dart'; +import '../repositories/example_repository.dart'; +import '../repositories/get_metadata_response_grpc_extension.dart'; + +/// Obtains versions from the backend. +class BuildMetadataController extends ChangeNotifier { + ComponentVersion? _routerVersion; + Future<GetMetadataResponse>? _routerVersionFuture; + + final _runnerVersions = <Sdk, ComponentVersion>{}; + final _runnerVersionFutures = <Sdk, Future<GetMetadataResponse>>{}; + + /// Returns the router version and starts loading if it is not started yet. + ComponentVersion? get routerVersion { Review Comment: The same about `getRunnerVersion` ########## playground/frontend/playground_components/lib/src/repositories/backend_urls.dart: ########## @@ -0,0 +1,112 @@ +/* + * 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. + */ + +// ignore_for_file: avoid_print + +import '../../playground_components.dart'; +import '../constants/backend_urls.dart'; + +const _routerNode = 'router'; + +Future<Uri> getRouterUrl() => _getBackendUrl(_routerNode); +Future<Uri> getRunnerUrl(Sdk sdk) => _getBackendUrl(sdk.id); + +/// Returns options for backend URLs for [node]. +/// +/// If an override is given in [backendUrlOverrides], it is the only option. +/// This can be used for development if the production backend +/// must not be used. +/// +/// Otherwise there are 2 options in the following order: +/// 1. [node] is prepended to the host of the current URL. +/// 2. [node] is inserted into [defaultBackendUrlTemplate] (the production URL). +/// If the two are the same, only one is returned. +/// This results in the following when looking up "node": +/// +/// For production Playground: +/// - node.play.beam.apache.org (the production URL). +/// +/// For production Tour of Beam: +/// - node.play.beam.apache.org (as node.tour.beam.apache.org is blacklisted +/// via [skipBackendUrls]). +/// +/// For any other stage of Playground or Tour of Beam (my-stage.com): +/// - node.my-stage.com +/// - node.play.beam.apache.org +/// This means that if the stage runs its own container for the given +/// backend node, it is used. Otherwise the production backend is used. +List<Uri> _getBackendUrlOptions(String node) { + final override = backendUrlOverrides[node]; + if (override != null) { + return [Uri.parse(override)]; + } + + final currentHost = Uri.base.host; + final nodeUriFromCurrentHost = Uri.base.replace( + host: '$node.$currentHost', + path: '', + queryParameters: {}, + ).removeFragment(); + + return { + nodeUriFromCurrentHost.toString(), + defaultBackendUrlTemplate.replaceAll('{node}', node), + }.where(_shouldAttemptUrl).map(Uri.parse).toList(); +} + +/// Whether [url] does not match any pattern in [skipBackendUrls]. +bool _shouldAttemptUrl(String url) { + return !skipBackendUrls.any((pattern) => pattern.allMatches(url).isNotEmpty); +} + +/// Returns the URL for the backend [node]. +/// +/// Calls [_getBackendUrlOptions] for options. +/// +/// If only one option exists, returns it. +/// This ensures fast initialization in production. +/// +/// If multiple options exist, each of them except the last one is probed +/// with getMetadata() call. This results in slower initialization for +/// custom stages but keeps the configuration simple. +Future<Uri> _getBackendUrl(String node) async { + final urls = _getBackendUrlOptions(node); + + if (urls.length == 1) { + return urls.first; + } + + print('Probing multiple options for $node backend:'); + urls.forEach(print); + + final lastUrl = urls.removeLast(); + + for (final url in urls) { + try { + final client = GrpcExampleClient(url: url); + await client.getMetadata(); + print('Using $url'); + return url; + } catch (ex) { Review Comment: avoid catches without on clauses ########## playground/frontend/playground_components/lib/src/controllers/build_metadata.dart: ########## @@ -0,0 +1,74 @@ +/* + * 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. + */ + +import 'dart:async'; + +import 'package:flutter/foundation.dart'; +import 'package:get_it/get_it.dart'; + +import '../api/v1/api.pbgrpc.dart' show GetMetadataResponse; +import '../models/component_version.dart'; +import '../models/sdk.dart'; +import '../repositories/code_repository.dart'; +import '../repositories/example_repository.dart'; +import '../repositories/get_metadata_response_grpc_extension.dart'; + +/// Obtains versions from the backend. +class BuildMetadataController extends ChangeNotifier { + ComponentVersion? _routerVersion; + Future<GetMetadataResponse>? _routerVersionFuture; + + final _runnerVersions = <Sdk, ComponentVersion>{}; + final _runnerVersionFutures = <Sdk, Future<GetMetadataResponse>>{}; + + /// Returns the router version and starts loading if it is not started yet. + ComponentVersion? get routerVersion { Review Comment: The single place, when this getter is used is `_ComponentVersionsWidget` Just use `FutureBuilder` there -- 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]
