1
0
Fork 0

adding mutex around directory and file creation in tgconstruct.

fixes corruption of stage 2 data files, which would crash stage 3
lower print verbosity at INFO level.
This commit is contained in:
Peter Sadrozinski 2014-01-02 19:33:25 -05:00
parent 2aff051ea7
commit 733f9347ab
7 changed files with 74 additions and 45 deletions

View file

@ -191,6 +191,7 @@ int main(int argc, char **argv) {
}
std::vector<TGConstruct *> constructs;
SGMutex filelock;
/* fill the workqueue */
for (unsigned int i=0; i<bucketList.size(); i++) {
@ -199,7 +200,7 @@ int main(int argc, char **argv) {
// now create the worker threads for stage 1
for (int i=0; i<num_threads; i++) {
TGConstruct* construct = new TGConstruct( areas, 1, wq );
TGConstruct* construct = new TGConstruct( areas, 1, wq, &filelock );
//construct->set_cover( cover );
construct->set_paths( work_dir, share_dir, output_dir, load_dirs );
construct->set_options( ignoreLandmass, nudge );
@ -232,7 +233,7 @@ int main(int argc, char **argv) {
}
for (int i=0; i<num_threads; i++) {
TGConstruct* construct = new TGConstruct( areas, 2, wq );
TGConstruct* construct = new TGConstruct( areas, 2, wq, &filelock );
//construct->set_cover( cover );
construct->set_paths( work_dir, share_dir, output_dir, load_dirs );
construct->set_options( ignoreLandmass, nudge );
@ -264,7 +265,7 @@ int main(int argc, char **argv) {
}
for (int i=0; i<num_threads; i++) {
TGConstruct* construct = new TGConstruct( areas, 3, wq );
TGConstruct* construct = new TGConstruct( areas, 3, wq, &filelock );
//construct->set_cover( cover );
construct->set_paths( work_dir, share_dir, output_dir, load_dirs );
construct->set_options( ignoreLandmass, nudge );

View file

@ -33,7 +33,7 @@
const double TGConstruct::gSnap = 0.00000001; // approx 1 mm
// Constructor
TGConstruct::TGConstruct( const TGAreaDefinitions& areas, unsigned int s, SGLockedQueue<SGBucket>& q) :
TGConstruct::TGConstruct( const TGAreaDefinitions& areas, unsigned int s, SGLockedQueue<SGBucket>& q, SGMutex* l) :
area_defs(areas),
workQueue(q),
stage(s),
@ -44,6 +44,7 @@ TGConstruct::TGConstruct( const TGAreaDefinitions& areas, unsigned int s, SGLock
{
total_tiles = q.size();
num_areas = areas.size();
lock = l;
}
@ -219,7 +220,7 @@ void TGConstruct::run()
// STEP 19)
// Write Custom objects to .stg file
SG_LOG(SG_GENERAL, SG_ALERT, bucket.gen_index_str() << " - Generate Custome Objects");
SG_LOG(SG_GENERAL, SG_ALERT, bucket.gen_index_str() << " - Generate Custom Objects");
AddCustomObjects();
}
break;

View file

@ -60,7 +60,7 @@ class TGConstruct : public SGThread
{
public:
// Constructor
TGConstruct( const TGAreaDefinitions& areas, unsigned int s, SGLockedQueue<SGBucket>& q );
TGConstruct( const TGAreaDefinitions& areas, unsigned int s, SGLockedQueue<SGBucket>& q, SGMutex* l );
// Destructor
~TGConstruct();
@ -223,6 +223,9 @@ private:
// Neighbor Faces
neighbor_face_list neighbor_faces;
// file lock
SGMutex* lock;
};
#endif // _CONSTRUCT_HXX

View file

@ -267,7 +267,7 @@ bool TGConstruct::ClipLandclassPolys( void ) {
remains.SetTexMethod( TG_TEX_BY_GEODE, bucket.get_center_lat() );
remains.SetId(9999);
SG_LOG( SG_CLIPPER, SG_INFO, "Adding remains to area " << area_defs.get_sliver_area_priority() );
SG_LOG( SG_CLIPPER, SG_DEBUG, "Adding remains to area " << area_defs.get_sliver_area_priority() );
polys_clipped.add_poly( area_defs.get_sliver_area_priority(), remains );
}
}

View file

@ -49,6 +49,8 @@ void TGConstruct::AddCustomObjects( void ) {
string dest_ind = dest_i.str_native();
FILE *fp;
lock->lock();
if ( (fp = fopen( dest_ind.c_str(), "w" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_ALERT, "ERROR: opening " << dest_ind << " for writing!" );
exit(-1);
@ -76,15 +78,15 @@ void TGConstruct::AddCustomObjects( void ) {
//No custom objects
} else {
while ( ! in.eof() ) {
SG_LOG( SG_GENERAL, SG_INFO, "Collecting custom objects from " << index_file );
SG_LOG( SG_GENERAL, SG_DEBUG, "Collecting custom objects from " << index_file );
in.getline(line, 2048);
SG_LOG( SG_GENERAL, SG_INFO, "line = " << line );
SG_LOG( SG_GENERAL, SG_DEBUG, "line = " << line );
int result = sscanf( line, "%s %s", token, name );
SG_LOG( SG_GENERAL, SG_INFO, "scanf scanned " << result << " tokens" );
SG_LOG( SG_GENERAL, SG_DEBUG, "scanf scanned " << result << " tokens" );
if ( result > 0 ) {
SG_LOG( SG_GENERAL, SG_INFO, "token = " << token << " name = " << name );
SG_LOG( SG_GENERAL, SG_DEBUG, "token = " << token << " name = " << name );
if ( strcmp( token, "OBJECT" ) == 0 ) {
SGPath srcbase(base);
@ -96,7 +98,7 @@ void TGConstruct::AddCustomObjects( void ) {
#else
string command = "cp " + basecom + " " + dest_dir;
#endif
SG_LOG( SG_GENERAL, SG_INFO, "running " << command );
SG_LOG( SG_GENERAL, SG_DEBUG, "running " << command );
system( command.c_str() );
fprintf(fp, "OBJECT %s\n", name);
@ -109,6 +111,8 @@ void TGConstruct::AddCustomObjects( void ) {
}
fclose(fp);
lock->unlock();
}
void TGConstruct::WriteBtgFile( void )
@ -222,7 +226,11 @@ void TGConstruct::WriteBtgFile( void )
obj.set_fan_materials( fan_materials );
bool result;
lock->lock();
result = obj.write_bin( base, binname, bucket );
lock->unlock();
if ( !result )
{
throw sg_exception("error writing file. :-(");

View file

@ -120,7 +120,7 @@ int TGConstruct::LoadLandclassPolys( void ) {
} // of directory file children
}
SG_LOG(SG_GENERAL, SG_ALERT, " Total polys read in this tile: " << total_polys_read );
SG_LOG(SG_GENERAL, SG_DEBUG, " Total polys read in this tile: " << total_polys_read );
return total_polys_read;
}

View file

@ -47,6 +47,9 @@ void TGConstruct::SaveSharedEdgeData( int stage )
filepath = share_base + "/stage1/" + bucket.gen_base_path() + "/" + bucket.gen_index_str() + "_edges";
SGPath file(filepath);
lock->lock();
file.create_dir( 0755 );
gzFile fp;
@ -86,6 +89,8 @@ void TGConstruct::SaveSharedEdgeData( int stage )
}
gzclose(fp);
lock->unlock();
}
break;
@ -102,23 +107,28 @@ void TGConstruct::SaveSharedEdgeData( int stage )
// are updated, we'll need to traverse all of these point lists, and update
// any border nodes elevation as well
string dir;
string file;
string file_north, file_south, file_east, file_west;
gzFile fp;
std::vector<SGGeod> north, south, east, west;
int nCount;
nodes.get_geod_edge( bucket, north, south, east, west );
dir = share_base + "/stage2/" + bucket.gen_base_path();
file_north = dir + "/" + bucket.gen_index_str() + "_north_edge";
file_south = dir + "/" + bucket.gen_index_str() + "_south_edge";
file_east = dir + "/" + bucket.gen_index_str() + "_east_edge";
file_west = dir + "/" + bucket.gen_index_str() + "_west_edge";
SGPath sgp( dir );
sgp.append( "dummy" );
lock->lock();
sgp.create_dir( 0755 );
// north edge
file = dir + "/" + bucket.gen_index_str() + "_north_edge";
gzFile fp;
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_north.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_north.c_str() << " for writing!" );
return;
}
sgClearWriteError();
@ -133,9 +143,8 @@ void TGConstruct::SaveSharedEdgeData( int stage )
gzclose(fp);
// south edge
file = dir + "/" + bucket.gen_index_str() + "_south_edge";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_south.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_south.c_str() << " for writing!" );
return;
}
sgClearWriteError();
@ -149,9 +158,8 @@ void TGConstruct::SaveSharedEdgeData( int stage )
gzclose(fp);
// east edge
file = dir + "/" + bucket.gen_index_str() + "_east_edge";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_east.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_east.c_str() << " for writing!" );
return;
}
sgClearWriteError();
@ -165,9 +173,8 @@ void TGConstruct::SaveSharedEdgeData( int stage )
gzclose(fp);
// west egde
file = dir + "/" + bucket.gen_index_str() + "_west_edge";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_west.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_west.c_str() << " for writing!" );
return;
}
sgClearWriteError();
@ -179,6 +186,8 @@ void TGConstruct::SaveSharedEdgeData( int stage )
WriteNeighborFaces( fp, west[i] );
}
gzclose(fp);
lock->unlock();
}
break;
}
@ -392,38 +401,41 @@ void TGConstruct::ReadNeighborFaces( gzFile& fp )
void TGConstruct::SaveToIntermediateFiles( int stage )
{
string dir;
string file;
string file_clipped;
string file_nodes;
gzFile fp;
switch( stage ) {
case 1: // Save the clipped polys and node list
{
/* Only create the file this isn't an ocean tile */
if ( !IsOceanTile() ) {
if ( !IsOceanTile() ) {
dir = share_base + "/stage1/" + bucket.gen_base_path();
SGPath sgp( dir );
sgp.append( "dummy" );
file_clipped = dir + "/" + bucket.gen_index_str() + "_clipped_polys";
file_nodes = dir + "/" + bucket.gen_index_str() + "_nodes";
lock->lock();
sgp.create_dir( 0755 );
file = dir + "/" + bucket.gen_index_str() + "_clipped_polys";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_clipped.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_clipped.c_str() << " for writing!" );
return;
}
sgClearWriteError();
polys_clipped.SaveToGzFile( fp );
gzclose( fp );
file = dir + "/" + bucket.gen_index_str() + "_nodes";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_nodes.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_nodes.c_str() << " for writing!" );
return;
}
sgClearWriteError();
nodes.SaveToGzFile( fp );
gzclose( fp );
lock->unlock();
}
break;
@ -436,25 +448,29 @@ void TGConstruct::SaveToIntermediateFiles( int stage )
SGPath sgp( dir );
sgp.append( "dummy" );
file_clipped = dir + "/" + bucket.gen_index_str() + "_clipped_polys";
file_nodes = dir + "/" + bucket.gen_index_str() + "_nodes";
lock->lock();
sgp.create_dir( 0755 );
file = dir + "/" + bucket.gen_index_str() + "_clipped_polys";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_clipped.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_clipped.c_str() << " for writing!" );
return;
}
sgClearWriteError();
polys_clipped.SaveToGzFile( fp );
gzclose( fp );
file = dir + "/" + bucket.gen_index_str() + "_nodes";
if ( (fp = gzopen( file.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file.c_str() << " for writing!" );
if ( (fp = gzopen( file_nodes.c_str(), "wb9" )) == NULL ) {
SG_LOG( SG_GENERAL, SG_INFO,"ERROR: opening " << file_nodes.c_str() << " for writing!" );
return;
}
sgClearWriteError();
nodes.SaveToGzFile( fp );
gzclose( fp );
lock->unlock();
}
break;
}