summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/schema/Schema_106.java
blob: 5bb3669a14d21de89eac32622eeccef547e292f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (C) 2015 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.google.gerrit.server.schema;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.LocalDiskRepositoryManager;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;

public class Schema_106 extends SchemaVersion {
  // we can use multiple threads per CPU as we can expect that threads will be
  // waiting for IO
  private static final int THREADS_PER_CPU = 4;
  private final GitRepositoryManager repoManager;
  private final PersonIdent serverUser;

  @Inject
  Schema_106(
      Provider<Schema_105> prior,
      GitRepositoryManager repoManager,
      @GerritPersonIdent PersonIdent serverUser) {
    super(prior);
    this.repoManager = repoManager;
    this.serverUser = serverUser;
  }

  @Override
  protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException {
    if (!(repoManager instanceof LocalDiskRepositoryManager)) {
      return;
    }

    ui.message("listing all repositories ...");
    SortedSet<Project.NameKey> repoList = repoManager.list();
    ui.message("done");

    ui.message(String.format("creating reflog files for %s branches ...", RefNames.REFS_CONFIG));

    ExecutorService executorPool = createExecutor(ui, repoList.size());
    List<Future<Void>> futures = new ArrayList<>();

    for (Project.NameKey project : repoList) {
      Callable<Void> callable = new ReflogCreator(project);
      futures.add(executorPool.submit(callable));
    }

    executorPool.shutdown();
    try {
      for (Future<Void> future : futures) {
        try {
          future.get();
        } catch (ExecutionException e) {
          ui.message(e.getCause().getMessage());
        }
      }
      ui.message("done");
    } catch (InterruptedException ex) {
      String msg =
          String.format(
              "Migration step 106 was interrupted. "
                  + "Reflog created in %d of %d repositories only.",
              countDone(futures), repoList.size());
      ui.message(msg);
    }
  }

  private static int countDone(List<Future<Void>> futures) {
    int count = 0;
    for (Future<Void> future : futures) {
      if (future.isDone()) {
        count++;
      }
    }

    return count;
  }

  private ExecutorService createExecutor(UpdateUI ui, int repoCount) {
    int procs = Runtime.getRuntime().availableProcessors();
    int threads = Math.min(procs * THREADS_PER_CPU, repoCount);
    ui.message(String.format("... using %d threads ...", threads));
    return Executors.newFixedThreadPool(threads);
  }

  private class ReflogCreator implements Callable<Void> {
    private final Project.NameKey project;

    ReflogCreator(Project.NameKey project) {
      this.project = project;
    }

    @Override
    public Void call() throws IOException {
      try (Repository repo = repoManager.openRepository(project)) {
        File metaConfigLog = new File(repo.getDirectory(), "logs/" + RefNames.REFS_CONFIG);
        if (metaConfigLog.exists()) {
          return null;
        }

        if (!metaConfigLog.getParentFile().mkdirs() || !metaConfigLog.createNewFile()) {
          throw new IOException();
        }

        ObjectId metaConfigId = repo.resolve(RefNames.REFS_CONFIG);
        if (metaConfigId != null) {
          try (PrintWriter writer = new PrintWriter(metaConfigLog, UTF_8.name())) {
            writer.print(ObjectId.zeroId().name());
            writer.print(" ");
            writer.print(metaConfigId.name());
            writer.print(" ");
            writer.print(serverUser.toExternalString());
            writer.print("\t");
            writer.print("create reflog");
            writer.println();
          }
        }
        return null;
      } catch (IOException e) {
        throw new IOException(
            String.format(
                "ERROR: Failed to create reflog file for the %s branch in repository %s",
                RefNames.REFS_CONFIG, project.get()));
      }
    }
  }
}