zyratlo commented on code in PR #8032:
URL: https://github.com/apache/texera/pull/8032#discussion_r3918489196


##########
notebook-migration-service/src/main/scala/org/apache/texera/service/util/JupyterProbe.scala:
##########
@@ -0,0 +1,70 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.texera.service.util
+
+import java.net.{HttpURLConnection, URL}
+
+/** Liveness check for a Jupyter server. */
+object JupyterProbe {
+
+  private val timeoutMillis = 2000
+
+  /**
+    * Whether Jupyter on `internalUrl` accepts `token`. Distinct from 
isAvailable: a pod
+    * started with a superseded token is still perfectly alive, so liveness 
alone cannot
+    * tell a healthy pod from one whose token no longer matches the derived 
value. Probes
+    * an endpoint that requires authentication, so 403 means the token is 
wrong.
+    */
+  def isAuthorized(internalUrl: String, token: String): Boolean = {
+    var conn: HttpURLConnection = null
+    try {
+      conn = new URL(s"$internalUrl/api/contents")
+        .openConnection()
+        .asInstanceOf[HttpURLConnection]
+      conn.setRequestMethod("GET")
+      conn.setRequestProperty("Authorization", s"token $token")
+      conn.setConnectTimeout(timeoutMillis)
+      conn.setReadTimeout(timeoutMillis)
+      conn.getResponseCode == 200
+    } catch {
+      case _: Exception => false
+    } finally {
+      if (conn != null) conn.disconnect()
+    }

Review Comment:
   Changed in ce3194462, though not for the reason given: InterruptedException 
is checked and nothing in the HttpURLConnection path declares it, and java.net 
socket reads are not interruptible, so no interrupt can reach that catch today. 
NonFatal is still the right idiom here, it is what JupyterProvisioner and 
NotebookMigrationResource already use, and it makes a future port to 
java.net.http.HttpClient safe, since send does declare InterruptedException. 
NonFatal excludes it, so the interrupt propagates rather than being caught and 
a flag restored.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to