Search in sources :

Example 1 with GeneratedRecipe

use of com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe in project cloudbreak by hortonworks.

the class RecipeTemplateService method isRecipeUpToDateInHostGroup.

private boolean isRecipeUpToDateInHostGroup(HostGroup hostGroup, Map<String, GeneratedRecipe> generatedRecipeNameMap, List<RecipeModel> recipeModelList) {
    for (RecipeModel recipeModel : recipeModelList) {
        String recipeName = recipeModel.getName();
        if (!generatedRecipeNameMap.containsKey(recipeName)) {
            LOGGER.debug("No generated recipe with name {} for host group '{}'. Recipes should be uploaded and regenerated.", recipeName, hostGroup.getName());
            return false;
        }
        GeneratedRecipe generatedRecipe = generatedRecipeNameMap.get(recipeName);
        if (!recipeModel.getGeneratedScript().equals(generatedRecipe.getExtendedRecipeText())) {
            LOGGER.debug("Regenerated recipe [name: {}] not matching with currently generated recipe for host group '{}'. " + "Recipes should be uploaded and regenerated.", recipeName, hostGroup.getName());
            return false;
        }
    }
    return true;
}
Also used : GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) RecipeModel(com.sequenceiq.cloudbreak.orchestrator.model.RecipeModel)

Example 2 with GeneratedRecipe

use of com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe in project cloudbreak by hortonworks.

the class RecipeTemplateService method isGeneratedRecipesInDbStale.

/**
 * Compare generated recipes from the database against on-the-fly generated recipes for every host groups
 * If any of those will differ from the source (in database) or no recipe relation found for generated recipe, the result will be false.
 */
public boolean isGeneratedRecipesInDbStale(Set<HostGroup> hostGroups, Map<HostGroup, List<RecipeModel>> generatedModels) {
    for (HostGroup hostGroup : hostGroups) {
        Set<GeneratedRecipe> generatedRecipes = hostGroup.getGeneratedRecipes();
        boolean hasRecipes = CollectionUtils.isNotEmpty(hostGroup.getRecipes());
        boolean hasGeneratedRecipes = CollectionUtils.isNotEmpty(generatedRecipes);
        boolean recipeModelsContainsHostGroup = MapUtils.isNotEmpty(generatedModels) && generatedModels.containsKey(hostGroup);
        if (hasRecipes && !hasGeneratedRecipes) {
            LOGGER.debug("No generated recipes found for host group '{}', but it has recipes. Recipes should be uploaded and regenerated.", hostGroup.getName());
            return false;
        } else if (!hasGeneratedRecipes) {
            LOGGER.debug("No source and generated recipes found for host group '{}', skip comparing source and generated recipes.", hostGroup.getName());
            continue;
        } else if (!recipeModelsContainsHostGroup) {
            LOGGER.debug("Generated recipe models do not contain host group {}. Recipes should be regenerated.", hostGroup.getName());
            return false;
        }
        boolean anyWithoutRecipeSource = generatedRecipes.stream().anyMatch(g -> g.getRecipe() == null);
        if (anyWithoutRecipeSource) {
            LOGGER.debug("Not found recipe source for generated recipe. Recipes should be uploaded and regenerated.");
            return false;
        }
        Map<String, GeneratedRecipe> generatedRecipeNameMap = generatedRecipes.stream().collect(Collectors.toMap(g -> g.getRecipe().getName(), g -> g, (g1, g2) -> g1));
        List<RecipeModel> recipeModelList = generatedModels.get(hostGroup);
        if (generatedRecipes.size() != recipeModelList.size()) {
            LOGGER.debug("Source and generated recipe counts are not matching for host group '{}'. Recipes should be uploaded and regenerated.", hostGroup.getName());
            return false;
        }
        if (isRecipeUpToDateInHostGroup(hostGroup, generatedRecipeNameMap, recipeModelList)) {
            LOGGER.debug("Recipes matches for host group '{}'", hostGroup.getName());
        } else {
            return false;
        }
    }
    return true;
}
Also used : MapUtils(org.apache.commons.collections4.MapUtils) Stack(com.sequenceiq.cloudbreak.domain.stack.Stack) Logger(org.slf4j.Logger) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Benchmark.measure(com.sequenceiq.cloudbreak.util.Benchmark.measure) LoggerFactory(org.slf4j.LoggerFactory) GeneratedRecipeService(com.sequenceiq.cloudbreak.service.recipe.GeneratedRecipeService) Set(java.util.Set) Collectors(java.util.stream.Collectors) TemplatePreparationObject(com.sequenceiq.cloudbreak.template.TemplatePreparationObject) Base64(org.apache.commons.codec.binary.Base64) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) RecipeModel(com.sequenceiq.cloudbreak.orchestrator.model.RecipeModel) List(java.util.List) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) CentralRecipeUpdater(com.sequenceiq.cloudbreak.recipe.CentralRecipeUpdater) Service(org.springframework.stereotype.Service) Map(java.util.Map) StackToTemplatePreparationObjectConverter(com.sequenceiq.cloudbreak.converter.StackToTemplatePreparationObjectConverter) StringUtils(io.micrometer.core.instrument.util.StringUtils) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) RecipeModel(com.sequenceiq.cloudbreak.orchestrator.model.RecipeModel)

Example 3 with GeneratedRecipe

use of com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe in project cloudbreak by hortonworks.

the class UpdateRecipeServiceTest method testDetachRecipeFromCluster.

