Search in sources :

Example 1 with NoteInfo

use of org.apache.zeppelin.notebook.NoteInfo in project zeppelin by apache.

the class NotebookRepoSync method sync.

/**
   * Copies new/updated notes from source to destination storage
   *
   * @throws IOException
   */
void sync(int sourceRepoIndex, int destRepoIndex, AuthenticationInfo subject) throws IOException {
    LOG.info("Sync started");
    NotebookAuthorization auth = NotebookAuthorization.getInstance();
    NotebookRepo srcRepo = getRepo(sourceRepoIndex);
    NotebookRepo dstRepo = getRepo(destRepoIndex);
    List<NoteInfo> allSrcNotes = srcRepo.list(subject);
    List<NoteInfo> srcNotes = auth.filterByUser(allSrcNotes, subject);
    List<NoteInfo> dstNotes = dstRepo.list(subject);
    Map<String, List<String>> noteIds = notesCheckDiff(srcNotes, srcRepo, dstNotes, dstRepo, subject);
    List<String> pushNoteIds = noteIds.get(pushKey);
    List<String> pullNoteIds = noteIds.get(pullKey);
    List<String> delDstNoteIds = noteIds.get(delDstKey);
    if (!pushNoteIds.isEmpty()) {
        LOG.info("Notes with the following IDs will be pushed");
        for (String id : pushNoteIds) {
            LOG.info("ID : " + id);
        }
        pushNotes(subject, pushNoteIds, srcRepo, dstRepo, false);
    } else {
        LOG.info("Nothing to push");
    }
    if (!pullNoteIds.isEmpty()) {
        LOG.info("Notes with the following IDs will be pulled");
        for (String id : pullNoteIds) {
            LOG.info("ID : " + id);
        }
        pushNotes(subject, pullNoteIds, dstRepo, srcRepo, true);
    } else {
        LOG.info("Nothing to pull");
    }
    if (!delDstNoteIds.isEmpty()) {
        LOG.info("Notes with the following IDs will be deleted from dest");
        for (String id : delDstNoteIds) {
            LOG.info("ID : " + id);
        }
        deleteNotes(subject, delDstNoteIds, dstRepo);
    } else {
        LOG.info("Nothing to delete from dest");
    }
    LOG.info("Sync ended");
}
Also used : NotebookAuthorization(org.apache.zeppelin.notebook.NotebookAuthorization) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with NoteInfo

use of org.apache.zeppelin.notebook.NoteInfo in project zeppelin by apache.

the class S3NotebookRepo method list.

