@Override public SiteEntity create(SiteEntity entity) { Preconditions.checkNotNull(entity.getSiteId(), "SiteId is null: " + entity.getSiteId()); if (siteId2EntityMap.containsKey(entity.getSiteId())) { throw new IllegalArgumentException("Duplicated siteId: " + entity.getSiteId()); } entity.ensureDefault(); siteId2EntityMap.put(entity.getSiteId(), entity); return entity; }
@Override public SiteEntity deleteBySiteId(String siteId) throws EntityNotFoundException { return siteId2EntityMap.remove(getBySiteId(siteId).getSiteId()); }
@Override public Collection<ApplicationEntity> findBySiteId(String siteId) { return applicationEntityMap.values().stream().filter((app) -> siteId.equals(app.getSite().getSiteId())).collect(Collectors.toList()); }
@Override public SiteEntity deleteByUUID(String uuid) throws EntityNotFoundException { return siteId2EntityMap.remove(getByUUID(uuid).getSiteId()); }
@Override public ApplicationEntity getBySiteIdAndAppType(String siteId, String appType) { Optional<ApplicationEntity> optional = applicationEntityMap.values().stream() .filter((app) -> siteId.equals(app.getSite().getSiteId()) && appType.equals(app.getDescriptor().getType())).findAny(); if (optional.isPresent()) { return optional.get(); } else { return null; } }
@Override public SiteEntity create(SiteEntity entity) { Preconditions.checkNotNull(entity.getSiteId(), "SiteId is null: " + entity.getSiteId()); List<SiteEntity> entities = new ArrayList<>(1); entity.ensureDefault(); entities.add(entity); try { queryService.insert(insertSql, entities, new SiteEntityToRelation()); } catch (SQLException e) { LOGGER.error("Error to insert SiteEntity: {}", entity, e); throw new IllegalArgumentException("Error to insert SiteEntity: " + entity + e); } return entity; } }
private SiteEntity getUniqueSite() { // Create local site SiteEntity siteEntity = new SiteEntity(); siteEntity.setSiteId("SIMULATED_SITE_" + incr.incrementAndGet()); siteEntity.setSiteName(siteEntity.getSiteId()); siteEntity.setDescription("Automatically generated unique simulation site " + siteEntity.getSiteId() + " (simulator: " + this + ")"); return siteEntity; }
@Override public SiteEntity update(SiteEntity siteEntity) throws EntityNotFoundException { if (siteEntity.getSiteId() == null && siteEntity.getUuid() == null) { throw new IllegalArgumentException("siteId and UUID are both null, don't know how to update"); } SiteEntity oldEntity = (siteEntity.getSiteId() != null) ? getBySiteId(siteEntity.getSiteId()) : getByUUID(siteEntity.getUuid()); siteEntity.setUuid(oldEntity.getUuid()); siteEntity.setCreatedTime(oldEntity.getCreatedTime()); siteEntity.ensureDefault(); siteId2EntityMap.put(siteEntity.getSiteId(), siteEntity); return siteEntity; } }
private Collection<String> loadSites(String appType) { Set<String> sites = new HashSet<>(); Collection<ApplicationEntity> apps = applicationEntityService.findAll(); for (ApplicationEntity app : apps) { if (app.getDescriptor().getType().equalsIgnoreCase(appType) && app.getStatus().equals(ApplicationEntity.Status.RUNNING)) { sites.add(app.getSite().getSiteId()); } } LOG.info("Detected {} sites where MR_HISTORY_JOB_APP is Running: {}", sites.size(), sites); return sites; }
@Override public String toString() { return String.format("SiteEntity[siteId = %s, siteName =%s, description = %s, uuid = %s]", getSiteId(), getSiteName(), getDescription(), getUuid()); }
@Override public void ensureDefault() { super.ensureDefault(); Preconditions.checkNotNull(this.getSite(), "site is null"); Preconditions.checkNotNull(this.getSite().getSiteId(), "siteId is null"); Preconditions.checkNotNull(this.getDescriptor(), "descriptor is null"); Preconditions.checkNotNull(this.getDescriptor().getType(), "descriptor type is null"); if (this.appId == null) { this.appId = String.format("%s_%s", this.getDescriptor().getType(), this.getSite().getSiteId()).toUpperCase(); } if (this.status == null) { this.status = Status.INITIALIZED; } }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public RESTResponse<SiteEntity> createSite(SiteEntity siteEntity) { return RESTResponse.<SiteEntity>async((builder) -> { SiteEntity entity = siteEntityService.create(siteEntity); builder.message("Successfully created site (siteId:" + entity.getSiteId() + ", uuid: " + entity.getUuid() + ")"); builder.data(entity); }).get(); }
@DELETE @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public RESTResponse<SiteEntity> deleteSiteByUUID(UUIDRequest uuidRequest) { return RESTResponse.async(() -> { int appCount = entityService.findBySiteId(siteEntityService.getByUUID(uuidRequest.getUuid()).getSiteId()).size(); if (appCount > 0) { throw new SiteDeleteException("This site has enabled applications, remove them first"); } return siteEntityService.deleteByUUID(uuidRequest.getUuid()); }).get(); }
@Override public ApplicationEntity create(ApplicationEntity entity) { entity.ensureDefault(); if (getBySiteIdAndAppType(entity.getSite().getSiteId(), entity.getDescriptor().getType()) != null) { throw new IllegalArgumentException("Duplicated appId: " + entity.getAppId()); } List<ApplicationEntity> entities = new ArrayList<>(1); entities.add(entity); try { queryService.insert(insertSql, entities, new ApplicationEntityToRelation()); } catch (SQLException e) { LOGGER.error("Error to insert ApplicationEntity: {}", entity, e); } return entity; } }
@Override public ApplicationEntity create(ApplicationEntity entity) { entity.ensureDefault(); if (getBySiteIdAndAppType(entity.getSite().getSiteId(), entity.getDescriptor().getType()) != null) { throw new IllegalArgumentException("Duplicated appId: " + entity.getAppId()); } applicationEntityMap.put(entity.getUuid(), entity); return entity; }
public ApplicationExecutionConfig(ApplicationEntity metadata) { this.siteId = metadata.getSite().getSiteId(); this.mode = metadata.getMode().name(); this.appId = metadata.getAppId(); this.jarPath = metadata.getJarPath(); }
@Override public SiteEntity update(SiteEntity siteEntity) throws EntityNotFoundException { if (siteEntity.getSiteId() == null && siteEntity.getUuid() == null) { throw new IllegalArgumentException("siteId and UUID are both null, don't know how to update"); } int result; try { SiteEntity oldEntity = getBySiteId(siteEntity.getSiteId()); siteEntity.setUuid(oldEntity.getUuid()); siteEntity.setCreatedTime(oldEntity.getCreatedTime()); siteEntity.ensureDefault(); result = queryService.update(updateSqlByUUID, siteEntity, new SiteEntityToRelation()); } catch (SQLException e) { LOGGER.error("Error to update SiteEntity: {}", siteEntity, e); throw new EntityNotFoundException(e); } if (result == 0) { throw new EntityNotFoundException("update " + siteEntity + "Not Found"); } return siteEntity; }
private void validateDependingApplicationInstalled(ApplicationEntity applicationEntity) { if (applicationEntity.getDescriptor().getDependencies() != null) { for (ApplicationDependency dependency : applicationEntity.getDescriptor().getDependencies()) { if (dependency.isRequired() && applicationEntityService.getBySiteIdAndAppType(applicationEntity.getSite().getSiteId(), dependency.getType()) == null) { throw new IllegalStateException("Required dependency " + dependency.toString() + " of " + applicationEntity.getDescriptor().getType() + " was not installed"); } } } }
private void fillApplicationDesc(List<ApplicationEntity> entities) { for (ApplicationEntity entity : entities) { entity.setDescriptor(applicationProviderService.getApplicationDescByType(entity.getDescriptor().getType())); if (entity.getDescriptor().getStreams() == null) { continue; } List<StreamDesc> streamDescToInstall = entity.getDescriptor().getStreams().stream().map((streamDefinition -> { StreamDefinition copied = streamDefinition.copy(); copied.setSiteId(entity.getSite().getSiteId()); copied.setStreamId(StreamIdConversions.formatSiteStreamId(entity.getSite().getSiteId(), copied.getStreamId())); Config effectiveConfig = ConfigFactory.parseMap(new HashMap<>(entity.getConfiguration())) .withFallback(config).withFallback(ConfigFactory.parseMap(entity.getContext())); ExecutionRuntime runtime = ExecutionRuntimeManager.getInstance().getRuntime( applicationProviderService.getApplicationProviderByType(entity.getDescriptor().getType()).getApplication().getEnvironmentType(), config); StreamSinkConfig streamSinkConfig = runtime.environment() .stream().getSinkConfig(StreamIdConversions.parseStreamTypeId(copied.getSiteId(), copied.getStreamId()), effectiveConfig); StreamDesc streamDesc = new StreamDesc(); streamDesc.setSchema(copied); streamDesc.setSinkConfig(streamSinkConfig); streamDesc.setStreamId(copied.getStreamId()); streamDesc.getSchema().setDataSource(entity.getAppId()); return streamDesc; })).collect(Collectors.toList()); entity.setStreams(streamDescToInstall); } }