Search in sources :

Example 1 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TagDBStore method getServiceTags.

@Override
public ServiceTags getServiceTags(String serviceName) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TagDBStore.getServiceTags(" + serviceName + ")");
    }
    ServiceTags ret = null;
    XXService xxService = daoManager.getXXService().findByName(serviceName);
    if (xxService == null) {
        throw new Exception("service does not exist. name=" + serviceName);
    }
    XXServiceVersionInfo serviceVersionInfoDbObj = daoManager.getXXServiceVersionInfo().findByServiceName(serviceName);
    if (serviceVersionInfoDbObj == null) {
        LOG.warn("serviceVersionInfo does not exist for service [" + serviceName + "]");
    }
    RangerServiceDef serviceDef = svcStore.getServiceDef(xxService.getType());
    if (serviceDef == null) {
        throw new Exception("service-def does not exist. id=" + xxService.getType());
    }
    RangerTagDBRetriever tagDBRetriever = new RangerTagDBRetriever(daoManager, xxService);
    Map<Long, RangerTagDef> tagDefMap = tagDBRetriever.getTagDefs();
    Map<Long, RangerTag> tagMap = tagDBRetriever.getTags();
    List<RangerServiceResource> resources = tagDBRetriever.getServiceResources();
    List<RangerTagResourceMap> tagResourceMaps = tagDBRetriever.getTagResourceMaps();
    Map<Long, List<Long>> resourceToTagIds = new HashMap<Long, List<Long>>();
    if (CollectionUtils.isNotEmpty(tagResourceMaps)) {
        Long resourceId = null;
        List<Long> tagIds = null;
        for (RangerTagResourceMap tagResourceMap : tagResourceMaps) {
            if (!tagResourceMap.getResourceId().equals(resourceId)) {
                if (resourceId != null) {
                    resourceToTagIds.put(resourceId, tagIds);
                }
                resourceId = tagResourceMap.getResourceId();
                tagIds = new ArrayList<Long>();
            }
            tagIds.add(tagResourceMap.getTagId());
        }
        if (resourceId != null) {
            resourceToTagIds.put(resourceId, tagIds);
        }
    }
    ret = new ServiceTags();
    ret.setServiceName(xxService.getName());
    ret.setTagVersion(serviceVersionInfoDbObj == null ? null : serviceVersionInfoDbObj.getTagVersion());
    ret.setTagUpdateTime(serviceVersionInfoDbObj == null ? null : serviceVersionInfoDbObj.getTagUpdateTime());
    ret.setTagDefinitions(tagDefMap);
    ret.setTags(tagMap);
    ret.setServiceResources(resources);
    ret.setResourceToTagIds(resourceToTagIds);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== TagDBStore.getServiceTags(" + serviceName + ")");
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) RangerServiceNotFoundException(org.apache.ranger.plugin.util.RangerServiceNotFoundException) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) ArrayList(java.util.ArrayList) List(java.util.List) PList(org.apache.ranger.plugin.store.PList) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo)

Example 2 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class AbstractTagSource method updateSink.

protected void updateSink(final ServiceTags toUpload) {
    if (toUpload == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No ServiceTags to upload");
        }
    } else {
        if (LOG.isDebugEnabled()) {
            String toUploadJSON = new Gson().toJson(toUpload);
            LOG.debug("Uploading serviceTags=" + toUploadJSON);
        }
        try {
            ServiceTags uploaded = tagSink.upload(toUpload);
            if (LOG.isDebugEnabled()) {
                String uploadedJSON = new Gson().toJson(uploaded);
                LOG.debug("Uploaded serviceTags=" + uploadedJSON);
            }
        } catch (Exception exception) {
            String toUploadJSON = new Gson().toJson(toUpload);
            LOG.error("Failed to upload serviceTags: " + toUploadJSON);
            LOG.error("Exception : ", exception);
        }
    }
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags) Gson(com.google.gson.Gson)

Example 3 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TagAdminRESTSink method run.

