CMIS creating duplicates when i make a folder creation call successively.

I have a logic in my client java code, which creates a document given the path, where it should reside. My logic checks for non-existing folders in a path. This check is in place to avoid FileNotFound exception and create the hierarchy first before creating the actual leaf document. As per our requirement, we don't want two documents to have the same name & path. What happens is, the folder creation logic when invoked in succession for missing folders, ends up creating two folders in the nuxeo. Both the folders have the same name & path extension. I know this is happening due to race condition. But i want to handle this issue in the nuxeo side by adding an eventHandler or something else.

parent.createFolder(properties)

To circumvent this issue, i added a eventHandler for 'AboutToCreate' and checked if the document with same name exists in that location. Provided which, i will throw an error back after marking the transaction as rollback.

This solution that i tried does not work for two successive or consecutive requests, which results in a racing conditions and ends up creating duplicates.

I m able to replicate this issue by calling the nuxeo rest api via nodeJS and try to create two folders with same name in the same location. Please find the nodeJS code below.

var nxfd = [{  "entity-type": "document",  "type": "Folder",  "name": "fol2",  "properties": {    "dc:title": "fol2"  }},{  "entity-type": "document",  "type": "Folder",  "name": "fol2",  "properties": {    "dc:title": "fol2"  }}];

nxfd.forEach(function (form) { 
        var options = {    uri: 'http://localhost:8080/nuxeo/api/v1/path/DevPortal/workspaces/Work/',    headers: {      "Content-Type": "application/json",      "Authorization": "Basic *****************"    },    body: JSON.stringify(form)  }; 

               request.post(options, function optionalCallback(err, httpResponse, body) {   if (err) {      console.log(err);      // res.send(err);    } else {      console.log(body);      if (!res._sent100)        res.sendStatus(201);    }  });
});

More related to this question below,

https://answers.nuxeo.com/general/q/c0b0588a811e40fe8882e877521ffad5/How-to-validate-duplicate-workspace-and-Folder-in-Nuxeo

0 votes

1 answers

214 views

ANSWER



Got a fix working yesterday after posting this.

Created a eventHandler for 'AboutToCreate' event, checked for the documentType, (so that i do this only for types i want it to kick in for), Made the handleEvent method of the eventHandler as 'SYNCHRONIZED'. This makes the second wait for the first thread to complete.

Added a caffiene cache to record the state of eventHandler just incase, if it is required for the other threads.

0 votes



rg1
Be aware that this solution will likely not work in a clustered environment and the synchronization may negatively impact the scalability of the platform.
09/15/2016