<commit_before>package io.cattle.platform.servicediscovery.deployment.impl;

import io.cattle.platform.core.constants.CommonStatesConstants;
import io.cattle.platform.core.constants.HealthcheckConstants;
import io.cattle.platform.core.constants.InstanceConstants;
import io.cattle.platform.core.model.Instance;
import io.cattle.platform.core.model.Service;
import io.cattle.platform.core.model.ServiceExposeMap;
import io.cattle.platform.engine.process.impl.ProcessCancelException;
import io.cattle.platform.object.process.StandardProcess;
import io.cattle.platform.object.resource.ResourcePredicate;
import io.cattle.platform.process.common.util.ProcessUtils;
import io.cattle.platform.servicediscovery.deployment.DeploymentUnitInstance;
import io.cattle.platform.servicediscovery.deployment.InstanceUnit;
import io.cattle.platform.servicediscovery.deployment.impl.DeploymentManagerImpl.DeploymentServiceContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.tuple.Pair;

public class DefaultDeploymentUnitInstance extends DeploymentUnitInstance implements InstanceUnit {
    protected String instanceName;
    protected Instance instance;

    public DefaultDeploymentUnitInstance() {
        super(null, null, null);
    }

    public DefaultDeploymentUnitInstance(DeploymentServiceContext context, String uuid,
            Service service,
            String instanceName, Instance instance, Map<String, String> labels) {
        super(context, uuid, service);
        this.instanceName = instanceName;
        this.instance = instance;
        if (this.instance != null) {
            exposeMap = context.exposeMapDao.findInstanceExposeMap(this.instance);
        }
    }

    @Override
    public boolean isError() {
        return this.instance.getRemoved() != null;
    }

    @Override
    public void remove() {
        // 1) schedule remove for the instance
        // removal instance is scheduled ahead of map removal, to avoid situation when map removal is scheduled, but
        // removal scheduling fails by some reason
        if (!(instance.getState().equals(CommonStatesConstants.REMOVED) || instance.getState().equals(
                CommonStatesConstants.REMOVING))) {
            try {
                context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, instance,
                        null);
            } catch (ProcessCancelException e) {
                context.objectProcessManager.scheduleProcessInstanceAsync(InstanceConstants.PROCESS_STOP,
                        instance, ProcessUtils.chainInData(new HashMap<String, Object>(),
                                InstanceConstants.PROCESS_STOP, InstanceConstants.PROCESS_REMOVE));
            }
        }

        // 2) remove the mapping
        List<? extends ServiceExposeMap> maps = context.objectManager.mappedChildren(
                context.objectManager.loadResource(Instance.class, instance.getId()),
                ServiceExposeMap.class);
        for (ServiceExposeMap map : maps) {
            context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, map, null);
        }
    }

    @Override
    public DeploymentUnitInstance start(Map<String, Object> deployParams) {
        if (createNew()) {
            deployParams.put("name", this.instanceName);
            Map<String, Object> launchConfigData = context.sdService.buildServiceInstanceLaunchData(service,
                    deployParams);
            Pair<Instance, ServiceExposeMap> instanceMapPair = context.exposeMapDao.createServiceInstance(launchConfigData,
                    service, this.instanceName);
            this.instance = instanceMapPair.getLeft();
            this.exposeMap = instanceMapPair.getRight();
        }
        context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.CREATE, instance,
                null);
        context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.CREATE, exposeMap,
                null);
<commit_msg>Override instanceName on service instances<commit_after>package io.cattle.platform.servicediscovery.deployment.impl;

import io.cattle.platform.core.constants.CommonStatesConstants;
import io.cattle.platform.core.constants.HealthcheckConstants;
import io.cattle.platform.core.constants.InstanceConstants;
import io.cattle.platform.core.model.Instance;
import io.cattle.platform.core.model.Service;
import io.cattle.platform.core.model.ServiceExposeMap;
import io.cattle.platform.engine.process.impl.ProcessCancelException;
import io.cattle.platform.object.process.StandardProcess;
import io.cattle.platform.object.resource.ResourcePredicate;
import io.cattle.platform.process.common.util.ProcessUtils;
import io.cattle.platform.servicediscovery.deployment.DeploymentUnitInstance;
import io.cattle.platform.servicediscovery.deployment.InstanceUnit;
import io.cattle.platform.servicediscovery.deployment.impl.DeploymentManagerImpl.DeploymentServiceContext;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.tuple.Pair;

public class DefaultDeploymentUnitInstance extends DeploymentUnitInstance implements InstanceUnit {
    protected String instanceName;
    protected Instance instance;

    public DefaultDeploymentUnitInstance() {
        super(null, null, null);
    }

    public DefaultDeploymentUnitInstance(DeploymentServiceContext context, String uuid,
            Service service,
            String instanceName, Instance instance, Map<String, String> labels) {
        super(context, uuid, service);
        this.instanceName = instanceName;
        this.instance = instance;
        if (this.instance != null) {
            exposeMap = context.exposeMapDao.findInstanceExposeMap(this.instance);
        }
    }

    @Override
    public boolean isError() {
        return this.instance.getRemoved() != null;
    }

    @Override
    public void remove() {
        // 1) schedule remove for the instance
        // removal instance is scheduled ahead of map removal, to avoid situation when map removal is scheduled, but
        // removal scheduling fails by some reason
        if (!(instance.getState().equals(CommonStatesConstants.REMOVED) || instance.getState().equals(
                CommonStatesConstants.REMOVING))) {
            try {
                context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, instance,
                        null);
            } catch (ProcessCancelException e) {
                context.objectProcessManager.scheduleProcessInstanceAsync(InstanceConstants.PROCESS_STOP,
                        instance, ProcessUtils.chainInData(new HashMap<String, Object>(),
                                InstanceConstants.PROCESS_STOP, InstanceConstants.PROCESS_REMOVE));
            }
        }

        // 2) remove the mapping
        List<? extends ServiceExposeMap> maps = context.objectManager.mappedChildren(
                context.objectManager.loadResource(Instance.class, instance.getId()),
                ServiceExposeMap.class);
        for (ServiceExposeMap map : maps) {
            context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.REMOVE, map, null);
        }
    }

    @Override
    public DeploymentUnitInstance start(Map<String, Object> deployParams) {
        if (createNew()) {
            Map<String, Object> launchConfigData = context.sdService.buildServiceInstanceLaunchData(service,
                    deployParams);
            launchConfigData.put("name", this.instanceName);
            Pair<Instance, ServiceExposeMap> instanceMapPair = context.exposeMapDao.createServiceInstance(launchConfigData,
                    service, this.instanceName);
            this.instance = instanceMapPair.getLeft();
            this.exposeMap = instanceMapPair.getRight();
        }
        context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.CREATE, instance,
                null);
        context.objectProcessManager.scheduleStandardProcessAsync(StandardProcess.CREATE, exposeMap,
                null);

{'fraction_non_alphanumeric': 0.08456918740041672, 'fraction_numerical': 0.0007353842382644932, 'mean_word_length': 3.1910631741140216, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5018483', 'n_tokens_mistral': 2029, 'n_tokens_neox': 2008, 'n_words': 380}