use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelper method evaluateCancellationAndCancelDeploymentIfNeeded.
private void evaluateCancellationAndCancelDeploymentIfNeeded() {
try {
GreengrassService deploymentServiceLocateResult = kernel.locate(DeploymentService.DEPLOYMENT_SERVICE_TOPICS);
if (deploymentServiceLocateResult instanceof DeploymentService) {
DeploymentService deploymentService = (DeploymentService) deploymentServiceLocateResult;
DeploymentTaskMetadata currentDeployment = deploymentService.getCurrentDeploymentTaskMetadata();
// If the queue is not empty then it means deployment(s) from other sources is/are queued in it,
// in that case don't add a cancellation deployment because it can't be added to the front of the queue
// we will just have to let current deployment finish
Deployment deployment = new Deployment(DeploymentType.IOT_JOBS, UUID.randomUUID().toString(), true);
if (deploymentQueue.isEmpty() && currentDeployment != null && currentDeployment.isCancellable() && DeploymentType.IOT_JOBS.equals(currentDeployment.getDeploymentType()) && deploymentQueue.offer(deployment)) {
logger.atInfo().log("Added cancellation deployment to the queue");
}
}
} catch (ServiceLoadException e) {
logger.atError().setCause(e).log("Failed to find deployment service");
}
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentService method setComponentsToGroupsMapping.
void setComponentsToGroupsMapping(Topics groupsToRootComponents) {
Set<String> pendingComponents = new HashSet<>();
Map<String, Object> componentsToGroupsMappingCache = new ConcurrentHashMap<>();
Topics componentsToGroupsTopics = getConfig().lookupTopics(COMPONENTS_TO_GROUPS_TOPICS);
/*
* Structure of COMPONENTS_TO_GROUPS_TOPICS is:
* COMPONENTS_TO_GROUPS_TOPICS :
* |_ <componentName> :
* |_ <deploymentID> : <GroupName>
* This stores all the components with the list of deployment IDs associated to it along with the thing group
* (if available) to be associated to the deployment.
*/
// Get all the groups associated to the root components.
groupsToRootComponents.forEach(groupNode -> ((Topics) groupNode).forEach(componentNode -> {
Topics componentTopics = (Topics) componentNode;
Topic groupConfigTopic = componentTopics.lookup(GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN);
String groupConfig = Coerce.toString(groupConfigTopic);
Topic groupNameTopic = componentTopics.lookup(GROUP_TO_ROOT_COMPONENTS_GROUP_NAME);
String groupName = Coerce.toString(groupNameTopic);
Map<String, Object> groupDeploymentIdSet = (Map<String, Object>) componentsToGroupsMappingCache.getOrDefault(componentTopics.getName(), new HashMap<>());
groupDeploymentIdSet.putIfAbsent(groupConfig, groupName);
componentsToGroupsMappingCache.put(componentTopics.getName(), groupDeploymentIdSet);
pendingComponents.add(componentTopics.getName());
}));
// Associate the groups to the dependant services based on the services it is depending on.
while (!pendingComponents.isEmpty()) {
String componentName = pendingComponents.iterator().next();
try {
GreengrassService greengrassService = kernel.locate(componentName);
Map<String, Object> groupNamesForComponent = (Map<String, Object>) componentsToGroupsMappingCache.getOrDefault(greengrassService.getName(), new HashMap<>());
greengrassService.getDependencies().forEach((greengrassService1, dependencyType) -> {
pendingComponents.add(greengrassService1.getName());
Map<String, Object> groupNamesForDependentComponent = (Map<String, Object>) componentsToGroupsMappingCache.getOrDefault(greengrassService1.getName(), new HashMap<>());
groupNamesForDependentComponent.putAll(groupNamesForComponent);
componentsToGroupsMappingCache.put(greengrassService1.getName(), groupNamesForDependentComponent);
});
} catch (ServiceLoadException ex) {
logger.atError().cause(ex).log("Unable to get status for {}.", componentName);
}
pendingComponents.remove(componentName);
}
if (componentsToGroupsTopics != null) {
componentsToGroupsTopics.replaceAndWait(componentsToGroupsMappingCache);
}
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException in project aws-greengrass-nucleus by aws-greengrass.
the class Kernel method locateExternalPlugin.
@SuppressWarnings({ "PMD.AvoidCatchingThrowable", "PMD.CloseResource" })
private Class<?> locateExternalPlugin(String name, Topics serviceRootTopics) throws ServiceLoadException {
ComponentIdentifier componentId = ComponentIdentifier.fromServiceTopics(serviceRootTopics);
Path pluginJar;
try {
pluginJar = nucleusPaths.artifactPath(componentId).resolve(componentId.getName() + JAR_FILE_EXTENSION);
} catch (IOException e) {
throw new ServiceLoadException(e);
}
if (!pluginJar.toFile().exists() || !pluginJar.toFile().isFile()) {
throw new ServiceLoadException(String.format("Unable to find %s because %s does not exist", name, pluginJar));
}
Topic storedDigest = config.find(SERVICES_NAMESPACE_TOPIC, MAIN_SERVICE_NAME, GreengrassService.RUNTIME_STORE_NAMESPACE_TOPIC, SERVICE_DIGEST_TOPIC_KEY, componentId.toString());
if (storedDigest == null || storedDigest.getOnce() == null) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local external plugin is not supported by this greengrass version");
throw new ServiceLoadException("Custom plugins is not supported by this greengrass version");
}
ComponentStore componentStore = context.get(ComponentStore.class);
if (!componentStore.validateComponentRecipeDigest(componentId, Coerce.toString(storedDigest))) {
logger.atError("plugin-load-error").kv(GreengrassService.SERVICE_NAME_KEY, name).log("Local plugin does not match the version in cloud!!");
throw new ServiceLoadException("Plugin has been modified after it was downloaded");
}
Class<?> clazz;
try {
AtomicReference<Class<?>> classReference = new AtomicReference<>();
EZPlugins ezPlugins = context.get(EZPlugins.class);
ezPlugins.loadPlugin(pluginJar, (sc) -> sc.matchClassesWithAnnotation(ImplementsService.class, (c) -> {
// Only use the class whose name matches what we want
ImplementsService serviceImplementation = c.getAnnotation(ImplementsService.class);
if (serviceImplementation.name().equals(name)) {
if (classReference.get() != null) {
logger.atWarn().log("Multiple classes implementing service found in {} " + "for component {}. Using the first one found: {}", pluginJar, name, classReference.get());
return;
}
classReference.set(c);
}
}));
clazz = classReference.get();
} catch (Throwable e) {
throw new ServiceLoadException(String.format("Unable to load %s as a plugin", name), e);
}
if (clazz == null) {
throw new ServiceLoadException(String.format("Unable to find %s. Could not find any ImplementsService annotation with the same name.", name));
}
return clazz;
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException in project aws-greengrass-nucleus by aws-greengrass.
the class ShadowDeploymentListener method shadowUpdated.
protected void shadowUpdated(Map<String, Object> desired, Map<String, Object> reported, Integer version) {
if (lastVersion.get() > version) {
logger.atDebug().kv("SHADOW_VERSION", version).log("Received an older version of shadow. Ignoring...");
return;
}
lastVersion.set(version);
// the reported section of the shadow was updated
if (reported != null && !reported.isEmpty()) {
syncShadowDeploymentStatus(reported);
}
if (desired == null || desired.isEmpty()) {
logger.debug("Empty desired state, no update to desired section or no device deployments created yet");
return;
}
String fleetConfigStr = (String) desired.get(FLEET_CONFIG_KEY);
Configuration configuration;
try {
configuration = SerializerFactory.getFailSafeJsonObjectMapper().readValue(fleetConfigStr, Configuration.class);
} catch (JsonProcessingException e) {
logger.atError().log("failed to process shadow update", e);
return;
}
String configurationArn = configuration.getConfigurationArn();
if (configurationArn == null) {
logger.atError().log("Desired state has null configuration ARN. Ignoring shadow update");
return;
}
String desiredStatus = (String) desired.get(DESIRED_STATUS_KEY);
if (desiredStatus == null) {
logger.atError().log("Desired status is null. Ignoring shadow update");
return;
}
boolean cancelDeployment = DESIRED_STATUS_CANCELED.equals(desiredStatus);
synchronized (ShadowDeploymentListener.class) {
// If lastConfigurationArn is null, this is the first shadow update since startup
if (lastConfigurationArn == null) {
lastConfigurationArn = configurationArn;
// Ignore if the latest deployment was canceled
if (cancelDeployment) {
logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Deployment was canceled. Ignoring shadow update at startup");
return;
}
// the reported status is terminal (i.e. not in_progress) because it's already fully processed
if (reported != null && configurationArn.equals(reported.get(ARN_FOR_STATUS_KEY)) && !JobStatus.IN_PROGRESS.toString().equals(reported.get(STATUS_KEY))) {
logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Deployment result already reported. Ignoring shadow update at startup");
return;
}
// Ignore if it's the ongoing deployment. This can happen if the last shadow deployment caused restart
try {
// Using locate instead of injection here because DeploymentService lacks usable injection
// constructor. Same as in IotJobsHelper.evaluateCancellationAndCancelDeploymentIfNeeded
GreengrassService deploymentServiceLocateResult = kernel.locate(DeploymentService.DEPLOYMENT_SERVICE_TOPICS);
if (deploymentServiceLocateResult instanceof DeploymentService) {
DeploymentTaskMetadata currentDeployment = ((DeploymentService) deploymentServiceLocateResult).getCurrentDeploymentTaskMetadata();
if (currentDeployment != null && configurationArn.equals(currentDeployment.getDeploymentId())) {
logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Ongoing deployment. Ignoring shadow update at startup");
return;
}
}
} catch (ServiceLoadException e) {
logger.atError().setCause(e).log("Failed to find deployment service");
}
} else {
if (lastConfigurationArn.equals(configurationArn) && !cancelDeployment) {
logger.atInfo().kv(CONFIGURATION_ARN_LOG_KEY_NAME, configurationArn).log("Duplicate deployment notification. Ignoring shadow update");
return;
}
lastConfigurationArn = configurationArn;
}
}
Deployment deployment;
if (cancelDeployment) {
deployment = new Deployment(DeploymentType.SHADOW, UUID.randomUUID().toString(), true);
} else {
deployment = new Deployment(fleetConfigStr, DeploymentType.SHADOW, configurationArn);
}
if (deploymentQueue.offer(deployment)) {
logger.atInfo().kv("ID", deployment.getId()).log("Added shadow deployment job");
}
}
use of com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException in project aws-greengrass-nucleus by aws-greengrass.
the class DefaultActivator method activate.
@Override
@SuppressWarnings("PMD.PrematureDeclaration")
public void activate(Map<String, Object> newConfig, Deployment deployment, CompletableFuture<DeploymentResult> totallyCompleteFuture) {
Map<String, Object> serviceConfig;
if (newConfig.containsKey(SERVICES_NAMESPACE_TOPIC)) {
serviceConfig = (Map<String, Object>) newConfig.get(SERVICES_NAMESPACE_TOPIC);
} else {
serviceConfig = new HashMap<>();
}
DeploymentDocument deploymentDocument = deployment.getDeploymentDocumentObj();
if (isAutoRollbackRequested(deploymentDocument) && !takeConfigSnapshot(totallyCompleteFuture)) {
return;
}
DeploymentConfigMerger.AggregateServicesChangeManager servicesChangeManager = new DeploymentConfigMerger.AggregateServicesChangeManager(kernel, serviceConfig);
// Get the timestamp before updateMap(). It will be used to check whether services have started.
long mergeTime = System.currentTimeMillis();
updateConfiguration(deploymentDocument.getTimestamp(), newConfig);
// wait until topic listeners finished processing mergeMap changes.
Throwable setDesiredStateFailureCause = kernel.getContext().runOnPublishQueueAndWait(() -> {
// polling to wait for all services to be started.
servicesChangeManager.startNewServices();
// Close unloadable service instances and initiate with new config
servicesChangeManager.replaceUnloadableService();
// Restart any services that may have been broken before this deployment
// This is added to allow deployments to fix broken services
servicesChangeManager.reinstallBrokenServices();
});
if (setDesiredStateFailureCause != null) {
handleFailure(servicesChangeManager, deploymentDocument, totallyCompleteFuture, setDesiredStateFailureCause);
return;
}
try {
Set<GreengrassService> servicesToTrack = servicesChangeManager.servicesToTrack();
logger.atDebug(MERGE_CONFIG_EVENT_KEY).kv("serviceToTrack", servicesToTrack).kv("mergeTime", mergeTime).log("Applied new service config. Waiting for services to complete update");
waitForServicesToStart(servicesToTrack, mergeTime);
logger.atDebug(MERGE_CONFIG_EVENT_KEY).log("new/updated services are running, will now remove old services");
servicesChangeManager.removeObsoleteServices();
logger.atInfo(MERGE_CONFIG_EVENT_KEY).kv(DEPLOYMENT_ID_LOG_KEY, deploymentDocument.getDeploymentId()).log("All services updated");
totallyCompleteFuture.complete(new DeploymentResult(DeploymentResult.DeploymentStatus.SUCCESSFUL, null));
} catch (InterruptedException | ExecutionException | ServiceUpdateException | ServiceLoadException e) {
handleFailure(servicesChangeManager, deploymentDocument, totallyCompleteFuture, e);
}
}
Aggregations