jsondb-core icon indicating copy to clipboard operation
jsondb-core copied to clipboard

Failed to move temporary collection file instances

Open zzsoszz opened this issue 6 years ago • 10 comments

package io.swagger.jsondata;

import io.jsondb.annotation.Document;
import io.jsondb.annotation.Id;

@Document(collection = "instances", schemaVersion= "1.0")
public class Instance {
    @Id
    private String name;
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
package io.swagger.utils;
import io.jsondb.JsonDBTemplate;
import io.jsondb.query.Update;
import java.io.File;
import io.swagger.jsondata.Instance;


public class GuidUtils {

    private static final Object lock = new Object();
    private static volatile JsonDBTemplate instance;
    private static String initValue="100000000";

    public static synchronized  JsonDBTemplate getJsonDBTemplate(){
        JsonDBTemplate r = instance;
        if (r == null) {
            synchronized (lock) {    // While we were waiting for the lock, another
                r = instance;        // thread may have instantiated the object.
                if (r == null) {
                    String dbFilesLocation = System.getProperty("user.home")+ File.separator+"jsondb";
                    String baseScanPackage = "io.swagger.jsondata";
                    r= new JsonDBTemplate(dbFilesLocation, baseScanPackage);
                    instance = r;
                }
            }
        }
        return r;
    }


    //C:\Users\EDZ\jsondb\instance.json
    public static String getUniqueId(){
        JsonDBTemplate jsonDBTemplate=GuidUtils.getJsonDBTemplate();
        if(!jsonDBTemplate.collectionExists(Instance.class)){
            jsonDBTemplate.createCollection(Instance.class);
            Instance instance = new Instance();
            instance.setName("guid");
            instance.setValue(initValue);
            jsonDBTemplate.insert(instance);
        }
        Instance instanceOld = jsonDBTemplate.findById("guid", Instance.class);
        Update update = Update.update("value", Long.parseLong(instanceOld.getValue())+1);
        String jxQuery = String.format("/.[name='%s']", "guid");
        Instance instanceNew = jsonDBTemplate.findAndModify(jxQuery, update, "instances");
        return instanceNew.getValue();
    }

    public static void main(String[] args) {
        for(int i=0;i<1000;i++){
            System.out.println(GuidUtils.getUniqueId());
        }
    }
    
}

19-01-17 09:56:43.260 ERROR [main] io.jsondb.io.JsonWriter                  updateInJsonFile 575 : Failed to move temporary collection file instances4765154015649112693.tmp to collection file instances.json
java.nio.file.AccessDeniedException: C:\Users\EDZ\jsondb\instances4765154015649112693.tmp -> C:\Users\EDZ\jsondb\instances.json
	at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
	at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
	at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:301)
	at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
	at java.nio.file.Files.move(Files.java:1395)
	at io.jsondb.io.JsonWriter.updateInJsonFile(JsonWriter.java:573)
	at io.jsondb.JsonDBTemplate.findAndModify(JsonDBTemplate.java:1333)
	at io.swagger.utils.GuidUtils.getUniqueId(GuidUtils.java:47)
	at io.swagger.utils.GuidUtils.main(GuidUtils.java:53)

zzsoszz avatar Jan 17 '19 02:01 zzsoszz

it will show errors below after run a few times.

5573 5574 5575 5576 5577 19-01-17 10:01:37.170 ERROR [main] io.jsondb.io.JsonWriter updateInJsonFile 575 : Failed to move temporary collection file instances6294430801854422529.tmp to collection file instances.json java.nio.file.AccessDeniedException: C:\Users\EDZ\jsondb\instances6294430801854422529.tmp -> C:\Users\EDZ\jsondb\instances.json at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:301) at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287) at java.nio.file.Files.move(Files.java:1395) at io.jsondb.io.JsonWriter.updateInJsonFile(JsonWriter.java:573) at io.jsondb.JsonDBTemplate.findAndModify(JsonDBTemplate.java:1333) at io.swagger.utils.GuidUtils.getUniqueId(GuidUtils.java:44) at io.swagger.utils.GuidUtils.main(GuidUtils.java:50) 5578 5579 5580 5581

