package com.pinterest.teletraan.worker;


import com.amazonaws.services.ec2.model.InstanceType;
import com.pinterest.arcee.dao.ReservedInstanceInfoDAO;
import com.pinterest.arcee.metrics.MetricSource;
import com.pinterest.deployservice.ServiceContext;

import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ReservedInstanceScheduler implements Runnable {
    private static Logger LOG = LoggerFactory.getLogger(ReservedInstanceScheduler.class);
    private ReservedInstanceInfoDAO reservedInstanceInfoDAO;
    private MetricSource metricSource;
    private static String FREEINSTANCE_METRIC_NAME = "running_instance.%s.count";
    private static int ALERT_THRESHOLD = 50;
    private static int THRESHOLD = 100;

    public ReservedInstanceScheduler(ServiceContext context) {
        reservedInstanceInfoDAO = context.getReservedInstanceInfoDAO();
        metricSource = context.getMetricSource();
    }


    public void scheduleReserveInstance(String instanceType) throws Exception {
        int reservedInstanceCount = reservedInstanceInfoDAO.getReservedInstanceCount(instanceType);
        int reservedRunningInstance = reservedInstanceInfoDAO.getRunningReservedInstanceCount(instanceType);
        int freeInstance = reservedInstanceCount - reservedRunningInstance;
        LOG.info(String.format("Reserved instance type: %s, reserved count: %d, running: %d, free %d", instanceType, reservedInstanceCount, reservedRunningInstance, freeInstance));
        metricSource.export(String.format(FREEINSTANCE_METRIC_NAME, instanceType), new HashMap<>(), (double)freeInstance, System.currentTimeMillis());
        if (freeInstance < ALERT_THRESHOLD) {
            //
        } else if (freeInstance < THRESHOLD) {
            // return instance
        } else {
            // TODO lend instance to the service
        }
    }

    @Override
    public void run() {
        try {
            List<InstanceType> instanceTypes = Arrays.asList(InstanceType.values());
            Collections.shuffle(instanceTypes);
            for (InstanceType type : instanceTypes) {
                LOG.info(String.format("Process instance type: %s", type.toString()));
                scheduleReserveInstance(type.toString());
            }

        } catch (Throwable t) {
            LOG.error("Failed to schedule instance.", t);
        }
    }
}

@@ -1418,16 +1418,32 @@
 nceType,
+%0A               
  reserve
@@ -1700,23 +1700,8 @@
 ) %7B%0A
-            //%0A
     
@@ -1777,59 +1777,298 @@
 ance
-%0A        %7D else %7B%0A            // TODO
+ to the pool%0A            int return_size = THRESHOLD - freeInstance;%0A            LOG.info(String.format(%22need to return %25d instances to the pool%22, return_size));%0A        %7D else %7B%0A            int lend_size =  freeInstance - THRESHOLD;%0A            LOG.info(String.format(%22can
  lend 
+%25d 
 instance
  to 
@@ -2055,32 +2055,33 @@
 lend %25d instance
+s
  to the service%0A
@@ -2079,16 +2079,31 @@
  service
+%22, lend_size));
 %0A       

Add log information to the scheduler