@Override
public void run() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> TagAdminRESTSink.run()");
    }
    while (true) {
        UploadWorkItem uploadWorkItem;
        try {
            uploadWorkItem = uploadWorkItems.take();
            ServiceTags toUpload = uploadWorkItem.getServiceTags();
            boolean doRetry;
            do {
                doRetry = false;
                try {
                    ServiceTags uploaded = doUpload(toUpload);
                    if (uploaded == null) {
                        // Treat this as if an Exception is thrown by doUpload
                        doRetry = true;
                        Thread.sleep(rangerAdminConnectionCheckInterval);
                    } else {
                        // ServiceTags uploaded successfully
                        uploadWorkItem.uploadCompleted(uploaded);
                    }
                } catch (InterruptedException interrupted) {
                    LOG.error("Caught exception..: ", interrupted);
                    return;
                } catch (Exception exception) {
                    doRetry = true;
                    Thread.sleep(rangerAdminConnectionCheckInterval);
                }
            } while (doRetry);
        } catch (InterruptedException exception) {
            LOG.error("Interrupted..: ", exception);
            return;
        }
    }
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags) IOException(java.io.IOException)

Example 4 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TagAdminRESTSink method upload.

@Override
public ServiceTags upload(ServiceTags toUpload) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> upload() ");
    }
    UploadWorkItem uploadWorkItem = new UploadWorkItem(toUpload);
    uploadWorkItems.put(uploadWorkItem);
    // Wait until message is successfully delivered
    ServiceTags ret = uploadWorkItem.waitForUpload();
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== upload()");
    }
    return ret;
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags)

Example 5 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class AtlasNotificationMapper method buildServiceTags.

private static ServiceTags buildServiceTags(RangerAtlasEntityWithTags entityWithTags, Map<String, ServiceTags> serviceTagsMap) {
    ServiceTags ret = null;
    RangerAtlasEntity entity = entityWithTags.getEntity();
    RangerServiceResource serviceResource = AtlasResourceMapperUtil.getRangerServiceResource(entity);
    if (serviceResource != null) {
        List<RangerTag> tags = getTags(entityWithTags);
        List<RangerTagDef> tagDefs = getTagDefs(entityWithTags);
        String serviceName = serviceResource.getServiceName();
        ret = createOrGetServiceTags(serviceTagsMap, serviceName);
        if (serviceTagsMap == null || CollectionUtils.isNotEmpty(tags)) {
            serviceResource.setId((long) ret.getServiceResources().size());
            ret.getServiceResources().add(serviceResource);
            List<Long> tagIds = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(tags)) {
                for (RangerTag tag : tags) {
                    tag.setId((long) ret.getTags().size());
                    ret.getTags().put(tag.getId(), tag);
                    tagIds.add(tag.getId());
                }
            }
            ret.getResourceToTagIds().put(serviceResource.getId(), tagIds);
            if (CollectionUtils.isNotEmpty(tagDefs)) {
                for (RangerTagDef tagDef : tagDefs) {
                    tagDef.setId((long) ret.getTagDefinitions().size());
                    ret.getTagDefinitions().put(tagDef.getId(), tagDef);
                }
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Entity " + entityWithTags + " does not have any tags associated with it when full-sync is being done.");
                LOG.debug("Will not add this entity to serviceTags, so that this entity, if exists,  will be removed from ranger");
            }
        }
    } else {
        LOG.error("Failed to build serviceResource for entity:" + entity.getGuid());
    }
    return ret;
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) RangerAtlasEntity(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) ArrayList(java.util.ArrayList)

Aggregations

ServiceTags (org.apache.ranger.plugin.util.ServiceTags)25 WebApplicationException (javax.ws.rs.WebApplicationException)10 XXService (org.apache.ranger.entity.XXService)9 Test (org.junit.Test)8 ExpectedException (org.junit.rules.ExpectedException)8 XXServiceDef (org.apache.ranger.entity.XXServiceDef)7 RangerService (org.apache.ranger.plugin.model.RangerService)7 XXServiceDao (org.apache.ranger.db.XXServiceDao)6 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 RangerAtlasEntity (org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity)3 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Map (java.util.Map)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)2 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)2