zzsoszz avatar Jan 17 '19 02:01 zzsoszz

@zzsoszz I see what you are trying to do, I will try to check this out later today when i get some time.

FarooqKhan avatar Jan 17 '19 02:01 FarooqKhan

I created the following Unittest copying your code with minor modifications, seems to work fine, maybe something to do with Windows

package io.jsondb.tests;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import io.jsondb.JsonDBTemplate;
import io.jsondb.Util;
import io.jsondb.query.Update;
import io.jsondb.tests.model.Instance;

/**
 * @version 1.0 17-Jan-2019
 */
public class FindAndModify2Tests {
  
  private static String dbFilesLocation = "src/test/resources/dbfiles/findAndModify2Tests";
  private File dbFilesFolder = new File(dbFilesLocation);

  /**
   * @throws java.lang.Exception
   */
  @Before
  public void setUp() throws Exception {
    dbFilesFolder.mkdir();
  }

  @After
  public void tearDown() throws Exception {
    Util.delete(dbFilesFolder);
  }
  
  private static final Object lock = new Object();
  private static volatile JsonDBTemplate instance;
  private static String initValue="100000000";
  
  public static synchronized  JsonDBTemplate getJsonDBTemplate(){
    JsonDBTemplate r = instance;
    if (r == null) {
      synchronized (lock) {    // While we were waiting for the lock, another
        r = instance;        // thread may have instantiated the object.
        if (r == null) {
          r= new JsonDBTemplate(dbFilesLocation, "io.jsondb.tests.model");
          instance = r;
        }
      }
    }
    return r;
  }


  public static String getUniqueId(){
    JsonDBTemplate jsonDBTemplate=FindAndModify2Tests.getJsonDBTemplate();
    if(!jsonDBTemplate.collectionExists(Instance.class)){
        jsonDBTemplate.createCollection(Instance.class);
        Instance instance = new Instance();
        instance.setId("guid");
        instance.setHostname(initValue);
        jsonDBTemplate.insert(instance);
    }
    Instance instanceOld = jsonDBTemplate.findById("guid", Instance.class);
    Update update = Update.update("hostname", Long.parseLong(instanceOld.getHostname())+1);
    String jxQuery = String.format("/.[id='%s']", "guid");
    Instance instanceNew = jsonDBTemplate.findAndModify(jxQuery, update, "instances");
    return instanceNew.getHostname();
  }
  
  @Test
  public void test() {
    for(int i=1;i<=1000;i++){
      String id = FindAndModify2Tests.getUniqueId();
      assertEquals(Long.parseLong(initValue)+i, Long.parseLong(id));
      System.out.println("Id is " + id);
    }
  }
}

FarooqKhan avatar Jan 17 '19 10:01 FarooqKhan

I think this might be a windows quirk. I see a few people facing similar problems on windows. See: Stackoverflow Link I dont have windows system to test this theory though.

Can you try this, check if the folder permissions has some SID information as mentioned in the link above, Or maybe the folder is marked readonly OR maybe try closing explorer and then run the code.

FarooqKhan avatar Jan 17 '19 11:01 FarooqKhan

You mentioned it gives this error after running a few times.

For every update operation i first create a temporary file and then replace the existing file with the temporary file as a Atomic Move. This way the update operation will be a atomic operation. Maybe a large number of file move operations are causing a problem on windows.

FarooqKhan avatar Jan 17 '19 11:01 FarooqKhan

i have tried delete directory of "jsondb" and closing explorer before running and recreated directory is not "ready only",but still get this error after running a few times. i found when you moving file to destination,but the destination still exists,does it case this probolem?

zzsoszz avatar Jan 18 '19 02:01 zzsoszz

If that was a problem it would have failed each time.

FarooqKhan avatar Jan 18 '19 12:01 FarooqKhan

I am going to try and get a Windows machine but that is going to take a week or two.

FarooqKhan avatar Jan 18 '19 12:01 FarooqKhan

Could you list here the Window OS your are using?

FarooqKhan avatar Jan 18 '19 12:01 FarooqKhan

win 10 professional

zzsoszz avatar Jan 19 '19 12:01 zzsoszz