1313 */
1414package io .trino .gateway .ha .resource ;
1515
16+ import com .fasterxml .jackson .core .type .TypeReference ;
17+ import com .fasterxml .jackson .databind .ObjectMapper ;
18+ import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
19+ import com .fasterxml .jackson .dataformat .yaml .YAMLParser ;
1620import com .google .common .base .Strings ;
1721import com .google .inject .Inject ;
22+ import com .google .inject .Singleton ;
1823import io .trino .gateway .ha .clustermonitor .ClusterStats ;
24+ import io .trino .gateway .ha .config .HaGatewayConfiguration ;
1925import io .trino .gateway .ha .config .ProxyBackendConfiguration ;
2026import io .trino .gateway .ha .domain .Result ;
27+ import io .trino .gateway .ha .domain .RoutingRules ;
2128import io .trino .gateway .ha .domain .TableData ;
2229import io .trino .gateway .ha .domain .request .GlobalPropertyRequest ;
2330import io .trino .gateway .ha .domain .request .QueryDistributionRequest ;
3643import io .trino .gateway .ha .router .ResourceGroupsManager ;
3744import jakarta .annotation .security .RolesAllowed ;
3845import jakarta .ws .rs .Consumes ;
46+ import jakarta .ws .rs .GET ;
3947import jakarta .ws .rs .POST ;
4048import jakarta .ws .rs .Path ;
4149import jakarta .ws .rs .Produces ;
4452import jakarta .ws .rs .core .Response ;
4553import jakarta .ws .rs .core .SecurityContext ;
4654
55+ import java .io .IOException ;
56+ import java .nio .file .Files ;
57+ import java .nio .file .Paths ;
4758import java .time .LocalDateTime ;
4859import java .time .ZoneId ;
4960import java .time .ZoneOffset ;
5061import java .time .ZonedDateTime ;
5162import java .time .format .DateTimeFormatter ;
63+ import java .util .ArrayList ;
5264import java .util .Collections ;
5365import java .util .List ;
5466import java .util .Map ;
5971import static java .util .Objects .requireNonNullElse ;
6072
6173@ Path ("/webapp" )
74+ @ Singleton
6275public class GatewayWebAppResource
6376{
6477 private static final LocalDateTime START_TIME = LocalDateTime .now ();
@@ -67,18 +80,21 @@ public class GatewayWebAppResource
6780 private final QueryHistoryManager queryHistoryManager ;
6881 private final BackendStateManager backendStateManager ;
6982 private final ResourceGroupsManager resourceGroupsManager ;
83+ private final HaGatewayConfiguration configuration ;
7084
7185 @ Inject
7286 public GatewayWebAppResource (
7387 GatewayBackendManager gatewayBackendManager ,
7488 QueryHistoryManager queryHistoryManager ,
7589 BackendStateManager backendStateManager ,
76- ResourceGroupsManager resourceGroupsManager )
90+ ResourceGroupsManager resourceGroupsManager ,
91+ HaGatewayConfiguration configuration )
7792 {
7893 this .gatewayBackendManager = requireNonNull (gatewayBackendManager , "gatewayBackendManager is null" );
7994 this .queryHistoryManager = requireNonNull (queryHistoryManager , "queryHistoryManager is null" );
8095 this .backendStateManager = requireNonNull (backendStateManager , "backendStateManager is null" );
8196 this .resourceGroupsManager = requireNonNull (resourceGroupsManager , "resourceGroupsManager is null" );
97+ this .configuration = requireNonNull (configuration , "configuration is null" );
8298 }
8399
84100 @ POST
@@ -423,4 +439,62 @@ public Response readExactMatchSourceSelector()
423439 List <ResourceGroupsManager .ExactSelectorsDetail > selectorsDetailList = resourceGroupsManager .readExactMatchSourceSelector ();
424440 return Response .ok (Result .ok (selectorsDetailList )).build ();
425441 }
442+
443+ @ GET
444+ @ RolesAllowed ("USER" )
445+ @ Produces (MediaType .APPLICATION_JSON )
446+ @ Path ("/getRoutingRules" )
447+ public Response getRoutingRules ()
448+ {
449+ try {
450+ String rulesConfigPath = configuration .getRoutingRules ().getRulesConfigPath ();
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 ();
457+ return Response .ok (Result .ok (routingRulesList )).build ();
458+ }
459+ catch (IOException e ) {
460+ throw new RuntimeException (e );
461+ }
462+ }
463+
464+ @ POST
465+ @ RolesAllowed ("ADMIN" )
466+ @ Consumes (MediaType .APPLICATION_JSON )
467+ @ Produces (MediaType .APPLICATION_JSON )
468+ @ Path ("/updateRoutingRules" )
469+ public synchronized Response updateRoutingRules (RoutingRules routingRules )
470+ {
471+ String rulesConfigPath = configuration .getRoutingRules ().getRulesConfigPath ();
472+ ObjectMapper yamlReader = new ObjectMapper (new YAMLFactory ());
473+ List <RoutingRules > routingRulesList = new ArrayList <>();
474+ YAMLFactory yamlFactory = new YAMLFactory ();
475+ try {
476+ YAMLParser yamlParser = yamlFactory .createParser (new String (Files .readAllBytes (Paths .get (rulesConfigPath ))));
477+ routingRulesList = yamlReader
478+ .readValues (yamlParser , new TypeReference <RoutingRules >() {})
479+ .readAll ();
480+
481+ for (int i = 0 ; i < routingRulesList .size (); i ++) {
482+ if (routingRulesList .get (i ).name ().equals (routingRules .name ())) {
483+ routingRulesList .set (i , routingRules );
484+ break ;
485+ }
486+ }
487+
488+ ObjectMapper yamlWriter = new ObjectMapper (new YAMLFactory ());
489+ StringBuilder yamlContent = new StringBuilder ();
490+ for (RoutingRules rule : routingRulesList ) {
491+ yamlContent .append (yamlWriter .writeValueAsString (rule ));
492+ }
493+ Files .write (Paths .get (rulesConfigPath ), yamlContent .toString ().getBytes ());
494+ }
495+ catch (IOException e ) {
496+ throw new RuntimeException (e );
497+ }
498+ return Response .ok (Result .ok (routingRulesList )).build ();
499+ }
426500}
0 commit comments