All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.gemini.engine.snapshot.DFSSnapshotOperation Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.runtime.state.gemini.engine.snapshot;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.state.SnapshotDirectory;
import org.apache.flink.runtime.state.gemini.engine.dbms.GContext;
import org.apache.flink.runtime.state.gemini.engine.filecache.PageBatchFlusher;
import org.apache.flink.runtime.state.gemini.engine.fs.FileManager;
import org.apache.flink.util.Preconditions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Snapshot operation which will only snapshot to dfs.
 */
public class DFSSnapshotOperation extends SnapshotOperation {

	private static final Logger LOG = LoggerFactory.getLogger(DFSSnapshotOperation.class);

	private final FileManager dfsFileManager;

	private SnapshotManager.PendingSnapshot pendingSnapshot;

	public DFSSnapshotOperation(
		GContext gContext,
		SnapshotManager snapshotManager,
		FileManager dfsFileManager) {
		super(gContext, snapshotManager);
		this.dfsFileManager = Preconditions.checkNotNull(dfsFileManager);
	}

	@Override
	public SnapshotManager.PendingSnapshot createPendingSnapshot(
		BackendSnapshotMeta backendSnapshotMeta,
		long accessNumber) {
		long checkpointId = backendSnapshotMeta.getCheckpointId();
		SnapshotCompletableFuture snapshotCompletableFuture =
			new SnapshotCompletableFuture(snapshotManager.getSnapshotExecutor());

		Path dfsSnapshotBasePath = dfsFileManager.getBasePath();
		Path dfsMetaPath = ((SnapshotManagerImpl) snapshotManager).getDFSSnapshotMetaPath(
			dfsSnapshotBasePath, checkpointId);

		PageBatchFlusher dfsPageBatchFlusher = new PageBatchFlusher(
			gContext.getGConfiguration().getSnapshotFlushBatchNumPage(),
			gContext.getGConfiguration().getSnapshotFlushBatchDataSize(),
			isForceFlushPage(),
			gContext.getSupervisor().getFileCache(),
			gContext.getSupervisor().getSnapshotExecutorGroup());

		SnapshotManager.PendingSnapshot pendingSnapshot =
			new SnapshotManager.PendingSnapshot(
				checkpointId,
				backendSnapshotMeta.getTimestamp(),
				snapshotCompletableFuture,
				dfsSnapshotBasePath,
				dfsMetaPath,
				null,
				null,
				accessNumber,
				this,
				dfsPageBatchFlusher);

		SnapshotCompaction snapshotCompaction =
			gContext.getGConfiguration().isSnapshotCompactionEnabled() ?
				new SnapshotCompactionImpl(gContext, pendingSnapshot) :
				new NoSnapshotCompaction();
		snapshotCompletableFuture.addSnapshotStage(snapshotCompaction);

		SnapshotFileSyncStage fileSyncStage = new SnapshotFileSyncStage(
			snapshotCompletableFuture,
			gContext.getSupervisor().getFileCache());
		snapshotCompletableFuture.addSnapshotStage(fileSyncStage);

		snapshotCompletableFuture.addSnapshotStage(
			new SnapshotMetaStage(snapshotCompletableFuture, this));

		snapshotCompletableFuture.setPendingSnapshot(pendingSnapshot);
		pendingSnapshot.setSnapshotCompaction(snapshotCompaction);
		this.pendingSnapshot = pendingSnapshot;

		return pendingSnapshot;
	}

	@Override
	public SnapshotManager.PendingSnapshot getPendingSnapshot() {
		return pendingSnapshot;
	}

	@Override
	public DBSnapshotResult getSnapshotResult() throws Exception {
		SnapshotMetaFile.Writer writer = null;
		try {
			writer = SnapshotMetaFile.getWriter(pendingSnapshot.getSnapshotMetaPath());
			RegionSnapshot regionSnapshot = new RegionSnapshot(dfsFileManager, writer, false);
			// {fileId -> {fileId, }}
			Map>> fileMapping = new HashMap<>();

			long[] regionOffsets = writeRegionMetaAndPageIndex(
				pendingSnapshot.getGRegionSnapshotMeta(),
				Collections.singletonList(regionSnapshot),
				Collections.singletonList(fileMapping)).iterator().next();

			pendingSnapshot.setFileMapping(fileMapping);
			Tuple2 fileMappingOffsets = writeDfsAndLocalFileMapping(pendingSnapshot, writer, null, dfsFileManager);

			long snapshotMetaSize = writer.getPos();

			SnapshotStat snapshotStat = pendingSnapshot.getSnapshotStat();
			snapshotStat.addAndGetTotalFiles(fileMapping.size());
			snapshotStat.setMetaFileSize(snapshotMetaSize);

			DBSnapshotMeta dbSnapshotMeta = new DBSnapshotMeta(
				pendingSnapshot.getCheckpointId(),
				gContext.getStartRegionId(),
				gContext.getEndRegionId(),
				regionOffsets,
				fileMappingOffsets.f0,
				fileMappingOffsets.f1,
				snapshotMetaSize,
				snapshotStat.addAndGetTotalSize(0),
				snapshotStat.addAndGetIncrementalSize(0),
				writer.getFilePath().toUri().toString(),
				snapshotManager.getNameSpace());

			writer.close();

			return new DBSnapshotResult(SnapshotDirectory.permanent(pendingSnapshot.getSnapshotBasePath()), dbSnapshotMeta);
		} catch (Exception e) {
			LOG.error("Failed to snapshot meta for checkpoint " + pendingSnapshot.getCheckpointId(), e);
			closeAndDeleteWriterQuietly(writer);
			throw e;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy