private int findLatestIndex() throws IOException { ImmutableMap<String, BlobMetaData> blobs = metaDataBlobContainer.listBlobsByPrefix("metadata-"); int index = -1; for (BlobMetaData md : blobs.values()) { if (logger.isTraceEnabled()) { logger.trace("[findLatestMetadata]: Processing [" + md.name() + "]"); } String name = md.name(); int fileIndex = Integer.parseInt(name.substring(name.indexOf('-') + 1)); if (fileIndex >= index) { // try and read the meta data byte[] data = null; try { data = metaDataBlobContainer.readBlobFully(name); readMetaData(data); index = fileIndex; } catch (IOException e) { logger.warn( "[findLatestMetadata]: failed to read metadata from [{}], data_length [{}] ignoring...", e, name, data == null ? "na" : data.length); } } } return index; }
private CommitPoints buildCommitPoints(ImmutableMap<String, BlobMetaData> blobs) { List<CommitPoint> commitPoints = Lists.newArrayList(); for (String name : blobs.keySet()) { if (name.startsWith("commit-")) { try { commitPoints.add(CommitPoints.fromXContent(blobContainer.readBlobFully(name))); } catch (Exception e) { logger.warn("failed to read commit point [{}]", e, name); } } } return new CommitPoints(commitPoints); }
@Override public MetaData read() throws GatewayException { try { this.currentIndex = findLatestIndex(); } catch (IOException e) { throw new GatewayException("Failed to find latest metadata to read from", e); } if (currentIndex == -1) return null; String metaData = "metadata-" + currentIndex; try { return readMetaData(metaDataBlobContainer.readBlobFully(metaData)); } catch (GatewayException e) { throw e; } catch (Exception e) { throw new GatewayException("Failed to read metadata [" + metaData + "] from gateway", e); } }
public CommitPoint findCommitPoint(String index, int shardId) throws IOException { BlobPath path = BlobStoreIndexGateway.shardPath(basePath, index, shardId); ImmutableBlobContainer container = blobStore.immutableBlobContainer(path); ImmutableMap<String, BlobMetaData> blobs = container.listBlobs(); List<CommitPoint> commitPointsList = Lists.newArrayList(); for (BlobMetaData md : blobs.values()) { if (md.length() == 0) { // a commit point that was not flushed yet... continue; } if (md.name().startsWith("commit-")) { try { commitPointsList.add(CommitPoints.fromXContent(container.readBlobFully(md.name()))); } catch (Exception e) { logger.warn("failed to read commit point at path {} with name [{}]", e, path, md.name()); } } } CommitPoints commitPoints = new CommitPoints(commitPointsList); if (commitPoints.commits().isEmpty()) { return null; } return commitPoints.commits().get(0); }
@Override public void recover(RecoveryStatus recoveryStatus) throws IndexShardGatewayRecoveryException { this.recoveryStatus = recoveryStatus; final ImmutableMap<String, BlobMetaData> blobs; try { blobs = blobContainer.listBlobs(); } catch (IOException e) { throw new IndexShardGatewayRecoveryException(shardId, "Failed to list content of gateway", e); } List<CommitPoint> commitPointsList = Lists.newArrayList(); boolean atLeastOneCommitPointExists = false; for (String name : blobs.keySet()) { if (name.startsWith("commit-")) { atLeastOneCommitPointExists = true; try { commitPointsList.add(CommitPoints.fromXContent(blobContainer.readBlobFully(name))); } catch (Exception e) { logger.warn("failed to read commit point [{}]", e, name); } } } if (atLeastOneCommitPointExists && commitPointsList.isEmpty()) { // no commit point managed to load, bail so we won't corrupt the index, will require manual // intervention throw new IndexShardGatewayRecoveryException( shardId, "Commit points exists but none could be loaded", null); } CommitPoints commitPoints = new CommitPoints(commitPointsList); if (commitPoints.commits().isEmpty()) { // no commit points, clean the store just so we won't recover wrong files try { indexShard.store().deleteContent(); } catch (IOException e) { logger.warn("failed to clean store before starting shard", e); } recoveryStatus.index().startTime(System.currentTimeMillis()); recoveryStatus.index().time(System.currentTimeMillis() - recoveryStatus.index().startTime()); recoveryStatus.translog().startTime(System.currentTimeMillis()); recoveryStatus .translog() .time(System.currentTimeMillis() - recoveryStatus.index().startTime()); return; } for (CommitPoint commitPoint : commitPoints) { if (!commitPointExistsInBlobs(commitPoint, blobs)) { logger.warn( "listed commit_point [{}]/[{}], but not all files exists, ignoring", commitPoint.name(), commitPoint.version()); continue; } try { recoveryStatus.index().startTime(System.currentTimeMillis()); recoveryStatus.updateStage(RecoveryStatus.Stage.INDEX); recoverIndex(commitPoint, blobs); recoveryStatus .index() .time(System.currentTimeMillis() - recoveryStatus.index().startTime()); recoveryStatus.translog().startTime(System.currentTimeMillis()); recoveryStatus.updateStage(RecoveryStatus.Stage.TRANSLOG); recoverTranslog(commitPoint, blobs); recoveryStatus .translog() .time(System.currentTimeMillis() - recoveryStatus.index().startTime()); return; } catch (Exception e) { throw new IndexShardGatewayRecoveryException( shardId, "failed to recover commit_point [" + commitPoint.name() + "]/[" + commitPoint.version() + "]", e); } } throw new IndexShardGatewayRecoveryException( shardId, "No commit point data is available in gateway", null); }