-
-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ Improve Performance for GraphQL App Definition Caching
- Loading branch information
Showing
11 changed files
with
234 additions
and
40 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
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
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
97 changes: 97 additions & 0 deletions
97
graphql/src/main/java/org/restheart/graphql/initializers/GraphAppsInitializer.java
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,97 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* restheart-security | ||
* %% | ||
* Copyright (C) 2018 - 2024 SoftInstigate | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* =========================LICENSE_END================================== | ||
*/ | ||
package org.restheart.graphql.initializers; | ||
|
||
import org.restheart.configuration.Configuration; | ||
import org.restheart.configuration.ConfigurationException; | ||
import org.restheart.graphql.GraphQLService; | ||
import org.restheart.plugins.Inject; | ||
import org.restheart.plugins.OnInit; | ||
import org.restheart.plugins.RegisterPlugin; | ||
|
||
import java.util.Map; | ||
|
||
import org.bson.BsonDocument; | ||
import org.restheart.graphql.GraphQLIllegalAppDefinitionException; | ||
import org.restheart.graphql.cache.AppDefinitionLoadingCache; | ||
import org.restheart.graphql.models.builder.AppBuilder; | ||
import org.restheart.plugins.Initializer; | ||
|
||
import com.mongodb.client.MongoClient; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@RegisterPlugin(name="graphAppsInitializer", | ||
description = "initializes and caches all GQL Apps at boot timeGraphQL", | ||
enabledByDefault = true | ||
) | ||
public class GraphAppsInitializer implements Initializer { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(GraphAppsInitializer.class); | ||
|
||
private String db = GraphQLService.DEFAULT_APP_DEF_DB; | ||
private String coll = GraphQLService.DEFAULT_APP_DEF_COLLECTION; | ||
|
||
private boolean enabled = false; | ||
|
||
@Inject("rh-config") | ||
private Configuration config; | ||
|
||
@Inject("mclient") | ||
private MongoClient mclient; | ||
|
||
@OnInit | ||
public void onInit() { | ||
try { | ||
Map<String, Object> graphqlArgs = config.getOrDefault("graphql", null); | ||
if (graphqlArgs != null) { | ||
this.db = arg(graphqlArgs, "db"); | ||
this.coll = arg(graphqlArgs, "collection"); | ||
this.enabled = true; | ||
} else { | ||
this.enabled = false; | ||
} | ||
} catch(ConfigurationException ce) { | ||
// nothing to do, using default values | ||
} | ||
} | ||
|
||
@Override | ||
public void init() { | ||
if (this.enabled) { | ||
this.mclient | ||
.getDatabase(this.db) | ||
.getCollection(this.coll) | ||
.withDocumentClass(BsonDocument.class) | ||
.find() | ||
.forEach(appDef -> { | ||
try { | ||
var app = AppBuilder.build(appDef); | ||
var appUri = app.getDescriptor().getUri() != null ? app.getDescriptor().getUri() : app.getDescriptor().getAppName(); | ||
AppDefinitionLoadingCache.getCache().put(appUri, app); | ||
LOGGER.debug("GQL App Definition {} initialized", appUri); | ||
} catch (GraphQLIllegalAppDefinitionException e) { | ||
LOGGER.warn("GQL App Definition {} is invalid", appDef.get("_id"), e); | ||
} | ||
}); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
.../src/main/java/org/restheart/graphql/interceptors/GraphAppDefinitionCacheInvalidator.java
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,89 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* restheart-security | ||
* %% | ||
* Copyright (C) 2018 - 2024 SoftInstigate | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* =========================LICENSE_END================================== | ||
*/ | ||
package org.restheart.graphql.interceptors; | ||
|
||
import org.restheart.configuration.Configuration; | ||
import org.restheart.configuration.ConfigurationException; | ||
import org.restheart.exchange.MongoRequest; | ||
import org.restheart.exchange.MongoResponse; | ||
import org.restheart.graphql.GraphQLService; | ||
import org.restheart.plugins.Inject; | ||
import org.restheart.plugins.MongoInterceptor; | ||
import org.restheart.plugins.OnInit; | ||
import org.restheart.plugins.RegisterPlugin; | ||
|
||
import java.util.Map; | ||
|
||
import org.restheart.graphql.cache.AppDefinitionLoadingCache; | ||
|
||
import static org.restheart.plugins.InterceptPoint.RESPONSE; | ||
import org.restheart.utils.HttpStatus; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@RegisterPlugin(name="graphAppDefinitionCacheInvalidator", | ||
description = "invalidate GraphQL cached application definitions on gqlapps delete requests", | ||
interceptPoint = RESPONSE, | ||
enabledByDefault = true | ||
) | ||
public class GraphAppDefinitionCacheInvalidator implements MongoInterceptor { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(GraphAppDefinitionCacheInvalidator.class); | ||
|
||
private String db = GraphQLService.DEFAULT_APP_DEF_DB; | ||
private String coll = GraphQLService.DEFAULT_APP_DEF_COLLECTION; | ||
|
||
private boolean enabled = false; | ||
|
||
@Inject("rh-config") | ||
private Configuration config; | ||
|
||
@OnInit | ||
public void init() { | ||
try { | ||
Map<String, Object> graphqlArgs = config.getOrDefault("graphql", null); | ||
if (graphqlArgs != null) { | ||
this.db = arg(graphqlArgs, "db"); | ||
this.coll = arg(graphqlArgs, "collection"); | ||
this.enabled = true; | ||
} else { | ||
this.enabled = false; | ||
} | ||
} catch(ConfigurationException ce) { | ||
// nothing to do, using default values | ||
} | ||
} | ||
|
||
@Override | ||
public void handle(MongoRequest request, MongoResponse response) throws Exception { | ||
LOGGER.debug("invalidating all gql app definitions cache entries"); | ||
AppDefinitionLoadingCache.getCache().invalidateAll(); | ||
} | ||
|
||
@Override | ||
public boolean resolve(MongoRequest request, MongoResponse response) { | ||
return enabled | ||
&& this.db.equals(request.getDBName()) | ||
&& this.coll.equals(request.getCollectionName()) | ||
&& request.isDelete() | ||
&& !response.isInError() | ||
&& response.getStatusCode() == HttpStatus.SC_NO_CONTENT; | ||
} | ||
} |
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
Oops, something went wrong.