org.apache.flink.runtime.state.KeyedStateBackend Java Examples

The following examples show how to use org.apache.flink.runtime.state.KeyedStateBackend. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlinkStateInternalsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
public static KeyedStateBackend<ByteBuffer> createStateBackend() throws Exception {
  MemoryStateBackend backend = new MemoryStateBackend();

  AbstractKeyedStateBackend<ByteBuffer> keyedStateBackend =
      backend.createKeyedStateBackend(
          new DummyEnvironment("test", 1, 0),
          new JobID(),
          "test_op",
          new GenericTypeInfo<>(ByteBuffer.class).createSerializer(new ExecutionConfig()),
          2,
          new KeyGroupRange(0, 1),
          new KvStateRegistry().createTaskRegistry(new JobID(), new JobVertexID()),
          TtlTimeProvider.DEFAULT,
          null,
          Collections.emptyList(),
          new CloseableRegistry());

  changeKey(keyedStateBackend);

  return keyedStateBackend;
}
 
Example #2
Source File: StreamingRuntimeContextTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static AbstractStreamOperator<?> createDescriptorCapturingMockOp(
		final AtomicReference<Object> ref, final ExecutionConfig config) throws Exception {

	AbstractStreamOperator<?> operatorMock = mock(AbstractStreamOperator.class);

	KeyedStateBackend keyedStateBackend = mock(KeyedStateBackend.class);

	DefaultKeyedStateStore keyedStateStore = new DefaultKeyedStateStore(keyedStateBackend, config);

	when(operatorMock.getExecutionConfig()).thenReturn(config);

	doAnswer(new Answer<Object>() {

		@Override
		public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
			ref.set(invocationOnMock.getArguments()[2]);
			return null;
		}
	}).when(keyedStateBackend).getPartitionedState(Matchers.any(), any(TypeSerializer.class), any(StateDescriptor.class));

	when(operatorMock.getKeyedStateStore()).thenReturn(keyedStateStore);
	when(operatorMock.getOperatorID()).thenReturn(new OperatorID());

	return operatorMock;
}
 
Example #3
Source File: KeyedStateReaderOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
private Context(KeyedStateBackend<K> keyedStateBackend, InternalTimerService<VoidNamespace> timerService) throws Exception {
	eventTimers = keyedStateBackend.getPartitionedState(
		USER_TIMERS_NAME,
		StringSerializer.INSTANCE,
		new ListStateDescriptor<>(EVENT_TIMER_STATE, Types.LONG));

	timerService.forEachEventTimeTimer((namespace, timer) -> {
		if (namespace.equals(VoidNamespace.INSTANCE)) {
			eventTimers.add(timer);
		}
	});

	procTimers = keyedStateBackend.getPartitionedState(
		USER_TIMERS_NAME,
		StringSerializer.INSTANCE,
		new ListStateDescriptor<>(PROC_TIMER_STATE, Types.LONG));

	timerService.forEachProcessingTimeTimer((namespace, timer) -> {
		if (namespace.equals(VoidNamespace.INSTANCE)) {
			procTimers.add(timer);
		}
	});
}
 
Example #4
Source File: FlinkStateInternals.java    From beam with Apache License 2.0 6 votes vote down vote up
FlinkCombiningStateWithContext(
    KeyedStateBackend<ByteBuffer> flinkStateBackend,
    String stateId,
    CombineWithContext.CombineFnWithContext<InputT, AccumT, OutputT> combineFn,
    StateNamespace namespace,
    Coder<AccumT> accumCoder,
    CombineWithContext.Context context) {

  this.namespace = namespace;
  this.stateId = stateId;
  this.combineFn = combineFn;
  this.flinkStateBackend = flinkStateBackend;
  this.context = context;

  flinkStateDescriptor =
      new ValueStateDescriptor<>(stateId, new CoderTypeSerializer<>(accumCoder));
}
 
