Introduce zookeeper-refdb init step

During init the plugin needs to check for existing
Zookeeper-based global-refdb and manage the update
of the refs modified during an init migration step.

Bug: Issue 14004
Change-Id: I7836b739bba7b0b84535645c9aa56cc6f9f7d214
diff --git a/BUILD b/BUILD
index 761fb57..826ddef 100644
--- a/BUILD
+++ b/BUILD
@@ -34,6 +34,7 @@
     manifest_entries = [
         "Gerrit-PluginName: zookeeper-refdb",
         "Gerrit-Module: com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.ZkValidationModule",
+        "Gerrit-InitStep: com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.ZkInit",
         "Implementation-Title: zookeeper ref-db plugin",
         "Implementation-URL: https://review.gerrithub.io/admin/repos/GerritForge/plugins_zookeeper",
     ],
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkInit.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkInit.java
new file mode 100644
index 0000000..893698e
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkInit.java
@@ -0,0 +1,94 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper;
+
+import com.google.gerrit.exceptions.StorageException;
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.pgm.init.api.ConsoleUI;
+import com.google.gerrit.pgm.init.api.InitStep;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.config.AllProjectsNameProvider;
+import com.google.gerrit.server.config.AllUsersName;
+import com.google.gerrit.server.config.AllUsersNameProvider;
+import com.google.gerrit.server.config.SitePaths;
+import com.google.gerrit.server.schema.NoteDbSchemaVersionManager;
+import com.google.inject.AbstractModule;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.migration.ZkMigrations;
+import java.io.IOException;
+import org.apache.curator.framework.CuratorFramework;
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.eclipse.jgit.util.FS;
+
+@Singleton
+public class ZkInit implements InitStep {
+
+  private final ConsoleUI ui;
+  private final FileBasedConfig config;
+
+  @Inject(optional = true)
+  private NoteDbSchemaVersionManager versionManager;
+
+  @Inject(optional = true)
+  private ZkMigrations zkMigrations;
+
+  private final String pluginName;
+  private final Injector initInjector;
+
+  @Inject
+  ZkInit(ConsoleUI ui, SitePaths site, @PluginName String pluginName, Injector initInjector)
+      throws IOException, ConfigInvalidException {
+    this.ui = ui;
+    this.pluginName = pluginName;
+    this.initInjector = initInjector;
+
+    config =
+        new FileBasedConfig(site.etc_dir.resolve(pluginName + ".config").toFile(), FS.DETECTED);
+    config.load();
+  }
+
+  @Override
+  public void run() throws Exception {
+    if (config.getSections().isEmpty()) {
+      ui.message("%s configuration not found: global-refdb migration skipped\n", pluginName);
+      return;
+    }
+
+    Injector injector = getInjector(initInjector);
+    injector.injectMembers(this);
+
+    try {
+      try (CuratorFramework curator = new ZookeeperConfig(config).buildCurator()) {
+        zkMigrations.migrate(injector, curator, versionManager.read());
+      }
+    } catch (StorageException e) {
+      // No version information: skip migration as it is most likely a new site
+    }
+  }
+
+  public static Injector getInjector(Injector parentInjector) {
+    return parentInjector.createChildInjector(
+        new AbstractModule() {
+          @Override
+          protected void configure() {
+            bind(AllProjectsName.class).toProvider(AllProjectsNameProvider.class);
+            bind(AllUsersName.class).toProvider(AllUsersNameProvider.class);
+          }
+        });
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkSharedRefDatabase.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkSharedRefDatabase.java
index 7def5f2..3fc54a3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkSharedRefDatabase.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZkSharedRefDatabase.java
@@ -204,7 +204,7 @@
     return client.checkExists().forPath(pathFor(projectName, oldRef)) == null;
   }
 
-  static String pathFor(Project.NameKey projectName, String refName) {
+  public static String pathFor(Project.NameKey projectName, String refName) {
     return "/" + projectName + "/" + refName;
   }
 
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZookeeperConfig.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZookeeperConfig.java
index d9c7a4a..8b17df0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZookeeperConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/ZookeeperConfig.java
@@ -94,7 +94,10 @@
 
   @Inject
   public ZookeeperConfig(PluginConfigFactory cfgFactory, @PluginName String pluginName) {
-    Config zkConfig = cfgFactory.getGlobalPluginConfig(pluginName);
+    this(cfgFactory.getGlobalPluginConfig(pluginName));
+  }
+
+  public ZookeeperConfig(Config zkConfig) {
     connectionString =
         getString(zkConfig, SECTION, SUBSECTION, KEY_CONNECT_STRING, DEFAULT_ZK_CONNECT);
     root = getString(zkConfig, SECTION, SUBSECTION, KEY_ROOT_NODE, "gerrit/multi-site");
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration.java
new file mode 100644
index 0000000..0b91c8b
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration.java
@@ -0,0 +1,22 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.migration;
+
+import org.apache.curator.framework.CuratorFramework;
+
+public interface ZkMigration {
+
+  void run(CuratorFramework curator) throws Exception;
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration_Schema_182.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration_Schema_182.java
new file mode 100644
index 0000000..8f0d497
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration_Schema_182.java
@@ -0,0 +1,42 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.migration;
+
+import com.google.gerrit.server.config.AllUsersName;
+import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.ZkSharedRefDatabase;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.zookeeper.KeeperException.NoNodeException;
+
+class ZkMigration_Schema_182 implements ZkMigration {
+  private final AllUsersName allUsers;
+
+  @Inject
+  ZkMigration_Schema_182(AllUsersName allUsers) {
+    this.allUsers = allUsers;
+  }
+
+  @Override
+  public void run(CuratorFramework curator) throws Exception {
+    try {
+      curator
+          .delete()
+          .deletingChildrenIfNeeded()
+          .forPath(ZkSharedRefDatabase.pathFor(allUsers, "refs/draft-comments"));
+    } catch (NoNodeException e) {
+      // Nothing to do: the node did not exist
+    }
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration_Schema_183.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration_Schema_183.java
new file mode 100644
index 0000000..6e0a96e
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigration_Schema_183.java
@@ -0,0 +1,40 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.migration;
+
+import com.google.gerrit.entities.RefNames;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.inject.Inject;
+import com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.ZkSharedRefDatabase;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.zookeeper.KeeperException.NoNodeException;
+
+class ZkMigration_Schema_183 implements ZkMigration {
+  private final AllProjectsName allProjects;
+
+  @Inject
+  ZkMigration_Schema_183(AllProjectsName allProjects) {
+    this.allProjects = allProjects;
+  }
+
+  @Override
+  public void run(CuratorFramework curator) throws Exception {
+    try {
+      curator.delete().forPath(ZkSharedRefDatabase.pathFor(allProjects, RefNames.REFS_CONFIG));
+    } catch (NoNodeException e) {
+      // Nothing to do: the node did not exist
+    }
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigrations.java b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigrations.java
new file mode 100644
index 0000000..6af0e30
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigrations.java
@@ -0,0 +1,66 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.migration;
+
+import static com.google.gerrit.server.schema.NoteDbSchemaVersions.LATEST;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.pgm.init.api.ConsoleUI;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import org.apache.curator.framework.CuratorFramework;
+
+public class ZkMigrations {
+  private final ConsoleUI ui;
+  private final String pluginName;
+
+  @Inject
+  public ZkMigrations(ConsoleUI ui, @PluginName String pluginName) {
+    this.ui = ui;
+    this.pluginName = pluginName;
+  }
+
+  @SuppressWarnings("unchecked")
+  public void migrate(Injector injector, CuratorFramework curator, int currentVersion)
+      throws Exception {
+    if (currentVersion <= 0 || currentVersion >= LATEST) {
+      return;
+    }
+
+    boolean headerDisplayed = false;
+
+    for (int version = currentVersion + 1; version <= LATEST; version++) {
+      try {
+        Class<ZkMigration> migration =
+            (Class<ZkMigration>) Class.forName(ZkMigration.class.getName() + "_Schema_" + version);
+
+        if (!headerDisplayed) {
+          ui.header("%s migration", pluginName);
+          headerDisplayed = true;
+        }
+
+        ui.message("Migrating %s data to schema %d ... ", pluginName, version);
+        injector.getInstance(migration).run(curator);
+        ui.message("DONE\n");
+      } catch (ClassNotFoundException e) {
+        // No migration found for the schema
+      }
+    }
+
+    if (headerDisplayed) {
+      ui.message("\n%s migration completed successfully\n\n", pluginName);
+    }
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigrationsTest.java b/src/test/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigrationsTest.java
new file mode 100644
index 0000000..3ba2eba
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/validation/dfsrefdb/zookeeper/migration/ZkMigrationsTest.java
@@ -0,0 +1,94 @@
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.migration;
+
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
+import com.google.gerrit.acceptance.TestPlugin;
+import com.google.gerrit.entities.RefNames;
+import com.google.gerrit.pgm.init.api.ConsoleUI;
+import com.google.gerrit.server.schema.NoteDbSchemaVersions;
+import org.apache.curator.framework.CuratorFramework;
+import org.apache.curator.framework.api.BackgroundVersionable;
+import org.apache.curator.framework.api.DeleteBuilder;
+import org.junit.Before;
+import org.junit.Test;
+
+@TestPlugin(
+    name = "foo",
+    sysModule = "com.googlesource.gerrit.plugins.validation.dfsrefdb.zookeeper.ZkValidationModule")
+public class ZkMigrationsTest extends LightweightPluginDaemonTest {
+
+  private CuratorFramework curatorMock;
+  private ConsoleUI uiMock;
+  private ZkMigrations migrations;
+  private DeleteBuilder deleteBuilderMock;
+  private BackgroundVersionable backgroundVersionableMock;
+
+  @Before
+  public void setup() {
+    curatorMock = mock(CuratorFramework.class, RETURNS_DEEP_STUBS);
+    deleteBuilderMock = mock(DeleteBuilder.class);
+    backgroundVersionableMock = mock(BackgroundVersionable.class);
+    when(curatorMock.delete()).thenReturn(deleteBuilderMock);
+    when(deleteBuilderMock.deletingChildrenIfNeeded()).thenReturn(backgroundVersionableMock);
+
+    uiMock = mock(ConsoleUI.class);
+    migrations = new ZkMigrations(uiMock, "foo");
+  }
+
+  @Test
+  public void shouldNotMigrateAnythingForNewSites() throws Exception {
+    migrations.migrate(plugin.getSysInjector(), curatorMock, 0);
+    verifyZeroInteractions(curatorMock);
+  }
+
+  @Test
+  public void shouldNotMigrateAnythingForSchema184() throws Exception {
+    migrations.migrate(plugin.getSysInjector(), curatorMock, 184);
+    verifyZeroInteractions(curatorMock);
+  }
+
+  @Test
+  public void shouldCallCuratorDeleteForMigrationFromSchema182PlusOneToLatest() throws Exception {
+    migrations.migrate(plugin.getSysInjector(), curatorMock, 182);
+
+    verify(curatorMock).delete();
+    verify(deleteBuilderMock).forPath(eq("/All-Projects/" + RefNames.REFS_CONFIG));
+
+    verifyNoMoreInteractions(curatorMock);
+    verifyNoMoreInteractions(deleteBuilderMock);
+    verifyZeroInteractions(backgroundVersionableMock);
+  }
+
+  @Test
+  public void shouldNotCallCuratorDeleteIfAlreadyOnLatestVersion() throws Exception {
+    migrations.migrate(plugin.getSysInjector(), curatorMock, NoteDbSchemaVersions.LATEST);
+    verifyZeroInteractions(curatorMock);
+  }
+
+  @Test
+  public void shouldNotCallCuratorDeleteIfOverLatestVersion() throws Exception {
+    migrations.migrate(plugin.getSysInjector(), curatorMock, NoteDbSchemaVersions.LATEST + 1);
+    verifyZeroInteractions(curatorMock);
+  }
+}