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

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

import org.apache.flink.runtime.state.gemini.engine.exceptions.GeminiRuntimeException;

import org.apache.flink.shaded.guava18.com.google.common.base.FinalizableReferenceQueue;
import org.apache.flink.shaded.guava18.com.google.common.base.FinalizableWeakReference;
import org.apache.flink.shaded.guava18.com.google.common.collect.Maps;

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

import java.lang.ref.Reference;
import java.util.Map;

/**
 * The default implement for {@link LeakDetector} using {@link FinalizableReferenceQueue}.
 */
public class LeakDetectorImpl implements LeakDetector {
	private static final Logger LOG = LoggerFactory.getLogger(LeakDetectorImpl.class);

	private LeakReportLevel reportLevel = LeakReportLevel.DEBUG;
	private volatile boolean leakDetected = false;

	private final FinalizableReferenceQueue frq = new FinalizableReferenceQueue();

	private final Map, FinalizableCleaner> references = Maps.newConcurrentMap();

	public void register(Finalizable finalizable) {
		Reference reference = new FinalizableWeakReference(finalizable, frq) {
			@Override public void finalizeReferent() {
				FinalizableCleaner cleaner = references.remove(this);
				if (!cleaner.cleaned()) {
					leakDetected = true;
					reportLeak(cleaner);
					free(cleaner);
				}
			}
		};
		FinalizableCleaner freer = finalizable.getCleaner();
		references.put(reference, freer);
	}

	public void setReportLevel(LeakReportLevel level) {
		this.reportLevel = level;
	}

	/**
	 * The actual free method. Internal use.
	 * @param cleaner a cleaner for the {@link Finalizable}.
	 */
	void free(FinalizableCleaner cleaner) {
		cleaner.clean();
		cleaner.cleanedByLeakDetector();
	}

	@Override
	public void close() {
		frq.close();
		// Sweep last round and release all.
		// Note that the Garbage Collector should have swept once, so this round we wouldn't find anything retained. Otherwise there is a LEAK.
		for (FinalizableCleaner cleaner: references.values()) {
			if (!cleaner.cleaned()) {
				leakDetected = true;
				reportLeak(cleaner);
				free(cleaner);
			}
		}
		if (leakDetected && reportLevel == LeakReportLevel.ERROR) {
			throw new GeminiRuntimeException("There is at least one leak detected.");
		}
	}

	@Override
	public boolean leakDetected() {
		return leakDetected;
	}

	private void reportLeak(FinalizableCleaner cleaner) {
		if (reportLevel == LeakReportLevel.ERROR) {
			LOG.error("There is a leak and {} help to release {}", this, cleaner);
		} else if (reportLevel == LeakReportLevel.DEBUG) {
			LOG.debug("There is a leak and {} help to release {}", this, cleaner);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy