0

well this is a strange one. The first save attampt usually works (1 more try max).
but in a heavy load (many saves in a row) the saved file disappears.
if uncommenting the "Thread.sleep" the error is captured otherwise the validation passes succesfully

public void save(Object key, T objToSave) throws FileNotFoundException, IOException {
    IOException ex = null;
    for (int i = 0; i < NUM_OF_RETRIES; i++) {
        try {
            /* saving */
            String filePath = getFilePath(key);
            OutputStream outStream = getOutStream(filePath);
            ObjectOutputStream os = new ObjectOutputStream(outStream);          
            os.writeObject(objToSave);
            os.close(); 

            /* validations warnings etc. */
            if (i>0){
                logger.warn(objToSave + "  saved on attamped " + i);
                /* sleep more on each fail */
                Thread.sleep(100+i*8);
            }

            //Thread.sleep(50);
            File doneFile = new File(filePath); 
            if (! (doneFile.exists())){
                logger.error("got here but file was not witten to disk ! id was" + key);
                throw new IOException();
            }
            logger.info("6. start persist " + key + " path=" + new File(filePath).getAbsolutePath() + " exists="+doneFile.exists());

            return;
        } catch (IOException e) {
            logger.error(objToSave + " failed on attamped " + i);
            ex = e;
        } catch (InterruptedException e) {          
            e.printStackTrace();
        }
    }
    throw ex;
}
ozma
  • 1,633
  • 1
  • 20
  • 28
  • It didn't disappear. You just overran the filesystem as to noticing through `exists` that the file really is there. – Marko Topolnik Jul 20 '12 at 10:34
  • thanks, but i added that validation only after the file was not saved in the first place (running "ls" on the directory). with the sleep i can see the file is missing and write it again – ozma Jul 20 '12 at 10:40
  • Is it possible you are writing to this file using more than one thread or process? – Peter Lawrey Jul 20 '12 at 11:54

1 Answers1

0

It is not a java writers issue. I was not using threads explicitly but in my test I was deleting the folder i was saving to using: Runtime.getRuntime("rm -rf saver_directory"); I found out the hard way that it is asynchronous and the exact delete and create time was changing in mili-seconds. so the solution was adding "sleep" after the delete. the correct answer would be using java for the delete and not making a shortcuts ;)

Thank you all.

ozma
  • 1,633
  • 1
  • 20
  • 28