Search in sources :

Example 1 with Timer1

use of com.google.gerrit.metrics.Timer1 in project gerrit by GerritCodeReview.

the class ChangeNotes method rebuildAndOpen.

private LoadHandle rebuildAndOpen(Repository repo, ObjectId oldId) throws IOException {
    Timer1.Context timer = args.metrics.autoRebuildLatency.start(CHANGES);
    try {
        Change.Id cid = getChangeId();
        ReviewDb db = args.db.get();
        ChangeRebuilder rebuilder = args.rebuilder.get();
        NoteDbUpdateManager.Result r;
        try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
            if (manager == null) {
                // May be null in tests.
                return super.openHandle(repo, oldId);
            }
            manager.setRefLogMessage("Auto-rebuilding change");
            r = manager.stageAndApplyDelta(change);
            try {
                rebuilder.execute(db, cid, manager);
                repo.scanForRepoChanges();
            } catch (OrmException | IOException e) {
                // Rebuilding failed. Most likely cause is contention on one or more
                // change refs; there are other types of errors that can happen during
                // rebuilding, but generally speaking they should happen during stage(),
                // not execute(). Assume that some other worker is going to successfully
                // store the rebuilt state, which is deterministic given an input
                // ChangeBundle.
                //
                // Parse notes from the staged result so we can return something useful
                // to the caller instead of throwing.
                log.debug("Rebuilding change {} failed: {}", getChangeId(), e.getMessage());
                args.metrics.autoRebuildFailureCount.increment(CHANGES);
                rebuildResult = checkNotNull(r);
                checkNotNull(r.newState());
                checkNotNull(r.staged());
                return LoadHandle.create(ChangeNotesCommit.newStagedRevWalk(repo, r.staged().changeObjects()), r.newState().getChangeMetaId());
            }
        }
        return LoadHandle.create(ChangeNotesCommit.newRevWalk(repo), r.newState().getChangeMetaId());
    } catch (NoSuchChangeException e) {
        return super.openHandle(repo, oldId);
    } catch (OrmException e) {
        throw new IOException(e);
    } finally {
        log.debug("Rebuilt change {} in project {} in {} ms", getChangeId(), getProjectName(), TimeUnit.MILLISECONDS.convert(timer.stop(), TimeUnit.NANOSECONDS));
    }
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) Timer1(com.google.gerrit.metrics.Timer1) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 2 with Timer1

use of com.google.gerrit.metrics.Timer1 in project gerrit by GerritCodeReview.

the class UiActions method describe.

@Nullable
private <R extends RestResource> UiAction.Description describe(Extension<RestView<R>> e, R resource) {
    int d = e.getExportName().indexOf('.');
    if (d < 0) {
        return null;
    }
    RestView<R> view;
    try {
        view = e.getProvider().get();
    } catch (RuntimeException err) {
        logger.atSevere().withCause(err).log("error creating view %s.%s", e.getPluginName(), e.getExportName());
        return null;
    }
    if (!(view instanceof UiAction)) {
        return null;
    }
    String name = e.getExportName().substring(d + 1);
    UiAction.Description dsc = null;
    try (Timer1.Context<String> ignored = uiActionLatency.start(name)) {
        dsc = ((UiAction<R>) view).getDescription(resource);
    } catch (Exception ex) {
        logger.atSevere().withCause(ex).log("Unable to render UIAction. Will omit from actions");
    }
    if (dsc == null) {
        return null;
    }
    Set<GlobalOrPluginPermission> globalRequired;
    try {
        globalRequired = GlobalPermission.fromAnnotation(e.getPluginName(), view.getClass());
    } catch (PermissionBackendException err) {
        logger.atSevere().withCause(err).log("exception testing view %s.%s", e.getPluginName(), e.getExportName());
        return null;
    }
    if (!globalRequired.isEmpty()) {
        PermissionBackend.WithUser withUser = permissionBackend.currentUser();
        Iterator<GlobalOrPluginPermission> i = globalRequired.iterator();
        BooleanCondition p = withUser.testCond(i.next());
        while (i.hasNext()) {
            p = or(p, withUser.testCond(i.next()));
        }
        dsc.setVisible(and(p, dsc.getVisibleCondition()));
    }
    PrivateInternals_UiActionDescription.setMethod(dsc, e.getExportName().substring(0, d));
    PrivateInternals_UiActionDescription.setId(dsc, PluginName.GERRIT.equals(e.getPluginName()) ? name : e.getPluginName() + '~' + name);
    return dsc;
}
Also used : BooleanCondition(com.google.gerrit.extensions.conditions.BooleanCondition) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Description(com.google.gerrit.extensions.webui.UiAction.Description) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) UiAction(com.google.gerrit.extensions.webui.UiAction) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Timer1(com.google.gerrit.metrics.Timer1) GlobalOrPluginPermission(com.google.gerrit.extensions.api.access.GlobalOrPluginPermission) Nullable(com.google.gerrit.common.Nullable)

Example 3 with Timer1

use of com.google.gerrit.metrics.Timer1 in project gerrit by GerritCodeReview.

the class DraftCommentNotes method rebuildAndOpen.

