Disable reading from changes tables under notedb

Wrap ReviewDb with an implementation that disables all read access
methods on all Change-related tables. (Currently Changes itself is an
exception because those calls have not all been migrated yet.)

Change-Id: I60f585f5ef12c660aa9298c3676c1e5c95c945f4
This commit is contained in:
Dave Borowitz
2016-01-28 08:39:36 -05:00
committed by Edwin Kempin
parent 77d45ed0b2
commit 220a3f5d03
9 changed files with 834 additions and 11 deletions

View File

@@ -18,17 +18,26 @@ import static com.google.inject.Scopes.SINGLETON;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gwtorm.jdbc.Database;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
/** Loads the database with standard dependencies. */
public class DatabaseModule extends FactoryModule {
@Override
protected void configure() {
bind(new TypeLiteral<SchemaFactory<ReviewDb>>() {}).to(
new TypeLiteral<Database<ReviewDb>>() {}).in(SINGLETON);
bind(new TypeLiteral<Database<ReviewDb>>() {}).toProvider(
ReviewDbDatabaseProvider.class).in(SINGLETON);
TypeLiteral<SchemaFactory<ReviewDb>> schemaFactory =
new TypeLiteral<SchemaFactory<ReviewDb>>() {};
TypeLiteral<Database<ReviewDb>> database =
new TypeLiteral<Database<ReviewDb>>() {};
bind(NotesMigration.class);
bind(schemaFactory).to(NotesMigrationSchemaFactory.class);
bind(Key.get(schemaFactory, ReviewDbFactory.class))
.to(database)
.in(SINGLETON);
bind(database).toProvider(ReviewDbDatabaseProvider.class);
}
}

View File