Example #5
Source File: StreamingRuntimeContextTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private static AbstractStreamOperator<?> createDescriptorCapturingMockOp(
		final AtomicReference<Object> ref, final ExecutionConfig config) throws Exception {

	AbstractStreamOperator<?> operatorMock = mock(AbstractStreamOperator.class);

	KeyedStateBackend keyedStateBackend = mock(KeyedStateBackend.class);

	DefaultKeyedStateStore keyedStateStore = new DefaultKeyedStateStore(keyedStateBackend, config);

	when(operatorMock.getExecutionConfig()).thenReturn(config);

	doAnswer(new Answer<Object>() {

		@Override
		public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
			ref.set(invocationOnMock.getArguments()[2]);
			return null;
		}
	}).when(keyedStateBackend).getPartitionedState(Matchers.any(), any(TypeSerializer.class), any(StateDescriptor.class));

	when(operatorMock.getKeyedStateStore()).thenReturn(keyedStateStore);
	when(operatorMock.getOperatorID()).thenReturn(new OperatorID());

	return operatorMock;
}
 
Example #6
Source File: AsyncOperationFailureNotifier.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
static void fireExpiredAsyncOperations(
    MapStateDescriptor<Long, Message> asyncOperationStateDescriptor,
    Reductions reductions,
    MapState<Long, Message> asyncOperationState,
    KeyedStateBackend<String> keyedStateBackend)
    throws Exception {

  AsyncOperationFailureNotifier asyncOperationFailureNotifier =
      new AsyncOperationFailureNotifier(reductions, asyncOperationState);

  keyedStateBackend.applyToAllKeys(
      VoidNamespace.get(),
      VoidNamespaceSerializer.INSTANCE,
      asyncOperationStateDescriptor,
      asyncOperationFailureNotifier);

  if (asyncOperationFailureNotifier.enqueued()) {
    reductions.processEnvelopes();
  }
}
 
Example #7
Source File: BufferingDoFnRunner.java    From beam with Apache License 2.0 6 votes vote down vote up
public static <InputT, OutputT> BufferingDoFnRunner<InputT, OutputT> create(
    DoFnRunner<InputT, OutputT> doFnRunner,
    String stateName,
    org.apache.beam.sdk.coders.Coder windowedInputCoder,
    org.apache.beam.sdk.coders.Coder windowCoder,
    OperatorStateBackend operatorStateBackend,
    @Nullable KeyedStateBackend<Object> keyedStateBackend,
    int maxConcurrentCheckpoints)
    throws Exception {
  return new BufferingDoFnRunner<>(
      doFnRunner,
      stateName,
      windowedInputCoder,
      windowCoder,
      operatorStateBackend,
      keyedStateBackend,
      maxConcurrentCheckpoints);
}
 
Example #8
Source File: TriggerTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public TestOnMergeContext(
		K key,
		W targetWindow,
		Collection<W> mergedWindows,
		InternalTimerService<W> timerService,
		KeyedStateBackend<Integer> stateBackend,
		TypeSerializer<W> windowSerializer) {
	super(key, targetWindow, timerService, stateBackend, windowSerializer);

	this.mergedWindows = mergedWindows;
}
 
Example #9
Source File: StateReaderOperator.java    From flink with Apache License 2.0 5 votes vote down vote up
public final void setup(
	ExecutionConfig executionConfig,
	KeyedStateBackend<KEY> keyKeyedStateBackend,
	InternalTimeServiceManager<KEY> timerServiceManager,
	SavepointRuntimeContext ctx) {

	this.executionConfig = executionConfig;
	this.keyedStateBackend = keyKeyedStateBackend;
	this.timerServiceManager = timerServiceManager;
	this.keySerializer = keyType.createSerializer(executionConfig);

	FunctionUtils.setFunctionRuntimeContext(function, ctx);
}
 
Example #10
Source File: StateBackendBenchmarkUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static KeyedStateBackend<Long> createKeyedStateBackend(StateBackendType backendType) throws IOException {
	switch (backendType) {
		case HEAP:
			rootDir = prepareDirectory(rootDirName, null);
			return createHeapKeyedStateBackend(rootDir);
		case ROCKSDB:
			rootDir = prepareDirectory(rootDirName, null);
			return createRocksDBKeyedStateBackend(rootDir);
		default:
			throw new IllegalArgumentException("Unknown backend type: " + backendType);
	}
}
 
Example #11
Source File: PerWindowStateDataViewStore.java    From flink with Apache License 2.0 5 votes vote down vote up
public PerWindowStateDataViewStore(
		KeyedStateBackend<?> keyedStateBackend,
		TypeSerializer<?> windowSerializer,
		RuntimeContext runtimeContext) {
	this.keyedStateBackend = keyedStateBackend;
	this.windowSerializer = windowSerializer;
	this.ctx = runtimeContext;
}
 
