forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement cache invalidation api (apache#10761)
* Add cache endpoints * Implement cache endpoint * Tests and address feedback * Set cache config * Address feedback * Expose only invalidate endpoint Co-authored-by: bogdan kyryliuk <bogdankyryliuk@dropbox.com>
- Loading branch information
Showing
10 changed files
with
422 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,16 @@ | ||
# 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. |
This file contains 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,123 @@ | ||
# 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 logging | ||
|
||
from flask import request, Response | ||
from flask_appbuilder import expose | ||
from flask_appbuilder.api import safe | ||
from flask_appbuilder.models.sqla.interface import SQLAInterface | ||
from flask_appbuilder.security.decorators import protect | ||
from marshmallow.exceptions import ValidationError | ||
from sqlalchemy.exc import SQLAlchemyError | ||
|
||
from superset.cachekeys.schemas import CacheInvalidationRequestSchema | ||
from superset.connectors.connector_registry import ConnectorRegistry | ||
from superset.extensions import cache_manager, db, event_logger | ||
from superset.models.cache import CacheKey | ||
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class CacheRestApi(BaseSupersetModelRestApi): | ||
datamodel = SQLAInterface(CacheKey) | ||
resource_name = "cachekey" | ||
allow_browser_login = True | ||
class_permission_name = "CacheRestApi" | ||
include_route_methods = { | ||
"invalidate", | ||
} | ||
|
||
openapi_spec_component_schemas = (CacheInvalidationRequestSchema,) | ||
|
||
@expose("/invalidate", methods=["POST"]) | ||
@event_logger.log_this | ||
@protect() | ||
@safe | ||
@statsd_metrics | ||
def invalidate(self) -> Response: | ||
""" | ||
Takes a list of datasources, finds the associated cache records and | ||
invalidates them and removes the database records | ||
--- | ||
post: | ||
description: >- | ||
Takes a list of datasources, finds the associated cache records and | ||
invalidates them and removes the database records | ||
requestBody: | ||
description: >- | ||
A list of datasources uuid or the tuples of database and datasource names | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/CacheInvalidationRequestSchema" | ||
responses: | ||
201: | ||
description: cache was successfully invalidated | ||
400: | ||
$ref: '#/components/responses/400' | ||
500: | ||
$ref: '#/components/responses/500' | ||
""" | ||
try: | ||
datasources = CacheInvalidationRequestSchema().load(request.json) | ||
except KeyError: | ||
return self.response_400(message="Request is incorrect") | ||
except ValidationError as error: | ||
return self.response_400(message=str(error)) | ||
datasource_uids = set(datasources.get("datasource_uids", [])) | ||
for ds in datasources.get("datasources", []): | ||
ds_obj = ConnectorRegistry.get_datasource_by_name( | ||
session=db.session, | ||
datasource_type=ds.get("datasource_type"), | ||
datasource_name=ds.get("datasource_name"), | ||
schema=ds.get("schema"), | ||
database_name=ds.get("database_name"), | ||
) | ||
if ds_obj: | ||
datasource_uids.add(ds_obj.uid) | ||
|
||
cache_key_objs = ( | ||
db.session.query(CacheKey) | ||
.filter(CacheKey.datasource_uid.in_(datasource_uids)) | ||
.all() | ||
) | ||
cache_keys = [c.cache_key for c in cache_key_objs] | ||
if cache_key_objs: | ||
all_keys_deleted = cache_manager.cache.delete_many(*cache_keys) | ||
|
||
if not all_keys_deleted: | ||
# expected behavior as keys may expire and cache is not a | ||
# persistent storage | ||
logger.info( | ||
"Some of the cache keys were not deleted in the list %s", cache_keys | ||
) | ||
|
||
try: | ||
delete_stmt = CacheKey.__table__.delete().where( # pylint: disable=no-member | ||
CacheKey.cache_key.in_(cache_keys) | ||
) | ||
db.session.execute(delete_stmt) | ||
db.session.commit() | ||
except SQLAlchemyError as ex: # pragma: no cover | ||
logger.error(ex) | ||
db.session.rollback() | ||
return self.response_500(str(ex)) | ||
db.session.commit() | ||
return self.response(201) |
This file contains 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,45 @@ | ||
# 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. | ||
# RISON/JSON schemas for query parameters | ||
from marshmallow import fields, Schema, validate | ||
|
||
from superset.charts.schemas import ( | ||
datasource_name_description, | ||
datasource_type_description, | ||
datasource_uid_description, | ||
) | ||
|
||
|
||
class Datasource(Schema): | ||
database_name = fields.String(description="Datasource name",) | ||
datasource_name = fields.String(description=datasource_name_description,) | ||
schema = fields.String(description="Datasource schema",) | ||
datasource_type = fields.String( | ||
description=datasource_type_description, | ||
validate=validate.OneOf(choices=("druid", "table", "view")), | ||
required=True, | ||
) | ||
|
||
|
||
class CacheInvalidationRequestSchema(Schema): | ||
datasource_uids = fields.List( | ||
fields.String(), description=datasource_uid_description, | ||
) | ||
datasources = fields.List( | ||
fields.Nested(Datasource), | ||
description="A list of the data source and database names", | ||
) |
This file contains 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
This file contains 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
This file contains 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,16 @@ | ||
# 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. |
Oops, something went wrong.