|
| 1 | +// Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See LICENSE in project root for information. |
| 3 | + |
| 4 | +package com.microsoft.azure.synapse.ml.services.openai |
| 5 | + |
| 6 | +import org.apache.hadoop.fs.{FSDataInputStream, FSInputStream, Path, RawLocalFileSystem} |
| 7 | +import org.apache.http.conn.ConnectTimeoutException |
| 8 | +import org.apache.spark.TaskContext |
| 9 | + |
| 10 | +import java.io.{ByteArrayInputStream, InterruptedIOException, IOException} |
| 11 | +import java.net.{SocketTimeoutException, URI} |
| 12 | +import java.nio.channels.ClosedByInterruptException |
| 13 | +import java.util.concurrent.{CancellationException, ConcurrentHashMap, ConcurrentLinkedQueue, CountDownLatch, TimeUnit} |
| 14 | +import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference} |
| 15 | + |
| 16 | +private[openai] object AttachmentReadFailures { |
| 17 | + val Location: URI = URI.create("attachmentfailure:///") |
| 18 | + |
| 19 | + class Gate(val content: Option[Array[Byte]] = None) { |
| 20 | + val entered = new CountDownLatch(1) |
| 21 | + val release = new CountDownLatch(1) |
| 22 | + val completed = new CountDownLatch(1) |
| 23 | + val context = new AtomicReference[TaskContext]() |
| 24 | + val taskFailure = new AtomicReference[Throwable]() |
| 25 | + val completionInterrupted = new AtomicBoolean() |
| 26 | + } |
| 27 | + |
| 28 | + val Active = new ConcurrentHashMap[String, Gate]() |
| 29 | + |
| 30 | + val TimeoutNames = Seq("socket-timeout.png", "connect-timeout.png", |
| 31 | + "close-socket-timeout.png", "close-connect-timeout.png", "wrapped-socket-timeout.png", |
| 32 | + "wrapped-connect-timeout.png", "relocated-connect-timeout.png", "relocated-pool-timeout.png", |
| 33 | + "nested-connect-timeout.png", "nested-interrupted-timeout.png", "httpclient5-timeout.png") |
| 34 | + |
| 35 | + private object RelocatedHttpClient { |
| 36 | + class ConnectTimeoutException extends InterruptedIOException("Synthetic private connect timeout") |
| 37 | + class ConnectionPoolTimeoutException extends ConnectTimeoutException |
| 38 | + } |
| 39 | + |
| 40 | + private def timeoutCausedBy(cause: Throwable): SocketTimeoutException = { |
| 41 | + val error = new SocketTimeoutException("Synthetic private storage timeout") |
| 42 | + error.initCause(new IOException("Synthetic private storage wrapper", cause)) |
| 43 | + error |
| 44 | + } |
| 45 | + |
| 46 | + def cyclicExceptionGraph(includeCauses: Boolean): (IOException, IOException) = { |
| 47 | + val root = new IOException("Synthetic private path") |
| 48 | + val last = (1 to 2048).foldLeft(root) { (previous, index) => |
| 49 | + val next = new IOException("Synthetic private cleanup failure") |
| 50 | + if (includeCauses && index % 2 == 0) previous.initCause(next) else previous.addSuppressed(next) |
| 51 | + next |
| 52 | + } |
| 53 | + last.addSuppressed(root) |
| 54 | + root -> last |
| 55 | + } |
| 56 | + |
| 57 | + private def throwWithClearedInterrupt(error: IOException): Nothing = { |
| 58 | + Thread.currentThread().interrupt() |
| 59 | + Thread.interrupted() |
| 60 | + throw error |
| 61 | + } |
| 62 | + |
| 63 | + private def cleanupFailure(id: String): Option[IOException] = id match { |
| 64 | + case "close-interrupted-io.png" => Some(new InterruptedIOException("Synthetic private close failure")) |
| 65 | + case "close-socket-timeout.png" => Some(new SocketTimeoutException("Synthetic private close failure")) |
| 66 | + case "close-connect-timeout.png" => Some(new ConnectTimeoutException("Synthetic private close failure")) |
| 67 | + case _ => None |
| 68 | + } |
| 69 | + |
| 70 | + private def storageTimeout(id: String): Option[IOException] = id match { |
| 71 | + case "relocated-connect-timeout.png" => Some(new RelocatedHttpClient.ConnectTimeoutException) |
| 72 | + case "relocated-pool-timeout.png" => Some(new RelocatedHttpClient.ConnectionPoolTimeoutException {}) |
| 73 | + case "nested-connect-timeout.png" => |
| 74 | + Some(timeoutCausedBy(new RelocatedHttpClient.ConnectTimeoutException)) |
| 75 | + case "nested-interrupted-timeout.png" => |
| 76 | + Some(timeoutCausedBy(new InterruptedIOException("Synthetic private timeout cause"))) |
| 77 | + case "httpclient5-timeout.png" => |
| 78 | + Some(new org.apache.hc.client5.http.ConnectTimeoutException("Synthetic private connect timeout")) |
| 79 | + case "timeout-close-interrupted.png" => |
| 80 | + val error = timeoutCausedBy(new RelocatedHttpClient.ConnectTimeoutException) |
| 81 | + error.addSuppressed(new InterruptedIOException("Synthetic private cleanup interruption")) |
| 82 | + Some(error) |
| 83 | + case _ => None |
| 84 | + } |
| 85 | + |
| 86 | + private def wrappedFailure(id: String): Option[IOException] = { |
| 87 | + val cause = id match { |
| 88 | + case "wrapped-interrupted.png" => Some(new InterruptedException("Synthetic private path")) |
| 89 | + case "wrapped-interrupted-io.png" => Some(new InterruptedIOException("Synthetic private path")) |
| 90 | + case "wrapped-closed-interrupt.png" => Some(new ClosedByInterruptException()) |
| 91 | + case "wrapped-socket-timeout.png" => Some(new SocketTimeoutException("Synthetic private path")) |
| 92 | + case "wrapped-connect-timeout.png" => Some(new ConnectTimeoutException("Synthetic private path")) |
| 93 | + case _ => None |
| 94 | + } |
| 95 | + cause.orElse(storageTimeout(id)).map(new IOException("Synthetic private wrapped failure", _)) |
| 96 | + } |
| 97 | + |
| 98 | + def open(id: String): FSDataInputStream = { |
| 99 | + val active = Option(Active.get(id)) |
| 100 | + active.foreach { gate => |
| 101 | + val context = TaskContext.get() |
| 102 | + gate.context.set(context) |
| 103 | + context.addTaskFailureListener((_: TaskContext, error: Throwable) => gate.taskFailure.set(error)) |
| 104 | + context.addTaskCompletionListener[Unit] { _ => |
| 105 | + gate.completionInterrupted.set(Thread.currentThread().isInterrupted) |
| 106 | + gate.completed.countDown() |
| 107 | + } |
| 108 | + gate.entered.countDown() |
| 109 | + require(gate.release.await(30, TimeUnit.SECONDS), "Attachment cancellation test gate timed out") |
| 110 | + } |
| 111 | + wrappedFailure(id).foreach(throwWithClearedInterrupt) |
| 112 | + if (id == "interrupted.png") throw new ClosedByInterruptException() |
| 113 | + if (id == "plain-interrupted.png") throw new InterruptedException("Synthetic private path") |
| 114 | + if (id == "interrupted-io.png") { |
| 115 | + throwWithClearedInterrupt(new InterruptedIOException("Synthetic private path")) |
| 116 | + } |
| 117 | + if (id == "socket-timeout.png") throw new SocketTimeoutException("Synthetic private path") |
| 118 | + if (id == "connect-timeout.png") throw new ConnectTimeoutException("Synthetic private path") |
| 119 | + if (id == "flagged.png") Thread.currentThread().interrupt() |
| 120 | + if (id == "cancelled.png") throw new CancellationException("Synthetic private path") |
| 121 | + val cleanupError = cleanupFailure(id) |
| 122 | + val streamContent = active.flatMap(_.content).orElse(cleanupError.map(_ => Array.emptyByteArray)) |
| 123 | + if (id == "read-close.png" || streamContent.isDefined) { |
| 124 | + val content = streamContent.getOrElse(Array.emptyByteArray) |
| 125 | + val bytes = new ByteArrayInputStream(content) |
| 126 | + new FSDataInputStream(new FSInputStream { |
| 127 | + override def getPos: Long = content.length - bytes.available() |
| 128 | + override def seek(position: Long): Unit = throw new UnsupportedOperationException("Test stream is not seekable") |
| 129 | + override def seekToNewSource(position: Long): Boolean = false |
| 130 | + override def read(): Int = { |
| 131 | + if (id == "read-close.png") throw new InterruptedException("Synthetic private path") |
| 132 | + if (cleanupError.isDefined) throw new IOException("Synthetic private read failure") |
| 133 | + bytes.read() |
| 134 | + } |
| 135 | + override def close(): Unit = { |
| 136 | + bytes.close() |
| 137 | + if (id == "read-close.png") throw new IOException("Synthetic private close failure") |
| 138 | + cleanupError.foreach(throwWithClearedInterrupt) |
| 139 | + } |
| 140 | + }) |
| 141 | + } else { |
| 142 | + throw new IOException("Synthetic attachment read failure") |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +class AttachmentReadFailureFileSystem extends RawLocalFileSystem { |
| 148 | + val opened = new ConcurrentLinkedQueue[String]() |
| 149 | + |
| 150 | + override def getUri: URI = AttachmentReadFailures.Location |
| 151 | + |
| 152 | + override def open(path: Path, bufferSize: Int): FSDataInputStream = { |
| 153 | + opened.add(path.getName) |
| 154 | + AttachmentReadFailures.open(path.getName) |
| 155 | + } |
| 156 | +} |
0 commit comments