<commit_before>/**
 * Copyright 2016 Pinterest, 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.pinterest.arcee.aws;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.*;

import com.pinterest.arcee.bean.GroupBean;
import com.pinterest.deployservice.bean.HostBean;
import com.pinterest.arcee.dao.HostInfoDAO;
import com.pinterest.deployservice.common.DeployInternalException;

import com.pinterest.deployservice.bean.HostState;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EC2HostInfoDAOImpl implements HostInfoDAO {
    private static final Logger LOG = LoggerFactory.getLogger(EC2HostInfoDAOImpl.class);
    private AmazonEC2Client ec2Client;
    // from aws sdk doc:
    // http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/InstanceState.html
    private static final int TERMINATED_CODE = 48;
    private static final int STOPPED_CODE = 80;
    private static final String RUNNING_CODE = "16";

    private static final Pattern NON_EXISTING_INSTANCE_ID_PATTERN =
            Pattern.compile("[i|r]-[a-z0-9]{8}");
    private static final String INSTANCE_NOT_FOUND_ERROR = "InvalidInstanceID.NotFound";

    private static final Pattern MALFORMED_INSTANCE_ID_PATTERN =
            Pattern.compile("Invalid id: \"(?<id>[a-zA-Z0-9-]+)\"");
    private static final String INSTANCE_MALFORMED_ERROR = "InvalidInstanceID.Malformed";

    public EC2HostInfoDAOImpl(AmazonEC2Client client) {
        this.ec2Client = client;
    }

    @Override
    public Set<String> getTerminatedHosts(Set<String> staleIds) throws Exception {
        HashSet<String> ids = new HashSet<>(staleIds);
        HashSet<String> terminatedHosts = new HashSet<>();
        while (!ids.isEmpty()) {
            DescribeInstancesRequest request = new DescribeInstancesRequest();
            request.setInstanceIds(ids);
            try {
                do {
                    DescribeInstancesResult results = ec2Client.describeInstances(request);
                    List<Reservation> reservations = results.getReservations();
                    for (Reservation reservation : reservations) {
                        for (Instance instance : reservation.getInstances()) {
                            int stateCode = instance.getState().getCode();
                            String id = instance.getInstanceId();
                            if (stateCode == TERMINATED_CODE || stateCode == STOPPED_CODE) {
                                LOG.info(String.format("Instance %s has already been terminated or stopped.", id));
                                terminatedHosts.add(id);
                            }

                            ids.remove(id);
                        }
                    }
                    if (results.getNextToken() == null || results.getNextToken().isEmpty()) {
                        break;<commit_msg>Fix InvalidInstanceID.Malformed regex pattern bug<commit_after>/**
 * Copyright 2016 Pinterest, 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.pinterest.arcee.aws;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.*;

import com.pinterest.arcee.bean.GroupBean;
import com.pinterest.deployservice.bean.HostBean;
import com.pinterest.arcee.dao.HostInfoDAO;
import com.pinterest.deployservice.common.DeployInternalException;

import com.pinterest.deployservice.bean.HostState;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EC2HostInfoDAOImpl implements HostInfoDAO {
    private static final Logger LOG = LoggerFactory.getLogger(EC2HostInfoDAOImpl.class);
    private AmazonEC2Client ec2Client;
    // from aws sdk doc:
    // http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/InstanceState.html
    private static final int TERMINATED_CODE = 48;
    private static final int STOPPED_CODE = 80;
    private static final String RUNNING_CODE = "16";

    private static final Pattern NON_EXISTING_INSTANCE_ID_PATTERN =
            Pattern.compile("[i|r]-[a-z0-9]{8}");
    private static final String INSTANCE_NOT_FOUND_ERROR = "InvalidInstanceID.NotFound";

    private static final Pattern MALFORMED_INSTANCE_ID_PATTERN =
            Pattern.compile("Invalid id: \"(?<id>.+)\"");
    private static final String INSTANCE_MALFORMED_ERROR = "InvalidInstanceID.Malformed";

    public EC2HostInfoDAOImpl(AmazonEC2Client client) {
        this.ec2Client = client;
    }

    @Override
    public Set<String> getTerminatedHosts(Set<String> staleIds) throws Exception {
        HashSet<String> ids = new HashSet<>(staleIds);
        HashSet<String> terminatedHosts = new HashSet<>();
        while (!ids.isEmpty()) {
            DescribeInstancesRequest request = new DescribeInstancesRequest();
            request.setInstanceIds(ids);
            try {
                do {
                    DescribeInstancesResult results = ec2Client.describeInstances(request);
                    List<Reservation> reservations = results.getReservations();
                    for (Reservation reservation : reservations) {
                        for (Instance instance : reservation.getInstances()) {
                            int stateCode = instance.getState().getCode();
                            String id = instance.getInstanceId();
                            if (stateCode == TERMINATED_CODE || stateCode == STOPPED_CODE) {
                                LOG.info(String.format("Instance %s has already been terminated or stopped.", id));
                                terminatedHosts.add(id);
                            }

                            ids.remove(id);
                        }
                    }
                    if (results.getNextToken() == null || results.getNextToken().isEmpty()) {
                        break;
{'fraction_non_alphanumeric': 0.09114336091143362, 'fraction_numerical': 0.0086803200868032, 'mean_word_length': 2.6925388082123183, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6081769', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1949, 'n_words': 499}