-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Add rest endpoints to dynamically add a catalog #12605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ramveer93
wants to merge
6
commits into
prestodb:master
Choose a base branch
from
ramveer93:dynamic-catalog-detection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7febea
Add rest endpoints to dynamically add and delete a catalog
ramveer93 bf4fe96
Add rest endpoint to dynamically add catalog
ramveer93 72d0319
Merge branch 'master' into dynamic-catalog-detection
ramveer93 429d209
Merge branch 'master' into dynamic-catalog-detection
ramveer93 d83c7e8
Fix import order error
ramveer93 46f029d
Fix import errors
ramveer93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
presto-main/src/main/java/com/facebook/presto/dynamicCatalog/CatalogVo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.dynamicCatalog; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Map; | ||
|
||
public class CatalogVo | ||
{ | ||
@JsonProperty("catalogName") | ||
public String catalogName; | ||
@JsonProperty("connectorName") | ||
public String connectorName; | ||
@JsonProperty("properties") | ||
public Map<String, String> properties; | ||
|
||
public CatalogVo(String catalogName, String connectorName, Map<String, String> properties) | ||
{ | ||
this.catalogName = catalogName; | ||
this.connectorName = connectorName; | ||
this.properties = properties; | ||
} | ||
|
||
public CatalogVo() | ||
{ | ||
} | ||
|
||
public String getCatalogName() | ||
{ | ||
return catalogName; | ||
} | ||
|
||
public void setCatalogName(String catalogName) | ||
{ | ||
this.catalogName = catalogName; | ||
} | ||
|
||
public String getConnectorName() | ||
{ | ||
return connectorName; | ||
} | ||
|
||
public void setConnectorName(String connectorName) | ||
{ | ||
this.connectorName = connectorName; | ||
} | ||
|
||
public Map<String, String> getProperties() | ||
{ | ||
return properties; | ||
} | ||
|
||
public void setProperties(Map<String, String> properties) | ||
{ | ||
this.properties = properties; | ||
} | ||
|
||
@Override | ||
public String toString() | ||
{ | ||
return "CatalogVo{" + | ||
"catalogName='" + catalogName + '\'' + | ||
", connectorName='" + connectorName + '\'' + | ||
", properties=" + properties + | ||
'}'; | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
presto-main/src/main/java/com/facebook/presto/dynamicCatalog/DynamicCatalogController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.dynamicCatalog; | ||
|
||
import com.facebook.presto.connector.ConnectorId; | ||
import com.facebook.presto.connector.ConnectorManager; | ||
import com.facebook.presto.metadata.CatalogManager; | ||
import com.facebook.presto.metadata.InternalNodeManager; | ||
import com.facebook.presto.metadata.StaticCatalogStoreConfig; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.base.Splitter; | ||
import io.airlift.discovery.client.Announcer; | ||
import io.airlift.discovery.client.ServiceAnnouncement; | ||
import io.airlift.log.Logger; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DELETE; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.OutputStream; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
|
||
import static com.google.common.base.Strings.nullToEmpty; | ||
import static io.airlift.discovery.client.ServiceAnnouncement.serviceAnnouncement; | ||
|
||
@Path("/v1/catalog") | ||
public class DynamicCatalogController | ||
{ | ||
private final CatalogManager catalogManager; | ||
private final ConnectorManager connectorManager; | ||
private final Announcer announcer; | ||
private final InternalNodeManager internalNodeManager; | ||
private final File catalogConfigurationDir; | ||
private static final Logger log = Logger.get(DynamicCatalogController.class); | ||
private final ResponseParser responseParser; | ||
|
||
private DynamicCatalogController(CatalogManager catalogManager, ConnectorManager connectorManager, Announcer announcer, InternalNodeManager internalNodeManager, File catalogConfigurationDir, ResponseParser responseParser) | ||
{ | ||
this.catalogManager = catalogManager; | ||
this.connectorManager = connectorManager; | ||
this.announcer = announcer; | ||
this.internalNodeManager = internalNodeManager; | ||
this.catalogConfigurationDir = catalogConfigurationDir; | ||
this.responseParser = responseParser; | ||
} | ||
|
||
@Inject | ||
public DynamicCatalogController(CatalogManager catalogManager, ConnectorManager connectorManager, Announcer announcer, InternalNodeManager internalNodeManager, StaticCatalogStoreConfig config, ResponseParser responseParser) | ||
{ | ||
this(catalogManager, connectorManager, announcer, internalNodeManager, config.getCatalogConfigurationDir(), responseParser); | ||
} | ||
|
||
@Path("add") | ||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response addCatalog(CatalogVo catalogVo) | ||
{ | ||
try { | ||
log.info("addCatalog : input values are " + catalogVo); | ||
ConnectorId connectorId = connectorManager.createConnection(catalogVo.getCatalogName(), catalogVo.getConnectorName(), catalogVo.getProperties()); | ||
updateConnectorIdAnnouncement(announcer, connectorId, internalNodeManager); | ||
log.info("addCatalog() : catalogConfigurationDir: " + catalogConfigurationDir.getAbsolutePath()); | ||
writeToFile(catalogVo); | ||
log.info("addCatalog() : Successfully added catalog " + catalogVo.getCatalogName()); | ||
return successResponse(responseParser.build("Successfully added catalog: " + catalogVo.getCatalogName(), 200)); | ||
} | ||
catch (Exception ex) { | ||
log.error("addCatalog() : Error adding catalog " + ex.getMessage()); | ||
return failedResponse(responseParser.build("Error adding Catalog: " + ex.getMessage(), 500)); | ||
} | ||
} | ||
|
||
@Path("delete") | ||
@DELETE | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public Response deleteCatalog(@QueryParam("catalogName") String catalogName) | ||
{ | ||
log.info("deleteCatalog(): Deleting catalog: " + catalogName); | ||
String responseMessage = ""; | ||
if (catalogManager.getCatalog(catalogName).isPresent()) { | ||
log.info("deleteCatalog() : Catalog exists so deleting catalog " + catalogName); | ||
connectorManager.dropConnection(catalogName); | ||
responseMessage = (deletePropertyFile(catalogName) == true) ? "Successfully deleted" : "Error deleting catalog"; | ||
} | ||
else { | ||
log.info("deleteCatalog() : Catalog doesn't exists, Can't be deleted " + catalogName); | ||
return failedResponse(responseParser.build("Catalog doesn't exists: " + catalogName, 500)); | ||
} | ||
log.info("deleteCatalog() : successfully deleted catalog " + catalogName); | ||
return successResponse(responseParser.build(responseMessage + " : " + catalogName, 200)); | ||
} | ||
|
||
private boolean deletePropertyFile(String catalogName) | ||
{ | ||
return new File(getPropertyFilePath(catalogName)).delete(); | ||
} | ||
|
||
private void writeToFile(CatalogVo catalogVo) | ||
throws Exception | ||
{ | ||
final Properties properties = new Properties(); | ||
properties.put("connector.name", catalogVo.getConnectorName()); | ||
properties.putAll(catalogVo.getProperties()); | ||
String filePath = getPropertyFilePath(catalogVo.getCatalogName()); | ||
log.info("filepath: " + filePath); | ||
File propertiesFile = new File(filePath); | ||
try (OutputStream out = new FileOutputStream(propertiesFile)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit: you might consider using Files.newOuputStream here instead. See #12614 for details about why File{Input,Output}Stream should be avoided in cases where any {Input,Output}Stream will do. |
||
properties.store(out, "adding catalog using endpoint"); | ||
} | ||
catch (Exception ex) { | ||
log.error("error while writing to a file :" + ex.getMessage()); | ||
throw new Exception("Error writing to file " + ex.getMessage()); | ||
} | ||
} | ||
|
||
private String getPropertyFilePath(String catalogName) | ||
{ | ||
return catalogConfigurationDir.getPath() + File.separator + catalogName + ".properties"; | ||
} | ||
|
||
private static ServiceAnnouncement getPrestoAnnouncement(Set<ServiceAnnouncement> announcements) | ||
{ | ||
for (ServiceAnnouncement announcement : announcements) { | ||
if (announcement.getType().equals("presto")) { | ||
return announcement; | ||
} | ||
} | ||
throw new IllegalArgumentException("Presto announcement not found: " + announcements); | ||
} | ||
|
||
private static void updateConnectorIdAnnouncement(Announcer announcer, ConnectorId connectorId, InternalNodeManager nodeManager) | ||
{ | ||
ServiceAnnouncement announcement = getPrestoAnnouncement(announcer.getServiceAnnouncements()); | ||
Map<String, String> properties = new LinkedHashMap<>(announcement.getProperties()); | ||
String property = nullToEmpty(properties.get("connectorIds")); | ||
Set<String> connectorIds = new LinkedHashSet<>(Splitter.on(',').trimResults().omitEmptyStrings().splitToList(property)); | ||
connectorIds.add(connectorId.toString()); | ||
properties.put("connectorIds", Joiner.on(',').join(connectorIds)); | ||
announcer.removeServiceAnnouncement(announcement.getId()); | ||
announcer.addServiceAnnouncement(serviceAnnouncement(announcement.getType()).addProperties(properties).build()); | ||
announcer.forceAnnounce(); | ||
nodeManager.refreshNodes(); | ||
} | ||
|
||
private Response successResponse(ResponseParser responseParser) | ||
{ | ||
return Response.status(Response.Status.OK).entity(responseParser).type(MediaType.APPLICATION_JSON).build(); | ||
} | ||
|
||
private Response failedResponse(ResponseParser responseParser) | ||
{ | ||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(responseParser).type(MediaType.APPLICATION_JSON).build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
presto-main/src/main/java/com/facebook/presto/dynamicCatalog/ResponseParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.dynamicCatalog; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class ResponseParser | ||
{ | ||
@JsonProperty("message") | ||
public String message; | ||
@JsonProperty("statusCode") | ||
public int statusCode; | ||
|
||
public ResponseParser build(String message, int statusCode) | ||
{ | ||
this.message = message; | ||
this.statusCode = statusCode; | ||
return this; | ||
} | ||
|
||
public ResponseParser() | ||
{ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this will try to delete whatever catalog is in
StaticCatalogStoreConfig
. I think it would be operationally better to let whatever is specified as a static resource to be immutable, but still allow one to delete or add on the fly. I don't think it should delete the files that are statically shipped with the Presto deployment. Worst case scenario, you don't specify any static catalogs and you need to add them via the REST endpoint. But that seems better than accidentally deleting an important catalog config and effectively not having a way to undo that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment,
Not sure I followed your comment fully.
But I am adding my explanation: Delete catalog endpoint will call
connectorManager.dropConnection(catalogName);
which will eventually callremoveCatalog
method ofcatalogManager
which deletes the catalog from concurrentMap.The endpoint will also delete the file associated with this catalog from etc/catalog which was created when we registered the catalog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the catalogs that this endpoint allows one to delete guaranteed to be registered through the same endpoint? If not, then I am questioning if it's really desirable to allow one to delete catalog configs which were statically loaded from
etc/catalog
. It seems like it's better, at the very least, to load and unload them in an isolated directory, and whatever is loaded on startup inetc/catalog
should be immutable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The endpoint deletes catalogs irrespective of added using the endpoint or already existing , because any new catalog will be added to etc/catalog and will be removed from same directory.
When presto is being install first time, it doesn't have any etc/catalog folder ,so there are no catalogs provided as a part of installation , it is up to the user to add whatever catalog they want to use, hence there is nothing to be immutable.
If the question is deletion existing catalog during development,there is already the etc/catalog folder checked in presto main which will sync back the etc/catalog.