1313 */
1414package io .trino .gateway .ha .resource ;
1515
16+ import com .fasterxml .jackson .core .type .TypeReference ;
1617import com .fasterxml .jackson .databind .ObjectMapper ;
1718import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
1819import com .fasterxml .jackson .dataformat .yaml .YAMLParser ;
1920import com .google .common .base .Strings ;
2021import com .google .inject .Inject ;
22+ import com .google .inject .Singleton ;
2123import io .trino .gateway .ha .clustermonitor .ClusterStats ;
2224import io .trino .gateway .ha .config .HaGatewayConfiguration ;
2325import io .trino .gateway .ha .config .ProxyBackendConfiguration ;
6971import static java .util .Objects .requireNonNullElse ;
7072
7173@ Path ("/webapp" )
74+ @ Singleton
7275public class GatewayWebAppResource
7376{
7477 private static final LocalDateTime START_TIME = LocalDateTime .now ();
@@ -443,17 +446,14 @@ public Response readExactMatchSourceSelector()
443446 @ Path ("/getRoutingRules" )
444447 public Response getRoutingRules ()
445448 {
446- String content = null ;
447449 try {
448450 String rulesConfigPath = configuration .getRoutingRules ().getRulesConfigPath ();
449- content = new String (Files .readAllBytes (Paths .get (rulesConfigPath )));
450- ObjectMapper yamlReader = new ObjectMapper (new YAMLFactory ());
451- YAMLParser parser = new YAMLFactory ().createParser (content );
452- List <RoutingRules > routingRulesList = new ArrayList <>();
453- while (parser .nextToken () != null ) {
454- RoutingRules routingRules = yamlReader .readValue (parser , RoutingRules .class );
455- routingRulesList .add (routingRules );
456- }
451+ YAMLFactory yamlFactory = new YAMLFactory ();
452+ ObjectMapper yamlReader = new ObjectMapper (yamlFactory );
453+ YAMLParser yamlParser = yamlFactory .createParser (new String (Files .readAllBytes (Paths .get (rulesConfigPath ))));
454+ List <RoutingRules > routingRulesList = yamlReader
455+ .readValues (yamlParser , new TypeReference <RoutingRules >() {})
456+ .readAll ();
457457 return Response .ok (Result .ok (routingRulesList )).build ();
458458 }
459459 catch (IOException e ) {
@@ -466,19 +466,17 @@ public Response getRoutingRules()
466466 @ Consumes (MediaType .APPLICATION_JSON )
467467 @ Produces (MediaType .APPLICATION_JSON )
468468 @ Path ("/updateRoutingRules" )
469- public Response updateRoutingRules (RoutingRules routingRules )
469+ public synchronized Response updateRoutingRules (RoutingRules routingRules )
470470 {
471471 String rulesConfigPath = configuration .getRoutingRules ().getRulesConfigPath ();
472472 ObjectMapper yamlReader = new ObjectMapper (new YAMLFactory ());
473473 List <RoutingRules > routingRulesList = new ArrayList <>();
474-
474+ YAMLFactory yamlFactory = new YAMLFactory ();
475475 try {
476- String content = new String (Files .readAllBytes (Paths .get (rulesConfigPath )));
477- YAMLParser parser = new YAMLFactory ().createParser (content );
478- while (parser .nextToken () != null ) {
479- RoutingRules routingRule = yamlReader .readValue (parser , RoutingRules .class );
480- routingRulesList .add (routingRule );
481- }
476+ YAMLParser yamlParser = yamlFactory .createParser (new String (Files .readAllBytes (Paths .get (rulesConfigPath ))));
477+ routingRulesList = yamlReader
478+ .readValues (yamlParser , new TypeReference <RoutingRules >() {})
479+ .readAll ();
482480
483481 for (int i = 0 ; i < routingRulesList .size (); i ++) {
484482 if (routingRulesList .get (i ).name ().equals (routingRules .name ())) {
0 commit comments