From 1d06bb86c944acd1d30d3842926cd3d4c099315e Mon Sep 17 00:00:00 2001 From: Jon Van Oast Date: Wed, 18 Dec 2024 14:15:45 -0700 Subject: [PATCH] add a couple try/catch to opensearchIndexPermissions() --- src/main/java/org/ecocean/Encounter.java | 107 ++++++++++++----------- 1 file changed, 58 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/ecocean/Encounter.java b/src/main/java/org/ecocean/Encounter.java index 28715f9e2c..a9c73acb2b 100644 --- a/src/main/java/org/ecocean/Encounter.java +++ b/src/main/java/org/ecocean/Encounter.java @@ -3935,19 +3935,23 @@ public static void opensearchIndexPermissions() { myShepherd.beginDBTransaction(); int nonAdminCt = 0; // it seems as though user.uuid is *required* so we can trust that - for (User user : myShepherd.getUsersWithUsername()) { - usernameToId.put(user.getUsername(), user.getId()); - if (user.isAdmin(myShepherd)) continue; - nonAdminCt++; - List collabsFor = Collaboration.collaborationsForUser(myShepherd, - user.getUsername()); - if (Util.collectionIsEmptyOrNull(collabsFor)) continue; - for (Collaboration col : collabsFor) { - if (!col.isApproved() && !col.isEditApproved()) continue; - if (!collab.containsKey(user.getId())) - collab.put(user.getId(), new HashSet()); - collab.get(user.getId()).add(col.getOtherUsername(user.getUsername())); + try { + for (User user : myShepherd.getUsersWithUsername()) { + usernameToId.put(user.getUsername(), user.getId()); + if (user.isAdmin(myShepherd)) continue; + nonAdminCt++; + List collabsFor = Collaboration.collaborationsForUser(myShepherd, + user.getUsername()); + if (Util.collectionIsEmptyOrNull(collabsFor)) continue; + for (Collaboration col : collabsFor) { + if (!col.isApproved() && !col.isEditApproved()) continue; + if (!collab.containsKey(user.getId())) + collab.put(user.getId(), new HashSet()); + collab.get(user.getId()).add(col.getOtherUsername(user.getUsername())); + } } + } catch (Exception ex) { + ex.printStackTrace(); } Util.mark("perm: user build done", startT); System.out.println("opensearchIndexPermissions(): " + usernameToId.size() + @@ -3958,48 +3962,53 @@ public static void opensearchIndexPermissions() { // we do not need full Encounter objects here to update index docs, so lets do this via sql/fields - much faster String sql = "SELECT \"CATALOGNUMBER\", \"SUBMITTERID\" FROM \"ENCOUNTER\" WHERE \"SUBMITTERID\" IS NOT NULL AND \"SUBMITTERID\" != '' AND \"SUBMITTERID\" != 'N/A' AND \"SUBMITTERID\" != 'public'"; - Query q = myShepherd.getPM().newQuery("javax.jdo.query.SQL", sql); - List results = (List)q.execute(); - Iterator it = results.iterator(); - Util.mark("perm: start encs, size=" + results.size(), startT); - while (it.hasNext()) { - Object[] row = (Object[])it.next(); - String id = (String)row[0]; - String submitterId = (String)row[1]; - org.json.JSONArray viewUsers = new org.json.JSONArray(); - String uid = usernameToId.get(submitterId); - if (uid == null) { - // see issue 939 for example :( - System.out.println("opensearchIndexPermissions(): WARNING invalid username " + - submitterId + " on enc " + id); - continue; - } - encCount++; - if (encCount % 1000 == 0) Util.mark("enc[" + encCount + "]", startT); - // viewUsers.put(uid); // we no longer do this as we use submitterUserId from regular indexing in query filter - if (collab.containsKey(uid)) { - for (String colUsername : collab.get(uid)) { - String colId = usernameToId.get(colUsername); - if (colId == null) { - System.out.println( - "opensearchIndexPermissions(): WARNING invalid username " + - colUsername + " in collaboration with userId=" + uid); - continue; + try { + Query q = myShepherd.getPM().newQuery("javax.jdo.query.SQL", sql); + List results = (List)q.execute(); + Iterator it = results.iterator(); + Util.mark("perm: start encs, size=" + results.size(), startT); + while (it.hasNext()) { + Object[] row = (Object[])it.next(); + String id = (String)row[0]; + String submitterId = (String)row[1]; + org.json.JSONArray viewUsers = new org.json.JSONArray(); + String uid = usernameToId.get(submitterId); + if (uid == null) { + // see issue 939 for example :( + System.out.println("opensearchIndexPermissions(): WARNING invalid username " + + submitterId + " on enc " + id); + continue; + } + encCount++; + if (encCount % 1000 == 0) Util.mark("enc[" + encCount + "]", startT); + // viewUsers.put(uid); // we no longer do this as we use submitterUserId from regular indexing in query filter + if (collab.containsKey(uid)) { + for (String colUsername : collab.get(uid)) { + String colId = usernameToId.get(colUsername); + if (colId == null) { + System.out.println( + "opensearchIndexPermissions(): WARNING invalid username " + + colUsername + " in collaboration with userId=" + uid); + continue; + } + viewUsers.put(colId); } - viewUsers.put(colId); } - } - if (viewUsers.length() > 0) { - updateData.put("viewUsers", viewUsers); - try { - os.indexUpdate("encounter", id, updateData); - } catch (Exception ex) { - // keeping this quiet cuz it can get noise while index builds - // System.out.println("opensearchIndexPermissions(): WARNING failed to update viewUsers on enc " + enc.getId() + "; likely has not been indexed yet: " + ex); + if (viewUsers.length() > 0) { + updateData.put("viewUsers", viewUsers); + try { + os.indexUpdate("encounter", id, updateData); + } catch (Exception ex) { + // keeping this quiet cuz it can get noise while index builds + // System.out.println("opensearchIndexPermissions(): WARNING failed to update viewUsers on enc " + enc.getId() + "; likely has not been indexed yet: " + ex); + } } } + q.closeAll(); + } catch (Exception ex) { + System.out.println("opensearchIndexPermissions(): failed during encounter loop: " + ex); + ex.printStackTrace(); } - q.closeAll(); Util.mark("perm: done encs", startT); myShepherd.rollbackAndClose(); System.out.println("opensearchIndexPermissions(): ...end [" + encCount + " encs; " +