/** * Update the metadata store, syncing the resources in the given collection for the given account. * * @param account Account containing resources to sync * @param resourceCollection Collection of synced resources * @throws IOException */ public void syncResourceMeta(Account account, ResourceCollection resourceCollection) throws IOException { metaStoreService.getAccountView(account).syncResources(resourceCollection); }
/** * Get all resource metadata of the given type that belong to the given account that have the given status. * * @param account Account containing the resources * @param resourceType Type of resource to get * @param status Status of the resources to get. If null, resources of any status are returned. * @return Immutable map of resource name to resource metadata * @throws IOException if there was an error getting the resources */ public Map<String, Set<ResourceMeta>> getAll(Account account, ResourceType resourceType, ResourceStatus status) throws IOException { if (status == null) { return metaStoreService.getResourceTypeView(account, resourceType).getAll(); } return metaStoreService.getResourceTypeView(account, resourceType).getAll(status); }
@Override protected void startUp() throws Exception { pluginStore.initialize(conf); metaStoreService.startAndWait(); }
@Test public void testGetNumResources() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); // for account1 write 6 resources (7 but one is deleted) in all different states service.getResourceTypeView(account1, type1).add(new ResourceMeta("r1", 1, ResourceStatus.ACTIVE)); service.getResourceTypeView(account1, type1).add(new ResourceMeta("r1", 2, ResourceStatus.INACTIVE)); service.getResourceTypeView(account1, type1).add(new ResourceMeta("r2", 1, ResourceStatus.INACTIVE)); service.getResourceTypeView(account1, type1).add(new ResourceMeta("r2", 2, ResourceStatus.STAGED)); service.getResourceTypeView(account1, type2).add(new ResourceMeta("r3", 1, ResourceStatus.RECALLED)); service.getResourceTypeView(account1, type2).add(new ResourceMeta("r3", 2, ResourceStatus.STAGED)); service.getResourceTypeView(account1, type2).add(new ResourceMeta("r3", 3, ResourceStatus.STAGED)); service.getResourceTypeView(account1, type2).delete("r3", 3); Assert.assertEquals(6, service.getAccountView(account1).numResources()); // account 2 should have nothing Assert.assertEquals(0, service.getAccountView(account2).numResources()); }
@Override protected void shutDown() throws Exception { metaStoreService.stopAndWait(); }
@Test public void testSyncStatus() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view1 = service.getResourceTypeView(account1, type1); PluginResourceTypeView view2 = service.getResourceTypeView(account1, type2); ResourceMeta hadoop1 = new ResourceMeta("hadoop", 1, ResourceStatus.INACTIVE); ResourceMeta hadoop2 = new ResourceMeta("hadoop", 2, ResourceStatus.STAGED); syncedResources.addResources(type2, new ResourceTypeSpecification(ResourceTypeFormat.FILE, "400"), ImmutableSet.of(bob2, sally1)); service.getAccountView(account1).syncResources(syncedResources);
/** * Get all metadata for versions of the given resource that have the given status. * * @param account Account containing the resource * @param resourceType Type of resource to get * @param name Name of the resource to get * @param status Status of the resources to get. If null, resources of any status are returned. * @return Immutable set of metadata for versions of the given module * @throws IOException if there was an error getting the module versions */ public Set<ResourceMeta> getAll(Account account, ResourceType resourceType, String name, ResourceStatus status) throws IOException { if (status == null) { return metaStoreService.getResourceTypeView(account, resourceType).getAll(name); } return metaStoreService.getResourceTypeView(account, resourceType).getAll(name, status); }
/** * Get the number of all versions of all resources in the account. Resources can be in any state. * * @param account Account to get the number of resources for * @return Number of resources in the account * @throws IOException */ public int numResources(Account account) throws IOException { return metaStoreService.getAccountView(account).numResources(); }
@Override public void handleError(Throwable t) { LOG.error("Error uploading version {} of resource {} of type {} for account {}.", version, name, resourceType, account, t); try { os.close(); // deletion flags the entry in the database as deleted metaStoreService.getResourceTypeView(account, resourceType).delete(name, version); // dont need the file in the plugin store if there was an error so delete it pluginStore.deleteResource(account, resourceType, name, version); responder.sendError(HttpResponseStatus.INTERNAL_SERVER_ERROR, t.getCause().getMessage()); } catch (IOException e) { LOG.error("Error uploading resource {} of type {} for account {}.", resourceMeta, resourceType, account, e); } } };
/** * Recall the given resource for the given account. A no-op if there is no staged version. * * @param account Account that contains the resource * @param resourceType Type of resource to deactivate * @param name Name of the resource to deactivate * @param version Version of resource to stage * @throws MissingEntityException if there is no such module * @throws IOException if there was an error deactivating all versions of the module */ public void recall(Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { LOG.debug("Recalling version {} of resource {} of type {} for account {}.", version, name, resourceType, account); Lock lock = getResourceLock(account, resourceType, name); lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); if (!view.exists(name, version)) { throw new MissingEntityException("Resource " + name + " does not exist."); } view.recall(name, version); } finally { lock.unlock(); } }
/** * Atomically stage the specified resource version for the given account and recall the previous staged version. * A staged version will get pushed to provisioners during a sync, and will stay staged unless explicitly recalled. * * @param account Account that contains the resource * @param resourceType Type of resource to stage * @param name Name of resource to stage * @param version Version of resource to stage * @throws MissingEntityException if there is no such resource version * @throws IOException if there was an error staging the resource */ public void stage(Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { LOG.debug("staging version {} of resource {} of type {} for account {}.", version, name, resourceType, account); Lock lock = getResourceLock(account, resourceType, name); lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); if (!view.exists(name, version)) { throw new MissingEntityException("Resource does not exist."); } view.stage(name, version); } finally { lock.unlock(); } }
PluginResourceTypeView metaView = metaStoreService.getResourceTypeView(account, type); if (!metaView.exists(name, version)) { metaView.add(meta);
lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); ResourceMeta meta = view.get(name, version); if (meta.getStatus() != ResourceStatus.INACTIVE) {
lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); metaStoreService.getResourceTypeView(account, resourceType).add(resourceMeta);
/** * Get the resources that should be synced for a given account. * * @param account Account for which to get resources to sync * @return Resources that should be synced for the given account */ public ResourceCollection getResourcesToSync(Account account) throws IOException { ResourceCollection resourceCollection = new ResourceCollection(); Set<ImmutablePair<ResourceType, ResourceTypeSpecification>> typeFormats = getTypesAndFormats(account); Set<ResourceType> resourceTypes = Sets.newHashSet(); for (ImmutablePair<ResourceType, ResourceTypeSpecification> typeFormat : typeFormats) { ResourceType resourceType = typeFormat.getFirst(); ResourceTypeSpecification typeSpec = typeFormat.getSecond(); Set<ResourceMeta> resources = metaStoreService.getResourceTypeView(account, resourceType).getResourcesToSync(); resourceCollection.addResources(resourceType, typeSpec, resources); resourceTypes.add(resourceType); } return resourceCollection; }
/** * Bootstrap an account's resources by copying what the superadmin has into the account. * * @param account Account to bootstrap */ public void bootstrapResources(Account account) throws IOException { for (ImmutablePair<ResourceType, ResourceTypeSpecification> typePair : getTypesAndFormats(Account.SUPERADMIN)) { ResourceType type = typePair.getFirst(); Map<String, Set<ResourceMeta>> resources = metaStoreService.getResourceTypeView(Account.SUPERADMIN, type).getAll(); for (Set<ResourceMeta> metas : resources.values()) { for (ResourceMeta meta : metas) { LOG.debug("copying version {} of resource {} of type {} from superadmin account to account {}", meta.getVersion(), meta.getName(), type, account); copySuperadminResource(account, type, meta); } } } }
/** * Get an input stream for reading the plugin resource. * * @param account Account the resource belongs to * @param resourceType Type of resource * @param name Name of resource to get an input stream for * @param version Version of resource to get an input stream for * @return Input stream for reading the given plugin resource * @throws MissingEntityException if there is no such resource version * @throws IOException if there was an error getting the input stream for the resource */ public InputStream getResourceInputStream(final Account account, ResourceType resourceType, String name, int version) throws MissingEntityException, IOException { // no lock needed since each resource uploaded gets its own id. ResourceMeta meta = metaStoreService.getResourceTypeView(account, resourceType).get(name, version); if (meta == null) { throw new MissingEntityException("Resource not found."); } LOG.debug("getting input stream for version {} of resource {} of type {} for account {}.", version, name, resourceType, account); return pluginStore.getResourceInputStream(account, resourceType, meta.getName(), meta.getVersion()); }
lock.lock(); try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); Set<ResourceMeta> metas = view.getAll(name); for (ResourceMeta meta : metas) {
/** * Get the active resources for a given account. * * @param account Account to get active resources for * @return Active resources for the given account */ public ResourceCollection getLiveResources(Account account) throws IOException { ResourceCollection resourceCollection = new ResourceCollection(); for (ImmutablePair<ResourceType, ResourceTypeSpecification> typeSpecs : getTypesAndFormats(account)) { ResourceType resourceType = typeSpecs.getFirst(); ResourceTypeSpecification typeSpec = typeSpecs.getSecond(); Set<ResourceMeta> resources = metaStoreService.getResourceTypeView(account, resourceType).getLiveResources(); resourceCollection.addResources(resourceType, typeSpec, resources); } return resourceCollection; }
@Test(expected = IllegalArgumentException.class) public void testOnlyAdminsHaveAccess() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); service.getResourceTypeView(new Account("notadmin", "tenant"), new ResourceType(PluginType.AUTOMATOR, "chef-solo", "cookbooks")); }