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

org.apache.solr.handler.component.ActiveTasksListComponent Maven / Gradle / Ivy

There is a newer version: 9.7.0
Show newest version
/*
 * 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.solr.handler.component;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.solr.common.MapWriter;
import org.apache.solr.common.util.NamedList;

/** List the active tasks that can be cancelled */
public class ActiveTasksListComponent extends SearchComponent {
  public static final String COMPONENT_NAME = "activetaskslist";

  private boolean shouldProcess;

  @Override
  public void prepare(ResponseBuilder rb) throws IOException {
    if (rb.isTaskListRequest()) {
      shouldProcess = true;
    }
  }

  @Override
  public void process(ResponseBuilder rb) {
    if (!shouldProcess) {
      return;
    }

    if (rb.getTaskStatusCheckUUID() != null) {
      boolean isActiveOnThisShard =
          rb.req
              .getCore()
              .getCancellableQueryTracker()
              .isQueryIdActive(rb.getTaskStatusCheckUUID());

      rb.rsp.add("taskStatus", isActiveOnThisShard);
      return;
    }

    rb.rsp.add(
        "taskList",
        (MapWriter)
            ew -> {
              Iterator> iterator =
                  rb.req.getCore().getCancellableQueryTracker().getActiveQueriesGenerated();

              while (iterator.hasNext()) {
                Map.Entry entry = iterator.next();
                ew.put(entry.getKey(), entry.getValue());
              }
            });
  }

  @Override
  @SuppressWarnings("unchecked")
  public void handleResponses(ResponseBuilder rb, ShardRequest sreq) {
    if (!shouldProcess) {
      return;
    }

    NamedList resultList = new NamedList<>();

    for (ShardResponse r : sreq.responses) {

      if (rb.getTaskStatusCheckUUID() != null) {
        boolean isTaskActiveOnShard = r.getSolrResponse().getResponse().getBooleanArg("taskStatus");

        if (isTaskActiveOnShard) {
          rb.rsp
              .getValues()
              .add("taskStatus", "id:" + rb.getTaskStatusCheckUUID() + ", status: active");
          return;
        } else {
          continue;
        }
      }

      LinkedHashMap result =
          (LinkedHashMap) r.getSolrResponse().getResponse().get("taskList");

      Iterator> iterator = result.entrySet().iterator();

      while (iterator.hasNext()) {
        Map.Entry entry = iterator.next();

        resultList.add(entry.getKey(), entry.getValue());
      }
    }

    if (rb.getTaskStatusCheckUUID() != null) {
      // We got here with the specific taskID check being specified -- this means that the taskID
      // was not found in active tasks on any shard
      rb.rsp
          .getValues()
          .add("taskStatus", "id:" + rb.getTaskStatusCheckUUID() + ", status: inactive");
      return;
    }

    rb.rsp.getValues().add("taskList", resultList);
  }

  @Override
  public String getDescription() {
    return "Responsible for listing all active cancellable tasks and also supports checking the status of "
        + "a particular task";
  }

  @Override
  public Category getCategory() {
    return Category.OTHER;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy