Skip to content

Commit

Permalink
PrintStream reference counting
Browse files Browse the repository at this point in the history
  • Loading branch information
hwellmann committed Dec 21, 2016
1 parent f727f85 commit 100b77a
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 54 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/fabric8/maven/docker/StartMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ public void log(int type, Timestamp timestamp, String txt) throws LogCallback.Do
public void error(String error) {
log.error("%s", error);
}

@Override
public void close() {
// empty
}
});
first = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.net.HttpURLConnection.HTTP_OK;
import static io.fabric8.maven.docker.log.LogCallbackFactory.createLogCallback;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -20,7 +19,6 @@
import java.util.List;
import java.util.Map;

import io.fabric8.maven.docker.access.hc.util.ClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
Expand All @@ -40,6 +38,7 @@
import io.fabric8.maven.docker.access.hc.ApacheHttpClientDelegate.HttpBodyAndStatus;
import io.fabric8.maven.docker.access.hc.http.HttpClientBuilder;
import io.fabric8.maven.docker.access.hc.unix.UnixSocketClientBuilder;
import io.fabric8.maven.docker.access.hc.util.ClientBuilder;
import io.fabric8.maven.docker.access.hc.win.NamedPipeClientBuilder;
import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.access.log.LogGetHandle;
Expand Down Expand Up @@ -146,7 +145,7 @@ public void startExecContainer(String containerId, LogOutputSpec outputSpec) thr
}

private ResponseHandler<Object> createExecResponseHandler(LogOutputSpec outputSpec) throws FileNotFoundException {
final LogCallback callback = createLogCallback(outputSpec);
final LogCallback callback = new DefaultLogCallback(outputSpec);
return new ResponseHandler<Object>() {
@Override
public Object handleResponse(HttpResponse response) throws IOException {
Expand All @@ -159,6 +158,8 @@ public Object handleResponse(HttpResponse response) throws IOException {
}
} catch (LogCallback.DoneException e) {
// Ok, we stop here ...
} finally {
callback.close();
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public interface LogCallback {
* @param error error description
*/
void error(String error);

void close();

/**
* Exception indicating that logging is done and should be finished
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public void fetchLogs() {
parseResponse(resp);
} catch (IOException exp) {
callback.error(exp.getMessage());
} finally {
callback.close();
}
}

Expand All @@ -105,6 +107,7 @@ public void run() {
} catch (IOException exp) {
callback.error("IO Error while requesting logs: " + exp);
} finally {
callback.close();
try {
synchronized (lock) {
client.close();
Expand Down Expand Up @@ -224,6 +227,7 @@ public void finish() {
}
}
}
callback.close();
}

@Override
Expand Down
51 changes: 46 additions & 5 deletions src/main/java/io/fabric8/maven/docker/log/DefaultLogCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
*/

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.util.Timestamp;
Expand All @@ -27,24 +30,61 @@
*/
public class DefaultLogCallback implements LogCallback {

private static Map<String, SharedPrintStream> printStreamMap = new HashMap<>();

private final LogOutputSpec outputSpec;
private final PrintStream ps;
private final SharedPrintStream sps;

public DefaultLogCallback(LogOutputSpec outputSpec, PrintStream ps) throws FileNotFoundException {
public DefaultLogCallback(LogOutputSpec outputSpec) throws FileNotFoundException {
this.outputSpec = outputSpec;
this.ps = ps;
this.sps = createOrReusePrintStream(outputSpec);
}

private synchronized SharedPrintStream createOrReusePrintStream(LogOutputSpec spec) throws FileNotFoundException {
String file = spec.getFile();
if (spec.isLogStdout() || file == null) {
return new SharedPrintStream(System.out);
}
SharedPrintStream sps = printStreamMap.get(file);
if (sps == null) {
PrintStream ps = new PrintStream(new FileOutputStream(file), true);
sps = new SharedPrintStream(ps);

printStreamMap.put(file, sps);
}
else {
sps.allocate();
}
return sps;
}

private PrintStream ps() {
return sps.getPrintStream();
}

@Override
public void log(int type, Timestamp timestamp, String txt) {
addLogEntry(ps, new LogEntry(type, timestamp, txt));
addLogEntry(ps(), new LogEntry(type, timestamp, txt));
}

@Override
public void error(String error) {
ps.println(error);
ps().println(error);
}

@Override
public synchronized void close() {
sps.free();
if (!sps.isUsed()) {
if (sps.getPrintStream() != System.out) {
sps.getPrintStream().close();
}
String file = outputSpec.getFile();
if (file != null) {
printStreamMap.remove(file);
}
}
}
private void addLogEntry(PrintStream ps, LogEntry logEntry) {
// TODO: Add the entry to a queue, and let the queue be picked up with a small delay from an extra
// thread which then can sort the entries by time before printing it out in order to avoid race conditions.
Expand Down Expand Up @@ -86,4 +126,5 @@ public int compareTo(LogEntry entry) {
return timestamp.compareTo(entry.timestamp);
}
}

}
39 changes: 0 additions & 39 deletions src/main/java/io/fabric8/maven/docker/log/LogCallbackFactory.java

This file was deleted.

11 changes: 4 additions & 7 deletions src/main/java/io/fabric8/maven/docker/log/LogDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
* limitations under the License.
*/

import static io.fabric8.maven.docker.log.LogCallbackFactory.createLogCallback;
import static io.fabric8.maven.docker.log.LogCallbackFactory.closeLogs;

import java.io.FileNotFoundException;
import java.util.*;
import java.util.HashMap;
import java.util.Map;

import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.access.log.LogGetHandle;
Expand All @@ -40,16 +38,15 @@ public LogDispatcher(DockerAccess dockerAccess) {
}

public synchronized void trackContainerLog(String containerId, LogOutputSpec spec) throws FileNotFoundException {
LogGetHandle handle = dockerAccess.getLogAsync(containerId, createLogCallback(spec));
LogGetHandle handle = dockerAccess.getLogAsync(containerId, new DefaultLogCallback(spec));
logHandles.put(containerId, handle);
}

public synchronized void fetchContainerLog(String containerId, LogOutputSpec spec) throws FileNotFoundException {
dockerAccess.getLogSync(containerId, createLogCallback(spec));
dockerAccess.getLogSync(containerId, new DefaultLogCallback(spec));
}

public synchronized void untrackAllContainerLogs() {
closeLogs();
for (String key : logHandles.keySet()) {
LogGetHandle handle = logHandles.get(key);
handle.finish();
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/fabric8/maven/docker/log/SharedPrintStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.fabric8.maven.docker.log;

import java.io.PrintStream;

class SharedPrintStream {
private PrintStream printStream;

private int numUsers;

public SharedPrintStream(PrintStream ps) {
this.printStream = ps;
this.numUsers = 1;
}

public PrintStream getPrintStream() {
return printStream;
}

public void setPrintStream(PrintStream printStream) {
this.printStream = printStream;
}

public boolean isUsed() {
return numUsers > 0;
}

public void allocate() {
numUsers++;
}

public void free() {
assert numUsers > 0;
numUsers--;
}
}
Loading

0 comments on commit 100b77a

Please sign in to comment.