/*
 *
 * 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.hadoop.resourceestimator.solver.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.resourceestimator.common.config.ResourceEstimatorConfiguration;
import org.apache.hadoop.yarn.api.protocolrecords.ReservationSubmissionRequest;
import org.apache.hadoop.yarn.api.records.ReservationDefinition;
import org.apache.hadoop.yarn.api.records.ReservationId;
import org.apache.hadoop.yarn.api.records.ReservationRequest;
import org.apache.hadoop.yarn.api.records.ReservationRequestInterpreter;
import org.apache.hadoop.yarn.api.records.ReservationRequests;
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.server.resourcemanager.reservation.RLESparseResourceAllocation;

/**
 * Common functions shared by {@code Solver} (translate predicted resource
 * allocation into Hadoop's {@link ReservationSubmissionRequest}.
 */
public abstract class BaseSolver {
  /**
   * Used to generate {@link ReservationId}.
   */
  private static final Random RAND = new Random();

  /**
   * Translate the estimated {@link Resource} requirements of the pipeline to
   * Hadoop's {@link ReservationSubmissionRequest}.
   *
   * @param containerSpec     the {@link Resource} to be allocated to each
   *                          container;
   * @param containerRequests the predicted {@link Resource} to be allocated to
   *                          the job in each discrete time intervals;
   * @param config            configuration file for BaseSolver.
   * @return {@link ReservationSubmissionRequest} to be submitted to Hadoop to
   * make recurring resource reservation for the pipeline.
   */
  public final ReservationSubmissionRequest toRecurringRDL(
      final Resource containerSpec,
      final RLESparseResourceAllocation containerRequests,
      final Configuration config) {
    final int timeInterval =
        config.getInt(ResourceEstimatorConfiguration.TIME_INTERVAL_KEY, 5);
    long pipelineSubmissionTime = containerRequests.getEarliestStartTime();
    long pipelineFinishTime = containerRequests.getLatestNonNullTime();
    final long containerMemAlloc = containerSpec.getMemorySize();
    final long jobLen =
        (pipelineFinishTime - pipelineSubmissionTime) / timeInterval;
    List<ReservationRequest> reservationRequestList = new ArrayList<>();
    for (int i = 0; i < jobLen; i++) {
      // container spec, # of containers, concurrency, duration
      ReservationRequest reservationRequest = ReservationRequest
          .newInstance(containerSpec, (int) (
              containerRequests.getCapacityAtTime(i * timeInterval)
                  .getMemorySize() / containerMemAlloc), 1, timeInterval);
      reservationRequestList.add(reservationRequest);
    }
    ReservationRequests reservationRequests = ReservationRequests
        .newInstance(reservationRequestList,
            ReservationRequestInterpreter.R_ALL);
    ReservationDefinition reservationDefinition = ReservationDefinition
        .newInstance(pipelineSubmissionTime, pipelineFinishTime,
            reservationRequests, "LpSolver#toRecurringRDL");
    ReservationId reservationId =
        ReservationId.newInstance(RAND.nextLong(), RAND.nextLong());
    ReservationSubmissionRequest reservationSubmissionRequest =
        ReservationSubmissionRequest
            .newInstance(reservationDefinition, "resourceestimator",
                reservationId);
    return reservationSubmissionRequest;
  }
}
