/** * Get an unmodifiable collection of all tenant specifications. * * @return Unmodifiable collection of all tenant specifications * @throws IOException */ public Collection<TenantSpecification> getAllTenantSpecifications() throws IOException { return tenantStore.getAllTenantSpecifications(); }
private String tenantIdToName(String id) throws IOException { String tenantName = tenantStore.getNameForId(id); if (tenantName == null) { LOG.warn("Could not map tenant id {} to a name, will use the id."); tenantName = id; } return tenantName; }
@Override public void clearState() throws Exception { sqlStore.clearData(); }
@Test public void testAddWriteDelete() throws IOException { Tenant tenant = new Tenant(UUID.randomUUID().toString(), new TenantSpecification("name", 10, 100, 1000)); store.writeTenant(tenant); Assert.assertEquals(tenant, store.getTenantByID(tenant.getId())); Assert.assertEquals(tenant, store.getTenantByName(tenant.getSpecification().getName())); store.deleteTenantByName(tenant.getSpecification().getName()); Assert.assertNull(store.getTenantByID(tenant.getId())); Assert.assertNull(store.getTenantByName(tenant.getSpecification().getName())); }
@Test public void testOverwrite() throws IOException { Tenant tenant = new Tenant(UUID.randomUUID().toString(), new TenantSpecification("name", 10, 100, 1000)); store.writeTenant(tenant); tenant = new Tenant(tenant.getId(), new TenantSpecification("name", 10, 100, 500)); store.writeTenant(tenant); Assert.assertEquals(tenant, store.getTenantByID(tenant.getId())); } }
@Test public void testGetNameForId() throws IOException { Tenant tenant1 = new Tenant(UUID.randomUUID().toString(), new TenantSpecification("name1", 10, 100, 1000)); Tenant tenant2 = new Tenant(UUID.randomUUID().toString(), new TenantSpecification("name2", 10, 100, 1000)); store.writeTenant(tenant1); store.writeTenant(tenant2); Assert.assertEquals(tenant1.getSpecification().getName(), store.getNameForId(tenant1.getId())); Assert.assertEquals(tenant2.getSpecification().getName(), store.getNameForId(tenant2.getId())); }
@Test public void testForbiddenIfNonadminGetsQueueMetrics() throws Exception { tenantStore.writeTenant( new Tenant(UUID.randomUUID().toString(), new TenantSpecification(USER1_ACCOUNT.getTenantId(), 10, 10, 100))); assertResponseStatus(doGetExternalAPI("/metrics/queues", USER1_HEADERS), HttpResponseStatus.FORBIDDEN); }
/** * Verify that tenant cluster and node quotas would not be exceeded if the given number of additional clusters and * nodes would be added. * * @param tenantId Id of the tenant to verify quotas for * @param additionalClusters Number of clusters that would be added * @param additionalNodes Number of nodes that would be added * @return true if the tenant quotas would be satisfied, false if they would be exceeded. */ public boolean satisfiesTenantQuotas(String tenantId, int additionalClusters, int additionalNodes) throws IOException { Tenant tenant = tenantStore.getTenantByID(tenantId); // if there is no tenant there are no quotas to voilate if (tenant == null) { return true; } return satisfiesTenantQuotas(tenant, additionalClusters, additionalNodes); }
/** * Get a tenant specification by tenant name. * * @param name Name of the tenant to get * @return Tenant for the given name, or null if none exists * @throws IOException */ public TenantSpecification getTenantSpecification(String name) throws IOException { Tenant tenant = tenantStore.getTenantByName(name); return tenant == null ? null : tenant.getSpecification(); }
@Override protected void startUp() throws Exception { if (dbConnectionPool.isEmbeddedDerbyDB()) { DBHelper.createDerbyTableIfNotExists( "CREATE TABLE tenants ( " + "id VARCHAR(255), " + "name VARCHAR(255), " + "workers INT, " + "deleted BOOLEAN, " + "create_time TIMESTAMP, " + "delete_time TIMESTAMP, " + "tenant BLOB )", dbConnectionPool); } // add superadmin if it doesn't exist Tenant superadminTenant = getTenantByName(Constants.SUPERADMIN_TENANT); if (superadminTenant == null) { writeTenant(Tenant.DEFAULT_SUPERADMIN); } }
private int getTotalQueueSize() throws IOException { int totalSize = 0; for (Tenant tenant : tenantStore.getAllTenants()) { totalSize += taskQueues.size(tenant.getId()); } return totalSize; }
/** * Get the name of the tenant with the given id. Id to name mapping is cached, so this will not involve an io * operation except for the first time it is called. * * @param id Id of the tenant to get the name for * @return Name of the tenant with the given id, or null if the tenant does not exist * @throws IOException */ @Override public String getNameForId(String id) throws IOException { if (idToNameMap.containsKey(id)) { return idToNameMap.get(id); } String name = getNameFromDB(id); // if there is no tenant, return null if (name == null) { return null; } // cache the result since id to name will never change idToNameMap.put(id, name); return name; }
@Before public void setupServiceTest() throws Exception { tenantProvisionerService.writeProvisioner(new Provisioner(PROVISIONER_ID, "host1", 12345, 100, null, null)); tenantStore.writeTenant(new Tenant(TENANT_ID, new TenantSpecification(TENANT, 10, 100, 1000))); }
@Test public void testGetNonExistantTenantReturnsNull() throws IOException { Assert.assertNull(store.getTenantByID(UUID.randomUUID().toString())); }
@Test public void testSuperadminExistsOnStart() throws IOException { Assert.assertEquals(Tenant.DEFAULT_SUPERADMIN, store.getTenantByName(Constants.SUPERADMIN_TENANT)); }
@Before public void setupSuperadminHandlerTest() throws SQLException, IOException { // base tests will write some tenants that we don't want. ((SQLTenantStore) tenantStore).clearData(); ((SQLProvisionerStore) provisionerStore).clearData(); ((SQLEntityStoreService) entityStoreService).clearData(); tenantProvisionerService.writeProvisioner( new Provisioner("p1", "host", 12345, 100, ImmutableMap.<String, Integer>of(), ImmutableMap.<String, Integer>of()) ); }
/** * Get a snapshot of the number of queued and in progress elements for all queues in the task queues. * * @return Snapshot of the number of queued and in progress elements in the task queues */ public Map<String, QueueMetrics> getTaskQueueMetricsSnapshot() throws IOException { // get tenant list from the tenant store instead of from the queue group because queues are loaded lazily // in the queue group and not derived from zookeeper, so we might not get all the queues. Map<String, QueueMetrics> queueMetrics = Maps.newHashMap(); for (Tenant tenant : tenantStore.getAllTenants()) { queueMetrics.put(tenant.getSpecification().getName(), queueMetricsCache.getUnchecked(tenant.getId())); } return queueMetrics; }
@Test(expected = IllegalStateException.class) public void testDeleteTenantWithAssignedWorkersThrowsException() throws Exception { Tenant tenant1 = new Tenant("tenant1", new TenantSpecification("tenant1", 10, 10, 100)); Provisioner provisioner1 = new Provisioner("p1", "host1", 12345, 100, null, ImmutableMap.<String, Integer>of( tenant1.getId(), 10)); service.writeProvisioner(provisioner1); tenantStore.writeTenant(tenant1); service.deleteTenantByName(tenant1.getId()); }
@Test public void testDeleteTenant() throws Exception { String id = UUID.randomUUID().toString(); tenantProvisionerService.writeTenantSpecification(new TenantSpecification("companyX", 0, 100, 1000)); // perform request to delete tenant HttpResponse response = doDeleteExternalAPI("/tenants/" + id, SUPERADMIN_HEADERS); assertResponseStatus(response, HttpResponseStatus.OK); Assert.assertNull(tenantStore.getTenantByID(id)); }
@Test public void testAddWorkersToProvisioner() throws Exception { Tenant tenant = new Tenant("id123", new TenantSpecification("tenantX", 50, 10, 100)); Provisioner provisioner = new Provisioner("p1", "host", 12345, 100, null, null); service.writeProvisioner(provisioner); tenantStore.writeTenant(tenant); service.rebalanceTenantWorkers(tenant.getId()); Provisioner actual = service.getProvisioner(provisioner.getId()); Assert.assertEquals(tenant.getSpecification().getWorkers(), actual.getAssignedWorkers(tenant.getSpecification().getName())); }