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

org.apache.flink.runtime.state.gemini.engine.fs.PersistenceGroupPageToDfs 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.fs;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.runtime.state.gemini.engine.GRegion;
import org.apache.flink.runtime.state.gemini.engine.GRegionContext;
import org.apache.flink.runtime.state.gemini.engine.dbms.GContext;
import org.apache.flink.runtime.state.gemini.engine.dbms.Supervisor;
import org.apache.flink.runtime.state.gemini.engine.page.DataPage;
import org.apache.flink.runtime.state.gemini.engine.page.PageAddress;

import org.apache.flink.shaded.guava18.com.google.common.base.MoreObjects;
import org.apache.flink.shaded.netty4.io.netty.util.concurrent.EventExecutor;
import org.apache.flink.shaded.netty4.io.netty.util.concurrent.EventExecutorGroup;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.BiConsumer;

/**
 * PersistenceGroupPageToDfs writes data to dfs ahead starting snapshot, best-effort batch the pages.
 */
public class PersistenceGroupPageToDfs implements PersistenceStrategy {
	private static final Logger LOG = LoggerFactory.getLogger(PersistenceGroupPageToDfs.class);

	private final EventExecutorGroup snapshotExecutorGroup;
	private final Supervisor supervisor;
	private Map batchMap = new ConcurrentHashMap<>();
	private AtomicInteger batchSize = new AtomicInteger(0);
	private final AtomicLong runningPersistTask = new AtomicLong(0);
	private final AtomicLong runningPersistPageSize = new AtomicLong(0);
	private final AtomicLong totalPersistPageSize = new AtomicLong(0);
	private final int batchPersistenceSize;
	private final int maxPersistenceRunningTask;
	final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

	public PersistenceGroupPageToDfs(GContext gContext) {
		this.snapshotExecutorGroup = gContext.getSupervisor().getSnapshotExecutorGroup();
		this.supervisor = gContext.getSupervisor();
		this.batchPersistenceSize = gContext.getGConfiguration().getBatchPersistenceSize();
		this.maxPersistenceRunningTask = gContext.getGConfiguration().getMaxPersistenceRunningTask() * gContext.getGConfiguration().getSnapshotThreadNum();
	}

	@Override
	public void persistPage(GRegion gRegion, PageAddress pageAddress, int compactedMemSize) {

		if (runningPersistTask.incrementAndGet() > maxPersistenceRunningTask) {
			//HDFS too slow
			runningPersistTask.decrementAndGet();
			return;
		}

		Iterator pageAddressIterator = pageAddress.pageIterator();
		lock.readLock().lock();
		try {
			while (pageAddressIterator.hasNext()) {
				PageAddress realPageAddress = pageAddressIterator.next();
				DataPage dataPage = realPageAddress.getDataPageNoReference();
				if (dataPage != null) {
					batchMap.computeIfAbsent(realPageAddress, nothing -> {
						batchSize.addAndGet(realPageAddress.getDataLen());
						return gRegion;
					});

				}
			}
		} finally {
			lock.readLock().unlock();
		}
		if (batchSize.get() < batchPersistenceSize) {
			runningPersistTask.decrementAndGet();
			return;
		}

		Map copyBatchMap;
		final int flushingSize;
		lock.writeLock().lock();
		try {
			if (batchSize.get() < batchPersistenceSize) {
				runningPersistTask.decrementAndGet();
				return;
			}
			copyBatchMap = batchMap;
			batchMap = new ConcurrentHashMap<>(copyBatchMap.size());
			flushingSize = batchSize.getAndSet(0);
		} finally {
			lock.writeLock().unlock();
		}

		//protect code.
		if (copyBatchMap.size() == 0) {
			runningPersistTask.decrementAndGet();
			return;
		}

		runningPersistPageSize.addAndGet(flushingSize);
		totalPersistPageSize.addAndGet(flushingSize);

		final EventExecutor snapshotEventExecutor = snapshotExecutorGroup.next();

		List pageAddressList = new ArrayList<>();
		List regionContextList = new ArrayList<>();
		List> callBacks = new ArrayList<>();
		for (Map.Entry entry : copyBatchMap.entrySet()) {
			pageAddressList.add(entry.getKey());
			regionContextList.add(entry.getValue().getGRegionContext());
		}

		BiConsumer callBack = (success, throwable) -> {
			runningPersistPageSize.addAndGet(-flushingSize);
			runningPersistTask.decrementAndGet();

			if (!success) {
				LOG.error("persistPage flush failed", throwable);
			}
		};
		callBacks.add(callBack);

		this.supervisor.getFileCache().flushBatchPages(pageAddressList,
			regionContextList,
			snapshotEventExecutor,
			false,
			false,
			callBacks);

	}

	@Override
	public void close() throws IOException {

	}

	@Override
	public String toString() {
		return MoreObjects.toStringHelper(this).
			add("batchSize", batchSize).
			add("runningPersistTask", runningPersistTask.get()).
			add("runningPersistPageSize", runningPersistPageSize.get()).
			add("totalPersistPageSize", totalPersistPageSize.get()).toString();
	}

	@VisibleForTesting
	public int getBatchSize() {
		return batchSize.get();
	}

	@VisibleForTesting
	public int getBatchMapSize() {
		return batchMap.size();
	}

	@VisibleForTesting
	public long getRunningPersistTask() {
		return runningPersistTask.get();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy