You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zeppelin.apache.org by "Reamer (via GitHub)" <gi...@apache.org> on 2023/06/07 12:33:42 UTC

[GitHub] [zeppelin] Reamer opened a new pull request, #4612: [ZEPPELIN-5926] Remove map entries if Collection is empty

Reamer opened a new pull request, #4612:
URL: https://github.com/apache/zeppelin/pull/4612

   ### What is this PR for?
   This PR deletes a map element if the collection behind the map key is empty, so that the map size can decrease.
   
   
   ### What type of PR is it?
   Bug Fix
   
   
   ### What is the Jira issue?
   * https://issues.apache.org/jira/browse/ZEPPELIN-5926
   
   ### How should this be tested?
   * CI
   
   ### Questions:
   * Does the license files need to update? No
   * Is there breaking changes for older versions? No
   * Does this needs documentation? No
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [zeppelin] jongyoul commented on a diff in pull request #4612: [ZEPPELIN-5926] Remove map entries if Collection is empty

Posted by "jongyoul (via GitHub)" <gi...@apache.org>.
jongyoul commented on code in PR #4612:
URL: https://github.com/apache/zeppelin/pull/4612#discussion_r1221824271


##########
zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java:
##########
@@ -124,11 +122,33 @@ public void removeNoteConnection(String noteId) {
   public void removeNoteConnection(String noteId, NotebookSocket socket) {
     LOGGER.debug("Remove connection {} from note: {}", socket, noteId);
     synchronized (noteSocketMap) {
-      List<NotebookSocket> socketList = noteSocketMap.getOrDefault(noteId, Collections.emptyList());
-      if (!socketList.isEmpty()) {
-        socketList.remove(socket);
+      Set<NotebookSocket> sockets = noteSocketMap.getOrDefault(noteId, Collections.emptySet());
+      removeNoteConnection(noteId, sockets, socket);
+      // Remove empty socket collection from map
+      if (sockets.isEmpty()) {
+        noteSocketMap.remove(noteId);
+      }
+    }
+  }
+
+  private void removeNoteConnection(String noteId, Set<NotebookSocket> sockets,
+    NotebookSocket socket) {
+    sockets.remove(socket);
+    checkCollaborativeStatus(noteId, sockets);
+  }
+
+  public void removeConnectionFromAllNote(NotebookSocket socket) {
+    LOGGER.debug("Remove connection {} from all notes", socket);
+    synchronized (noteSocketMap) {
+      Iterator<Entry<String, Set<NotebookSocket>>> iterator = noteSocketMap.entrySet().iterator();
+      while (iterator.hasNext()) {
+        Entry<String, Set<NotebookSocket>> noteSocketMapEntry = iterator.next();
+        removeNoteConnection(noteSocketMapEntry.getKey(), noteSocketMapEntry.getValue(), socket);
+        // Remove empty socket collection from map
+        if (noteSocketMapEntry.getValue().isEmpty()) {
+          iterator.remove();

Review Comment:
   Oh... I didn't know that. 😀 Thank you for clarifying it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [zeppelin] zhugezifang commented on a diff in pull request #4612: [ZEPPELIN-5926] Remove map entries if Collection is empty

Posted by "zhugezifang (via GitHub)" <gi...@apache.org>.
zhugezifang commented on code in PR #4612:
URL: https://github.com/apache/zeppelin/pull/4612#discussion_r1222353767


##########
zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java:
##########
@@ -124,11 +122,33 @@ public void removeNoteConnection(String noteId) {
   public void removeNoteConnection(String noteId, NotebookSocket socket) {
     LOGGER.debug("Remove connection {} from note: {}", socket, noteId);
     synchronized (noteSocketMap) {
-      List<NotebookSocket> socketList = noteSocketMap.getOrDefault(noteId, Collections.emptyList());
-      if (!socketList.isEmpty()) {
-        socketList.remove(socket);
+      Set<NotebookSocket> sockets = noteSocketMap.getOrDefault(noteId, Collections.emptySet());
+      removeNoteConnection(noteId, sockets, socket);
+      // Remove empty socket collection from map
+      if (sockets.isEmpty()) {
+        noteSocketMap.remove(noteId);
+      }
+    }
+  }
+
+  private void removeNoteConnection(String noteId, Set<NotebookSocket> sockets,
+    NotebookSocket socket) {
+    sockets.remove(socket);
+    checkCollaborativeStatus(noteId, sockets);
+  }
+
+  public void removeConnectionFromAllNote(NotebookSocket socket) {
+    LOGGER.debug("Remove connection {} from all notes", socket);
+    synchronized (noteSocketMap) {
+      Iterator<Entry<String, Set<NotebookSocket>>> iterator = noteSocketMap.entrySet().iterator();
+      while (iterator.hasNext()) {
+        Entry<String, Set<NotebookSocket>> noteSocketMapEntry = iterator.next();
+        removeNoteConnection(noteSocketMapEntry.getKey(), noteSocketMapEntry.getValue(), socket);
+        // Remove empty socket collection from map
+        if (noteSocketMapEntry.getValue().isEmpty()) {
+          iterator.remove();

Review Comment:
   hi @Reamer  @jongyoul  could you help me to review this pr, thanks a lot
    https://github.com/apache/zeppelin/pull/4611



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [zeppelin] Reamer commented on a diff in pull request #4612: [ZEPPELIN-5926] Remove map entries if Collection is empty

Posted by "Reamer (via GitHub)" <gi...@apache.org>.
Reamer commented on code in PR #4612:
URL: https://github.com/apache/zeppelin/pull/4612#discussion_r1221819108


##########
zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java:
##########
@@ -124,11 +122,33 @@ public void removeNoteConnection(String noteId) {
   public void removeNoteConnection(String noteId, NotebookSocket socket) {
     LOGGER.debug("Remove connection {} from note: {}", socket, noteId);
     synchronized (noteSocketMap) {
-      List<NotebookSocket> socketList = noteSocketMap.getOrDefault(noteId, Collections.emptyList());
-      if (!socketList.isEmpty()) {
-        socketList.remove(socket);
+      Set<NotebookSocket> sockets = noteSocketMap.getOrDefault(noteId, Collections.emptySet());
+      removeNoteConnection(noteId, sockets, socket);
+      // Remove empty socket collection from map
+      if (sockets.isEmpty()) {
+        noteSocketMap.remove(noteId);
+      }
+    }
+  }
+
+  private void removeNoteConnection(String noteId, Set<NotebookSocket> sockets,
+    NotebookSocket socket) {
+    sockets.remove(socket);
+    checkCollaborativeStatus(noteId, sockets);
+  }
+
+  public void removeConnectionFromAllNote(NotebookSocket socket) {
+    LOGGER.debug("Remove connection {} from all notes", socket);
+    synchronized (noteSocketMap) {
+      Iterator<Entry<String, Set<NotebookSocket>>> iterator = noteSocketMap.entrySet().iterator();
+      while (iterator.hasNext()) {
+        Entry<String, Set<NotebookSocket>> noteSocketMapEntry = iterator.next();
+        removeNoteConnection(noteSocketMapEntry.getKey(), noteSocketMapEntry.getValue(), socket);
+        // Remove empty socket collection from map
+        if (noteSocketMapEntry.getValue().isEmpty()) {
+          iterator.remove();

Review Comment:
   You get the `ConcurrentModificationException` if you modify the Map inside a for-each loop. It is safe to delete elements in the iterator.
   Checkout: https://www.baeldung.com/java-hashmap-remove-entry#removing-an-entry-while-iterating



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [zeppelin] jongyoul commented on a diff in pull request #4612: [ZEPPELIN-5926] Remove map entries if Collection is empty

Posted by "jongyoul (via GitHub)" <gi...@apache.org>.
jongyoul commented on code in PR #4612:
URL: https://github.com/apache/zeppelin/pull/4612#discussion_r1221525956


##########
zeppelin-server/src/main/java/org/apache/zeppelin/socket/ConnectionManager.java:
##########
@@ -124,11 +122,33 @@ public void removeNoteConnection(String noteId) {
   public void removeNoteConnection(String noteId, NotebookSocket socket) {
     LOGGER.debug("Remove connection {} from note: {}", socket, noteId);
     synchronized (noteSocketMap) {
-      List<NotebookSocket> socketList = noteSocketMap.getOrDefault(noteId, Collections.emptyList());
-      if (!socketList.isEmpty()) {
-        socketList.remove(socket);
+      Set<NotebookSocket> sockets = noteSocketMap.getOrDefault(noteId, Collections.emptySet());
+      removeNoteConnection(noteId, sockets, socket);
+      // Remove empty socket collection from map
+      if (sockets.isEmpty()) {
+        noteSocketMap.remove(noteId);
+      }
+    }
+  }
+
+  private void removeNoteConnection(String noteId, Set<NotebookSocket> sockets,
+    NotebookSocket socket) {
+    sockets.remove(socket);
+    checkCollaborativeStatus(noteId, sockets);
+  }
+
+  public void removeConnectionFromAllNote(NotebookSocket socket) {
+    LOGGER.debug("Remove connection {} from all notes", socket);
+    synchronized (noteSocketMap) {
+      Iterator<Entry<String, Set<NotebookSocket>>> iterator = noteSocketMap.entrySet().iterator();
+      while (iterator.hasNext()) {
+        Entry<String, Set<NotebookSocket>> noteSocketMapEntry = iterator.next();
+        removeNoteConnection(noteSocketMapEntry.getKey(), noteSocketMapEntry.getValue(), socket);
+        // Remove empty socket collection from map
+        if (noteSocketMapEntry.getValue().isEmpty()) {
+          iterator.remove();

Review Comment:
   Doesn't it occur ConcurrentModificationException because the code is `iterator.remove()` instead of `map.remove(..)`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [zeppelin] Reamer merged pull request #4612: [ZEPPELIN-5926] Remove map entries if Collection is empty

Posted by "Reamer (via GitHub)" <gi...@apache.org>.
Reamer merged PR #4612:
URL: https://github.com/apache/zeppelin/pull/4612


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@zeppelin.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org