RocksDB 状态清理源码分析

rocksdb 状态创建流程在算子初始化的时候,获取状态将调用,org.apache.flink.runtime.state.AbstractKeyedStateBackend.getOrCreateKeyedState public <N, S extends State, V> S getOrCreateKeyedState( final TypeSerializer<N> namespaceSerializer, StateDescriptor<S, V&
摘要由CSDN通过智能技术生成

rocksdb 状态创建流程

在算子初始化的时候,获取状态将调用,org.apache.flink.runtime.state.AbstractKeyedStateBackend.getOrCreateKeyedState

	public <N, S extends State, V> S getOrCreateKeyedState(
			final TypeSerializer<N> namespaceSerializer,
			StateDescriptor<S, V> stateDescriptor) throws Exception {
		checkNotNull(namespaceSerializer, "Namespace serializer");
		checkNotNull(keySerializer, "State key serializer has not been configured in the config. " +
				"This operation cannot use partitioned state.");

        // 这个地方缓存了state。
		InternalKvState<K, ?, ?> kvState = keyValueStatesByName.get(stateDescriptor.getName());
		if (kvState == null) {
			if (!stateDescriptor.isSerializerInitialized()) {
				stateDescriptor.initializeSerializerUnlessSet(executionConfig);
			}
			// 调用TtlStateFactory。
			kvState = TtlStateFactory.createStateAndWrapWithTtlIfEnabled(
				namespaceSerializer, stateDescriptor, this, ttlTimeProvider);
			keyValueStatesByName.put(stateDescriptor.getName(), kvState);
			publishQueryableStateIfEnabled(stateDescriptor, kvState);
		}
		return (S) kvState;
	}

TtlStateFactory.createStateAndWrapWithTtlIfEnabled的代码如下:

 	public static <K, N, SV, TTLSV, S extends State, IS extends S> IS createStateAndWrapWithTtlIfEnabled(
 		TypeSerializer<N> namespaceSerializer,
 		StateDescriptor<S, SV> stateDesc,
 		KeyedStateBackend<K> stateBackend,
 		TtlTimeProvider timeProvider) throws Exception {
 		Preconditions.checkNotNull(namespaceSerializer);
 		Preconditions.checkNotNull(stateDesc);
 		Preconditions.checkNotNull(stateBackend);
 		Preconditions.checkNotNull(timeProvider);
 		// 如果开启了状态清理,使用TtlStateFactory创建state,这个会将状态进行一层包装,会添加一个时间戳字段,用来保存读写时间。
 		// 这个时间是状态清理、是否过期的依据
 		// 如果没有开启状态清理,直接使用stateBackend创建状态。
 		return  stateDesc.getTtlConfig().isEnabled() ?
 			new TtlStateFactory<K, N, SV, TTLSV, S, IS>(
 				namespaceSerializer, stateDesc, stateBackend, timeProvider)
 				.createState() :
 			stateBackend.createInternalState(namespaceSerializer, stateDesc);
 	}

org.apache.flink.runtime.state.ttl.TtlStateFactory.createState:


    #这里使用了工厂模式来创建状态,工程类定义如下:
	private IS createState() throws Exception {
		SupplierWithException<IS, Exception> stateFactory = stateFactories.get(stateDesc.getClass());
		if (stateFactory == null) {
			String message = String.format("State %s is not supported by %s",
				stateDesc.getClass(), TtlStateFactory.class);
			throw new FlinkRuntimeException(message);
		}
		IS state = stateFactory.get();
		if (incrementalCleanup != null) {
			incrementalCleanup.setTtlState((AbstractTtlState<K, N, ?, TTLSV, ?>) state);
		}
		return state;
	}
	
定义的工厂类:
	private Map<Class<? extends StateDescriptor>, SupplierWithException<IS, Exception>> createStateFactories() {
		return Stream.of(
			Tuple2.of(ValueStateDescriptor.class, (SupplierWithException<IS, Exception>) this::createValueState),
			Tuple2.of(ListStateDescriptor.class, (SupplierWithException<IS, Exception>) this::createListState),
			Tuple2.of(MapStateDescriptor.class, (SupplierWithException<IS
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值