private void addToStream( final Operation operation, final Timestamp<?> currentTimestamp, final DBObject data, final String collection) throws InterruptedException { if (logger.isTraceEnabled()) { logger.trace( "addToStream - operation [{}], currentTimestamp [{}], data [{}], collection [{}]", operation, currentTimestamp, data, collection); } if (operation == Operation.DROP_DATABASE) { if (definition.isImportAllCollections()) { for (String name : slurpedDb.getCollectionNames()) { context .getStream() .put( new MongoDBRiver.QueueEntry( currentTimestamp, Operation.DROP_COLLECTION, data, name)); } } else { context .getStream() .put( new MongoDBRiver.QueueEntry( currentTimestamp, Operation.DROP_COLLECTION, data, collection)); } } else { context .getStream() .put(new MongoDBRiver.QueueEntry(currentTimestamp, operation, data, collection)); } }
@Override public void run() { while (context.getStatus() == Status.RUNNING) { try { if (!assignCollections()) { break; // failed to assign oplogCollection or // slurpedCollection } Timestamp<?> startTimestamp = null; if (!definition.isSkipInitialImport()) { if (!riverHasIndexedFromOplog() && definition.getInitialTimestamp() == null) { if (!isIndexEmpty()) { MongoDBRiverHelper.setRiverStatus( client, definition.getRiverName(), Status.INITIAL_IMPORT_FAILED); break; } if (definition.isImportAllCollections()) { for (String name : slurpedDb.getCollectionNames()) { DBCollection collection = slurpedDb.getCollection(name); startTimestamp = doInitialImport(collection); } } else { DBCollection collection = slurpedDb.getCollection(definition.getMongoCollection()); startTimestamp = doInitialImport(collection); } } } else { logger.info("Skip initial import from collection {}", definition.getMongoCollection()); } // Slurp from oplog DBCursor cursor = null; try { cursor = oplogCursor(startTimestamp); if (cursor == null) { cursor = processFullOplog(); } while (cursor.hasNext()) { DBObject item = cursor.next(); startTimestamp = processOplogEntry(item, startTimestamp); } logger.debug("Before waiting for 500 ms"); Thread.sleep(500); } catch (MongoException.CursorNotFound e) { logger.info( "Cursor {} has been closed. About to open a new cusor.", cursor.getCursorId()); logger.debug("Total document inserted [{}]", totalDocuments.get()); } catch (SlurperException sEx) { logger.warn("Exception in slurper", sEx); break; } catch (Exception ex) { logger.warn("Exception while looping in cursor", ex); Thread.currentThread().interrupt(); break; } finally { if (cursor != null) { logger.trace("Closing oplog cursor"); cursor.close(); } } } catch (MongoInterruptedException mIEx) { logger.warn("Mongo driver has been interrupted", mIEx); if (mongo != null) { mongo.close(); mongo = null; } Thread.currentThread().interrupt(); break; } catch (MongoException e) { logger.error("Mongo gave an exception", e); try { Thread.sleep(5000); } catch (InterruptedException iEx) { } } catch (NoSuchElementException e) { logger.warn("A mongoDB cursor bug ?", e); } catch (InterruptedException e) { logger.info("river-mongodb slurper interrupted"); Thread.currentThread().interrupt(); break; } } }