flink akka

原创 2015年11月20日 18:40:01
/*
 * 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.akka;


import akka.actor.UntypedActor;
import org.apache.flink.runtime.messages.JobManagerMessages.LeaderSessionMessage;
import org.apache.flink.runtime.messages.RequiresLeaderSessionID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.UUID;


/**
 * Base class for Flink's actors implemented with Java. Actors inheriting from this class
 * automatically log received messages when the debug log level is activated. Furthermore,
 * they filter out {@link LeaderSessionMessage} with the wrong leader session ID. If a message
 * of type {@link RequiresLeaderSessionID} without being wrapped in a LeaderSessionMessage is
 * detected, then an Exception is thrown.
 *
 * In order to implement the actor behavior, an implementing subclass has to override the method
 * handleMessage, which defines how messages are processed. Furthermore, the subclass has to provide
 * a leader session ID option which is returned by getLeaderSessionID.
 */
public abstract class FlinkUntypedActor extends UntypedActor {

protected final Logger LOG = LoggerFactory.getLogger(getClass());


/**
* This method is called by Akka if a new message has arrived for the actor. It logs the
* processing time of the incoming message if the logging level is set to debug. After logging
* the handleLeaderSessionID method is called.
*
* Important: This method cannot be overriden. The actor specific message handling logic is
* implemented by the method handleMessage.
*
* @param message Incoming message
* @throws Exception
*/
@Override
public final void onReceive(Object message) throws Exception {
if(LOG.isDebugEnabled()) {
LOG.debug("Received message {} at {} from {}.", message, getSelf().path(), getSender());


long start = System.nanoTime();


handleLeaderSessionID(message);


long duration = (System.nanoTime() - start)/ 1000000;


LOG.debug("Handled message {} in {} ms from {}.", message, duration, getSender());
} else {
handleLeaderSessionID(message);
}
}


/**
* This method filters out {@link LeaderSessionMessage} whose leader session ID is not equal
* to the actors leader session ID. If a message of type {@link RequiresLeaderSessionID}
* arrives, then an Exception is thrown, because these messages have to be wrapped in a
* {@link LeaderSessionMessage}.
*
* @param message Incoming message
* @throws Exception
*/
private void handleLeaderSessionID(Object message) throws Exception {
if(message instanceof LeaderSessionMessage) {
LeaderSessionMessage msg = (LeaderSessionMessage) message;
UUID expectedID = getLeaderSessionID();
UUID actualID = msg.leaderSessionID();


if(expectedID == actualID || (expectedID != null && expectedID.equals(actualID))) {
handleMessage(msg.message());
} else {
handleDiscardedMessage(expectedID, msg);
}
} else if (message instanceof RequiresLeaderSessionID) {
throw new Exception("Received a message " + message + " without a leader session " +
"ID, even though the message requires a leader session ID.");
} else {
// call method to handle message
handleMessage(message);
}
}


private void handleDiscardedMessage(UUID expectedLeaderSessionID, LeaderSessionMessage msg) {
LOG.warn("Discard message {} because the expected leader session ID {} did not " +
"equal the received leader session ID {}.", msg, expectedLeaderSessionID,
msg.leaderSessionID());
}


/**
* This method contains the actor logic which defines how to react to incoming messages.
*
* @param message Incoming message
* @throws Exception
*/
protected abstract void handleMessage(Object message) throws Exception;


/**
* Returns the current leader session ID associcated with this actor.
* @return
*/
abstract protected UUID getLeaderSessionID();


/**
* This method should be called for every outgoing message. It wraps messages which require
* a leader session ID (indicated by {@link RequiresLeaderSessionID}) in a
* {@link LeaderSessionMessage} with the actor's leader session ID.
*
* This method can be overriden to implement a different decoration behavior.
*
* @param message Message to be decorated
* @return The deocrated message
*/
protected Object decorateMessage(Object message) {
if(message instanceof  RequiresLeaderSessionID) {
return new LeaderSessionMessage(getLeaderSessionID(), message);
} else {
return message;
}
}


}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Akka在Flink中的使用剖析

Akka与Actor 模型Akka是一个用来开发支持并发、容错、扩展性的应用程序框架。它是actor model的实现,因此跟Erlang的并发模型很像。在actor模型的上下文中,所有的活动实体都被...

flink最佳实践ppt

  • 2017-09-28 20:28
  • 1.61MB
  • 下载

Learning apache flink

  • 2017-09-18 19:34
  • 7.20MB
  • 下载

谷歌DataFlow编程模型以及Spark/Flink/StreamCQL的相关实现

流式计算框架编程接口的标准化,傻瓜化,SQL化,自打谷歌发表Dataflow编程模型的Paper起,就有走上台面的趋势。各家计算框架都开始认真考虑相关的问题,俨然成为大家竞争的热点方向。在过去一年多的...

flink任务分process处理方案

  • 2017-05-03 16:45
  • 183KB
  • 下载

Flink流计算编程:双流中实现Inner Join、Left Join与Right Join

Flink coGroup
  • lmalds
  • lmalds
  • 2016-08-01 15:11
  • 1619

Matering Apache Flink

  • 2017-04-25 13:01
  • 6.00MB
  • 下载

Flink流处理之迭代任务

前面我们分析过Flink对迭代在流图中的特殊处理,使得迭代中的反馈环得以转化为普通的DAG模型。这一篇我们将剖析运行时的流处理迭代任务的执行机制。这里涉及到两个任务类:
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)