Example #12
Source File: FlinkStateInternalsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected StateInternals createStateInternals() {
  try {
    KeyedStateBackend<ByteBuffer> keyedStateBackend = createStateBackend();
    return new FlinkStateInternals<>(keyedStateBackend, StringUtf8Coder.of());
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #13
Source File: StateBackendBenchmarkUtilsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompactState() throws Exception {
	KeyedStateBackend<Long> backend = createKeyedStateBackend(backendType);
	ListState<Long> listState = getListState(backend, listStateDescriptor);
	for (long i = 0; i < 10; i++) {
		backend.setCurrentKey(i);
		listState.add(i);
	}
	if (backend instanceof RocksDBKeyedStateBackend) {
		RocksDBKeyedStateBackend<Long> rocksDBKeyedStateBackend = (RocksDBKeyedStateBackend<Long>) backend;
		compactState(rocksDBKeyedStateBackend, listStateDescriptor);
	}
	cleanUp(backend);
}
 
Example #14
Source File: DoFnOperator.java    From beam with Apache License 2.0 5 votes vote down vote up
private void populateOutputTimestampQueue() {
  Preconditions.checkState(
      outputTimestampQueue.isEmpty(),
      "Output timestamp queue should be empty when recomputing the minimum output timestamp across all timers.");
  final KeyedStateBackend<Object> keyedStateBackend = getKeyedStateBackend();
  final Object currentKey = keyedStateBackend.getCurrentKey();
  try (Stream<Object> keys =
      keyedStateBackend.getKeys(PENDING_TIMERS_STATE_NAME, VoidNamespace.INSTANCE)) {
    keys.forEach(
        key -> {
          keyedStateBackend.setCurrentKey(key);
          try {
            for (TimerData timerData : pendingTimersById.values()) {
              if (timerData.getDomain() == TimeDomain.EVENT_TIME) {
                long outputTimeStampMs = timerData.getOutputTimestamp().getMillis();
                if (timerUsesOutputTimestamp(timerData)) {
                  outputTimestampQueue.add(outputTimeStampMs);
                }
              }
            }
          } catch (Exception e) {
            throw new RuntimeException(
                "Exception while reading set of timers for key: " + key, e);
          }
        });
  } finally {
    if (currentKey != null) {
      keyedStateBackend.setCurrentKey(currentKey);
    }
  }
}
 
Example #15
Source File: FlinkStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkMapState(
    KeyedStateBackend<ByteBuffer> flinkStateBackend,
    String stateId,
    StateNamespace namespace,
    Coder<KeyT> mapKeyCoder,
    Coder<ValueT> mapValueCoder) {
  this.namespace = namespace;
  this.stateId = stateId;
  this.flinkStateBackend = flinkStateBackend;
  this.flinkStateDescriptor =
      new MapStateDescriptor<>(
          stateId,
          new CoderTypeSerializer<>(mapKeyCoder),
          new CoderTypeSerializer<>(mapValueCoder));
}
 
Example #16
Source File: FlinkStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkBagState(
    KeyedStateBackend<ByteBuffer> flinkStateBackend,
    String stateId,
    StateNamespace namespace,
    Coder<T> coder) {

  this.namespace = namespace;
  this.stateId = stateId;
  this.flinkStateBackend = flinkStateBackend;
  this.storesVoidValues = coder instanceof VoidCoder;
  this.flinkStateDescriptor =
      new ListStateDescriptor<>(stateId, new CoderTypeSerializer<>(coder));
}
 
Example #17
Source File: FlinkStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkCombiningState(
    KeyedStateBackend<ByteBuffer> flinkStateBackend,
    String stateId,
    Combine.CombineFn<InputT, AccumT, OutputT> combineFn,
    StateNamespace namespace,
    Coder<AccumT> accumCoder) {

  this.namespace = namespace;
  this.stateId = stateId;
  this.combineFn = combineFn;
  this.flinkStateBackend = flinkStateBackend;

  flinkStateDescriptor =
      new ValueStateDescriptor<>(stateId, new CoderTypeSerializer<>(accumCoder));
}
 
Example #18
Source File: FlinkStateInternals.java    From beam with Apache License 2.0 5 votes vote down vote up
FlinkValueState(
    KeyedStateBackend<ByteBuffer> flinkStateBackend,
    String stateId,
    StateNamespace namespace,
    Coder<T> coder) {

  this.namespace = namespace;
  this.stateId = stateId;
  this.flinkStateBackend = flinkStateBackend;

  flinkStateDescriptor = new ValueStateDescriptor<>(stateId, new CoderTypeSerializer<>(coder));
}
 
Example #19
Source File: KeyedTwoInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public int numKeyedStateEntries() {
	AbstractStreamOperator<?> abstractStreamOperator = (AbstractStreamOperator<?>) operator;
	KeyedStateBackend<Object> keyedStateBackend = abstractStreamOperator.getKeyedStateBackend();
	if (keyedStateBackend instanceof HeapKeyedStateBackend) {
		return ((HeapKeyedStateBackend) keyedStateBackend).numKeyValueStateEntries();
	} else {
		throw new UnsupportedOperationException();
	}
}
 
Example #20
Source File: BufferingDoFnRunner.java    From beam with Apache License 2.0 5 votes vote down vote up
private BufferingDoFnRunner(
    DoFnRunner<InputT, OutputT> underlying,
    String stateName,
    org.apache.beam.sdk.coders.Coder inputCoder,
    org.apache.beam.sdk.coders.Coder windowCoder,
    OperatorStateBackend operatorStateBackend,
    @Nullable KeyedStateBackend keyedStateBackend,
    int maxConcurrentCheckpoints)
    throws Exception {
  Preconditions.checkArgument(
      maxConcurrentCheckpoints > 0 && maxConcurrentCheckpoints < Short.MAX_VALUE,
      "Maximum number of concurrent checkpoints not within the bounds of 0 and %s",
      Short.MAX_VALUE);

  this.underlying = underlying;
  this.notYetAcknowledgedSnapshots =
      operatorStateBackend.getUnionListState(
          new ListStateDescriptor<>("notYetAcknowledgedSnapshots", CheckpointIdentifier.class));
  this.bufferingElementsHandlerFactory =
      (stateId) -> {
        ListStateDescriptor<BufferedElement> stateDescriptor =
            new ListStateDescriptor<>(
                stateName + stateId,
                new CoderTypeSerializer<>(
                    new BufferedElements.Coder(inputCoder, windowCoder, null)));
        if (keyedStateBackend != null) {
          return KeyedBufferingElementsHandler.create(keyedStateBackend, stateDescriptor);
        } else {
          return NonKeyedBufferingElementsHandler.create(
              operatorStateBackend.getListState(stateDescriptor));
        }
      };
  this.numCheckpointBuffers = initializeState(maxConcurrentCheckpoints);
  this.currentBufferingElementsHandler =
      bufferingElementsHandlerFactory.get(rotateAndGetStateIndex());
}
 
Example #21
Source File: StateBackendBenchmarkUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static <K, S extends State, T> void applyToAllKeys(
	KeyedStateBackend<K> backend,
	final StateDescriptor<S, T> stateDescriptor,
	final KeyedStateFunction<K, S> function) throws Exception {
	backend.applyToAllKeys(
		VoidNamespace.INSTANCE,
		VoidNamespaceSerializer.INSTANCE,
		stateDescriptor,
		function);
}
 
Example #22
Source File: KeyedOneInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public <N> int numKeyedStateEntries(N namespace) {
	AbstractStreamOperator<?> abstractStreamOperator = (AbstractStreamOperator<?>) operator;
	KeyedStateBackend<Object> keyedStateBackend = abstractStreamOperator.getKeyedStateBackend();
	if (keyedStateBackend instanceof HeapKeyedStateBackend) {
		return ((HeapKeyedStateBackend) keyedStateBackend).numKeyValueStateEntries(namespace);
	} else {
		throw new UnsupportedOperationException();
	}
}
 
Example #23
Source File: KeyedOneInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public int numKeyedStateEntries() {
	AbstractStreamOperator<?> abstractStreamOperator = (AbstractStreamOperator<?>) operator;
	KeyedStateBackend<Object> keyedStateBackend = abstractStreamOperator.getKeyedStateBackend();
	if (keyedStateBackend instanceof HeapKeyedStateBackend) {
		return ((HeapKeyedStateBackend) keyedStateBackend).numKeyValueStateEntries();
	} else {
		throw new UnsupportedOperationException();
	}
}
 
Example #24
Source File: FlinkState.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
@Inject
public FlinkState(
    @Label("runtime-context") RuntimeContext runtimeContext,
    @Label("keyed-state-backend") KeyedStateBackend<Object> keyedStateBackend,
    DynamicallyRegisteredTypes types) {

  this.runtimeContext = Objects.requireNonNull(runtimeContext);
  this.keyedStateBackend = Objects.requireNonNull(keyedStateBackend);
  this.types = Objects.requireNonNull(types);
}
 
Example #25
Source File: FunctionGroupOperator.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
private InternalListState<String, Long, Message> delayedMessagesBufferState(
    ListStateDescriptor<Message> delayedMessageStateDescriptor) {
  try {
    KeyedStateBackend<String> keyedStateBackend = getKeyedStateBackend();
    return (InternalListState<String, Long, Message>)
        keyedStateBackend.getOrCreateKeyedState(
            LongSerializer.INSTANCE, delayedMessageStateDescriptor);
  } catch (Exception e) {
    throw new RuntimeException("Error registered Flink state for delayed messages buffer.", e);
  }
}
 
Example #26
Source File: FlinkState.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@Inject
public FlinkState(
    @Label("runtime-context") RuntimeContext runtimeContext,
    @Label("keyed-state-backend") KeyedStateBackend<Object> keyedStateBackend,
    DynamicallyRegisteredTypes types) {

  this.runtimeContext = Objects.requireNonNull(runtimeContext);
  this.keyedStateBackend = Objects.requireNonNull(keyedStateBackend);
  this.types = Objects.requireNonNull(types);
}
 
Example #27
Source File: MultiplexedState.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
@Inject
public MultiplexedState(
    @Label("runtime-context") RuntimeContext runtimeContext,
    @Label("keyed-state-backend") KeyedStateBackend<Object> keyedStateBackend,
    DynamicallyRegisteredTypes types) {

  this.keyedStateBackend = Objects.requireNonNull(keyedStateBackend);
  this.types = Objects.requireNonNull(types);
  this.sharedMapStateHandle = createSharedMapState(runtimeContext);
  this.executionConfiguration = Objects.requireNonNull(runtimeContext.getExecutionConfig());
}
 
Example #28
Source File: KeyedOneInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public <N> int numKeyedStateEntries(N namespace) {
	AbstractStreamOperator<?> abstractStreamOperator = (AbstractStreamOperator<?>) operator;
	KeyedStateBackend<Object> keyedStateBackend = abstractStreamOperator.getKeyedStateBackend();
	if (keyedStateBackend instanceof HeapKeyedStateBackend) {
		return ((HeapKeyedStateBackend) keyedStateBackend).numKeyValueStateEntries(namespace);
	} else {
		throw new UnsupportedOperationException();
	}
}
 
Example #29
Source File: KeyedOneInputStreamOperatorTestHarness.java    From flink with Apache License 2.0 5 votes vote down vote up
public int numKeyedStateEntries() {
	AbstractStreamOperator<?> abstractStreamOperator = (AbstractStreamOperator<?>) operator;
	KeyedStateBackend<Object> keyedStateBackend = abstractStreamOperator.getKeyedStateBackend();
	if (keyedStateBackend instanceof HeapKeyedStateBackend) {
		return ((HeapKeyedStateBackend) keyedStateBackend).numKeyValueStateEntries();
	} else {
		throw new UnsupportedOperationException();
	}
}
 
Example #30
Source File: TtlStateFactory.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private TtlStateFactory(
	@Nonnull TypeSerializer<N> namespaceSerializer,
	@Nonnull StateDescriptor<S, SV> stateDesc,
	@Nonnull KeyedStateBackend<K> stateBackend,
	@Nonnull TtlTimeProvider timeProvider) {
	this.namespaceSerializer = namespaceSerializer;
	this.stateDesc = stateDesc;
	this.stateBackend = stateBackend;
	this.ttlConfig = stateDesc.getTtlConfig();
	this.timeProvider = timeProvider;
	this.ttl = ttlConfig.getTtl().toMilliseconds();
	this.stateFactories = createStateFactories();
	this.incrementalCleanup = getTtlIncrementalCleanup();
}