[GATEWAY] Don't rename recovery translog in gateway

we used to reuse files / write them twice in translog. Today we don't
do this anymore so the rename logic is unneeded.
This commit is contained in:
Simon Willnauer 2015-02-17 11:51:03 +01:00
parent 2e3c6a9118
commit 41fd4d8722
1 changed files with 13 additions and 27 deletions

View File

@ -28,6 +28,7 @@ import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.cluster.action.index.MappingUpdatedAction; import org.elasticsearch.cluster.action.index.MappingUpdatedAction;
import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.io.FileSystemUtils;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
@ -43,6 +44,7 @@ import org.elasticsearch.index.shard.AbstractIndexShardComponent;
import org.elasticsearch.index.shard.IndexShardState; import org.elasticsearch.index.shard.IndexShardState;
import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.store.DirectoryUtils;
import org.elasticsearch.index.translog.*; import org.elasticsearch.index.translog.*;
import org.elasticsearch.indices.recovery.RecoveryState; import org.elasticsearch.indices.recovery.RecoveryState;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
@ -65,8 +67,6 @@ import java.util.concurrent.TimeUnit;
*/ */
public class IndexShardGateway extends AbstractIndexShardComponent implements Closeable { public class IndexShardGateway extends AbstractIndexShardComponent implements Closeable {
private static final int RECOVERY_TRANSLOG_RENAME_RETRIES = 3;
private final ThreadPool threadPool; private final ThreadPool threadPool;
private final MappingUpdatedAction mappingUpdatedAction; private final MappingUpdatedAction mappingUpdatedAction;
private final IndexService indexService; private final IndexService indexService;
@ -184,36 +184,20 @@ public class IndexShardGateway extends AbstractIndexShardComponent implements Cl
return; return;
} }
// move an existing translog, if exists, to "recovering" state, and start reading from it final Translog translog = indexShard.translog();
Translog translog = indexShard.translog();
final Path translogName = translog.getPath(translogId); final Path translogName = translog.getPath(translogId);
final Path recoverTranslogName = translogName.resolveSibling(translogName.getFileName() + ".recovering");
logger.trace("try recover from translog file {} locations: {}", translogName, Arrays.toString(translog.locations())); logger.trace("try recover from translog file {} locations: {}", translogName, Arrays.toString(translog.locations()));
Path recoveringTranslogFile = null; Path recoveringTranslogFile = null;
OUTER:
for (Path translogLocation : translog.locations()) { for (Path translogLocation : translog.locations()) {
final Path tmpRecoveringFile = translogLocation.resolve(recoverTranslogName); // we have to support .recovering since it's a leftover from previous version but might still be on the filesystem
if (Files.exists(tmpRecoveringFile) == false) { // we used to rename the foo into foo.recovering since foo was reused / overwritten but we fixed that in 1.5
Path tmpTranslogFile = translogLocation.resolve(translogName); for (Path recoveryFiles : FileSystemUtils.files(translogLocation, translogName.getFileName() + "{.recovering,}")) {
if (Files.exists(tmpTranslogFile)) { logger.trace("Translog file found in {}", recoveryFiles);
logger.trace("Translog file found in {} - renaming", translogLocation); recoveringTranslogFile = recoveryFiles;
for (int i = 0; i < RECOVERY_TRANSLOG_RENAME_RETRIES; i++) { break OUTER;
try {
Files.move(tmpTranslogFile, tmpRecoveringFile, StandardCopyOption.ATOMIC_MOVE);
recoveringTranslogFile = tmpRecoveringFile;
logger.trace("Renamed translog from {} to {}", tmpTranslogFile.getFileName(), recoveringTranslogFile.getFileName());
break;
} catch (Exception ex) {
logger.debug("Failed to rename tmp recovery file", ex);
}
}
} else {
logger.trace("Translog file NOT found in {} - continue", translogLocation);
}
} else {
recoveringTranslogFile = tmpRecoveringFile;
break;
} }
logger.trace("Translog file NOT found in {} - continue", translogLocation);
} }
if (recoveringTranslogFile == null || Files.exists(recoveringTranslogFile) == false) { if (recoveringTranslogFile == null || Files.exists(recoveringTranslogFile) == false) {
@ -293,6 +277,8 @@ public class IndexShardGateway extends AbstractIndexShardComponent implements Cl
} catch (Exception ex) { } catch (Exception ex) {
logger.debug("Failed to delete recovering translog file {}", ex, recoveringTranslogFile); logger.debug("Failed to delete recovering translog file {}", ex, recoveringTranslogFile);
} }
} catch (IOException e) {
throw new IndexShardGatewayRecoveryException(shardId, "failed to recovery from gateway", e);
} finally { } finally {
indexShard.store().decRef(); indexShard.store().decRef();
} }