Skip to content

Commit 4877cec

Browse files
authored
More detailed tracing when writing metadata (#31319)
Packaging tests are occasionally failing (#30295) because of very slow index template creation. It looks like the slow part is updating the on-disk cluster state, and this change will help to confirm this.
1 parent bbfe1ec commit 4877cec

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

server/src/main/java/org/elasticsearch/gateway/MetaDataStateFormat.java

+13-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.lucene.store.IndexInput;
3030
import org.apache.lucene.store.OutputStreamIndexOutput;
3131
import org.apache.lucene.store.SimpleFSDirectory;
32+
import org.elasticsearch.common.logging.Loggers;
3233
import org.elasticsearch.core.internal.io.IOUtils;
3334
import org.elasticsearch.ExceptionsHelper;
3435
import org.elasticsearch.common.bytes.BytesArray;
@@ -76,6 +77,7 @@ public abstract class MetaDataStateFormat<T> {
7677
private final String prefix;
7778
private final Pattern stateFilePattern;
7879

80+
private static final Logger logger = Loggers.getLogger(MetaDataStateFormat.class);
7981

8082
/**
8183
* Creates a new {@link MetaDataStateFormat} instance
@@ -134,6 +136,7 @@ public void close() throws IOException {
134136
IOUtils.fsync(tmpStatePath, false); // fsync the state file
135137
Files.move(tmpStatePath, finalStatePath, StandardCopyOption.ATOMIC_MOVE);
136138
IOUtils.fsync(stateLocation, true);
139+
logger.trace("written state to {}", finalStatePath);
137140
for (int i = 1; i < locations.length; i++) {
138141
stateLocation = locations[i].resolve(STATE_DIR_NAME);
139142
Files.createDirectories(stateLocation);
@@ -145,12 +148,15 @@ public void close() throws IOException {
145148
// we are on the same FileSystem / Partition here we can do an atomic move
146149
Files.move(tmpPath, finalPath, StandardCopyOption.ATOMIC_MOVE);
147150
IOUtils.fsync(stateLocation, true);
151+
logger.trace("copied state to {}", finalPath);
148152
} finally {
149153
Files.deleteIfExists(tmpPath);
154+
logger.trace("cleaned up {}", tmpPath);
150155
}
151156
}
152157
} finally {
153158
Files.deleteIfExists(tmpStatePath);
159+
logger.trace("cleaned up {}", tmpStatePath);
154160
}
155161
cleanupOldFiles(prefix, fileName, locations);
156162
}
@@ -211,20 +217,19 @@ protected Directory newDirectory(Path dir) throws IOException {
211217
}
212218

213219
private void cleanupOldFiles(final String prefix, final String currentStateFile, Path[] locations) throws IOException {
214-
final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
215-
@Override
216-
public boolean accept(Path entry) throws IOException {
217-
final String entryFileName = entry.getFileName().toString();
218-
return Files.isRegularFile(entry)
219-
&& entryFileName.startsWith(prefix) // only state files
220-
&& currentStateFile.equals(entryFileName) == false; // keep the current state file around
221-
}
220+
final DirectoryStream.Filter<Path> filter = entry -> {
221+
final String entryFileName = entry.getFileName().toString();
222+
return Files.isRegularFile(entry)
223+
&& entryFileName.startsWith(prefix) // only state files
224+
&& currentStateFile.equals(entryFileName) == false; // keep the current state file around
222225
};
223226
// now clean up the old files
224227
for (Path dataLocation : locations) {
228+
logger.trace("cleanupOldFiles: cleaning up {}", dataLocation);
225229
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dataLocation.resolve(STATE_DIR_NAME), filter)) {
226230
for (Path stateFile : stream) {
227231
Files.deleteIfExists(stateFile);
232+
logger.trace("cleanupOldFiles: cleaned up {}", stateFile);
228233
}
229234
}
230235
}

server/src/main/java/org/elasticsearch/gateway/MetaStateService.java

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public void writeIndex(String reason, IndexMetaData indexMetaData) throws IOExce
123123
try {
124124
IndexMetaData.FORMAT.write(indexMetaData,
125125
nodeEnv.indexPaths(indexMetaData.getIndex()));
126+
logger.trace("[{}] state written", index);
126127
} catch (Exception ex) {
127128
logger.warn(() -> new ParameterizedMessage("[{}]: failed to write index state", index), ex);
128129
throw new IOException("failed to write state for [" + index + "]", ex);
@@ -136,6 +137,7 @@ void writeGlobalState(String reason, MetaData metaData) throws IOException {
136137
logger.trace("[_global] writing state, reason [{}]", reason);
137138
try {
138139
MetaData.FORMAT.write(metaData, nodeEnv.nodeDataPaths());
140+
logger.trace("[_global] state written");
139141
} catch (Exception ex) {
140142
logger.warn("[_global]: failed to write global state", ex);
141143
throw new IOException("failed to write global state", ex);

0 commit comments

Comments
 (0)