bruno-roustant commented on code in PR #115:
URL: https://github.com/apache/solr-sandbox/pull/115#discussion_r1908750260


##########
encryption/src/main/java/org/apache/solr/encryption/EncryptionRequestHandler.java:
##########
@@ -260,12 +330,116 @@ public void handleRequestBody(SolrQueryRequest req, 
SolrQueryResponse rsp) throw
       } else {
         rsp.add(STATUS, STATUS_FAILURE);
       }
-      log.info("Responding encryption state={} success={} for keyId={}",
-               encryptionState, success, keyId);
-      rsp.add(ENCRYPTION_STATE, encryptionState);
+      rsp.add(ENCRYPTION_STATE, state.value);
+      long timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - 
startTimeNs);
+      log.info("Responding encryption state={} success={} for keyId={} 
timeMs={}",
+          state.value, success, keyId, timeMs);
     }
   }
 
+  private void distributeRequest(SolrQueryRequest req, SolrQueryResponse rsp, 
String keyId, long startTimeNs) {
+    boolean success = false;
+    String collectionName = null;
+    State collectionState = null;
+    long timeAllowedMs = req.getParams().getLong(TIME_ALLOWED, 0);
+    long maxTimeNs = timeAllowedMs <= 0 ? Long.MAX_VALUE : startTimeNs + 
timeAllowedMs;
+    try {
+      collectionName = req.getCore().getCoreDescriptor().getCollectionName();
+      if (collectionName == null) {
+        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, 
"Parameter " + DISTRIB + " can only be used in Solr Cloud mode.");
+      }
+      log.debug("Encrypt request distributed for keyId={} collection={}", 
keyId, collectionName);
+      DocCollection docCollection = 
req.getCore().getCoreContainer().getZkController().getZkStateReader().getCollection(collectionName);
+      if (docCollection == null) {
+        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, 
"Parameter " + DISTRIB + " present but collection '" + collectionName + "' not 
found.");
+      }
+      try (SolrClientHolder solrClient = getHttpSolrClient(req)) {
+        ModifiableSolrParams params = createDistributedRequestParams(req, rsp, 
keyId);
+        for (Slice slice : docCollection.getActiveSlices()) {
+          if (isTimeout(maxTimeNs)) {
+            log.warn("Timeout distributing encryption request for keyId={} 
collection={}", keyId, collectionName);
+            if (collectionState == null || State.TIMEOUT.priority > 
collectionState.priority) {
+              collectionState = State.TIMEOUT;
+            }
+            break;
+          }
+          Replica replica = slice.getLeader();
+          if (replica == null) {
+            log.error("No leader found for shard {}", slice.getName());
+            collectionState = State.ERROR;
+            continue;
+          }
+          State state = sendEncryptionRequestWithRetry(replica, params, 
solrClient.getClient(), keyId, collectionName);
+          if (collectionState == null || state.priority > 
collectionState.priority) {
+            collectionState = state;
+          }
+        }
+        success = collectionState == null || collectionState.isSuccess();
+      }
+    } finally {
+      if (success) {
+        rsp.add(STATUS, STATUS_SUCCESS);
+      } else {
+        rsp.add(STATUS, STATUS_FAILURE);
+      }
+      if (collectionState != null) {
+        rsp.add(ENCRYPTION_STATE, collectionState.value);
+      }
+      if (log.isInfoEnabled()) {
+        long timeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - 
startTimeNs);
+        log.info("Responding encryption distributed state={} success={} for 
keyId={} collection={} timeMs={}",
+            (collectionState == null ? null : collectionState.value), success, 
keyId, collectionName, timeMs);
+      }
+    }
+  }
+
+  private SolrClientHolder getHttpSolrClient(SolrQueryRequest req) {

Review Comment:
   It is not available in Solr 9.6, the Solr version used here. I propose to 
let this code for now, and replace it when I upgrade to 9.8 (I need to upgrade 
anyway to fix an issue with commit metadata transfer).



-- 
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: issues-unsubscr...@solr.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to