@@ -0,0 +1,256 @@
// Copyright (C) 2016 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 com.google.common.util.concurrent.CheckedFuture;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;
import com.google.gerrit.reviewdb.client.PatchLineComment;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.server.ChangeMessageAccess;
import com.google.gerrit.reviewdb.server.PatchLineCommentAccess;
import com.google.gerrit.reviewdb.server.PatchSetAccess;
import com.google.gerrit.reviewdb.server.PatchSetApprovalAccess;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.reviewdb.server.ReviewDbWrapper;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.ResultSet;
public class DisabledChangesReviewDbWrapper extends ReviewDbWrapper {
private static final String MSG = "This table has been migrated to notedb";
private final DisabledPatchSetApprovalAccess patchSetApprovals;
private final DisabledChangeMessageAccess changeMessages;
private final DisabledPatchSetAccess patchSets;
private final DisabledPatchLineCommentAccess patchComments;
DisabledChangesReviewDbWrapper(ReviewDb db) {
super(db);
patchSetApprovals =
new DisabledPatchSetApprovalAccess(delegate.patchSetApprovals());
changeMessages = new DisabledChangeMessageAccess(delegate.changeMessages());
patchSets = new DisabledPatchSetAccess(delegate.patchSets());
patchComments =
new DisabledPatchLineCommentAccess(delegate.patchComments());
}
public ReviewDb unsafeGetDelegate() {
return delegate;
}
@Override
public PatchSetApprovalAccess patchSetApprovals() {
return patchSetApprovals;
}
@Override
public ChangeMessageAccess changeMessages() {
return changeMessages;
}
@Override
public PatchSetAccess patchSets() {
return patchSets;
}
@Override
public PatchLineCommentAccess patchComments() {
return patchComments;
}
private static class DisabledPatchSetApprovalAccess
extends PatchSetApprovalAccessWrapper {
DisabledPatchSetApprovalAccess(PatchSetApprovalAccess delegate) {
super(delegate);
}
@Override
public ResultSet<PatchSetApproval> iterateAllEntities() {
throw new UnsupportedOperationException(MSG);
}
@Override
public CheckedFuture<PatchSetApproval, OrmException> getAsync(
PatchSetApproval.Key key) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchSetApproval> get(
Iterable<PatchSetApproval.Key> keys) {
throw new UnsupportedOperationException(MSG);
}
@Override
public PatchSetApproval get(PatchSetApproval.Key key) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchSetApproval> byChange(Change.Id id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchSetApproval> byPatchSet(PatchSet.Id id) {
throw new UnsupportedOperationException(MSG);
}
}
private static class DisabledChangeMessageAccess
extends ChangeMessageAccessWrapper {
DisabledChangeMessageAccess(ChangeMessageAccess delegate) {
super(delegate);
}
@Override
public ResultSet<ChangeMessage> iterateAllEntities() {
throw new UnsupportedOperationException(MSG);
}
@Override
public CheckedFuture<ChangeMessage, OrmException> getAsync(
ChangeMessage.Key key) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<ChangeMessage> get(Iterable<ChangeMessage.Key> keys) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ChangeMessage get(ChangeMessage.Key id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<ChangeMessage> byChange(Change.Id id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<ChangeMessage> byPatchSet(PatchSet.Id id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<ChangeMessage> all() {
throw new UnsupportedOperationException(MSG);
}
}
private static class DisabledPatchSetAccess extends PatchSetAccessWrapper {
DisabledPatchSetAccess(PatchSetAccess delegate) {
super(delegate);
}
@Override
public ResultSet<PatchSet> iterateAllEntities() {
throw new UnsupportedOperationException(MSG);
}
@Override
public CheckedFuture<PatchSet, OrmException> getAsync(PatchSet.Id key) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchSet> get(Iterable<PatchSet.Id> keys) {
throw new UnsupportedOperationException(MSG);
}
@Override
public PatchSet get(PatchSet.Id id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchSet> byChange(Change.Id id) {
throw new UnsupportedOperationException(MSG);
}
}
private static class DisabledPatchLineCommentAccess
extends PatchLineCommentAccessWrapper {
DisabledPatchLineCommentAccess(PatchLineCommentAccess delegate) {
super(delegate);
}
@Override
public ResultSet<PatchLineComment> iterateAllEntities() {
throw new UnsupportedOperationException(MSG);
}
@Override
public CheckedFuture<PatchLineComment, OrmException> getAsync(
PatchLineComment.Key key) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> get(
Iterable<PatchLineComment.Key> keys) {
throw new UnsupportedOperationException(MSG);
}
@Override
public PatchLineComment get(PatchLineComment.Key id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> byChange(Change.Id id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> byPatchSet(PatchSet.Id id) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> publishedByChangeFile(Change.Id id,
String file) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> publishedByPatchSet(
PatchSet.Id patchset) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> draftByPatchSetAuthor(
PatchSet.Id patchset, Account.Id author) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> draftByChangeFileAuthor(Change.Id id,
String file, Account.Id author) {
throw new UnsupportedOperationException(MSG);
}
@Override
public ResultSet<PatchLineComment> draftByAuthor(Account.Id author) {
throw new UnsupportedOperationException(MSG);
}
}
}

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2016 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 com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gwtorm.server.OrmException;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@Singleton
public class NotesMigrationSchemaFactory implements SchemaFactory<ReviewDb> {
private final SchemaFactory<ReviewDb> delegate;
private final boolean disableChangesTables;
@Inject
NotesMigrationSchemaFactory(
@ReviewDbFactory SchemaFactory<ReviewDb> delegate,
NotesMigration migration) {
this.delegate = delegate;
disableChangesTables = migration.readChanges();
}
@Override
public ReviewDb open() throws OrmException {
ReviewDb db = delegate.open();
if (!disableChangesTables) {
return db;
}
return new DisabledChangesReviewDbWrapper(db);
}
}

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2016 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.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
/**
* Marker on {@link com.google.gwtorm.server.SchemaFactory} implementation
* that talks to the underlying traditional {@link
* com.google.gerrit.reviewdb.server.ReviewDb} database.
* <p>
* During the migration to notedb, the actual {@code ReviewDb} will be a wrapper
* with certain tables enabled/disabled; this marker goes on the low-level
* implementation that has all tables.
*/
@Retention(RUNTIME)
@BindingAnnotation
public @interface ReviewDbFactory {
}