Flink MapState的ConcurrentModificationException问题

直接上代码

import com.alibaba.fastjson.JSON;
import com.tc.flink.analysis.label.bean.output.ItemIdWithAction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.*;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;

import java.util.HashMap;
import java.util.Map;

public class CityItemIdClickedState extends RichMapFunction<Tuple2<ItemIdWithAction, Integer>, Tuple2<String, String>> {

    private transient MapState<ItemIdWithAction, Integer> map;

    public transient static final String CLICK_PREFIX_KEY = "cityClicked";

    public transient static final String CREATE_PREFIX_KEY = "cityCreated";

    @Override
    public void open(Configuration parameters) throws Exception {
        StateTtlConfig ttlConfig = StateTtlConfig.newBuilder(Time.minutes(60 * 2)).setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite).setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired).build();
        MapStateDescriptor<ItemIdWithAction, Integer> descriptor = new MapStateDescriptor<ItemIdWithAction, Integer>("paln_click_num", ItemIdWithAction.class, Integer.class);
        descriptor.enableTimeToLive(ttlConfig);
        map = getRuntimeContext().getMapState(descriptor);
        super.open(parameters);
    }

    @Override
    public Tuple2<String, String> map(Tuple2<ItemIdWithAction, Integer> keyValue) throws Exception {
        Integer num = keyValue.f1;
        ItemIdWithAction itemIdWithAction = keyValue.f0;
        if (num.equals(map.get(itemIdWithAction))) {
            return Tuple2.of(null, null);
        }
        map.put(itemIdWithAction, num);
        String prefixKey = itemIdWithAction.getAction().equals("click") ? CLICK_PREFIX_KEY : CREATE_PREFIX_KEY;
        String key = String.format("%s@%s@%s", prefixKey, itemIdWithAction.getStartCityId(), itemIdWithAction.getEndCityId());
        Map<String, Integer> valueMap = new HashMap<String, Integer>();
        for (ItemIdWithAction tmp : map.keys()) { //报错的异常点
            valueMap.put(tmp.getItemId(), map.get(tmp));
        }
        return Tuple2.of(key, JSON.toJSONString(valueMap));
    }
}

map是一条条处理,每次取所有MapState数据输出。
本地跑,集群跑都没问题,但当流量大MapState过大时候,就报如下错误,每天报错一两次,重启。

java.util.ConcurrentModificationException
	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1442)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1476)
	at java.util.HashMap$EntryIterator.next(HashMap.java:1474)
	at org.apache.flink.runtime.state.ttl.TtlMapState$EntriesIterator.hasNext(TtlMapState.java:161)
	at com.tc.flink.operator.state.CityItemIdClickedState.map(CityItemIdClickedState.java:42)
	at com.tc.flink.operator.state.CityItemIdClickedState.map(CityItemIdClickedState.java:14)
	at org.apache.flink.streaming.api.operators.StreamMap.processElement(StreamMap.java:41)
	at org.apache.flink.streaming.runtime.io.StreamInputProcessor.processInput(StreamInputProcessor.java:202)
	at org.apache.flink.streaming.runtime.tasks.OneInputStreamTask.run(OneInputStreamTask.java:105)
	at org.apache.flink.streaming.runtime.tasks.StreamTask.invoke(StreamTask.java:302)
	at org.apache.flink.runtime.taskmanager.Task.run(Task.java:711)
	at java.lang.Thread.run(Thread.java:748)

报错原因在于 for (ItemIdWithAction tmp : map.keys()) mapstate被并发修改了。
比较奇怪的是map-function单线程处理,为什么出现ConcurrentModificationException
查看TtlMapState源码
在这里插入图片描述
originalIterator::remove是剔除动作。有点类似redis,当再次访问时候,才会触发剔除(有可能产生内存泄漏)。
但是map-funciton是key-by下单线程操作,为什么会出现并发问题。
再看TtlStateFactory类
在这里插入图片描述

确实异步删除,所以mapstate过大的时候,就会出现这种问题。
修改代码

      Iterator< Map.Entry<ItemAction,Integer>> mapIterator= map.iterator();
      while(mapIterator.hasNext()){
            Map.Entry<ItemIdWithAction,Integer>  entry= mapIterator.next();
            valueMap.put(entry.getKey().getItemId(),entry.getValue());
        }

犯了低级错误,不过也细读了源码

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Flink中的MapState是一种用于存储键值对数据的状态类型。它可以在算子的运行过程中维护和访问状态数据。MapState是一个类似于Java的Map的接口,它提供了put、get和remove等操作来操作键值对数据。 在Flink中,MapState可以被用于在算子的状态中存储和管理中间结果,以及在流处理任务中维护一些需要随时间变化的状态。它可以被广泛应用于各种场景,比如窗口操作、连接操作等。 要使用MapState,你需要先创建一个MapStateDescriptor对象来描述状态的名称和类型。然后,通过调用RuntimeContext的getState方法来获取一个MapState对象。接下来,你就可以使用MapState对象进行put、get、remove等操作了。 下面是一个简单的示例代码,展示了如何在Flink中使用MapState: ``` import org.apache.flink.api.common.functions.RichFlatMapFunction; import org.apache.flink.api.common.state.MapState; import org.apache.flink.api.common.state.MapStateDescriptor; import org.apache.flink.util.Collector; public class MyFlatMapFunction extends RichFlatMapFunction<String, String> { private MapState<String, Integer> mapState; @Override public void open(Configuration parameters) throws Exception { MapStateDescriptor<String, Integer> mapStateDescriptor = new MapStateDescriptor<>( "myMapState", String.class, Integer.class ); mapState = getRuntimeContext().getMapState(mapStateDescriptor); } @Override public void flatMap(String value, Collector<String> out) throws Exception { // 将value作为键,将长度作为值存储到MapStatemapState.put(value, value.length()); // 从MapState中获取所有的键值对并输出 for (Map.Entry<String, Integer> entry : mapState.entries()) { out.collect(entry.getKey() + ": " + entry.getValue()); } } } ``` 在上面的示例中,我们在`open`方法中创建了一个`MapStateDescriptor`对象,并通过`getRuntimeContext().getMapState()`方法获取了一个`MapState`对象。在`flatMap`方法中,我们使用`mapState.put()`方法将`value`作为键,将其长度作为值存储到`MapState`中,并通过遍历`mapState.entries()`来获取所有的键值对并输出。 希望对你有帮助!如果有更多问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值