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

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

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