Skip to content

Commit 421caa1

Browse files
author
Andrey Ershov
committed
Fix code review issues
1 parent 39e4686 commit 421caa1

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ private void maybeUpgradeMetaData(MetaDataIndexUpgradeService metaDataIndexUpgra
114114
List<Runnable> cleanupActions = new ArrayList<>();
115115
// If globalStateGeneration is non-negative, it means we should have some metadata on disk
116116
// Always re-write it even if upgrade plugins do not upgrade it, to be sure it's properly persisted on all data path
117-
Map<Index, Long> indices = new HashMap<>(metaState.getIndices());
118117
final MetaData upgradedMetaData = upgradeMetaData(metaData, metaDataIndexUpgradeService, metaDataUpgrader);
119118

120119
if (MetaData.isGlobalStateEquals(metaData, upgradedMetaData) == false) {
@@ -125,6 +124,7 @@ private void maybeUpgradeMetaData(MetaDataIndexUpgradeService metaDataIndexUpgra
125124
final long currentGlobalStateGeneration = globalStateGeneration;
126125
cleanupActions.add(() -> metaStateService.cleanupGlobalState(currentGlobalStateGeneration));
127126

127+
Map<Index, Long> indices = new HashMap<>();
128128
for (IndexMetaData indexMetaData : upgradedMetaData) {
129129
long generation;
130130
if (metaData.hasIndexMetaData(indexMetaData) == false) {
@@ -143,7 +143,7 @@ private void maybeUpgradeMetaData(MetaDataIndexUpgradeService metaDataIndexUpgra
143143
performCleanup(cleanupActions);
144144
}
145145
} catch (Exception e) {
146-
logger.error("failed to read local state, exiting...", e);
146+
logger.error("failed to read or re-write local state, exiting...", e);
147147
throw e;
148148
}
149149
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public final long write(final T state, final Path... locations) throws WriteStat
209209
}
210210
assert maxStateId >= 0 : "maxStateId must be positive but was: [" + maxStateId + "]";
211211

212-
final String fileName = prefix + maxStateId + STATE_FILE_EXTENSION;
212+
final String fileName = getStateFileName(maxStateId);
213213
final String tmpFileName = fileName + ".tmp";
214214
List<Tuple<Path, Directory>> directories = new ArrayList<>();
215215

@@ -291,6 +291,9 @@ protected Directory newDirectory(Path dir) throws IOException {
291291
return new SimpleFSDirectory(dir);
292292
}
293293

294+
/**
295+
* Whether to perform autoCleanup of old state files after successful {@link #write(Object, Path...)}.
296+
*/
294297
protected boolean autoCleanup() {
295298
return true;
296299
}
@@ -302,7 +305,7 @@ protected boolean autoCleanup() {
302305
* @param locations state paths.
303306
*/
304307
public void cleanupOldFiles(final long currentGeneration, Path[] locations) {
305-
final String fileNameToKeep = prefix + currentGeneration + STATE_FILE_EXTENSION;
308+
final String fileNameToKeep = getStateFileName(currentGeneration);
306309
for (Path location : locations) {
307310
logger.trace("cleanupOldFiles: cleaning up {}", location);
308311
Path stateLocation = location.resolve(STATE_DIR_NAME);
@@ -351,7 +354,7 @@ private List<Path> findStateFilesByGeneration(final long generation, Path... loc
351354
return files;
352355
}
353356

354-
final String fileName = prefix + generation + STATE_FILE_EXTENSION;
357+
final String fileName = getStateFileName(generation);
355358
for (Path dataLocation : locations) {
356359
final Path stateFilePath = dataLocation.resolve(STATE_DIR_NAME).resolve(fileName);
357360
if (Files.exists(stateFilePath)) {
@@ -363,6 +366,10 @@ private List<Path> findStateFilesByGeneration(final long generation, Path... loc
363366
return files;
364367
}
365368

369+
private String getStateFileName(long generation) {
370+
return prefix + generation + STATE_FILE_EXTENSION;
371+
}
372+
366373
/**
367374
* Tries to load the state of particular generation from the given data-locations. If any of data locations contain state files with
368375
* given generation, state will be loaded from these state files.

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.gateway;
2121

22-
import org.apache.logging.log4j.message.ParameterizedMessage;
2322
import org.elasticsearch.Version;
2423
import org.elasticsearch.cluster.metadata.IndexMetaData;
2524
import org.elasticsearch.cluster.metadata.MetaData;
@@ -56,7 +55,7 @@ public MetaStateService(Settings settings, NodeEnvironment nodeEnv, NamedXConten
5655
* Loads the full state, which includes both the global state and all the indices meta data. <br>
5756
* When loading, manifest file is consulted (represented by {@link MetaState} class), to load proper generations. <br>
5857
* If there is no manifest file on disk, this method fallbacks to BWC mode, where latest generation of global and indices
59-
* metadata is loaded. Please note that currently where is no way to distinguish between manifest file being removed and manifest
58+
* metadata is loaded. Please note that currently there is no way to distinguish between manifest file being removed and manifest
6059
* file was not yet created. It means that this method always fallbacks to BWC mode, if there is no manifest file.
6160
*
6261
* @return tuple of {@link MetaState} and {@link MetaData} with global metadata and indices metadata. If there is no state on disk,
@@ -66,12 +65,6 @@ public MetaStateService(Settings settings, NodeEnvironment nodeEnv, NamedXConten
6665
Tuple<MetaState, MetaData> loadFullState() throws IOException {
6766
final MetaState metaState = loadMetaState();
6867
if (metaState == null) {
69-
MetaData globalMetaData =
70-
MetaData.FORMAT.loadLatestState(logger, namedXContentRegistry, nodeEnv.nodeDataPaths());
71-
boolean isFreshStartup = globalMetaData == null;
72-
if (isFreshStartup == false && Version.CURRENT.major >= 8) {
73-
throw new IOException("failed to find manifest file, which is mandatory staring with ElasticSearch version 8.0");
74-
}
7568
return loadFullStateBWC();
7669
}
7770
final MetaData.Builder metaDataBuilder;
@@ -107,6 +100,12 @@ private Tuple<MetaState, MetaData> loadFullStateBWC() throws IOException {
107100
MetaData.FORMAT.loadLatestStateWithGeneration(logger, namedXContentRegistry, nodeEnv.nodeDataPaths());
108101
MetaData globalMetaData = metaDataAndGeneration.v1();
109102
long globalStateGeneration = metaDataAndGeneration.v2();
103+
boolean isFreshStartup = globalMetaData == null;
104+
105+
if (isFreshStartup) {
106+
assert Version.CURRENT.major < 8 : "failed to find manifest file, which is mandatory staring with Elasticsearch version 8.0";
107+
}
108+
110109
MetaData.Builder metaDataBuilder;
111110
if (globalMetaData != null) {
112111
metaDataBuilder = MetaData.builder(globalMetaData);
@@ -185,8 +184,7 @@ public long writeMetaState(String reason, MetaState metaState) throws WriteState
185184
logger.trace("[_meta] state written (generation: {})", generation);
186185
return generation;
187186
} catch (WriteStateException ex) {
188-
logger.warn("[_meta]: failed to write meta state", ex);
189-
throw ex;
187+
throw new WriteStateException(ex.isDirty(), "[_meta]: failed to write meta state", ex);
190188
}
191189
}
192190

@@ -207,9 +205,7 @@ public long writeIndex(String reason, IndexMetaData indexMetaData) throws WriteS
207205
logger.trace("[{}] state written", index);
208206
return generation;
209207
} catch (WriteStateException ex) {
210-
logger.warn(() -> new ParameterizedMessage("[{}]: failed to write index state", index), ex);
211-
ex.resetDirty();
212-
throw ex;
208+
throw new WriteStateException(false, "[" + index + "]: failed to write index state", ex);
213209
}
214210
}
215211

@@ -226,9 +222,7 @@ long writeGlobalState(String reason, MetaData metaData) throws WriteStateExcepti
226222
logger.trace("[_global] state written");
227223
return generation;
228224
} catch (WriteStateException ex) {
229-
logger.warn("[_global]: failed to write global state", ex);
230-
ex.resetDirty();
231-
throw ex;
225+
throw new WriteStateException(false, "[_global]: failed to write global state", ex);
232226
}
233227
}
234228

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* This exception is thrown when there is a problem of writing state to disk.
2525
*/
2626
public class WriteStateException extends IOException {
27-
private boolean dirty;
27+
private final boolean dirty;
2828

2929
WriteStateException(boolean dirty, String message, Exception cause) {
3030
super(message, cause);
@@ -38,8 +38,4 @@ public class WriteStateException extends IOException {
3838
public boolean isDirty() {
3939
return dirty;
4040
}
41-
42-
public void resetDirty() {
43-
this.dirty = false;
44-
}
4541
}

0 commit comments

Comments
 (0)