flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gyfora <...@git.apache.org>
Subject [GitHub] flink pull request: New operator state interfaces
Date Mon, 22 Jun 2015 09:04:53 GMT
Github user gyfora commented on a diff in the pull request:

    https://github.com/apache/flink/pull/747#discussion_r32916035
  
    --- Diff: flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/state/PartitionedStreamOperatorState.java
---
    @@ -0,0 +1,134 @@
    +/*
    + * 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.streaming.api.state;
    +
    +import java.io.Serializable;
    +import java.util.Map;
    +
    +import org.apache.flink.api.common.state.OperatorState;
    +import org.apache.flink.api.common.state.StateCheckpointer;
    +import org.apache.flink.api.java.functions.KeySelector;
    +import org.apache.flink.runtime.state.PartitionedStateStore;
    +import org.apache.flink.runtime.state.StateHandle;
    +import org.apache.flink.runtime.state.StateHandleProvider;
    +import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
    +
    +/**
    + * Implementation of the {@link OperatorState} interface for partitioned user
    + * states. It provides methods for checkpointing and restoring partitioned
    + * operator states upon failure.
    + * 
    + * @param <IN>
    + *            Input type of the underlying {@link OneInputStreamOperator}
    + * @param <S>
    + *            Type of the underlying {@link OperatorState}.
    + * @param <C>
    + *            Type of the state snapshot.
    + */
    +public class PartitionedStreamOperatorState<IN, S, C extends Serializable> extends
    +		StreamOperatorState<S, C> {
    +
    +	// KeySelector for getting the state partition key for each input
    +	private final KeySelector<IN, Serializable> keySelector;
    +
    +	private final PartitionedStateStore<S, C> stateStore;
    +	
    +	private S defaultState;
    +
    +	// The currently processed input, used to extract the appropriate key
    +	private IN currentInput;
    +
    +	public PartitionedStreamOperatorState(StateCheckpointer<S, C> checkpointer,
    +			StateHandleProvider<C> provider, KeySelector<IN, Serializable> keySelector)
{
    +		super(checkpointer, provider);
    +		this.keySelector = keySelector;
    +		this.stateStore = new EagerStateStore<S, C>(checkpointer, provider);
    +	}
    +	
    +	@SuppressWarnings("unchecked")
    +	public PartitionedStreamOperatorState(StateHandleProvider<C> provider,
    +			KeySelector<IN, Serializable> keySelector) {
    +		this((StateCheckpointer<S, C>) new BasicCheckpointer(), provider, keySelector);
    +	}
    +
    +	@Override
    +	public S getState() {
    +		if (currentInput == null) {
    +			return null;
    +		} else {
    +			try {
    +				Serializable key = keySelector.getKey(currentInput);
    +				if(stateStore.containsKey(key)){
    +					return stateStore.getStateForKey(key);
    +				}else{
    +					return defaultState;
    +				}
    +			} catch (Exception e) {
    +				throw new RuntimeException(e);
    +			}
    +		}
    +	}
    +
    +	@Override
    +	public void updateState(S state) {
    +		if (state == null) {
    +			throw new RuntimeException("Cannot set state to null.");
    +		}
    +		if (currentInput == null) {
    +			throw new RuntimeException("Need a valid input for updating a state.");
    --- End diff --
    
    good point


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message