diff options
author | Harald Musum <musum@oath.com> | 2017-11-15 19:15:27 +0100 |
---|---|---|
committer | Harald Musum <musum@oath.com> | 2017-11-15 19:15:27 +0100 |
commit | fd0541930ea6bf04b5188000eff95bcb0e36b64f (patch) | |
tree | 3e07701f972ba57cd78a063bb4c76d600d1e71e9 /configserver | |
parent | c7c7f25a47b9debfc07a6ebbe7702d3d8ed8c476 (diff) |
Cleanup and simplify code related to zk paths for tenants
No functional changes
Diffstat (limited to 'configserver')
11 files changed, 153 insertions, 111 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ZKTenantApplications.java b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ZKTenantApplications.java index 648e6bb7180..9d22f7ee08b 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/application/ZKTenantApplications.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/application/ZKTenantApplications.java @@ -47,6 +47,7 @@ public class ZKTenantApplications implements TenantApplications, PathChildrenCac private ZKTenantApplications(Curator curator, Path applicationsPath, ReloadHandler reloadHandler, TenantName tenant) { this.curator = curator; this.applicationsPath = applicationsPath; + curator.create(applicationsPath); this.reloadHandler = reloadHandler; this.tenant = tenant; this.directoryCache = curator.createDirectoryCache(applicationsPath.getAbsolute(), false, false, pathChildrenExecutor); @@ -54,9 +55,9 @@ public class ZKTenantApplications implements TenantApplications, PathChildrenCac this.directoryCache.addListener(this); } - public static TenantApplications create(Curator curator, Path applicationsPath, ReloadHandler reloadHandler, TenantName tenant) { + public static TenantApplications create(Curator curator, ReloadHandler reloadHandler, TenantName tenant) { try { - return new ZKTenantApplications(curator, applicationsPath, reloadHandler, tenant); + return new ZKTenantApplications(curator, Tenants.getApplicationsPath(tenant), reloadHandler, tenant); } catch (Exception e) { throw new RuntimeException(Tenants.logPre(tenant) + "Error creating application repo", e); } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionFactory.java b/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionFactory.java index 298acaca901..9aea683a83f 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionFactory.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionFactory.java @@ -6,6 +6,7 @@ import com.yahoo.path.Path; import com.yahoo.config.model.api.ConfigDefinitionRepo; import com.yahoo.cloud.config.ConfigserverConfig; import com.yahoo.vespa.config.server.GlobalComponentRegistry; +import com.yahoo.vespa.config.server.tenant.Tenants; import com.yahoo.vespa.config.server.zookeeper.ConfigCurator; import com.yahoo.vespa.curator.Curator; @@ -19,20 +20,19 @@ public class RemoteSessionFactory { private final GlobalComponentRegistry componentRegistry; private final Curator curator; private final ConfigCurator configCurator; - private final Path sessionDirPath; + private final Path sessionPath; private final ConfigDefinitionRepo defRepo; private final TenantName tenant; private final ConfigserverConfig configserverConfig; private final Clock clock; public RemoteSessionFactory(GlobalComponentRegistry componentRegistry, - Path sessionsPath, TenantName tenant, Clock clock) { this.componentRegistry = componentRegistry; this.curator = componentRegistry.getCurator(); this.configCurator = componentRegistry.getConfigCurator(); - this.sessionDirPath = sessionsPath; + this.sessionPath = Tenants.getSessionsPath(tenant); this.tenant = tenant; this.defRepo = componentRegistry.getConfigDefinitionRepo(); this.configserverConfig = componentRegistry.getConfigserverConfig(); @@ -40,7 +40,7 @@ public class RemoteSessionFactory { } public RemoteSession createSession(long sessionId) { - Path sessionPath = sessionDirPath.append(String.valueOf(sessionId)); + Path sessionPath = this.sessionPath.append(String.valueOf(sessionId)); SessionZooKeeperClient sessionZKClient = new SessionZooKeeperClient(curator, configCurator, sessionPath, diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionRepo.java b/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionRepo.java index 659a44bb339..2269a7ed997 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionRepo.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/session/RemoteSessionRepo.java @@ -8,10 +8,12 @@ import java.util.logging.Logger; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; +import com.yahoo.config.provision.TenantName; import com.yahoo.log.LogLevel; import com.yahoo.path.Path; import com.yahoo.transaction.NestedTransaction; import com.yahoo.vespa.config.server.application.ApplicationSet; +import com.yahoo.vespa.config.server.tenant.Tenants; import com.yahoo.vespa.curator.Curator; import com.yahoo.yolean.Exceptions; import com.yahoo.vespa.config.server.ReloadHandler; @@ -49,19 +51,19 @@ public class RemoteSessionRepo extends SessionRepo<RemoteSession> implements Nod * @param curator a {@link Curator} instance. * @param remoteSessionFactory a {@link com.yahoo.vespa.config.server.session.RemoteSessionFactory} * @param reloadHandler a {@link com.yahoo.vespa.config.server.ReloadHandler} - * @param sessionsPath a {@link com.yahoo.path.Path} to the sessions dir. - * @param applicationRepo an {@link TenantApplications} object. + * @param tenant a {@link TenantName} instance. + * @param applicationRepo a {@link TenantApplications} instance. * @param executorService an {@link ExecutorService} to run callbacks from ZooKeeper. */ public RemoteSessionRepo(Curator curator, - RemoteSessionFactory remoteSessionFactory, - ReloadHandler reloadHandler, - Path sessionsPath, - TenantApplications applicationRepo, - MetricUpdater metricUpdater, - ExecutorService executorService) { + RemoteSessionFactory remoteSessionFactory, + ReloadHandler reloadHandler, + TenantName tenant, + TenantApplications applicationRepo, + MetricUpdater metricUpdater, + ExecutorService executorService) { this.curator = curator; - this.sessionsPath = sessionsPath; + this.sessionsPath = Tenants.getSessionsPath(tenant); this.applicationRepo = applicationRepo; this.remoteSessionFactory = remoteSessionFactory; this.reloadHandler = reloadHandler; diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionFactoryImpl.java b/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionFactoryImpl.java index 1d5025f2e61..fdc681b5fb6 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionFactoryImpl.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionFactoryImpl.java @@ -16,6 +16,7 @@ import com.yahoo.config.provision.TenantName; import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs; import com.yahoo.vespa.config.server.host.HostValidator; +import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.Tenants; import com.yahoo.vespa.config.server.zookeeper.SessionCounter; import com.yahoo.vespa.config.server.zookeeper.ConfigCurator; @@ -57,7 +58,6 @@ public class SessionFactoryImpl implements SessionFactory, LocalSessionLoader { public SessionFactoryImpl(GlobalComponentRegistry globalComponentRegistry, SessionCounter sessionCounter, - Path sessionsPath, TenantApplications applicationRepo, TenantFileSystemDirs tenantFileSystemDirs, HostValidator<ApplicationId> hostRegistry, @@ -68,7 +68,7 @@ public class SessionFactoryImpl implements SessionFactory, LocalSessionLoader { this.curator = globalComponentRegistry.getCurator(); this.configCurator = globalComponentRegistry.getConfigCurator(); this.sessionCounter = sessionCounter; - this.sessionsPath = sessionsPath; + this.sessionsPath = Tenants.getSessionsPath(tenant); this.applicationRepo = applicationRepo; this.tenantFileSystemDirs = tenantFileSystemDirs; this.superModelGenerationCounter = globalComponentRegistry.getSuperModelGenerationCounter(); diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java index 084d35a42d4..61145c2a138 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java @@ -34,7 +34,6 @@ public class TenantBuilder { private final Path tenantPath; private final GlobalComponentRegistry componentRegistry; private final TenantName tenant; - private final Path sessionsPath; private RemoteSessionRepo remoteSessionRepo; private LocalSessionRepo localSessionRepo; private SessionFactory sessionFactory; @@ -47,15 +46,14 @@ public class TenantBuilder { private TenantFileSystemDirs tenantFileSystemDirs; private HostValidator<ApplicationId> hostValidator; - private TenantBuilder(GlobalComponentRegistry componentRegistry, TenantName tenant, Path zkPath) { + private TenantBuilder(GlobalComponentRegistry componentRegistry, TenantName tenant) { this.componentRegistry = componentRegistry; - this.tenantPath = zkPath; + this.tenantPath = Tenants.getTenantPath(tenant); this.tenant = tenant; - this.sessionsPath = tenantPath.append(Tenant.SESSIONS); } - public static TenantBuilder create(GlobalComponentRegistry componentRegistry, TenantName tenant, Path zkPath) { - return new TenantBuilder(componentRegistry, tenant, zkPath); + public static TenantBuilder create(GlobalComponentRegistry componentRegistry, TenantName tenant) { + return new TenantBuilder(componentRegistry, tenant); } public TenantBuilder withSessionFactory(SessionFactory sessionFactory) { @@ -123,7 +121,7 @@ public class TenantBuilder { private void createSessionFactory() { if (sessionFactory == null || localSessionLoader == null) { - SessionFactoryImpl impl = new SessionFactoryImpl(componentRegistry, sessionCounter, sessionsPath, + SessionFactoryImpl impl = new SessionFactoryImpl(componentRegistry, sessionCounter, applicationRepo, tenantFileSystemDirs, hostValidator, tenant); if (sessionFactory == null) { sessionFactory = impl; @@ -136,13 +134,13 @@ public class TenantBuilder { private void createApplicationRepo() { if (applicationRepo == null) { - applicationRepo = ZKTenantApplications.create(componentRegistry.getCurator(), tenantPath.append(Tenant.APPLICATIONS), reloadHandler, tenant); + applicationRepo = ZKTenantApplications.create(componentRegistry.getCurator(), reloadHandler, tenant); } } private void createSessionCounter() { if (sessionCounter == null) { - sessionCounter = new SessionCounter(componentRegistry.getCurator(), tenantPath, sessionsPath); + sessionCounter = new SessionCounter(componentRegistry.getCurator(), tenant); } } @@ -167,7 +165,7 @@ public class TenantBuilder { private void createRemoteSessionFactory(Clock clock) { if (remoteSessionFactory == null) { - remoteSessionFactory = new RemoteSessionFactory(componentRegistry, sessionsPath, tenant, clock); + remoteSessionFactory = new RemoteSessionFactory(componentRegistry, tenant, clock); } } @@ -176,7 +174,7 @@ public class TenantBuilder { remoteSessionRepo = new RemoteSessionRepo(componentRegistry.getCurator(), remoteSessionFactory, reloadHandler, - sessionsPath, + tenant, applicationRepo, componentRegistry.getMetrics().getOrCreateMetricUpdater(Metrics.createDimensions(tenant)), createSingleThreadedExecutorService(RemoteSessionRepo.class.getName())); diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/Tenants.java b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/Tenants.java index 528a30e0191..d2cf17a38d4 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/Tenants.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/Tenants.java @@ -189,7 +189,7 @@ public class Tenants implements ConnectionStateListener, PathChildrenCacheListen if (tenants.containsKey(tenantName)) return; try { - Tenant tenant = TenantBuilder.create(globalComponentRegistry, tenantName, getTenantPath(tenantName)).build(); + Tenant tenant = TenantBuilder.create(globalComponentRegistry, tenantName).build(); notifyNewTenant(tenant); tenants.put(tenantName, tenant); } catch (Exception e) { @@ -351,6 +351,7 @@ public class Tenants implements ConnectionStateListener, PathChildrenCacheListen /** * Gets zookeeper path for tenant data + * * @param tenantName tenant name * @return a {@link com.yahoo.path.Path} to the zookeeper data for a tenant */ @@ -358,4 +359,24 @@ public class Tenants implements ConnectionStateListener, PathChildrenCacheListen return tenantsPath.append(tenantName.value()); } + /** + * Gets zookeeper path for session data for a tenant + * + * @param tenantName tenant name + * @return a {@link com.yahoo.path.Path} to the zookeeper sessions data for a tenant + */ + public static Path getSessionsPath(TenantName tenantName) { + return getTenantPath(tenantName).append(Tenant.SESSIONS); + } + + /** + * Gets zookeeper path for application data for a tenant + * + * @param tenantName tenant name + * @return a {@link com.yahoo.path.Path} to the zookeeper application data for a tenant + */ + public static Path getApplicationsPath(TenantName tenantName) { + return getTenantPath(tenantName).append(Tenant.APPLICATIONS); + } + } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/SessionCounter.java b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/SessionCounter.java index 2d95a013da9..4df292dd204 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/SessionCounter.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/zookeeper/SessionCounter.java @@ -1,7 +1,8 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.config.server.zookeeper; -import com.yahoo.path.Path; +import com.yahoo.config.provision.TenantName; +import com.yahoo.vespa.config.server.tenant.Tenants; import com.yahoo.vespa.curator.Curator; /** @@ -12,8 +13,10 @@ import com.yahoo.vespa.curator.Curator; */ public class SessionCounter extends InitializedCounter { - public SessionCounter(Curator curator, Path rootPath, Path sessionsDir) { - super(curator, rootPath.append("sessionCounter").getAbsolute(), sessionsDir.getAbsolute()); + public SessionCounter(Curator curator, TenantName tenantName) { + super(curator, + Tenants.getTenantPath(tenantName).append("sessionCounter").getAbsolute(), + Tenants.getSessionsPath(tenantName).getAbsolute()); } /** diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java index 1a14ac1761c..08cfa74da3b 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java @@ -3,11 +3,11 @@ package com.yahoo.vespa.config.server.application; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; -import com.yahoo.path.Path; import com.yahoo.text.Utf8; import com.yahoo.vespa.config.server.MockReloadHandler; import com.yahoo.vespa.config.server.TestWithCurator; +import com.yahoo.vespa.config.server.tenant.Tenants; import org.junit.Test; import java.util.Arrays; @@ -18,60 +18,61 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.*; /** - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class TenantApplicationsTest extends TestWithCurator { + private static final TenantName tenantName = TenantName.from("tenant"); + @Test public void require_that_applications_are_read_from_zookeeper() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz", Utf8.toAsciiBytes(3)); - curatorFramework.create().creatingParentsIfNeeded().forPath("/bar:test:bim", Utf8.toAsciiBytes(4)); + writeApplicationData(createApplicationId("foo"), 3L); + writeApplicationData(createApplicationId("bar"), 4L); TenantApplications repo = createZKAppRepo(); List<ApplicationId> applications = repo.listApplications(); assertThat(applications.size(), is(2)); - assertThat(applications.get(0).application().value(), is("dev")); - assertThat(applications.get(1).application().value(), is("test")); + assertThat(applications.get(0).application().value(), is("foo")); + assertThat(applications.get(1).application().value(), is("bar")); assertThat(repo.getSessionIdForApplication(applications.get(0)), is(3L)); assertThat(repo.getSessionIdForApplication(applications.get(1)), is(4L)); } @Test public void require_that_invalid_entries_are_skipped() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz"); - curatorFramework.create().creatingParentsIfNeeded().forPath("/invalid"); + writeApplicationData(createApplicationId("foo"), 3L); + writeApplicationData("invalid", 3L); TenantApplications repo = createZKAppRepo(); List<ApplicationId> applications = repo.listApplications(); assertThat(applications.size(), is(1)); - assertThat(applications.get(0).application().value(), is("dev")); + assertThat(applications.get(0).application().value(), is("foo")); } @Test(expected = IllegalArgumentException.class) public void require_that_requesting_session_for_unknown_application_throws_exception() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz:bim"); TenantApplications repo = createZKAppRepo(); - repo.getSessionIdForApplication(new ApplicationId.Builder() - .tenant("exist") - .applicationName("tenant").instanceName("here").build()); + repo.getSessionIdForApplication(createApplicationId("nonexistent")); } @Test(expected = IllegalArgumentException.class) public void require_that_requesting_session_for_empty_application_throws_exception() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz:bim"); + ApplicationId baz = createApplicationId("baz"); + // No data in node + curatorFramework.create().creatingParentsIfNeeded() + .forPath(Tenants.getApplicationsPath(tenantName).append(baz.serializedForm()).getAbsolute()); TenantApplications repo = createZKAppRepo(); - repo.getSessionIdForApplication(new ApplicationId.Builder() - .tenant("tenant") - .applicationName("foo").instanceName("bim").build()); + repo.getSessionIdForApplication(baz); } @Test public void require_that_application_ids_can_be_written() throws Exception { TenantApplications repo = createZKAppRepo(); - repo.createPutApplicationTransaction(createAppplicationId("myapp"), 3l).commit(); - String path = "/mytenant:myapp:myinst"; + ApplicationId myapp = createApplicationId("myapp"); + repo.createPutApplicationTransaction(myapp, 3l).commit(); + String path = Tenants.getApplicationsPath(tenantName).append(myapp.serializedForm()).getAbsolute(); assertTrue(curatorFramework.checkExists().forPath(path) != null); assertThat(Utf8.toString(curatorFramework.getData().forPath(path)), is("3")); - repo.createPutApplicationTransaction(createAppplicationId("myapp"), 5l).commit(); + repo.createPutApplicationTransaction(myapp, 5l).commit(); assertTrue(curatorFramework.checkExists().forPath(path) != null); assertThat(Utf8.toString(curatorFramework.getData().forPath(path)), is("5")); } @@ -79,8 +80,8 @@ public class TenantApplicationsTest extends TestWithCurator { @Test public void require_that_application_ids_can_be_deleted() throws Exception { TenantApplications repo = createZKAppRepo(); - ApplicationId id1 = createAppplicationId("myapp"); - ApplicationId id2 = createAppplicationId("myapp2"); + ApplicationId id1 = createApplicationId("myapp"); + ApplicationId id2 = createApplicationId("myapp2"); repo.createPutApplicationTransaction(id1, 1).commit(); repo.createPutApplicationTransaction(id2, 1).commit(); assertThat(repo.listApplications().size(), is(2)); @@ -95,8 +96,8 @@ public class TenantApplicationsTest extends TestWithCurator { TenantApplications zkRepo = createZKAppRepo(); TenantApplications memRepo = new MemoryTenantApplications(); for (TenantApplications repo : Arrays.asList(zkRepo, memRepo)) { - ApplicationId id1 = createAppplicationId("myapp"); - ApplicationId id2 = createAppplicationId("myapp2"); + ApplicationId id1 = createApplicationId("myapp"); + ApplicationId id2 = createApplicationId("myapp2"); repo.createPutApplicationTransaction(id1, 4).commit(); repo.createPutApplicationTransaction(id2, 5).commit(); List<ApplicationId> lst = repo.listApplications(); @@ -122,21 +123,19 @@ public class TenantApplicationsTest extends TestWithCurator { @Test public void require_that_reload_handler_is_called_when_apps_are_removed() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:test:baz", Utf8.toAsciiBytes(3)); - curatorFramework.create().creatingParentsIfNeeded().forPath("/bar:dev:bim", Utf8.toAsciiBytes(4)); + ApplicationId foo = createApplicationId("foo"); + writeApplicationData(foo, 3L); + writeApplicationData(createApplicationId("bar"), 4L); MockReloadHandler reloadHandler = new MockReloadHandler(); TenantApplications repo = createZKAppRepo(reloadHandler); assertNull(reloadHandler.lastRemoved); - repo.deleteApplication(new ApplicationId.Builder() - .tenant("foo") - .applicationName("test").instanceName("baz").build()) - .commit(); + repo.deleteApplication(foo).commit(); long endTime = System.currentTimeMillis() + 60_000; while (System.currentTimeMillis() < endTime && reloadHandler.lastRemoved == null) { Thread.sleep(100); } assertNotNull(reloadHandler.lastRemoved); - assertThat(reloadHandler.lastRemoved.serializedForm(), is("foo:test:baz")); + assertThat(reloadHandler.lastRemoved.serializedForm(), is(foo.serializedForm())); } private TenantApplications createZKAppRepo() { @@ -144,12 +143,26 @@ public class TenantApplicationsTest extends TestWithCurator { } private TenantApplications createZKAppRepo(MockReloadHandler reloadHandler) { - return ZKTenantApplications.create(curator, Path.createRoot(), reloadHandler, TenantName.from("mytenant")); + return ZKTenantApplications.create(curator, reloadHandler, tenantName); } - private static ApplicationId createAppplicationId(String name) { + private static ApplicationId createApplicationId(String name) { return new ApplicationId.Builder() - .tenant("mytenant") - .applicationName(name).instanceName("myinst").build(); + .tenant(tenantName.value()) + .applicationName(name) + .instanceName("myinst") + .build(); + } + + private void writeApplicationData(ApplicationId applicationId, long sessionId) throws Exception { + writeApplicationData(applicationId.serializedForm(), sessionId); + } + + private void writeApplicationData(String applicationId, long sessionId) throws Exception { + curatorFramework + .create() + .creatingParentsIfNeeded() + .forPath(Tenants.getApplicationsPath(tenantName).append(applicationId).getAbsolute(), + Utf8.toAsciiBytes(sessionId)); } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java index 959dfab1bee..16ce605d4d1 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java @@ -4,8 +4,8 @@ package com.yahoo.vespa.config.server.http.v2; import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.yahoo.config.provision.TenantName; -import com.yahoo.path.Path; -import com.yahoo.vespa.config.server.*; +import com.yahoo.vespa.config.server.GlobalComponentRegistry; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.MemoryTenantApplications; import com.yahoo.vespa.config.server.session.LocalSessionRepo; import com.yahoo.vespa.config.server.session.RemoteSessionRepo; @@ -18,7 +18,7 @@ import java.util.*; /** * Test utility for creating tenants used for testing and setup wiring of tenant stuff. * - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class TestTenantBuilder { @@ -32,7 +32,7 @@ public class TestTenantBuilder { public TenantBuilder createTenant(TenantName tenantName) { MemoryTenantApplications applicationRepo = new MemoryTenantApplications(); - TenantBuilder builder = TenantBuilder.create(componentRegistry, tenantName, Path.createRoot().append(tenantName.value())) + TenantBuilder builder = TenantBuilder.create(componentRegistry, tenantName) .withSessionFactory(new SessionCreateHandlerTest.MockSessionFactory()) .withLocalSessionRepo(new LocalSessionRepo(componentRegistry.getClock())) .withRemoteSessionRepo(new RemoteSessionRepo()) diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java index 5753b2959f7..3d34d08edeb 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java @@ -2,10 +2,11 @@ package com.yahoo.vespa.config.server.session; import com.yahoo.config.model.application.provider.FilesApplicationPackage; -import com.yahoo.path.Path; import com.yahoo.test.ManualClock; -import com.yahoo.vespa.config.server.*; import com.yahoo.config.provision.TenantName; +import com.yahoo.vespa.config.server.GlobalComponentRegistry; +import com.yahoo.vespa.config.server.TestComponentRegistry; +import com.yahoo.vespa.config.server.TestWithCurator; import com.yahoo.vespa.config.server.application.MemoryTenantApplications; import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs; import com.yahoo.io.IOUtils; @@ -20,13 +21,12 @@ import java.io.File; import java.time.Duration; import java.time.Instant; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; /** - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class LocalSessionRepoTest extends TestWithCurator { @@ -51,10 +51,7 @@ public class LocalSessionRepoTest extends TestWithCurator { } clock = new ManualClock(Instant.ofEpochSecond(1)); LocalSessionLoader loader = new SessionFactoryImpl(globalComponentRegistry, - new SessionCounter(globalComponentRegistry.getCurator(), - Path.fromString("counter"), - Path.fromString("sessions")), - Path.createRoot(), + new SessionCounter(globalComponentRegistry.getCurator(), tenantName), new MemoryTenantApplications(), tenantFileSystemDirs, new HostRegistry<>(), tenantName); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java index 462062ce8a8..878339bd703 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java @@ -2,18 +2,22 @@ package com.yahoo.vespa.config.server.session; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; import com.yahoo.path.Path; import com.yahoo.text.Utf8; import com.yahoo.transaction.Transaction; -import com.yahoo.vespa.config.server.*; +import com.yahoo.vespa.config.server.TestComponentRegistry; +import com.yahoo.vespa.config.server.TestWithCurator; import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantBuilder; +import com.yahoo.vespa.config.server.tenant.Tenants; import com.yahoo.vespa.curator.Curator; import org.junit.Before; import org.junit.Test; @@ -27,33 +31,35 @@ import java.util.concurrent.TimeUnit; import java.util.function.LongPredicate; /** - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class RemoteSessionRepoTest extends TestWithCurator { + private static final TenantName tenantName = TenantName.defaultName(); + private RemoteSessionRepo remoteSessionRepo; @Before public void setupFacade() throws Exception { - createSession(2l, false); - createSession(3l, false); - curator.create(Path.fromString("/applications")); - curator.create(Path.fromString("/sessions")); - Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(), - TenantName.defaultName(), - Path.createRoot()).build(); + Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder() + .curator(curator) + .build(), + tenantName) + .build(); this.remoteSessionRepo = tenant.getRemoteSessionRepo(); + curator.create(Tenants.getTenantPath(tenantName).append("/applications")); + curator.create(Tenants.getSessionsPath(tenantName)); + createSession(1l, false); + createSession(2l, false); } private void createSession(long sessionId, boolean wait) { - createSession("", sessionId, wait); + createSession(sessionId, wait, tenantName); } - - private void createSession(String root, long sessionId, boolean wait) { - Path sessionsPath = Path.fromString(root).append("sessions"); - curator.create(sessionsPath); + private void createSession(long sessionId, boolean wait, TenantName tenantName) { + Path sessionsPath = Tenants.getSessionsPath(tenantName); SessionZooKeeperClient zkc = new SessionZooKeeperClient(curator, sessionsPath.append(String.valueOf(sessionId))); zkc.createNewSession(System.currentTimeMillis(), TimeUnit.MILLISECONDS); if (wait) { @@ -64,27 +70,28 @@ public class RemoteSessionRepoTest extends TestWithCurator { @Test public void testInitialize() { + assertSessionExists(1l); assertSessionExists(2l); - assertSessionExists(3l); } @Test public void testCreateSession() throws Exception { - createSession(0l, true); - assertSessionExists(0l); + createSession(3l, true); + assertSessionExists(3l); } @Test public void testSessionStateChange() throws Exception { - Path session = Path.fromString("/sessions/0"); - createSession(0l, true); - assertSessionStatus(0l, Session.Status.NEW); - assertStatusChange(0l, Session.Status.PREPARE); - assertStatusChange(0l, Session.Status.ACTIVATE); + long sessionId = 3L; + createSession(sessionId, true); + assertSessionStatus(sessionId, Session.Status.NEW); + assertStatusChange(sessionId, Session.Status.PREPARE); + assertStatusChange(sessionId, Session.Status.ACTIVATE); + Path session = Tenants.getSessionsPath(tenantName).append("" + sessionId); curator.delete(session); - assertSessionRemoved(0l); - assertNull(remoteSessionRepo.getSession(0l)); + assertSessionRemoved(sessionId); + assertNull(remoteSessionRepo.getSession(sessionId)); } // If reading a session throws an exception it should be handled and not prevent other applications @@ -93,25 +100,25 @@ public class RemoteSessionRepoTest extends TestWithCurator { // throw an exception). @Test public void testBadApplicationRepoOnActivate() throws Exception { + long sessionId = 3L; TenantApplications applicationRepo = new FailingTenantApplications(); - curator.framework().create().forPath("/mytenant"); - Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(), - TenantName.from("mytenant"), - Path.fromString("mytenant")) + TenantName mytenant = TenantName.from("mytenant"); + Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(), mytenant) .withApplicationRepo(applicationRepo) .build(); + curator.create(Tenants.getSessionsPath(mytenant)); remoteSessionRepo = tenant.getRemoteSessionRepo(); assertThat(remoteSessionRepo.listSessions().size(), is(0)); - createSession("/mytenant", 2l, true); + createSession(sessionId, true, mytenant); assertThat(remoteSessionRepo.listSessions().size(), is(1)); } private void assertStatusChange(long sessionId, Session.Status status) throws Exception { - Path statePath = Path.fromString("/sessions/" + sessionId).append(ConfigCurator.SESSIONSTATE_ZK_SUBPATH); + Path statePath = Tenants.getSessionsPath(tenantName).append("" + sessionId).append(ConfigCurator.SESSIONSTATE_ZK_SUBPATH); curator.create(statePath); curatorFramework.setData().forPath(statePath.getAbsolute(), Utf8.toBytes(status.toString())); System.out.println("Setting status " + status + " for " + sessionId); - assertSessionStatus(0l, status); + assertSessionStatus(sessionId, status); } private void assertSessionRemoved(long sessionId) { |