Skip to content

Commit 1e918c4

Browse files
committed
[STATE] Refactor state format to use incremental state IDs
Today there is a chance that the state version for shard, index or cluster state goes backwards or is reset on a full restart etc. depending on several factors not related to the state. To prevent any collisions with already existing state files and to maintain write-once properties this change introductes an incremental state ID instead of using the plain state version. This also fixes a bug when the previous legacy state had a greater version than the current state which causes an exception on node startup or if left-over files are present. Closes elastic#10316
1 parent 730bcb2 commit 1e918c4

File tree

4 files changed

+286
-233
lines changed

4 files changed

+286
-233
lines changed

src/main/java/org/elasticsearch/gateway/local/state/meta/LocalGatewayMetaState.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public class LocalGatewayMetaState extends AbstractComponent implements ClusterS
6161

6262
static final String GLOBAL_STATE_FILE_PREFIX = "global-";
6363
private static final String INDEX_STATE_FILE_PREFIX = "state-";
64-
static final Pattern GLOBAL_STATE_FILE_PATTERN = Pattern.compile(GLOBAL_STATE_FILE_PREFIX + "(\\d+)(" + MetaDataStateFormat.STATE_FILE_EXTENSION + ")?");
65-
static final Pattern INDEX_STATE_FILE_PATTERN = Pattern.compile(INDEX_STATE_FILE_PREFIX + "(\\d+)(" + MetaDataStateFormat.STATE_FILE_EXTENSION + ")?");
6664
private static final String GLOBAL_STATE_LOG_TYPE = "[_global]";
6765
static enum AutoImportDangledState {
6866
NO() {
@@ -119,6 +117,8 @@ public static AutoImportDangledState fromString(String value) {
119117
private final Object danglingMutex = new Object();
120118
private final IndicesService indicesService;
121119
private final ClusterService clusterService;
120+
private final MetaDataStateFormat<IndexMetaData> indexStateFormat;
121+
private final MetaDataStateFormat<MetaData> globalStateFormat;
122122

123123
@Inject
124124
public LocalGatewayMetaState(Settings settings, ThreadPool threadPool, NodeEnvironment nodeEnv,
@@ -152,6 +152,8 @@ public LocalGatewayMetaState(Settings settings, ThreadPool threadPool, NodeEnvir
152152

153153
logger.debug("using gateway.local.auto_import_dangled [{}], gateway.local.delete_timeout [{}], with gateway.local.dangling_timeout [{}]",
154154
this.autoImportDangled, this.deleteTimeout, this.danglingTimeout);
155+
indexStateFormat = indexStateFormat(format, formatParams);
156+
globalStateFormat = globalStateFormat(format, gatewayModeFormatParams);
155157
if (DiscoveryNode.masterNode(settings) || DiscoveryNode.dataNode(settings)) {
156158
nodeEnv.ensureAtomicMoveSupported();
157159
}
@@ -319,8 +321,8 @@ public void onFailure(Throwable e) {
319321
/**
320322
* Returns a StateFormat that can read and write {@link MetaData}
321323
*/
322-
static MetaDataStateFormat<MetaData> globalStateFormat(XContentType format, final ToXContent.Params formatParams, final boolean deleteOldFiles) {
323-
return new MetaDataStateFormat<MetaData>(format, deleteOldFiles) {
324+
static MetaDataStateFormat<MetaData> globalStateFormat(XContentType format, final ToXContent.Params formatParams) {
325+
return new MetaDataStateFormat<MetaData>(format, GLOBAL_STATE_FILE_PREFIX) {
324326

325327
@Override
326328
public void toXContent(XContentBuilder builder, MetaData state) throws IOException {
@@ -337,8 +339,8 @@ public MetaData fromXContent(XContentParser parser) throws IOException {
337339
/**
338340
* Returns a StateFormat that can read and write {@link IndexMetaData}
339341
*/
340-
static MetaDataStateFormat<IndexMetaData> indexStateFormat(XContentType format, final ToXContent.Params formatParams, boolean deleteOldFiles) {
341-
return new MetaDataStateFormat<IndexMetaData>(format, deleteOldFiles) {
342+
static MetaDataStateFormat<IndexMetaData> indexStateFormat(XContentType format, final ToXContent.Params formatParams) {
343+
return new MetaDataStateFormat<IndexMetaData>(format, INDEX_STATE_FILE_PREFIX) {
342344

343345
@Override
344346
public void toXContent(XContentBuilder builder, IndexMetaData state) throws IOException {
@@ -353,10 +355,8 @@ public IndexMetaData fromXContent(XContentParser parser) throws IOException {
353355

354356
private void writeIndex(String reason, IndexMetaData indexMetaData, @Nullable IndexMetaData previousIndexMetaData) throws Exception {
355357
logger.trace("[{}] writing state, reason [{}]", indexMetaData.index(), reason);
356-
final boolean deleteOldFiles = previousIndexMetaData != null && previousIndexMetaData.version() != indexMetaData.version();
357-
final MetaDataStateFormat<IndexMetaData> writer = indexStateFormat(format, formatParams, deleteOldFiles);
358358
try {
359-
writer.write(indexMetaData, INDEX_STATE_FILE_PREFIX, indexMetaData.version(),
359+
indexStateFormat.write(indexMetaData, indexMetaData.version(),
360360
nodeEnv.indexLocations(new Index(indexMetaData.index())));
361361
} catch (Throwable ex) {
362362
logger.warn("[{}]: failed to write index state", ex, indexMetaData.index());
@@ -366,9 +366,8 @@ private void writeIndex(String reason, IndexMetaData indexMetaData, @Nullable In
366366

367367
private void writeGlobalState(String reason, MetaData metaData) throws Exception {
368368
logger.trace("{} writing state, reason [{}]", GLOBAL_STATE_LOG_TYPE, reason);
369-
final MetaDataStateFormat<MetaData> writer = globalStateFormat(format, gatewayModeFormatParams, true);
370369
try {
371-
writer.write(metaData, GLOBAL_STATE_FILE_PREFIX, metaData.version(), nodeEnv.nodeDataLocations());
370+
globalStateFormat.write(metaData, metaData.version(), nodeEnv.nodeDataLocations());
372371
} catch (Throwable ex) {
373372
logger.warn("{}: failed to write global state", ex, GLOBAL_STATE_LOG_TYPE);
374373
throw new IOException("failed to write global state", ex);
@@ -398,12 +397,11 @@ private MetaData loadState() throws Exception {
398397

399398
@Nullable
400399
private IndexMetaData loadIndexState(String index) {
401-
return MetaDataStateFormat.loadLatestState(logger, indexStateFormat(format, formatParams, true),
402-
INDEX_STATE_FILE_PATTERN, "[" + index + "]", nodeEnv.indexLocations(new Index(index)));
400+
return indexStateFormat.loadLatestState(logger, nodeEnv.indexLocations(new Index(index)));
403401
}
404402

405403
private MetaData loadGlobalState() {
406-
return MetaDataStateFormat.loadLatestState(logger, globalStateFormat(format, gatewayModeFormatParams, true), GLOBAL_STATE_FILE_PATTERN, GLOBAL_STATE_LOG_TYPE, nodeEnv.nodeDataLocations());
404+
return globalStateFormat.loadLatestState(logger, nodeEnv.nodeDataLocations());
407405
}
408406

409407

0 commit comments

Comments
 (0)