|
16 | 16 | */ |
17 | 17 | package org.apache.spark.status.api.v1 |
18 | 18 |
|
19 | | -import java.io.{FileInputStream, OutputStream, File, InputStream} |
| 19 | +import java.io.{BufferedInputStream, FileInputStream, OutputStream, File, InputStream} |
| 20 | +import javax.ws.rs.ext.Provider |
20 | 21 | import javax.ws.rs.{GET, Produces} |
21 | | -import javax.ws.rs.core.{MultivaluedMap, MediaType} |
| 22 | +import javax.ws.rs.core.{StreamingOutput, MultivaluedMap, MediaType} |
22 | 23 |
|
23 | 24 | import org.apache.spark.deploy.history.HistoryServer |
24 | 25 | import org.apache.spark.util.Utils |
25 | 26 |
|
26 | | -@Produces(Array(MediaType.APPLICATION_OCTET_STREAM)) |
| 27 | + |
27 | 28 | private[v1] class EventLogDownloadResource(val uIRoot: UIRoot, val appId: String) { |
28 | 29 |
|
| 30 | + private def getErrorOutput(err: String): StreamingOutput = { |
| 31 | + new StreamingOutput { |
| 32 | + override def write(outputStream: OutputStream): Unit = { |
| 33 | + outputStream.write( |
| 34 | + s"File download not available for application : $appId due to $err".getBytes("utf-8")) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
29 | 39 | @GET |
30 | | - def getEventLogs(headers: MultivaluedMap[String, AnyRef], outputStream: OutputStream): Unit = { |
| 40 | + @Produces(Array(MediaType.APPLICATION_OCTET_STREAM)) |
| 41 | + def getEventLogs(): StreamingOutput = { |
31 | 42 | uIRoot match { |
32 | 43 | case hs: HistoryServer => |
33 | 44 | val dir = Utils.createTempDir() |
34 | 45 | Utils.chmod700(dir) |
35 | 46 | hs.copyEventLogsToDirectory(appId, dir) |
36 | | - dir.listFiles().headOption.foreach { zipFile => |
37 | | - headers.add("Content-Length", zipFile.length().toString) |
38 | | - headers.add("Content-Type", MediaType.APPLICATION_OCTET_STREAM) |
39 | | - headers.add("Content-Disposition", s"attachment; filename=${zipFile.getName}") |
40 | | - var inputStream: InputStream = null |
41 | | - try { |
42 | | - inputStream = new FileInputStream(zipFile) |
43 | | - val buffer = new Array[Byte](1024 * 1024) |
44 | | - var remaining = true |
45 | | - while (remaining) { |
46 | | - val read = inputStream.read(buffer) |
47 | | - if (read != -1) { |
48 | | - outputStream.write(buffer, 0, read) |
49 | | - } else { |
50 | | - remaining = false |
| 47 | + dir.listFiles().headOption.foreach { file => |
| 48 | + return new StreamingOutput { |
| 49 | + override def write(output: OutputStream): Unit = { |
| 50 | + val inStream = new BufferedInputStream(new FileInputStream(file)) |
| 51 | + val buffer = new Array[Byte](1024 * 1024) |
| 52 | + var dataRemains = true |
| 53 | + while (dataRemains) { |
| 54 | + val read = inStream.read(buffer) |
| 55 | + if (read > 0) { |
| 56 | + output.write(buffer, 0, read) |
| 57 | + } else { |
| 58 | + dataRemains = false |
| 59 | + } |
51 | 60 | } |
| 61 | + output.flush() |
52 | 62 | } |
53 | | - outputStream.flush() |
54 | | - } finally { |
55 | | - inputStream.close() |
56 | | - Utils.deleteRecursively(dir) |
57 | 63 | } |
58 | 64 | } |
59 | | - case _ => outputStream.write( |
60 | | - s"File download not available for application : $appId".getBytes("utf-8")) |
| 65 | + getErrorOutput("No files in dir.") |
| 66 | + case _ => getErrorOutput("hs not history server") |
61 | 67 | } |
62 | 68 | } |
63 | 69 | } |
|
0 commit comments