Skip to content

Commit 97556df

Browse files
authored
Fixing flaky LoggedExec (tests) (#133215) (#133239)
1 parent 6ed5578 commit 97556df

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

build-tools/src/main/java/org/elasticsearch/gradle/LazyFileOutputStream.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
* or more contributor license agreements. Licensed under the "Elastic License
44
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
55
* Public License v 1"; you may not use this file except in compliance with, at
6-
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7-
* License v3.0 only", or the "Server Side Public License, v 1".
6+
* your election, the "Server Side Public License v3.0 only", or the "Server Side Public License, v 1".
87
*/
98

109
package org.elasticsearch.gradle;
@@ -18,48 +17,58 @@
1817
* An outputstream to a File that is lazily opened on the first write.
1918
*/
2019
class LazyFileOutputStream extends OutputStream {
21-
private OutputStream delegate;
20+
private final File file;
21+
private volatile OutputStream delegate;
22+
private volatile boolean initialized = false;
23+
private final Object lock = new Object();
2224

2325
LazyFileOutputStream(File file) {
24-
// use an initial dummy delegate to avoid doing a conditional on every write
25-
this.delegate = new OutputStream() {
26-
private void bootstrap() throws IOException {
27-
file.getParentFile().mkdirs();
28-
delegate = new FileOutputStream(file);
29-
}
30-
31-
@Override
32-
public void write(int b) throws IOException {
33-
bootstrap();
34-
delegate.write(b);
35-
}
36-
37-
@Override
38-
public void write(byte b[], int off, int len) throws IOException {
39-
bootstrap();
40-
delegate.write(b, off, len);
41-
}
26+
this.file = file;
27+
}
4228

43-
@Override
44-
public void write(byte b[]) throws IOException {
45-
bootstrap();
46-
delegate.write(b);
29+
private void ensureInitialized() throws IOException {
30+
if (initialized == false) {
31+
synchronized (lock) {
32+
if (initialized == false) {
33+
file.getParentFile().mkdirs();
34+
delegate = new FileOutputStream(file);
35+
initialized = true;
36+
}
4737
}
48-
};
38+
}
4939
}
5040

5141
@Override
5242
public void write(int b) throws IOException {
43+
ensureInitialized();
5344
delegate.write(b);
5445
}
5546

5647
@Override
5748
public void write(byte b[], int off, int len) throws IOException {
49+
ensureInitialized();
5850
delegate.write(b, off, len);
5951
}
6052

53+
@Override
54+
public void write(byte b[]) throws IOException {
55+
ensureInitialized();
56+
delegate.write(b);
57+
}
58+
6159
@Override
6260
public void close() throws IOException {
63-
delegate.close();
61+
synchronized (lock) {
62+
if (initialized && delegate != null) {
63+
delegate.close();
64+
}
65+
}
66+
}
67+
68+
@Override
69+
public void flush() throws IOException {
70+
if (initialized && delegate != null) {
71+
delegate.flush();
72+
}
6473
}
6574
}

0 commit comments

Comments
 (0)