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

io.druid.indexing.common.task.AbstractTask Maven / Gradle / Ivy

/*
 * Druid - a distributed column store.
 * Copyright 2012 - 2015 Metamarkets Group 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 io.druid.indexing.common.task;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import io.druid.indexing.common.TaskLock;
import io.druid.indexing.common.TaskStatus;
import io.druid.indexing.common.TaskToolbox;
import io.druid.indexing.common.actions.LockListAction;
import io.druid.query.Query;
import io.druid.query.QueryRunner;
import java.util.Map;
import org.joda.time.DateTime;
import org.joda.time.Interval;

import java.io.IOException;

public abstract class AbstractTask implements Task
{
  private static final Joiner ID_JOINER = Joiner.on("_");

  @JsonIgnore
  private final String id;

  @JsonIgnore
  private final String groupId;

  @JsonIgnore
  private final TaskResource taskResource;

  @JsonIgnore
  private final String dataSource;

  private final Map context;

  protected AbstractTask(String id, String dataSource, Map context)
  {
    this(id, id, new TaskResource(id, 1), dataSource, context);
  }

  protected AbstractTask(String id, String groupId, String dataSource, Map context)
  {
    this(id, groupId, new TaskResource(id, 1), dataSource, context);
  }

  protected AbstractTask(String id, String groupId, TaskResource taskResource, String dataSource, Map context)
  {
    this.id = Preconditions.checkNotNull(id, "id");
    this.groupId = Preconditions.checkNotNull(groupId, "groupId");
    this.taskResource = Preconditions.checkNotNull(taskResource, "resource");
    this.dataSource = Preconditions.checkNotNull(dataSource, "dataSource");
    this.context = context;
  }

  public static String makeId(String id, final String typeName, String dataSource, Interval interval)
  {
    return id != null ? id : joinId(
        typeName,
        dataSource,
        interval.getStart(),
        interval.getEnd(),
        new DateTime().toString()
    );
  }

  @JsonProperty
  @Override
  public String getId()
  {
    return id;
  }

  @JsonProperty
  @Override
  public String getGroupId()
  {
    return groupId;
  }

  @JsonProperty("resource")
  @Override
  public TaskResource getTaskResource()
  {
    return taskResource;
  }

  @Override
  public String getNodeType()
  {
    return null;
  }

  @JsonProperty
  @Override
  public String getDataSource()
  {
    return dataSource;
  }

  @Override
  public  QueryRunner getQueryRunner(Query query)
  {
    return null;
  }

  @Override
  public String getClasspathPrefix()
  {
    return null;
  }

  @Override
  public String toString()
  {
    return Objects.toStringHelper(this)
                  .add("id", id)
                  .add("type", getType())
                  .add("dataSource", dataSource)
                  .toString();
  }

  /**
   * Start helper methods
   *
   * @param objects objects to join
   * @return string of joined objects
   */
  public static String joinId(Object... objects)
  {
    return ID_JOINER.join(objects);
  }

  public TaskStatus success()
  {
    return TaskStatus.success(getId());
  }

  @Override
  public boolean equals(Object o)
  {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    AbstractTask that = (AbstractTask) o;

    if (!id.equals(that.id)) {
      return false;
    }

    return true;
  }

  @Override
  public int hashCode()
  {
    return id.hashCode();
  }

  protected Iterable getTaskLocks(TaskToolbox toolbox) throws IOException
  {
    return toolbox.getTaskActionClient().submit(new LockListAction());
  }

  @Override
  @JsonProperty
  public Map getContext()
  {
    return context;
  }

  @Override
  public Object getContextValue(String key)
  {
    return context == null ? null : context.get(key);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy