Skip to content

Commit

Permalink
raster code proper handling of Thread exception
Browse files Browse the repository at this point in the history
  • Loading branch information
elidwa committed Jun 20, 2024
1 parent d67e33f commit 432e891
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
33 changes: 22 additions & 11 deletions packages/geo/GeoIndexedRaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,6 @@ bool GeoIndexedRaster::openGeoIndex(const OGRGeometry* geo)
*----------------------------------------------------------------------------*/
void GeoIndexedRaster::sampleRasters(OGRGeometry* geo)
{
/* Create additional reader threads if needed */
createThreads();

/* For each raster which is marked to be sampled, give it to the reader thread */
int signaledReaders = 0;
int i = 0;
Expand Down Expand Up @@ -496,8 +493,13 @@ bool GeoIndexedRaster::sample(OGRGeometry* geo, int64_t gps)

if(findRasters(geo) && filterRasters(gps) && updateCache())
{
sampleRasters(geo);
status = true;
/* Create additional reader threads if needed */
status = createThreads();
if(status)
{
sampleRasters(geo);
status = true;
}
}

return status;
Expand Down Expand Up @@ -677,21 +679,30 @@ void* GeoIndexedRaster::readingThread(void *param)
/*----------------------------------------------------------------------------
* createThreads
*----------------------------------------------------------------------------*/
void GeoIndexedRaster::createThreads(void)
bool GeoIndexedRaster::createThreads(void)
{
const int threadsNeeded = cache.length();
const int threadsNow = readers.length();
const int newThreadsCnt = threadsNeeded - threadsNow;

if(threadsNeeded <= threadsNow)
return;
return true;

for(int i = 0; i < newThreadsCnt; i++)
try
{
Reader* r = new Reader(this);
readers.add(r);
for(int i = 0; i < newThreadsCnt; i++)
{
Reader* r = new Reader(this);
readers.add(r);
}
}
catch (const RunTimeException &e)
{
ssError |= SS_RESOURCE_LIMIT_ERROR;
mlog(CRITICAL, "Failed to create reader threads, needed: %d, created: %d", newThreadsCnt, readers.length() - threadsNow);
}
assert(readers.length() == threadsNeeded);

return readers.length() == threadsNeeded;
}

/*----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion packages/geo/GeoIndexedRaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class GeoIndexedRaster: public RasterObject

static void* readingThread (void *param);

void createThreads (void);
bool createThreads (void);
bool updateCache (void);
bool filterRasters (int64_t gps);
};
Expand Down
1 change: 1 addition & 0 deletions packages/geo/GeoParms.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#define SS_WRITE_ERROR (1 << 4)
#define SS_SUBRASTER_ERROR (1 << 5)
#define SS_INDEX_FILE_ERROR (1 << 6)
#define SS_RESOURCE_LIMIT_ERROR (1 << 7)


/******************************************************************************
Expand Down
12 changes: 12 additions & 0 deletions packages/geo/RasterObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ int RasterObject::luaSamples(lua_State *L)
mlog(CRITICAL, "Too many rasters to sample, max allowed: %d, limit your AOI/temporal range or use filters", GeoIndexedRaster::MAX_READER_THREADS);
}

if(err & SS_RESOURCE_LIMIT_ERROR)
{
listvalid = false;
mlog(CRITICAL, "System resource limit reached, could not sample rasters");
}

/* Create return table */
lua_createtable(L, slist.length(), 0);
num_ret++;
Expand Down Expand Up @@ -394,6 +400,12 @@ int RasterObject::slist2table(const List<RasterSubset*>& slist, uint32_t errors,
mlog(CRITICAL, "Some rasters could not be subset, requested memory size > max allowed: %ld MB", RasterSubset::MAX_SIZE / (1024 * 1024));
}

if(errors & SS_RESOURCE_LIMIT_ERROR)
{
listvalid = false;
mlog(CRITICAL, "System resource limit reached, could not subset rasters");
}

/* Create return table */
lua_createtable(L, slist.length(), 0);
num_ret++;
Expand Down

0 comments on commit 432e891

Please sign in to comment.