private Translog openTranslog(EngineConfig engineConfig, IndexWriter writer) throws IOException { final TranslogConfig translogConfig = engineConfig.getTranslogConfig(); Translog.TranslogGeneration generation = null; if (openMode == EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG) { generation = loadTranslogIdFromCommit(writer); // We expect that this shard already exists, so it must already have an existing translog else // something is badly wrong! if (generation == null) { throw new IllegalStateException( "no translog generation present in commit data but translog is expected to exist"); } if (generation != null && generation.translogUUID == null) { throw new IndexFormatTooOldException( "trasnlog", "translog has no generation nor a UUID - this might be an index from a previous version consider upgrading to N-1 first"); } } final Translog translog = new Translog(translogConfig, generation); if (generation == null || generation.translogUUID == null) { assert openMode != EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG : "OpenMode must not be " + EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG; if (generation == null) { logger.debug("no translog ID present in the current generation - creating one"); } else if (generation.translogUUID == null) { logger.debug( "upgraded translog to pre 2.0 format, associating translog with index - writing translog UUID"); } boolean success = false; try { commitIndexWriter( writer, translog, openMode == EngineConfig.OpenMode.OPEN_INDEX_CREATE_TRANSLOG ? writer.getCommitData().get(SYNC_COMMIT_ID) : null); success = true; } finally { if (success == false) { IOUtils.closeWhileHandlingException(translog); } } } return translog; }
/** * Reads the current stored translog ID from the IW commit data. If the id is not found, recommits * the current translog id into lucene and returns null. */ @Nullable private Translog.TranslogGeneration loadTranslogIdFromCommit(IndexWriter writer) throws IOException { // commit on a just opened writer will commit even if there are no changes done to it // we rely on that for the commit data translog id key final Map<String, String> commitUserData = writer.getCommitData(); if (commitUserData.containsKey("translog_id")) { assert commitUserData.containsKey(Translog.TRANSLOG_UUID_KEY) == false : "legacy commit contains translog UUID"; return new Translog.TranslogGeneration( null, Long.parseLong(commitUserData.get("translog_id"))); } else if (commitUserData.containsKey(Translog.TRANSLOG_GENERATION_KEY)) { if (commitUserData.containsKey(Translog.TRANSLOG_UUID_KEY) == false) { throw new IllegalStateException("commit doesn't contain translog UUID"); } final String translogUUID = commitUserData.get(Translog.TRANSLOG_UUID_KEY); final long translogGen = Long.parseLong(commitUserData.get(Translog.TRANSLOG_GENERATION_KEY)); return new Translog.TranslogGeneration(translogUUID, translogGen); } return null; }