@Test
public void testDetachRecipeFromCluster() {
    // GIVEN
    Set<Recipe> recipes = createRecipes(Set.of(POST_CLDR_START_RECIPE));
    Map<String, Set<String>> hostGroupsSample = new HashMap<>();
    hostGroupsSample.put(MASTER_HOST_GROUP_NAME, Set.of(POST_CLDR_START_RECIPE));
    Set<HostGroup> hostGroups = createHostGroupWithRecipes(hostGroupsSample);
    Recipe sampleRecipe = recipes.stream().findFirst().orElse(null);
    HostGroup sampleHostGroup = hostGroups.stream().findFirst().orElse(null);
    given(hostGroupService.getByClusterIdAndNameWithRecipes(anyLong(), anyString())).willReturn(sampleHostGroup);
    given(recipeService.getByNameForWorkspaceId(anyString(), anyLong())).willReturn(sampleRecipe);
    given(hostGroupService.save(any())).willReturn(sampleHostGroup);
    given(generatedRecipeService.save(any())).willReturn(new GeneratedRecipe());
    // WHEN
    underTest.detachRecipeFromCluster(DUMMY_ID, createStack(), POST_CLDR_START_RECIPE, MASTER_HOST_GROUP_NAME);
    // THEN
    verify(hostGroupService, times(1)).save(any());
    verify(hostGroupService, times(1)).getByClusterIdAndNameWithRecipes(anyLong(), anyString());
    verify(generatedRecipeService, times(1)).save(any());
}
Also used : GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) HashSet(java.util.HashSet) Set(java.util.Set) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HashMap(java.util.HashMap) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 4 with GeneratedRecipe

use of com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe in project cloudbreak by hortonworks.

the class UpdateRecipeServiceTest method createHostGroupWithRecipes.

private Set<HostGroup> createHostGroupWithRecipes(Map<String, Set<String>> hostGroupRecipesMap) {
    Set<HostGroup> result = new HashSet<>();
    for (Map.Entry<String, Set<String>> entry : hostGroupRecipesMap.entrySet()) {
        HostGroup hostGroup = new HostGroup();
        hostGroup.setName(entry.getKey());
        Set<Recipe> recipeSet = new HashSet<>();
        Set<GeneratedRecipe> generatedRecipeSet = new HashSet<>();
        for (String recipeName : entry.getValue()) {
            Recipe recipe = new Recipe();
            GeneratedRecipe generatedRecipe = new GeneratedRecipe();
            generatedRecipe.setRecipe(recipe);
            recipe.setName(recipeName);
            recipe.setGeneratedRecipes(Set.of(generatedRecipe));
            recipeSet.add(recipe);
            generatedRecipeSet.add(generatedRecipe);
        }
        hostGroup.setRecipes(recipeSet);
        hostGroup.setGeneratedRecipes(generatedRecipeSet);
        result.add(hostGroup);
    }
    return result;
}
Also used : GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) ArgumentMatchers.anySet(org.mockito.ArgumentMatchers.anySet) HashSet(java.util.HashSet) Set(java.util.Set) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 5 with GeneratedRecipe

use of com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe in project cloudbreak by hortonworks.

the class RecipeTemplateServiceTest method testCompareGenerateRecipesWithGeneratedRecipeButNoRecipe.

@Test
public void testCompareGenerateRecipesWithGeneratedRecipeButNoRecipe() {
    // GIVEN
    Recipe recipe = recipe("r1", DUMMY_CONTENT_2);
    Set<HostGroup> hgs = new HashSet<>();
    Set<GeneratedRecipe> generatedRecipes = new HashSet<>();
    GeneratedRecipe generatedRecipe = new GeneratedRecipe();
    generatedRecipe.setExtendedRecipeText(DUMMY_CONTENT_1);
    generatedRecipe.setRecipe(recipe);
    generatedRecipes.add(generatedRecipe);
    HostGroup hgMaster = hostGroup("master", new HashSet<>(), generatedRecipes);
    hgs.add(hgMaster);
    Map<HostGroup, List<RecipeModel>> recipeModels = new HashMap<>();
    // WHEN
    boolean result = recipeTemplateService.isGeneratedRecipesInDbStale(hgs, recipeModels);
    // THEN
    assertFalse(result);
}
Also used : GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) GeneratedRecipe(com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe) Recipe(com.sequenceiq.cloudbreak.domain.Recipe) HashMap(java.util.HashMap) HostGroup(com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

GeneratedRecipe (com.sequenceiq.cloudbreak.domain.stack.cluster.host.GeneratedRecipe)8 Recipe (com.sequenceiq.cloudbreak.domain.Recipe)7 HostGroup (com.sequenceiq.cloudbreak.domain.stack.cluster.host.HostGroup)6 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)4 List (java.util.List)4 Set (java.util.Set)4 RecipeModel (com.sequenceiq.cloudbreak.orchestrator.model.RecipeModel)3 Map (java.util.Map)3 Test (org.junit.jupiter.api.Test)3 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)2 Collectors (java.util.stream.Collectors)2 ArgumentMatchers.anySet (org.mockito.ArgumentMatchers.anySet)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Service (org.springframework.stereotype.Service)2 Joiner (com.google.common.base.Joiner)1 UpdateHostGroupRecipes (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.base.UpdateHostGroupRecipes)1 AttachRecipeV4Response (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.response.recipe.AttachRecipeV4Response)1