Improving code for race condition

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@120299 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-11-18 11:25:06 +00:00
parent 6c8da75cd3
commit 907f2b9707
2 changed files with 16 additions and 4 deletions

View File

@ -111,7 +111,7 @@ public abstract class AccountingPersistenceBackendFactory {
};
protected static AccountingPersistenceBackend rediscoverAccountingPersistenceBackend(AccountingPersistenceBackend actual, String scope){
long now = Calendar.getInstance().getTimeInMillis();
Long now = Calendar.getInstance().getTimeInMillis();
Long lastCheckTimestamp = fallbackLastCheck.get(scope);
logger.trace("Last check was {}", lastCheckTimestamp);
@ -122,8 +122,13 @@ public abstract class AccountingPersistenceBackendFactory {
try {
lock.lock();
logger.trace("Renewing Last check Timestamp. The next one will be {}", now);
Long reCheckTimestamp = fallbackLastCheck.get(scope);
if(lastCheckTimestamp.compareTo(reCheckTimestamp)!=0){
// Someone else arrived here before. Stopping to try to
logger.debug("Someone else arrived here before. Stopping to try to rediscover AccountingPersistenceBackend");
return actual;
}
fallbackLastCheck.put(scope, now);
lastCheckTimestamp = fallbackLastCheck.get(scope);
lock.unlock();
AccountingPersistenceBackend discoveredPersistenceBackend = discoverAccountingPersistenceBackend(scope);

View File

@ -26,6 +26,7 @@ public class AccountingPersistenceBackendMonitor implements Runnable {
private final static Logger logger = LoggerFactory.getLogger(AccountingPersistenceBackendMonitorTest.class);
private final static String ELABORATION_FILE_SUFFIX = ".ELABORATION";
private final static String ELABORATION_FILE_NOT_DELETED_SUFFIX = ".ELABORATION.NOT-DELETED";
protected final ScheduledExecutorService scheduler;
@ -44,8 +45,7 @@ public class AccountingPersistenceBackendMonitor implements Runnable {
UsageRecord usageRecord = BasicUsageRecord.getUsageRecord(line);
accountingPersistenceBackend.accountWithFallback(usageRecord);
} catch(Exception e){
logger.error("", e);
// TODO write line on file
logger.error("Line {} will be lost", line, e);
}
}
} catch (FileNotFoundException e) {
@ -74,6 +74,13 @@ public class AccountingPersistenceBackendMonitor implements Runnable {
if(elaborationFile!=null){
elaborateFile(elaborationFile);
boolean deleted = elaborationFile.delete();
if(!deleted){
logger.debug("Failed to delete file {}", elaborationFile.getAbsolutePath());
File elaborationFileNotDeleted = new File(elaborationFile.getAbsolutePath()+ELABORATION_FILE_NOT_DELETED_SUFFIX);
elaborationFile.renameTo(elaborationFileNotDeleted);
}
}
}