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模型的上下文中,所有的活动实体都被...
  • yanghua_kobe
  • yanghua_kobe
  • 2016年04月14日 23:48
  • 9999

Flink运行时之客户端提交作业图-上

客户端提交作业图作业图(JobGraph)是Flink的运行时所能理解的作业表示,无论程序通过是DataStream还是DataSet API编写的,它们的JobGraph提交给JobManager以...
  • yanghua_kobe
  • yanghua_kobe
  • 2017年03月31日 21:13
  • 1411

Flink流处理之迭代案例

当前Flink将迭代的重心集中在批处理上,之前我们谈及了批量迭代和增量迭代主要是针对批处理(DataSet)API而言的,并且Flink为批处理中的迭代提供了针对性的优化。但是对于流处理(DataSt...
  • yanghua_kobe
  • yanghua_kobe
  • 2016年11月29日 20:14
  • 4897

Akka在Flink中的使用剖析

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

flink最佳实践ppt

  • 2017年09月28日 20:28
  • 1.61MB
  • 下载

Learning Apache Flink (True PDF)-Packt Publishing(2017)【E文】

  • 2017年08月21日 18:21
  • 3.97MB
  • 下载

flink任务分process处理方案

  • 2017年05月03日 16:45
  • 183KB
  • 下载

Matering Apache Flink

  • 2017年04月25日 13:01
  • 6MB
  • 下载

Flink入门教程

  • 2017年04月15日 17:20
  • 246KB
  • 下载

Flink和Spark比较

  • 2016年06月20日 18:26
  • 354KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:flink akka
举报原因:
原因补充:

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