Skip to content

Commit

Permalink
Release and reacquire netcdf file in the ThreddsWmsCatalogue
Browse files Browse the repository at this point in the history
  • Loading branch information
tdrwenski committed Feb 1, 2024
1 parent dfef517 commit c7c90f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,11 @@ protected NetcdfDataset getNetcdfDatasetFromLocation(String location, boolean fo
return this.netcdfDataset;
}

void release() throws IOException {
netcdfDataset.release();
}

void reacquire() throws IOException {
netcdfDataset.reacquire();
}
}
14 changes: 13 additions & 1 deletion tds/src/main/java/thredds/server/wms/ThreddsWmsCatalogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package thredds.server.wms;

import java.io.Closeable;
import org.joda.time.DateTime;
import thredds.server.config.TdsServerInfoBean;
import thredds.server.config.WmsConfigBean;
Expand Down Expand Up @@ -75,7 +76,7 @@
*
* @author Guy Griffiths
*/
public class ThreddsWmsCatalogue implements WmsCatalogue {
public class ThreddsWmsCatalogue implements WmsCatalogue, Closeable {
/*
* I have declared this as static to be shared between all instances of this
* class.
Expand Down Expand Up @@ -431,4 +432,15 @@ public EnhancedVariableMetadata getLayerMetadata(final VariableMetadata metadata
*/
return new TdsEnhancedVariableMetadata(this, metadata);
}

// release netcdfFile locks
@Override
public void close() throws IOException {
datasetFactory.release();
}

// reacquire netcdfFile resources
void reacquire() throws IOException {
datasetFactory.reacquire();
}
}
16 changes: 9 additions & 7 deletions tds/src/main/java/thredds/server/wms/ThreddsWmsServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void resetCache() {
@Override
@RequestMapping(value = "**", method = {RequestMethod.GET})
protected void dispatchWmsRequest(String request, RequestParams params, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, WmsCatalogue catalogue) throws Exception {
HttpServletResponse httpServletResponse, WmsCatalogue wmsCatalogue) throws Exception {
/*
* The super implementation of this gets called with a servlet-wide
* catalogue, which "should" have been injected with the
Expand All @@ -96,13 +96,14 @@ protected void dispatchWmsRequest(String request, RequestParams params, HttpServ
// Look - is setting this to null the right thing to do??
String removePrefix = null;
TdsRequestedDataset tdsDataset = new TdsRequestedDataset(httpServletRequest, removePrefix);
catalogue = acquireCatalogue(httpServletRequest, httpServletResponse, tdsDataset);
try (ThreddsWmsCatalogue catalogue = acquireCatalogue(httpServletRequest, httpServletResponse, tdsDataset)) {

/*
* Now that we've got a WmsCatalogue, we can pass this request to the
* super implementation which will handle things from here.
*/
super.dispatchWmsRequest(request, params, httpServletRequest, httpServletResponse, catalogue);
/*
* Now that we've got a WmsCatalogue, we can pass this request to the
* super implementation which will handle things from here.
*/
super.dispatchWmsRequest(request, params, httpServletRequest, httpServletResponse, catalogue);
}
}

private ThreddsWmsCatalogue acquireCatalogue(HttpServletRequest httpServletRequest,
Expand All @@ -111,6 +112,7 @@ private ThreddsWmsCatalogue acquireCatalogue(HttpServletRequest httpServletReque

if (useCachedCatalogue(tdsDataset.getPath())) {
catalogue = catalogueCache.get(tdsDataset.getPath()).wmsCatalogue;
catalogue.reacquire();
} else {
NetcdfDataset ncd = acquireNetcdfDataset(httpServletRequest, httpServletResponse, tdsDataset);

Expand Down

0 comments on commit c7c90f1

Please sign in to comment.