@Override
public List<NoteInfo> list(AuthenticationInfo subject) throws IOException {
    List<NoteInfo> infos = new LinkedList<>();
    NoteInfo info;
    try {
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withPrefix(user + "/" + "notebook");
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                if (objectSummary.getKey().endsWith("note.json")) {
                    info = getNoteInfo(objectSummary.getKey());
                    if (info != null) {
                        infos.add(info);
                    }
                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonClientException ace) {
        throw new IOException("Unable to list objects in S3: " + ace, ace);
    }
    return infos;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) NoteInfo(org.apache.zeppelin.notebook.NoteInfo) AmazonClientException(com.amazonaws.AmazonClientException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 3 with NoteInfo

use of org.apache.zeppelin.notebook.NoteInfo in project zeppelin by apache.

the class MongoNotebookRepo method list.

@Override
public List<NoteInfo> list(AuthenticationInfo subject) throws IOException {
    syncId();
    List<NoteInfo> infos = new LinkedList<>();
    MongoCursor<Document> cursor = coll.find().iterator();
    while (cursor.hasNext()) {
        Document doc = cursor.next();
        Note note = documentToNote(doc);
        NoteInfo info = new NoteInfo(note);
        infos.add(info);
    }
    cursor.close();
    return infos;
}
Also used : NoteInfo(org.apache.zeppelin.notebook.NoteInfo) Note(org.apache.zeppelin.notebook.Note) Document(org.bson.Document)

Example 4 with NoteInfo

use of org.apache.zeppelin.notebook.NoteInfo in project zeppelin by apache.

the class MongoNotebookRepo method insertFileSystemNotes.

/**
   * If environment variable ZEPPELIN_NOTEBOOK_MONGO_AUTOIMPORT is true,
   * this method will insert local notes into MongoDB on startup.
   * If a note already exists in MongoDB, skip it.
   */
private void insertFileSystemNotes() throws IOException {
    // docs to be imported
    LinkedList<Document> docs = new LinkedList<>();
    NotebookRepo vfsRepo = new VFSNotebookRepo(this.conf);
    List<NoteInfo> infos = vfsRepo.list(null);
    // collect notes to be imported
    for (NoteInfo info : infos) {
        Note note = vfsRepo.get(info.getId(), null);
        Document doc = noteToDocument(note);
        docs.add(doc);
    }
    /*
     * 'ordered(false)' option allows to proceed bulk inserting even though
     * there are duplicated documents. The duplicated documents will be skipped
     * and print a WARN log.
     */
    try {
        coll.insertMany(docs, new InsertManyOptions().ordered(false));
    } catch (MongoBulkWriteException e) {
        //print duplicated document warning log
        printDuplicatedException(e);
    }
    // it does nothing for now but maybe in the future...
    vfsRepo.close();
}
Also used : NoteInfo(org.apache.zeppelin.notebook.NoteInfo) Note(org.apache.zeppelin.notebook.Note) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) Document(org.bson.Document) InsertManyOptions(com.mongodb.client.model.InsertManyOptions)

Example 5 with NoteInfo

use of org.apache.zeppelin.notebook.NoteInfo in project zeppelin by apache.

the class NotebookRepoSync method notesCheckDiff.

private Map<String, List<String>> notesCheckDiff(List<NoteInfo> sourceNotes, NotebookRepo sourceRepo, List<NoteInfo> destNotes, NotebookRepo destRepo, AuthenticationInfo subject) {
    List<String> pushIDs = new ArrayList<>();
    List<String> pullIDs = new ArrayList<>();
    List<String> delDstIDs = new ArrayList<>();
    NoteInfo dnote;
    Date sdate, ddate;
    for (NoteInfo snote : sourceNotes) {
        dnote = containsID(destNotes, snote.getId());
        if (dnote != null) {
            try {
                /* note exists in source and destination storage systems */
                sdate = lastModificationDate(sourceRepo.get(snote.getId(), subject));
                ddate = lastModificationDate(destRepo.get(dnote.getId(), subject));
            } catch (IOException e) {
                LOG.error("Cannot access previously listed note {} from storage ", dnote.getId(), e);
                continue;
            }
            if (sdate.compareTo(ddate) != 0) {
                if (sdate.after(ddate) || oneWaySync) {
                    /* if source contains more up to date note - push
             * if oneWaySync is enabled, always push no matter who's newer */
                    pushIDs.add(snote.getId());
                    LOG.info("Modified note is added to push list : " + sdate);
                } else {
                    /* destination contains more up to date note - pull */
                    LOG.info("Modified note is added to pull list : " + ddate);
                    pullIDs.add(snote.getId());
                }
            }
        } else {
            /* note exists in source storage, and absent in destination
         * view source as up to date - push
         * (another scenario : note was deleted from destination - not considered)*/
            pushIDs.add(snote.getId());
        }
    }
    for (NoteInfo note : destNotes) {
        dnote = containsID(sourceNotes, note.getId());
        if (dnote == null) {
            /* note exists in destination storage, and absent in source */
            if (oneWaySync) {
                /* if oneWaySync is enabled, delete the note from destination */
                LOG.info("Extraneous note is added to delete dest list : " + note.getId());
                delDstIDs.add(note.getId());
            } else {
                /* if oneWaySync is disabled, pull the note from destination */
                LOG.info("Missing note is added to pull list : " + note.getId());
                pullIDs.add(note.getId());
            }
        }
    }
    Map<String, List<String>> map = new HashMap<>();
    map.put(pushKey, pushIDs);
    map.put(pullKey, pullIDs);
    map.put(delDstKey, delDstIDs);
    return map;
}
Also used : NoteInfo(org.apache.zeppelin.notebook.NoteInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

NoteInfo (org.apache.zeppelin.notebook.NoteInfo)7 IOException (java.io.IOException)4 LinkedList (java.util.LinkedList)3 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Note (org.apache.zeppelin.notebook.Note)2 Document (org.bson.Document)2 AmazonClientException (com.amazonaws.AmazonClientException)1 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)1 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)1 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)1 StorageException (com.microsoft.azure.storage.StorageException)1 CloudFileDirectory (com.microsoft.azure.storage.file.CloudFileDirectory)1 ListFileItem (com.microsoft.azure.storage.file.ListFileItem)1 MongoBulkWriteException (com.mongodb.MongoBulkWriteException)1 InsertManyOptions (com.mongodb.client.model.InsertManyOptions)1 InvalidKeyException (java.security.InvalidKeyException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1