private BootstrapStepResult executeStep(BootstrapStep step, Predicate<BootstrapStep> shouldSkip) throws InterruptedException { try { step.validate(); } catch (IllegalArgumentException e) { LOG.warn("Bootstrap step {} failed because it is malformed: {}", step.getLabel(), e.getMessage()); return new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.FAILED, e.getMessage()); } if (shouldSkip.test(step)) { return new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.SKIPPED, null); } BootstrapStepExecutor bootstrapStepExecutor = bootstrapStepExecutors.get(step.getType()); if (bootstrapStepExecutor == null) { // should not be possible, as deserialization of the file into a BootStrapConfig should have failed return new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.FAILED, String.format("Unknown bootstrap step type '%s' for '%s'.", step.getType(), step.getLabel())); } return bootstrapStepExecutor.execute(step.getLabel(), step.getArguments()); }
@Test public void testRunCondition() throws Exception { BootstrapResult result = bootstrapService.bootstrap(step -> step.getRunCondition() == BootstrapStep.RunCondition.ONCE); List<BootstrapStepResult> stepResults = new ArrayList<>(3); stepResults.add(new BootstrapStepResult(STEP1.getLabel(), BootstrapStepResult.Status.SKIPPED)); stepResults.add(new BootstrapStepResult(STEP2.getLabel(), BootstrapStepResult.Status.SUCCEEDED)); stepResults.add(new BootstrapStepResult(STEP3.getLabel(), BootstrapStepResult.Status.SKIPPED)); Assert.assertEquals(new BootstrapResult(stepResults), result); }
@Test public void testContinuesAfterFailures() throws Exception { EXECUTOR1.shouldFail(); BootstrapResult result = bootstrapService.bootstrap(); List<BootstrapStepResult> stepResults = new ArrayList<>(3); stepResults.add(new BootstrapStepResult(STEP1.getLabel(), BootstrapStepResult.Status.FAILED)); stepResults.add(new BootstrapStepResult(STEP2.getLabel(), BootstrapStepResult.Status.SUCCEEDED)); stepResults.add(new BootstrapStepResult(STEP3.getLabel(), BootstrapStepResult.Status.SUCCEEDED)); BootstrapResult expected = new BootstrapResult(stepResults); Assert.assertEquals(expected, result); }
@Override protected void startUp() { LOG.info("Starting {}", getClass().getSimpleName()); config = bootstrapConfigProvider.getConfig(); executorService = Executors.newSingleThreadExecutor(Threads.createDaemonThreadFactory("bootstrap-service")); executorService.execute(() -> { try { if (isBootstrappedWithRetries()) { // if the system is already bootstrapped, skip any bootstrap step that is supposed to only run once bootstrap(step -> step.getRunCondition() == BootstrapStep.RunCondition.ONCE); } else { bootstrap(); } } catch (InterruptedException e) { LOG.info("Bootstrapping could not complete due to interruption. It will be re-run the next time CDAP starts."); } }); LOG.info("Started {}", getClass().getSimpleName()); }
@BeforeClass public static void setupClass() { Map<BootstrapStep.Type, BootstrapStepExecutor> executors = new HashMap<>(); executors.put(STEP1.getType(), EXECUTOR1); executors.put(STEP2.getType(), EXECUTOR2); executors.put(STEP3.getType(), EXECUTOR3); List<BootstrapStep> steps = new ArrayList<>(3); steps.add(STEP1); steps.add(STEP2); steps.add(STEP3); bootstrapConfig = new BootstrapConfig(steps); BootstrapConfigProvider bootstrapConfigProvider = new InMemoryBootstrapConfigProvider(bootstrapConfig); bootstrapStore = AppFabricTestHelper.getInjector().getInstance(BootstrapStore.class); bootstrapService = new BootstrapService(bootstrapConfigProvider, bootstrapStore, executors); bootstrapService.reload(); }
/** * Check that this is a valid config, throwing an exception if not. * * @throws IllegalArgumentException if the config is invalid */ public void validate() { for (BootstrapStep step : steps) { step.validate(); } } }
@Test public void testRetries() throws Exception { EXECUTOR1.setNumRetryableFailures(3); BootstrapResult result = bootstrapService.bootstrap(); List<BootstrapStepResult> expectedStepResults = new ArrayList<>(); for (BootstrapStep step : bootstrapConfig.getSteps()) { expectedStepResults.add(new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.SUCCEEDED)); } BootstrapResult expected = new BootstrapResult(expectedStepResults); Assert.assertEquals(expected, result); }
@Override protected void startUp() { LOG.info("Starting {}", getClass().getSimpleName()); config = bootstrapConfigProvider.getConfig(); executorService = Executors.newSingleThreadExecutor(Threads.createDaemonThreadFactory("bootstrap-service")); executorService.execute(() -> { try { if (isBootstrappedWithRetries()) { // if the system is already bootstrapped, skip any bootstrap step that is supposed to only run once bootstrap(step -> step.getRunCondition() == BootstrapStep.RunCondition.ONCE); } else { bootstrap(); } } catch (InterruptedException e) { LOG.info("Bootstrapping could not complete due to interruption. It will be re-run the next time CDAP starts."); } }); LOG.info("Started {}", getClass().getSimpleName()); }
/** * Check that this is a valid config, throwing an exception if not. * * @throws IllegalArgumentException if the config is invalid */ public void validate() { for (BootstrapStep step : steps) { step.validate(); } } }
private BootstrapStepResult executeStep(BootstrapStep step, Predicate<BootstrapStep> shouldSkip) throws InterruptedException { try { step.validate(); } catch (IllegalArgumentException e) { LOG.warn("Bootstrap step {} failed because it is malformed: {}", step.getLabel(), e.getMessage()); return new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.FAILED, e.getMessage()); } if (shouldSkip.test(step)) { return new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.SKIPPED, null); } BootstrapStepExecutor bootstrapStepExecutor = bootstrapStepExecutors.get(step.getType()); if (bootstrapStepExecutor == null) { // should not be possible, as deserialization of the file into a BootStrapConfig should have failed return new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.FAILED, String.format("Unknown bootstrap step type '%s' for '%s'.", step.getType(), step.getLabel())); } return bootstrapStepExecutor.execute(step.getLabel(), step.getArguments()); }
@Test public void testAllSuccess() throws Exception { BootstrapResult result = bootstrapService.bootstrap(); List<BootstrapStepResult> expectedStepResults = new ArrayList<>(); for (BootstrapStep step : bootstrapConfig.getSteps()) { expectedStepResults.add(new BootstrapStepResult(step.getLabel(), BootstrapStepResult.Status.SUCCEEDED)); } BootstrapResult expected = new BootstrapResult(expectedStepResults); Assert.assertEquals(expected, result); Assert.assertTrue(bootstrapService.isBootstrapped()); }