@Test public void testWriteDeleteExistsGetWithinAccount() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view = service.getResourceTypeView(account1, type1); String name = "name"; int version = 1; ResourceMeta meta = new ResourceMeta(name, version); view.add(meta); Assert.assertTrue(view.exists(name, version)); Assert.assertEquals(meta, view.get(name, version)); view.delete(name, version); Assert.assertFalse(view.exists(name, version)); Assert.assertNull(view.get(name, version)); }
try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); Set<ResourceMeta> metas = view.getAll(name); for (ResourceMeta meta : metas) { if (meta.getStatus() != ResourceStatus.INACTIVE) { view.delete(name, meta.getVersion()); pluginStore.deleteResource(account, resourceType, meta.getName(), meta.getVersion()); LOG.debug("deleted version {} of resource {} of type {} for account {}.",
/** * 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(); } }
try { PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); ResourceMeta meta = view.get(name, version); if (meta.getStatus() != ResourceStatus.INACTIVE) { throw new IllegalStateException("Resource must be inactive before it can be deleted."); view.delete(name, version); LOG.debug("deleted version {} of resource {} of type {} for account {} from meta store.", version, name, resourceType, account);
@Test public void testStageOnNothingIsNoOp() throws Exception { PluginMetaStoreService service = getPluginResourceMetaStoreService(); PluginResourceTypeView view = service.getResourceTypeView(account1, type1); ResourceMeta hadoop = new ResourceMeta("hadoop", 1, ResourceStatus.STAGED); view.add(hadoop); // if we stage a non-existent version, the current staged version should not be affected view.stage(hadoop.getName(), hadoop.getVersion() + 1); Assert.assertEquals(ResourceStatus.STAGED, view.get(hadoop.getName(), hadoop.getVersion()).getStatus()); }
view.add(hadoop1); view.add(hadoop2); view.add(hadoop3); view.add(mysql1); view.add(mysql2); view.recall(hadoop1.getName(), hadoop1.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.RECALLED, view.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop3.getName(), hadoop3.getVersion()).getStatus()); view.recall(hadoop2.getName(), hadoop2.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.RECALLED, view.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop3.getName(), hadoop3.getVersion()).getStatus()); view.recall(hadoop3.getName(), hadoop3.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.RECALLED, view.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop3.getName(), hadoop3.getVersion()).getStatus()); view.recall(mysql1.getName(), mysql1.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(mysql1.getName(), mysql1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.ACTIVE, view.get(mysql2.getName(), mysql2.getVersion()).getStatus()); view.recall(mysql2.getName(), mysql2.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(mysql1.getName(), mysql1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.RECALLED, view.get(mysql2.getName(), mysql2.getVersion()).getStatus());
view1.add(hadoop1); view1.add(hadoop2); view1.add(hadoop3); view1.add(mysql1); view1.add(mysql2); view1.add(apache1); view1.add(apache2); view2.add(bob1); view2.add(bob2); view2.add(sally1); view2.add(sue1); Assert.assertEquals(ResourceStatus.INACTIVE, view1.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.ACTIVE, view1.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view1.get(hadoop3.getName(), hadoop3.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view1.get(mysql1.getName(), mysql1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.ACTIVE, view1.get(mysql2.getName(), mysql2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view1.get(apache1.getName(), apache1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view1.get(apache2.getName(), apache2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view2.get(bob1.getName(), bob1.getVersion()).getStatus());
view.add(meta); "mysql", ImmutableSet.<ResourceMeta>of(mysql1, mysql2), "apache", ImmutableSet.<ResourceMeta>of(apache)), ImmutableMap.copyOf(view.getAll()) ); Assert.assertEquals(hadoops, ImmutableSet.copyOf(view.getAll("hadoop"))); Assert.assertEquals(mysqls, ImmutableSet.copyOf(view.getAll("mysql"))); Assert.assertEquals(apaches, ImmutableSet.copyOf(view.getAll("apache"))); "hadoop", ImmutableSet.<ResourceMeta>of(hadoop3), "mysql", ImmutableSet.<ResourceMeta>of(mysql2)), ImmutableMap.copyOf(view.getAll(ResourceStatus.ACTIVE)) ); Assert.assertEquals(Sets.newHashSet(hadoop3), view.getAll("hadoop", ResourceStatus.ACTIVE)); Assert.assertEquals(Sets.newHashSet(mysql2), view.getAll("mysql", ResourceStatus.ACTIVE)); Assert.assertTrue(view.getAll("apache", ResourceStatus.ACTIVE).isEmpty()); "hadoop", ImmutableSet.<ResourceMeta>of(hadoop2), "mysql", ImmutableSet.<ResourceMeta>of(mysql1)), ImmutableMap.copyOf(view.getAll(ResourceStatus.STAGED)) ); Assert.assertEquals(Sets.newHashSet(hadoop2), view.getAll("hadoop", ResourceStatus.STAGED)); Assert.assertEquals(Sets.newHashSet(mysql1), view.getAll("mysql", ResourceStatus.STAGED)); Assert.assertTrue(view.getAll("apache", ResourceStatus.STAGED).isEmpty()); ImmutableMap.<String, Set<ResourceMeta>>of( "apache", ImmutableSet.<ResourceMeta>of(apache)), ImmutableMap.copyOf(view.getAll(ResourceStatus.RECALLED))
@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()); }
PluginResourceTypeView view = metaStoreService.getResourceTypeView(account, resourceType); final int version = view.getHighestVersion(name) + 1; final ResourceMeta resourceMeta = new ResourceMeta(name, version); LOG.debug("getting output stream for version {} of resource {} of type {} for account {}", metaStoreService.getResourceTypeView(account, resourceType).add(resourceMeta);
/** * 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 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); } } };
/** * 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()); }
/** * 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; }
/** * 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; }
view.add(hadoop1); view.add(hadoop2); view.add(hadoop3); view.add(mysql); view.add(apache); view.add(php1); view.add(php2); view.stage(mysql.getName(), mysql.getVersion()); Assert.assertEquals(ResourceStatus.STAGED, view.get(mysql.getName(), mysql.getVersion()).getStatus()); view.stage(apache.getName(), apache.getVersion()); Assert.assertEquals(ResourceStatus.ACTIVE, view.get(apache.getName(), apache.getVersion()).getStatus()); view.stage(hadoop2.getName(), hadoop2.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.ACTIVE, view.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop3.getName(), hadoop3.getVersion()).getStatus()); view.stage(hadoop1.getName(), hadoop1.getVersion()); Assert.assertEquals(ResourceStatus.STAGED, view.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.RECALLED, view.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop3.getName(), hadoop3.getVersion()).getStatus()); view.stage(hadoop3.getName(), hadoop3.getVersion()); Assert.assertEquals(ResourceStatus.INACTIVE, view.get(hadoop1.getName(), hadoop1.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.RECALLED, view.get(hadoop2.getName(), hadoop2.getVersion()).getStatus()); Assert.assertEquals(ResourceStatus.STAGED, view.get(hadoop3.getName(), hadoop3.getVersion()).getStatus());
ResourceMeta meta1 = new ResourceMeta("name1", 3, ResourceStatus.ACTIVE); ResourceMeta meta2 = new ResourceMeta("name2", 2, ResourceStatus.INACTIVE); metaStoreService.getResourceTypeView(Account.SUPERADMIN, type1).add(meta1); metaStoreService.getResourceTypeView(Account.SUPERADMIN, type1).add(meta2); writePluginResource(Account.SUPERADMIN, type1, meta1.getName(), meta1.getVersion(), "meta1 contents"); writePluginResource(Account.SUPERADMIN, type1, meta2.getName(), meta2.getVersion(), "meta2 contents"); metaStoreService.getResourceTypeView(account, type1).get(meta1.getName(), meta1.getVersion())); Assert.assertEquals(meta2, metaStoreService.getResourceTypeView(account, type1).get(meta2.getName(), meta2.getVersion())); Assert.assertEquals("meta1 contents", readPluginResource(account, type1, meta1.getName(), meta1.getVersion())); Assert.assertEquals("meta2 contents", readPluginResource(account, type1, meta2.getName(), meta2.getVersion()));
/** * 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); }