Skip to content

Commit

Permalink
Merge pull request #133 from lyft/addrgchangestogateway
Browse files Browse the repository at this point in the history
Gateway api changes to support querying different presto proxy databases
  • Loading branch information
riteshvaryani authored Mar 12, 2021
2 parents 39d7d5f + 6310bdc commit b827c57
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 127 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ The Gateway admin page is used to configure the gateway to multiple backends. Ex

## Resource Groups API

For resource group and selector apis, we can now specify a query parameter with the request supporting multiple presto databases for different presto backends. This allows a user to configure a db for every presto backend with their own resource groups and selector tables. To use this, just specify the query parameter ?useSchema=<schemaname> to the request. Example, to list all resource groups,
```$xslt
curl -X GET http://localhost:8080/presto/resourcegroup/read/{INSERT_ID_HERE}?useSchema=newdatabasename
```

### Add a resource group
To add a single resource group, specify all relevant fields in the body.
```$xslt
Expand Down
6 changes: 5 additions & 1 deletion gateway-ha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ Next time onwards, run the following commands to start mysqldb
```$xslt
docker start mysqldb
```
Enter the mysql container

```
docker exec -it mysqldb bash -l
```
Now open mysql console and install the presto-gateway tables:
```$xslt
mysql -uroot -proot123 -h127.0.0.1 -Dprestogateway
```
Once logged in to mysql console, please run [gateway-ha-persistence.sql](/src/main/resources/gateway-ha-persistence.sql) to populate the tables.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.javalite.activejdbc.Base;

Expand All @@ -21,11 +22,20 @@ public JdbcConnectionManager(DataStoreConfiguration configuration) {
}

public void open() {
this.open(null);
}

public void open(@Nullable String routingGroupDatabase) {
String jdbcUrl = configuration.getJdbcUrl();
if (routingGroupDatabase != null) {
jdbcUrl = jdbcUrl.substring(0, jdbcUrl.lastIndexOf('/') + 1) + routingGroupDatabase;
}
log.debug("Jdbc url is " + jdbcUrl);
Base.open(
configuration.getDriver(),
configuration.getJdbcUrl(),
configuration.getUser(),
configuration.getPassword());
configuration.getDriver(),
jdbcUrl,
configuration.getUser(),
configuration.getPassword());
log.debug("Connection opened");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.google.inject.Inject;

import com.lyft.data.gateway.ha.router.ResourceGroupsManager;
import com.lyft.data.gateway.ha.router.ResourceGroupsManager.ExactSelectorsDetail;
import com.lyft.data.gateway.ha.router.ResourceGroupsManager.GlobalPropertiesDetail;
import com.lyft.data.gateway.ha.router.ResourceGroupsManager.ResourceGroupsDetail;
import com.lyft.data.gateway.ha.router.ResourceGroupsManager.SelectorsDetail;
Expand Down Expand Up @@ -34,12 +33,13 @@ public class PrestoResource {

@POST
@Path("/resourcegroup/create")
public Response createResourceGroup(String jsonPayload) {
public Response createResourceGroup(@QueryParam("useSchema")
String useSchema, String jsonPayload) {
try {
ResourceGroupsDetail resourceGroup =
OBJECT_MAPPER.readValue(jsonPayload, ResourceGroupsDetail.class);
ResourceGroupsDetail newResourceGroup =
this.resourceGroupsManager.createResourceGroup(resourceGroup);
this.resourceGroupsManager.createResourceGroup(resourceGroup, useSchema);
return Response.ok(newResourceGroup).build();
} catch (IOException e) {
log.error(e.getMessage(), e);
Expand All @@ -49,30 +49,37 @@ public Response createResourceGroup(String jsonPayload) {

@GET
@Path("/resourcegroup/read")
public Response readAllResourceGroups() {
return Response.ok(this.resourceGroupsManager.readAllResourceGroups()).build();
public Response readAllResourceGroups(@QueryParam("useSchema")
String useSchema) {
return Response.ok(this.resourceGroupsManager.readAllResourceGroups(
useSchema)).build();
}

@GET
@Path("/resourcegroup/read/{resourceGroupId}")
public Response readResourceGroup(@PathParam("resourceGroupId") String resourceGroupIdStr) {
public Response readResourceGroup(@PathParam("resourceGroupId") String resourceGroupIdStr,
@QueryParam("useSchema")
String useSchema) {
if (Strings.isNullOrEmpty(resourceGroupIdStr)) { // if query not specified, return all
return Response.ok(this.resourceGroupsManager.readAllResourceGroups()).build();
return Response.ok(this.resourceGroupsManager.readAllResourceGroups(useSchema))
.build();
}
long resourceGroupId = Long.parseLong(resourceGroupIdStr);
List<ResourceGroupsDetail> resourceGroup =
this.resourceGroupsManager.readResourceGroup(resourceGroupId);
this.resourceGroupsManager.readResourceGroup(resourceGroupId, useSchema);
return Response.ok(resourceGroup).build();
}

@Path("/resourcegroup/update")
@POST
public Response updateResourceGroup(String jsonPayload) {
public Response updateResourceGroup(String jsonPayload,
@QueryParam("useSchema")
String useSchema) {
try {
ResourceGroupsDetail resourceGroup =
OBJECT_MAPPER.readValue(jsonPayload, ResourceGroupsDetail.class);
ResourceGroupsDetail updatedResourceGroup =
this.resourceGroupsManager.updateResourceGroup(resourceGroup);
this.resourceGroupsManager.updateResourceGroup(resourceGroup, useSchema);
return Response.ok(updatedResourceGroup).build();
} catch (IOException e) {
log.error(e.getMessage(), e);
Expand All @@ -82,21 +89,25 @@ public Response updateResourceGroup(String jsonPayload) {

@Path("/resourcegroup/delete/{resourceGroupId}")
@POST
public Response deleteResourceGroup(@PathParam("resourceGroupId") String resourceGroupIdStr) {
public Response deleteResourceGroup(@PathParam("resourceGroupId") String resourceGroupIdStr,
@QueryParam("useSchema")
String useSchema) {
if (Strings.isNullOrEmpty(resourceGroupIdStr)) { // if query not specified, return all
throw new WebApplicationException("EntryType can not be null");
}
long resourceGroupId = Long.parseLong(resourceGroupIdStr);
resourceGroupsManager.deleteResourceGroup(resourceGroupId);
resourceGroupsManager.deleteResourceGroup(resourceGroupId, useSchema);
return Response.ok().build();
}

@POST
@Path("/selector/create")
public Response createSelector(String jsonPayload) {
public Response createSelector(String jsonPayload,
@QueryParam("useSchema") String useSchema) {
try {
SelectorsDetail selector = OBJECT_MAPPER.readValue(jsonPayload, SelectorsDetail.class);
SelectorsDetail updatedSelector = this.resourceGroupsManager.createSelector(selector);
SelectorsDetail updatedSelector = this.resourceGroupsManager.createSelector(selector,
useSchema);
return Response.ok(updatedSelector).build();
} catch (IOException e) {
log.error(e.getMessage(), e);
Expand All @@ -106,24 +117,28 @@ public Response createSelector(String jsonPayload) {

@GET
@Path("/selector/read")
public Response readAllSelectors() {
return Response.ok(this.resourceGroupsManager.readAllSelectors()).build();
public Response readAllSelectors(@QueryParam("useSchema")
String useSchema) {
return Response.ok(this.resourceGroupsManager.readAllSelectors(useSchema)).build();
}

@GET
@Path("/selector/read/{resourceGroupId}")
public Response readSelector(@QueryParam("resourceGroupId") String resourceGroupIdStr) {
public Response readSelector(@QueryParam("resourceGroupId") String resourceGroupIdStr,
@QueryParam("useSchema") String useSchema) {
if (Strings.isNullOrEmpty(resourceGroupIdStr)) { // if query not specified, return all
return Response.ok(this.resourceGroupsManager.readAllSelectors()).build();
return Response.ok(this.resourceGroupsManager.readAllSelectors(useSchema)).build();
}
long resourceGroupId = Long.parseLong(resourceGroupIdStr);
List<SelectorsDetail> selectors = this.resourceGroupsManager.readSelector(resourceGroupId);
List<SelectorsDetail> selectors = this.resourceGroupsManager.readSelector(resourceGroupId,
useSchema);
return Response.ok(selectors).build();
}

@Path("/selector/update")
@POST
public Response updateSelector(String jsonPayload) {
public Response updateSelector(String jsonPayload,
@QueryParam("useSchema") String useSchema) {
try {
JsonNode selectors = OBJECT_MAPPER.readValue(jsonPayload, JsonNode.class);
SelectorsDetail selector =
Expand All @@ -132,7 +147,7 @@ public Response updateSelector(String jsonPayload) {
OBJECT_MAPPER.readValue(selectors.get("update").toString(), SelectorsDetail.class);

SelectorsDetail updatedSelector =
this.resourceGroupsManager.updateSelector(selector, newSelector);
this.resourceGroupsManager.updateSelector(selector, newSelector, useSchema);
return Response.ok(updatedSelector).build();
} catch (IOException e) {
log.error(e.getMessage(), e);
Expand All @@ -142,13 +157,14 @@ public Response updateSelector(String jsonPayload) {

@Path("/selector/delete/")
@POST
public Response deleteSelector(String jsonPayload) {
public Response deleteSelector(String jsonPayload, @QueryParam("useSchema")
String useSchema) {
if (Strings.isNullOrEmpty(jsonPayload)) {
throw new WebApplicationException("EntryType can not be null");
}
try {
SelectorsDetail selector = OBJECT_MAPPER.readValue(jsonPayload, SelectorsDetail.class);
resourceGroupsManager.deleteSelector(selector);
resourceGroupsManager.deleteSelector(selector, useSchema);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
Expand All @@ -157,12 +173,14 @@ public Response deleteSelector(String jsonPayload) {

@POST
@Path("/globalproperty/create")
public Response createGlobalProperty(String jsonPayload) {
public Response createGlobalProperty(String jsonPayload,
@QueryParam("useSchema")
String useSchema) {
try {
GlobalPropertiesDetail globalProperty =
OBJECT_MAPPER.readValue(jsonPayload, ResourceGroupsManager.GlobalPropertiesDetail.class);
GlobalPropertiesDetail newGlobalProperty =
this.resourceGroupsManager.createGlobalProperty(globalProperty);
this.resourceGroupsManager.createGlobalProperty(globalProperty, useSchema);
return Response.ok(newGlobalProperty).build();
} catch (IOException e) {
log.error(e.getMessage(), e);
Expand All @@ -172,29 +190,36 @@ public Response createGlobalProperty(String jsonPayload) {

@GET
@Path("/globalproperty/read")
public Response readAllGlobalProperties() {
return Response.ok(this.resourceGroupsManager.readAllGlobalProperties()).build();
public Response readAllGlobalProperties(@QueryParam("useSchema")
String useSchema) {
return Response.ok(this.resourceGroupsManager.readAllGlobalProperties(useSchema))
.build();
}

@GET
@Path("/globalproperty/read/{name}")
public Response readGlobalProperty(@PathParam("name") String name) {
public Response readGlobalProperty(@PathParam("name") String name,
@QueryParam("useSchema")
String useSchema) {
if (Strings.isNullOrEmpty(name)) {
return Response.ok(this.resourceGroupsManager.readAllGlobalProperties()).build();
return Response.ok(this.resourceGroupsManager.readAllGlobalProperties(useSchema))
.build();
}
List<GlobalPropertiesDetail> globalProperty =
this.resourceGroupsManager.readGlobalProperty(name);
this.resourceGroupsManager.readGlobalProperty(name, useSchema);
return Response.ok(globalProperty).build();
}

@Path("/globalproperty/update")
@POST
public Response updateGlobalProperty(String jsonPayload) {
public Response updateGlobalProperty(String jsonPayload,
@QueryParam("useSchema")
String useSchema) {
try {
GlobalPropertiesDetail globalProperty =
OBJECT_MAPPER.readValue(jsonPayload, ResourceGroupsManager.GlobalPropertiesDetail.class);
GlobalPropertiesDetail updatedGlobalProperty =
this.resourceGroupsManager.updateGlobalProperty(globalProperty);
this.resourceGroupsManager.updateGlobalProperty(globalProperty, useSchema);
return Response.ok(updatedGlobalProperty).build();
} catch (IOException e) {
log.error(e.getMessage(), e);
Expand All @@ -204,8 +229,10 @@ public Response updateGlobalProperty(String jsonPayload) {

@Path("/globalproperty/delete/{name}")
@POST
public Response deleteGlobalProperty(@PathParam("name") String name) {
resourceGroupsManager.deleteGlobalProperty(name);
public Response deleteGlobalProperty(@PathParam("name") String name,
@QueryParam("useSchema")
String useSchema) {
resourceGroupsManager.deleteGlobalProperty(name, useSchema);
return Response.ok().build();
}

Expand Down
Loading

0 comments on commit b827c57

Please sign in to comment.