private LoadHandle rebuildAndOpen(Repository repo) throws NoSuchChangeException, IOException {
    Timer1.Context timer = args.metrics.autoRebuildLatency.start(CHANGES);
    try {
        Change.Id cid = getChangeId();
        ReviewDb db = args.db.get();
        ChangeRebuilder rebuilder = args.rebuilder.get();
        NoteDbUpdateManager.Result r;
        try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
            if (manager == null) {
                // May be null in tests.
                return super.openHandle(repo);
            }
            r = manager.stageAndApplyDelta(change);
            try {
                rebuilder.execute(db, cid, manager);
                repo.scanForRepoChanges();
            } catch (OrmException | IOException e) {
                // See ChangeNotes#rebuildAndOpen.
                log.debug("Rebuilding change {} via drafts failed: {}", getChangeId(), e.getMessage());
                args.metrics.autoRebuildFailureCount.increment(CHANGES);
                checkNotNull(r.staged());
                return LoadHandle.create(ChangeNotesCommit.newStagedRevWalk(repo, r.staged().allUsersObjects()), draftsId(r));
            }
        }
        return LoadHandle.create(ChangeNotesCommit.newRevWalk(repo), draftsId(r));
    } catch (NoSuchChangeException e) {
        return super.openHandle(repo);
    } catch (OrmException e) {
        throw new IOException(e);
    } finally {
        log.debug("Rebuilt change {} in {} in {} ms via drafts", getChangeId(), change != null ? "project " + change.getProject() : "unknown project", TimeUnit.MILLISECONDS.convert(timer.stop(), TimeUnit.NANOSECONDS));
    }
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) Timer1(com.google.gerrit.metrics.Timer1) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 4 with Timer1

use of com.google.gerrit.metrics.Timer1 in project gerrit by GerritCodeReview.

the class NoteDbUpdateManager method stage.

/**
   * Stage updates in the manager's internal list of commands.
   *
   * @return map of the state that would get written to the applicable repo(s) for each affected
   *     change.
   * @throws OrmException if a database layer error occurs.
   * @throws IOException if a storage layer error occurs.
   */
public Map<Change.Id, StagedResult> stage() throws OrmException, IOException {
    if (staged != null) {
        return staged;
    }
    try (Timer1.Context timer = metrics.stageUpdateLatency.start(CHANGES)) {
        staged = new HashMap<>();
        if (isEmpty()) {
            return staged;
        }
        initChangeRepo();
        if (!draftUpdates.isEmpty() || !toDelete.isEmpty()) {
            initAllUsersRepo();
        }
        checkExpectedState();
        addCommands();
        Table<Change.Id, Account.Id, ObjectId> allDraftIds = getDraftIds();
        Set<Change.Id> changeIds = new HashSet<>();
        for (ReceiveCommand cmd : changeRepo.getCommandsSnapshot()) {
            Change.Id changeId = Change.Id.fromRef(cmd.getRefName());
            if (changeId == null || !cmd.getRefName().equals(RefNames.changeMetaRef(changeId))) {
                // Not a meta ref update, likely due to a repo update along with the change meta update.
                continue;
            }
            changeIds.add(changeId);
            Optional<ObjectId> metaId = Optional.of(cmd.getNewId());
            staged.put(changeId, StagedResult.create(changeId, NoteDbChangeState.Delta.create(changeId, metaId, allDraftIds.rowMap().remove(changeId)), changeRepo, allUsersRepo));
        }
        for (Map.Entry<Change.Id, Map<Account.Id, ObjectId>> e : allDraftIds.rowMap().entrySet()) {
            // If a change remains in the table at this point, it means we are
            // updating its drafts but not the change itself.
            StagedResult r = StagedResult.create(e.getKey(), NoteDbChangeState.Delta.create(e.getKey(), Optional.empty(), e.getValue()), changeRepo, allUsersRepo);
            checkState(r.changeCommands().isEmpty(), "should not have change commands when updating only drafts: %s", r);
            staged.put(r.id(), r);
        }
        return staged;
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Account(com.google.gerrit.reviewdb.client.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) ObjectId(org.eclipse.jgit.lib.ObjectId) Timer1(com.google.gerrit.metrics.Timer1) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 5 with Timer1

use of com.google.gerrit.metrics.Timer1 in project gerrit by GerritCodeReview.

the class AbstractChangeNotes method load.

public T load() throws OrmException {
    if (loaded) {
        return self();
    }
    boolean read = args.migration.readChanges();
    if (!read && primaryStorage == PrimaryStorage.NOTE_DB) {
        throw new OrmException("NoteDb is required to read change " + changeId);
    }
    boolean readOrWrite = read || args.migration.rawWriteChangesSetting();
    if (!readOrWrite && !autoRebuild) {
        loadDefaults();
        return self();
    }
    if (args.migration.failOnLoad()) {
        throw new OrmException("Reading from NoteDb is disabled");
    }
    try (Timer1.Context timer = args.metrics.readLatency.start(CHANGES);
        Repository repo = args.repoManager.openRepository(getProjectName());
        // auto-rebuilding before this object may get passed to a ChangeUpdate.
        LoadHandle handle = openHandle(repo)) {
        if (read) {
            revision = handle.id();
            onLoad(handle);
        } else {
            loadDefaults();
        }
        loaded = true;
    } catch (ConfigInvalidException | IOException e) {
        throw new OrmException(e);
    }
    return self();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) Timer1(com.google.gerrit.metrics.Timer1)

Aggregations

Timer1 (com.google.gerrit.metrics.Timer1)6 Change (com.google.gerrit.reviewdb.client.Change)3 OrmException (com.google.gwtorm.server.OrmException)3 IOException (java.io.IOException)3 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 ChangeRebuilder (com.google.gerrit.server.notedb.rebuild.ChangeRebuilder)2 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 ReceiveCommand (org.eclipse.jgit.transport.ReceiveCommand)2 Nullable (com.google.gerrit.common.Nullable)1 GlobalOrPluginPermission (com.google.gerrit.extensions.api.access.GlobalOrPluginPermission)1 BooleanCondition (com.google.gerrit.extensions.conditions.BooleanCondition)1 UiAction (com.google.gerrit.extensions.webui.UiAction)1 Description (com.google.gerrit.extensions.webui.UiAction.Description)1 Account (com.google.gerrit.reviewdb.client.Account)1 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1