Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-83

E11000 duplicate key error while parallel writing to GridFS

    • Type: Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Priority: Icon: Major - P3 Major - P3
    • 1.4
    • Affects Version/s: None
    • Component/s: None

      var path = './test.jpg'; // Test file path (approx 2MB)

      ////

      var mongodb = require('mongodb');
      var fs = require('fs');

      var mongodbClient = new mongodb.Db('test', new mongodb.Server('127.0.0.1', 27017, {}),

      { w: 1 }

      );

      var writeFile = function (path, sample, cb) {
      var name = 'file' + new mongodb.ObjectID().toString();
      var gs = new mongodb.GridStore(mongodbClient, name, 'w');
      gs.open(function (err, gs) {
      if (err) throw err;
      console.log('start writing ' + sample);
      var stdout = fs.createReadStream(path);
      stdout.on('close', function () {
      console.log('finish writing ' + sample);
      gs.close(function ()

      { cb(null, name); }

      );
      });
      stdout.pipe(gs);
      });
      };

      mongodbClient.open(function (err) {
      if (!err) {
      for (var i = 0; i < 10; i++) {
      writeFile(path, i, function (err, name)

      { console.log(name); }

      );
      }
      }
      });

      In most cases, the code is working properly but sometimes I got the error like this:

      bash-3.2$ node ./gfs-test.js
      start writing 1
      start writing 0
      start writing 3
      start writing 6
      start writing 5
      start writing 7
      start writing 2
      start writing 4
      start writing 8
      start writing 9
      finish writing 5
      finish writing 1
      finish writing 3
      finish writing 8
      finish writing 4
      finish writing 6
      finish writing 0
      finish writing 7
      file51b85a3a95b9e6000000000b
      file51b85a3a95b9e6000000000f

      /Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/connection/server.js:569
      throw err;
      ^
      MongoError: E11000 duplicate key error index: test.fs.chunks.$id dup key: { : ObjectId('51b8561bb5cc43000000005f') }
      at Object.exports.toError (/Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/utils.js:108:11)
      at null.<anonymous> (/Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/collection.js:337:24)
      at g (events.js:175:14)
      at EventEmitter.emit (events.js:106:17)
      at Server.Base._callHandler (/Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/connection/base.js:409:25)
      at /Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/connection/server.js:562:20
      at MongoReply.parseBody (/Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:131:5)
      at null.<anonymous> (/Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/connection/server.js:521:22)
      at EventEmitter.emit (events.js:95:17)
      at null.<anonymous> (/Users/vadim/Documents/Projects/test/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:190:13)

      I'm tryed to use journal : true and fsync : true options, but it didn't help.

      My environment:
      OS: OS X 10.8.3
      MongoDB v 2.4.4 installed from HomeBrew repository with default configuration
      Node.JS v 0.10.10
      mongodb-native driver from npm v 1.3.9
      4 participants

      christkv commented 4 months ago
      fix in master c7adad9
      Closed
      christkv closed the issue 4 months ago

      baryshev commented 4 months ago
      Now (with master) the test case works fine, but i still get this error in my app with additionally business logic. I will try to create new test case that represents the error.

      dypsilon commented 4 days ago
      I have the same problem in 1.3.19 when piping a stream into a gridstore, please consider reopening this ticket.

      psi-4ward commented 3 days ago
      same here

      christkv commented 3 days ago
      Please provide some code and instructions to reproduce the issue
      Reopened
      christkv reopened the issue 3 days ago
      Closed
      christkv closed the issue a day ago

      christkv commented a day ago
      The code above passes perfectly, can someone please put a case in that reflects their particular issue ?
      Reopened
      christkv reopened the issue a day ago

      psi-4ward commented a day ago
      I will try to prepare a testcase next week.

      dypsilon commented a day ago
      In my case the issue was appearing because I used the module wrong: I called "close()" directly on the "end" event of the incoming stream. That caused the dupkey problems.

      psi-4ward commented a day ago
      @dypsilon can you post a code sample plase?

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            christkv Christian Amor Kvalheim
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: