Skip to content

Commit aa179b5

Browse files
committed
Cluster metadata files destroyed when using blob store gateway causing data loss, closes elastic#1564.
1 parent 9cf454f commit aa179b5

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/gateway/shared/SharedStorageGateway.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* Licensed to Elastic Search and Shay Banon under one
2+
* Licensed to ElasticSearch and Shay Banon under one
33
* or more contributor license agreements. See the NOTICE file
44
* distributed with this work for additional information
5-
* regarding copyright ownership. Elastic Search licenses this
5+
* regarding copyright ownership. ElasticSearch licenses this
66
* file to you under the Apache License, Version 2.0 (the
77
* "License"); you may not use this file except in compliance
88
* with the License. You may obtain a copy of the License at
@@ -32,35 +32,55 @@
3232
import org.elasticsearch.gateway.GatewayException;
3333
import org.elasticsearch.threadpool.ThreadPool;
3434

35+
import java.util.concurrent.ExecutorService;
36+
import java.util.concurrent.TimeUnit;
37+
38+
import static java.util.concurrent.Executors.*;
39+
import static org.elasticsearch.common.util.concurrent.EsExecutors.*;
40+
3541
/**
36-
* @author kimchy (shay.banon)
42+
*
3743
*/
3844
public abstract class SharedStorageGateway extends AbstractLifecycleComponent<Gateway> implements Gateway, ClusterStateListener {
3945

4046
private final ClusterService clusterService;
4147

4248
private final ThreadPool threadPool;
4349

50+
private ExecutorService writeStateExecutor;
51+
4452
public SharedStorageGateway(Settings settings, ThreadPool threadPool, ClusterService clusterService) {
4553
super(settings);
4654
this.threadPool = threadPool;
4755
this.clusterService = clusterService;
4856
}
4957

50-
@Override protected void doStart() throws ElasticSearchException {
58+
@Override
59+
protected void doStart() throws ElasticSearchException {
5160
clusterService.add(this);
61+
this.writeStateExecutor = newSingleThreadExecutor(daemonThreadFactory(settings, "gateway#writeMetaData"));
5262
}
5363

54-
@Override protected void doStop() throws ElasticSearchException {
64+
@Override
65+
protected void doStop() throws ElasticSearchException {
5566
clusterService.remove(this);
67+
writeStateExecutor.shutdown();
68+
try {
69+
writeStateExecutor.awaitTermination(10, TimeUnit.SECONDS);
70+
} catch (InterruptedException e) {
71+
// ignore
72+
}
5673
}
5774

58-
@Override protected void doClose() throws ElasticSearchException {
75+
@Override
76+
protected void doClose() throws ElasticSearchException {
5977
}
6078

61-
@Override public void performStateRecovery(final GatewayStateRecoveredListener listener) throws GatewayException {
79+
@Override
80+
public void performStateRecovery(final GatewayStateRecoveredListener listener) throws GatewayException {
6281
threadPool.cached().execute(new Runnable() {
63-
@Override public void run() {
82+
@Override
83+
public void run() {
6484
logger.debug("reading state from gateway {} ...", this);
6585
StopWatch stopWatch = new StopWatch().start();
6686
MetaData metaData;
@@ -81,7 +101,8 @@ public SharedStorageGateway(Settings settings, ThreadPool threadPool, ClusterSer
81101
});
82102
}
83103

84-
@Override public void clusterChanged(final ClusterChangedEvent event) {
104+
@Override
105+
public void clusterChanged(final ClusterChangedEvent event) {
85106
if (!lifecycle.started()) {
86107
return;
87108
}
@@ -95,8 +116,9 @@ public SharedStorageGateway(Settings settings, ThreadPool threadPool, ClusterSer
95116
if (!event.metaDataChanged()) {
96117
return;
97118
}
98-
threadPool.cached().execute(new Runnable() {
99-
@Override public void run() {
119+
writeStateExecutor.execute(new Runnable() {
120+
@Override
121+
public void run() {
100122
logger.debug("writing to gateway {} ...", this);
101123
StopWatch stopWatch = new StopWatch().start();
102124
try {

0 commit comments

Comments
 (0)