All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.cloud.dataflow.sdk.util.DirectModeExecutionContext Maven / Gradle / Ivy

Go to download

Google Cloud Dataflow Java SDK provides a simple, Java-based interface for processing virtually any size data using Google cloud resources. This artifact includes entire Dataflow Java SDK.

There is a newer version: 2.5.0
Show newest version
/*
 * Copyright (C) 2015 Google Inc.
 *
 * Licensed 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 com.google.cloud.dataflow.sdk.util;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.cloud.dataflow.sdk.runners.DirectPipelineRunner.ValueWithMetadata;
import com.google.cloud.dataflow.sdk.util.common.worker.StateSampler;
import com.google.cloud.dataflow.sdk.util.state.InMemoryStateInternals;
import com.google.cloud.dataflow.sdk.util.state.StateInternals;
import com.google.cloud.dataflow.sdk.values.TupleTag;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * {@link ExecutionContext} for use in direct mode.
 */
public class DirectModeExecutionContext
    extends BaseExecutionContext {

  private Object key;
  private List> output = Lists.newArrayList();
  private Map, List>> sideOutputs = Maps.newHashMap();

  protected DirectModeExecutionContext() {}

  public static DirectModeExecutionContext create() {
    return new DirectModeExecutionContext();
  }

  @Override
  protected StepContext createStepContext(
      String stepName, String transformName, StateSampler stateSampler) {
    return new StepContext(this, stepName, transformName);
  }

  public Object getKey() {
    return key;
  }

  public void setKey(Object newKey) {
    // The direct mode runner may reorder elements, so we need to keep
    // around the state used for each key.
    for (ExecutionContext.StepContext stepContext : getAllStepContexts()) {
      ((StepContext) stepContext).switchKey(newKey);
    }
    key = newKey;
  }

  @Override
  public void noteOutput(WindowedValue outputElem) {
    output.add(ValueWithMetadata.of(outputElem).withKey(getKey()));
  }

  @Override
  public void noteSideOutput(TupleTag tag, WindowedValue outputElem) {
    List> output = sideOutputs.get(tag);
    if (output == null) {
      output = Lists.newArrayList();
      sideOutputs.put(tag, output);
    }
    output.add(ValueWithMetadata.of(outputElem).withKey(getKey()));
  }

  public  List> getOutput(@SuppressWarnings("unused") TupleTag tag) {
    @SuppressWarnings({"unchecked", "rawtypes"}) // Cast not expressible without rawtypes
    List> typedOutput = (List) output;
    return typedOutput;
  }

  public  List> getSideOutput(TupleTag tag) {
    if (sideOutputs.containsKey(tag)) {
      @SuppressWarnings({"unchecked", "rawtypes"}) // Cast not expressible without rawtypes
      List> typedOutput = (List) sideOutputs.get(tag);
      return typedOutput;
    } else {
      return Lists.newArrayList();
    }
  }

  /**
   * {@link ExecutionContext.StepContext} used in direct mode.
   */
  public static class StepContext extends BaseExecutionContext.StepContext {

    /** A map from each key to the state associated with it. */
    private final Map> stateInternals = Maps.newHashMap();
    private InMemoryStateInternals currentStateInternals = null;

    private StepContext(ExecutionContext executionContext, String stepName, String transformName) {
      super(executionContext, stepName, transformName);
      switchKey(null);
    }

    public void switchKey(Object newKey) {
      currentStateInternals = stateInternals.get(newKey);
      if (currentStateInternals == null) {
        currentStateInternals = InMemoryStateInternals.forKey(newKey);
        stateInternals.put(newKey, currentStateInternals);
      }
    }

    @Override
    public StateInternals stateInternals() {
      return checkNotNull(currentStateInternals);
    }

    @Override
    public TimerInternals timerInternals() {
      throw new UnsupportedOperationException("Direct mode cannot return timerInternals");
    }
  }
}