diff --git a/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/MetaMongoDBAdaptor.java b/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/MetaMongoDBAdaptor.java index 9361f48d0..e5cd4d38c 100644 --- a/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/MetaMongoDBAdaptor.java +++ b/cellbase-lib/src/main/java/org/opencb/cellbase/lib/impl/core/MetaMongoDBAdaptor.java @@ -16,17 +16,19 @@ package org.opencb.cellbase.lib.impl.core; +import com.mongodb.ReadPreference; +import com.mongodb.WriteConcern; import com.mongodb.client.model.Filters; import com.mongodb.client.model.Updates; import org.bson.BsonDocument; import org.bson.Document; import org.bson.conversions.Bson; import org.codehaus.jackson.map.ObjectMapper; +import org.opencb.cellbase.core.api.key.ApiKeyStats; import org.opencb.cellbase.core.api.query.AbstractQuery; import org.opencb.cellbase.core.api.query.ProjectionQueryOptions; import org.opencb.cellbase.core.exception.CellBaseException; import org.opencb.cellbase.core.result.CellBaseDataResult; -import org.opencb.cellbase.core.api.key.ApiKeyStats; import org.opencb.cellbase.lib.iterator.CellBaseIterator; import org.opencb.commons.datastore.core.FacetField; import org.opencb.commons.datastore.core.QueryOptions; @@ -55,7 +57,7 @@ public MetaMongoDBAdaptor(MongoDataStore mongoDataStore) { private void init() { logger.debug("MetaMongoDBAdaptor: in 'constructor'"); mongoDBCollection = mongoDataStore.getCollection("metadata"); - apiKeyStatsMongoDBCollection = mongoDataStore.getCollection("apikey_stats"); + apiKeyStatsMongoDBCollection = mongoDataStore.getCollection("apikey_stats", WriteConcern.ACKNOWLEDGED, ReadPreference.primary()); } public CellBaseDataResult getAll() { diff --git a/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/GenericRestWSServer.java b/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/GenericRestWSServer.java index a8daf4fa6..fc6cf0811 100755 --- a/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/GenericRestWSServer.java +++ b/cellbase-server/src/main/java/org/opencb/cellbase/server/rest/GenericRestWSServer.java @@ -116,16 +116,18 @@ public GenericRestWSServer(@PathParam("version") String version, @PathParam("spe this.species = species; try { - init(); + if (!INITIALIZED.get()) { + init(); + } initQuery(); } catch (Exception e) { throw new CellBaseServerException(e.getMessage()); } } - private void init() throws IOException, CellBaseException { + private synchronized void init() throws IOException, CellBaseException { // we need to make sure we only init one single time - if (INITIALIZED.compareAndSet(false, true)) { + if (!INITIALIZED.get()) { SERVICE_START_DATE = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); WATCH = new StopWatch(); WATCH.start(); @@ -159,19 +161,21 @@ private void init() throws IOException, CellBaseException { cellBaseManagerFactory = new CellBaseManagerFactory(cellBaseConfiguration); logger.info("***************************************************"); + // Get default API key (for anonymous queries) + if (apiKeyManager == null) { + apiKeyManager = new ApiKeyManager(cellBaseConfiguration.getSecretKey()); + defaultApiKey = apiKeyManager.getDefaultApiKey(); + logger.info("default API key {}", defaultApiKey); + } + // Initialize Monitor monitor = new Monitor(cellBaseManagerFactory.getMetaManager()); + + INITIALIZED.set(true); } } private void initQuery() throws CellBaseException { - // Get default API key (for anonymous queries) - if (apiKeyManager == null) { - apiKeyManager = new ApiKeyManager(cellBaseConfiguration.getSecretKey()); - defaultApiKey = apiKeyManager.getDefaultApiKey(); - logger.info("default API key {}", defaultApiKey); - } - startTime = System.currentTimeMillis(); query = new Query(); uriParams = convertMultiToMap(uriInfo.getQueryParameters()); @@ -184,12 +188,10 @@ private void initQuery() throws CellBaseException { // Set default API key, if necessary String apiKey = uriParams.getOrDefault(API_KEY_PARAM, null); - logger.info("Before checking, API key {}", apiKey); if (StringUtils.isEmpty(apiKey)) { apiKey = defaultApiKey; uriParams.put(API_KEY_PARAM, apiKey); } - logger.info("After checking, API key {}", uriParams.get(API_KEY_PARAM)); checkLimit(); @@ -228,11 +230,6 @@ protected int getDataRelease() throws CellBaseException { throw new CellBaseException("Invalid data release number '" + uriParams.get(DATA_RELEASE_PARAM) + "'"); } } - // If no data release is present in the query, then use the default data release - if (!DONT_CHECK_SPECIES.equals(species)) { - logger.info("No data release present in query: using the default data release '" + defaultDataRelease + "' for CellBase version" - + " '" + version + "'"); - } return defaultDataRelease; } @@ -273,7 +270,8 @@ private void checkVersion() throws CellBaseException { } private void checkApiKey() throws CellBaseException { - if (!uriInfo.getPath().contains("health")) { + // Update the API key content only for non-meta endpoints + if (!uriInfo.getPath().contains("/meta/")) { String apiKey = getApiKey(); ApiKeyJwtPayload payload = apiKeyManager.decode(apiKey); @@ -414,7 +412,7 @@ protected Response createOkResponse(Object obj) { // Update API key stats, if necessary try { - if (!uriInfo.getPath().contains("health")) { + if (!uriInfo.getPath().contains("/meta/")) { String apiKey = getApiKey(); MetaManager metaManager = cellBaseManagerFactory.getMetaManager(); long bytes = (jsonResponse.getEntity() != null) ? jsonResponse.getEntity().toString().length() : 0;