org.apache.flink.runtime.webmonitor.retriever.LeaderRetriever Maven / Gradle / Ivy
/*
* 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.webmonitor.retriever;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.runtime.leaderretrieval.LeaderRetrievalListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
/**
* Retrieves and stores the current leader address.
*/
public class LeaderRetriever implements LeaderRetrievalListener {
protected final Logger log = LoggerFactory.getLogger(getClass());
private AtomicReference>> atomicLeaderFuture;
public LeaderRetriever() {
atomicLeaderFuture = new AtomicReference<>(new CompletableFuture<>());
}
/**
* Returns the current leader information if available. Otherwise it returns an
* empty optional.
*
* @return The current leader information if available. Otherwise it returns an
* empty optional.
* @throws Exception if the leader future has been completed with an exception
*/
public Optional> getLeaderNow() throws Exception {
CompletableFuture> leaderFuture = this.atomicLeaderFuture.get();
if (leaderFuture != null) {
if (leaderFuture.isDone()) {
return Optional.of(leaderFuture.get());
} else {
return Optional.empty();
}
} else {
return Optional.empty();
}
}
/**
* Returns the current JobManagerGateway future.
*/
public CompletableFuture> getLeaderFuture() {
return atomicLeaderFuture.get();
}
@Override
public void notifyLeaderAddress(final String leaderAddress, final UUID leaderSessionID) {
if (leaderAddress != null && !leaderAddress.equals("")) {
try {
final CompletableFuture> newLeaderFuture = CompletableFuture.completedFuture(Tuple2.of(leaderAddress, leaderSessionID));
final CompletableFuture> oldLeaderFuture = atomicLeaderFuture.getAndSet(newLeaderFuture);
if (!oldLeaderFuture.isDone()) {
// initial leader future
oldLeaderFuture.complete(Tuple2.of(leaderAddress, leaderSessionID));
}
notifyNewLeaderAddress(newLeaderFuture);
}
catch (Exception e) {
handleError(e);
}
}
}
@Override
public void handleError(Exception exception) {
log.error("Received error from LeaderRetrievalService.", exception);
atomicLeaderFuture.get().completeExceptionally(exception);
}
protected void notifyNewLeaderAddress(CompletableFuture> newLeaderAddressFuture) {}
}