From 58fcad9da6d3fa848d4d56fc7df4ba013df50bf3 Mon Sep 17 00:00:00 2001 From: Fabio Sinibaldi Date: Fri, 8 Apr 2022 15:20:21 +0200 Subject: [PATCH] Rethrow exceptions --- .../engine/mongo/ProfiledMongoManager.java | 87 ++++++++++--------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java index 614d322..75f9675 100644 --- a/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java +++ b/geoportal-service/src/main/java/org/gcube/application/geoportal/service/engine/mongo/ProfiledMongoManager.java @@ -139,7 +139,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< } protected Project unlockAndUpdate(Project proj) throws InvalidLockException, ProjectNotFoundException, JsonProcessingException, InvalidUserRoleException, UnauthorizedAccess { - log.trace("Unlocking {} lock is {} ",proj.getId(),proj.getLock()); + log.trace("Unlocking for update {} lock is {} ",proj.getId(),proj.getLock()); // find one and update Lock oldLock = proj.getLock(); proj.setLock(null); @@ -158,15 +158,31 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< Project p = getByID(proj.getId()); throw new InvalidLockException("Found lock for "+p.getId()+" is "+p.getLock()+", expected is "+oldLock); }return Serialization.convert(obj,Project.class); - - // filter : id, lock id - // update with project (NB without lock) - // if none matched - // not found if proj id non existent - // else invalid lock -// return null; } + protected Project unlock(Project proj) throws InvalidLockException, InvalidUserRoleException, ProjectNotFoundException, UnauthorizedAccess { + log.trace("Unlocking for update {} lock is {} ",proj.getId(),proj.getLock()); + // find one and update + Lock oldLock = proj.getLock(); + Document filter = new Document(mongoIDFieldName(),asId(proj.getId())).append(Project.LOCK+"."+Lock.ID,oldLock.getId()); + + Object obj = getCollection().findOneAndUpdate( + // filter by id and missing lock + filter, + // update lock info + new Document("$set",new Document(Project.LOCK, null)), + new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER) + ); + + if(obj== null){ + // can-t unlock, check cause + Project p = getByID(proj.getId()); + throw new InvalidLockException("Found lock for "+p.getId()+" is "+p.getLock()+", expected is "+oldLock); + }return Serialization.convert(obj,Project.class); + } + + + @Getter(lazy = true) private final LifecycleManager manager=getLCManager(); @@ -195,7 +211,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< @Override - public Project registerNew(Document toRegisterDoc) throws IOException, StepException, EventException, InvalidUserRoleException { + public Project registerNew(Document toRegisterDoc) throws IOException, InvalidUserRoleException { log.info("Registering new document in {} ", useCaseDescriptor.getId()); log.trace("Going to register {}",toRegisterDoc.toJson()); @@ -274,8 +290,11 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< toUpdate.setTheDocument(toSet); toUpdate.getLifecycleInformation().cleanState(); toUpdate = onUpdate(toUpdate); - }finally{ return unlockAndUpdate(toUpdate); + }catch(Throwable t){ + log.error("Unexpected exception ",t); + unlock(toUpdate); + throw t; } } @@ -420,8 +439,6 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< @Override public Project performStep(String id, String step, Document options) throws StepException, JsonProcessingException, ProjectLockedException, ProjectNotFoundException, InvalidLockException, InvalidUserRoleException, UnauthorizedAccess, ConfigurationException, InsufficientPrivileges { Project document = lock(id,"Step "+step+" execution"); - Boolean store = true; - try { User u = UserUtils.getCurrent().asInfo().getUser(); @@ -437,33 +454,21 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< document.getLifecycleInformation().cleanState(); document = step(document, step, options); - - }catch (UnrecognizedStepException e){ - store = false; + return unlockAndUpdate(document); + }catch (UnrecognizedStepException | ConfigurationException | InsufficientPrivileges e){ + log.debug("Unable to perform step ",e); + unlock(document); throw e; - }catch (ConfigurationException e){ - store = false; - throw e; - }catch (InsufficientPrivileges e){ - store = false; - throw e; - } catch(Throwable t){ - log.error("[UseCaseDescriptor {} ] ERROR Invoking Step {} on document {}" , useCaseDescriptor.getId(),step,id,t); + } catch(Throwable t) { + log.error("[UseCaseDescriptor {} ] ERROR Invoking Step {} on document {}", useCaseDescriptor.getId(), step, id, t); LifecycleInformation info = new LifecycleInformation(); info.setPhase(document.getLifecycleInformation().getPhase()); info.setLastOperationStatus(LifecycleInformation.Status.ERROR); info.addErrorMessage(t.getMessage()); info.setLastInvokedStep(step); document.setLifecycleInformation(info); - }finally{ - if (store) { - log.info("Storing {} [UseCaseDescriptor {}] After Step {}, Status : {} " ,id, useCaseDescriptor.getId(), - step,document.getLifecycleInformation().getLastOperationStatus()); - log.debug("LifecycleInformation is {} ",document.getLifecycleInformation()); - if(log.isTraceEnabled())log.trace("Document is {} ",Serialization.write(document)); - } + return unlockAndUpdate(document); } - return unlockAndUpdate(document); } @@ -481,7 +486,7 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< * */ @Override - public Project registerFileSet(String id, RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, StepException, JsonProcessingException, DeletionException, EventException, ProjectLockedException, ProjectNotFoundException, InvalidLockException, InvalidUserRoleException, UnauthorizedAccess { + public Project registerFileSet(String id, RegisterFileSetRequest request) throws ConfigurationException, StorageHubException, StorageException, JsonProcessingException, EventException, ProjectLockedException, ProjectNotFoundException, InvalidLockException, InvalidUserRoleException, UnauthorizedAccess { log.info("Registering Fileset for {} [useCaseDescriptor ID {}], Request is {} ",id, useCaseDescriptor.getId(),request); List files=request.getStreams(); @@ -578,12 +583,13 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< doc.setTheDocument(Document.parse(docWrapper.getValueCTX().jsonString())); doc = onUpdate(doc); - }catch (Throwable t){ - log.warn("Unexpected Exception while trying to registering fileset on {}.",id,t); - log.debug("Request was {}",request); - log.debug("Complete doc was {} ",doc); - }finally { return unlockAndUpdate(doc); + }catch (Throwable t) { + log.warn("Unexpected Exception while trying to registering fileset on {}.", id, t); + log.debug("Request was {}", request); + log.debug("Complete doc was {} ", doc); + unlock(doc); + throw t; } } @@ -607,10 +613,11 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI< doc.getLifecycleInformation().cleanState().setLastOperationStatus(LifecycleInformation.Status.OK); doc= deleteFileSetRoutine(doc,force,path); - doc = onUpdate(doc); - return doc; - }finally { return unlockAndUpdate(doc); + }catch (Throwable t) { + log.warn("Unexpected Exception while trying to delete fileset on {}.", id, t); + unlock(doc); + throw t; } }