Search in sources :

Example 21 with ReferenceConflictException

use of org.projectnessie.versioned.ReferenceConflictException in project nessie by projectnessie.

the class TxDatabaseAdapter method assign.

@Override
public void assign(NamedRef assignee, Optional<Hash> expectedHead, Hash assignTo) throws ReferenceNotFoundException, ReferenceConflictException {
    try {
        opLoop("assignRef", assignee, true, (conn, assigneeHead) -> {
            assigneeHead = fetchNamedRefHead(conn, assignee);
            verifyExpectedHash(assigneeHead, assignee, expectedHead);
            if (!NO_ANCESTOR.equals(assignTo) && fetchFromCommitLog(conn, assignTo) == null) {
                throw referenceNotFound(assignTo);
            }
            Hash resultHash = tryMoveNamedReference(conn, assignee, assigneeHead, assignTo);
            commitRefLog(conn, commitTimeInMicros(), assignTo, assignee, RefLogEntry.Operation.ASSIGN_REFERENCE, Collections.singletonList(assigneeHead));
            return resultHash;
        }, () -> assignConflictMessage("Conflict", assignee, expectedHead, assignTo), () -> assignConflictMessage("Retry-Failure", assignee, expectedHead, assignTo));
    } catch (ReferenceNotFoundException | ReferenceConflictException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) ReferenceRetryFailureException(org.projectnessie.versioned.ReferenceRetryFailureException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) SQLException(java.sql.SQLException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException)

Example 22 with ReferenceConflictException

use of org.projectnessie.versioned.ReferenceConflictException in project nessie by projectnessie.

the class NonTransactionalDatabaseAdapter method commit.

@Override
public Hash commit(CommitAttempt commitAttempt) throws ReferenceConflictException, ReferenceNotFoundException {
    try {
        return casOpLoop("commit", commitAttempt.getCommitToBranch(), CasOpVariant.COMMIT, (ctx, pointer, x, newKeyLists) -> {
            Hash branchHead = branchHead(pointer, commitAttempt.getCommitToBranch());
            long timeInMicros = commitTimeInMicros();
            CommitLogEntry newBranchCommit = commitAttempt(ctx, timeInMicros, branchHead, commitAttempt, newKeyLists);
            GlobalStateLogEntry newGlobalHead = writeGlobalCommit(ctx, timeInMicros, pointer, commitAttempt.getGlobal().entrySet().stream().map(e -> ContentIdAndBytes.of(e.getKey(), e.getValue())).collect(Collectors.toList()));
            RefLogEntry newRefLog = writeRefLogEntry(ctx, pointer, commitAttempt.getCommitToBranch().getName(), RefLogEntry.RefType.Branch, newBranchCommit.getHash(), RefLogEntry.Operation.COMMIT, timeInMicros, Collections.emptyList());
            return updateGlobalStatePointer(commitAttempt.getCommitToBranch(), pointer, newBranchCommit.getHash(), newGlobalHead, newRefLog);
        }, () -> commitConflictMessage("Retry-Failure", commitAttempt.getCommitToBranch(), commitAttempt.getExpectedHead()));
    } catch (ReferenceNotFoundException | ReferenceConflictException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) GlobalStateLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStateLogEntry) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException)

Example 23 with ReferenceConflictException

use of org.projectnessie.versioned.ReferenceConflictException in project nessie by projectnessie.

the class NonTransactionalDatabaseAdapter method delete.

@Override
public void delete(NamedRef reference, Optional<Hash> expectedHead) throws ReferenceNotFoundException, ReferenceConflictException {
    try {
        casOpLoop("deleteRef", reference, CasOpVariant.DELETE_REF, (ctx, pointer, branchCommits, newKeyLists) -> {
            Hash branchHead = branchHead(pointer, reference);
            verifyExpectedHash(branchHead, reference, expectedHead);
            GlobalStateLogEntry newGlobalHead = noopGlobalLogEntry(ctx, pointer);
            RefLogEntry.RefType refType = reference instanceof TagName ? RefLogEntry.RefType.Tag : RefLogEntry.RefType.Branch;
            RefLogEntry newRefLog = writeRefLogEntry(ctx, pointer, reference.getName(), refType, branchHead, RefLogEntry.Operation.DELETE_REFERENCE, commitTimeInMicros(), Collections.emptyList());
            return updateGlobalStatePointer(reference, pointer, null, newGlobalHead, newRefLog);
        }, () -> deleteConflictMessage("Retry-Failure", reference, expectedHead));
    } catch (ReferenceNotFoundException | ReferenceConflictException | RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) GlobalStateLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStateLogEntry) RefType(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry.RefType) TagName(org.projectnessie.versioned.TagName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) DatabaseAdapterUtil.verifyExpectedHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry) ReferenceAlreadyExistsException(org.projectnessie.versioned.ReferenceAlreadyExistsException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) VersionStoreException(org.projectnessie.versioned.VersionStoreException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException)

Example 24 with ReferenceConflictException

use of org.projectnessie.versioned.ReferenceConflictException in project nessie by projectnessie.

the class NonTransactionalDatabaseAdapter method initializeRepo.

@Override
public void initializeRepo(String defaultBranchName) {
    NonTransactionalOperationContext ctx = NON_TRANSACTIONAL_OPERATION_CONTEXT;
    if (fetchGlobalPointer(ctx) == null) {
        GlobalStateLogEntry globalHead;
        RefLogEntry newRefLog;
        try {
            long timeInMicros = commitTimeInMicros();
            GlobalStatePointer dummyPointer = GlobalStatePointer.newBuilder().setGlobalId(NO_ANCESTOR.asBytes()).addGlobalParentsInclHead(NO_ANCESTOR.asBytes()).setRefLogId(NO_ANCESTOR.asBytes()).addRefLogParentsInclHead(NO_ANCESTOR.asBytes()).build();
            globalHead = writeGlobalCommit(ctx, timeInMicros, dummyPointer, Collections.emptyList());
            newRefLog = writeRefLogEntry(ctx, dummyPointer, defaultBranchName, RefLogEntry.RefType.Branch, NO_ANCESTOR, RefLogEntry.Operation.CREATE_REFERENCE, commitTimeInMicros(), Collections.emptyList());
        } catch (ReferenceConflictException e) {
            throw new RuntimeException(e);
        }
        unsafeWriteGlobalPointer(ctx, GlobalStatePointer.newBuilder().setGlobalId(globalHead.getId()).addNamedReferences(NamedReference.newBuilder().setName(defaultBranchName).setRef(RefPointer.newBuilder().setType(Type.Branch).setHash(NO_ANCESTOR.asBytes()))).setRefLogId(newRefLog.getRefLogId()).addRefLogParentsInclHead(newRefLog.getRefLogId()).addRefLogParentsInclHead(NO_ANCESTOR.asBytes()).addGlobalParentsInclHead(globalHead.getId()).addGlobalParentsInclHead(NO_ANCESTOR.asBytes()).build());
    }
}
Also used : GlobalStateLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStateLogEntry) GlobalStatePointer(org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStatePointer) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogEntry(org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)

Example 25 with ReferenceConflictException

use of org.projectnessie.versioned.ReferenceConflictException in project nessie by projectnessie.

the class AbstractDatabaseAdapter method mergeAttempt.

/**
 * Logic implementation of a merge-attempt.
 *
 * @param ctx technical operation context
 * @param from merge-from commit
 * @param toBranch merge-into reference with expected hash of HEAD
 * @param expectedHead if present, {@code toBranch}'s current HEAD must be equal to this value
 * @param toHead current HEAD of {@code toBranch}
 * @param branchCommits consumer for the individual commits to merge
 * @param newKeyLists consumer for optimistically written {@link KeyListEntity}s
 * @param rewriteMetadata function to rewrite the commit-metadata for copied commits
 * @return hash of the last commit-log-entry written to {@code toBranch}
 */
protected Hash mergeAttempt(OP_CONTEXT ctx, long timeInMicros, Hash from, BranchName toBranch, Optional<Hash> expectedHead, Hash toHead, Consumer<Hash> branchCommits, Consumer<Hash> newKeyLists, Function<ByteString, ByteString> rewriteMetadata) throws ReferenceNotFoundException, ReferenceConflictException {
    validateHashExists(ctx, from);
    // 1. ensure 'expectedHash' is a parent of HEAD-of-'toBranch'
    hashOnRef(ctx, toBranch, expectedHead, toHead);
    // 2. find nearest common-ancestor between 'from' + 'fromHash'
    Hash commonAncestor = findCommonAncestor(ctx, from, toBranch, toHead);
    // 3. Collect commit-log-entries
    List<CommitLogEntry> toEntriesReverseChronological = takeUntilExcludeLast(readCommitLogStream(ctx, toHead), e -> e.getHash().equals(commonAncestor)).collect(Collectors.toList());
    Collections.reverse(toEntriesReverseChronological);
    List<CommitLogEntry> commitsToMergeChronological = takeUntilExcludeLast(readCommitLogStream(ctx, from), e -> e.getHash().equals(commonAncestor)).collect(Collectors.toList());
    if (commitsToMergeChronological.isEmpty()) {
        // Nothing to merge, shortcut
        throw new IllegalArgumentException(String.format("No hashes to merge from '%s' onto '%s' @ '%s'.", from.asString(), toBranch.getName(), toHead));
    }
    // 4. Collect modified keys.
    Set<Key> keysTouchedOnTarget = collectModifiedKeys(toEntriesReverseChronological);
    // 5. check for key-collisions
    checkForKeyCollisions(ctx, toHead, keysTouchedOnTarget, commitsToMergeChronological);
    // (no need to verify the global states during a transplant)
    // 6. re-apply commits in 'sequenceToTransplant' onto 'targetBranch'
    toHead = copyCommits(ctx, timeInMicros, toHead, commitsToMergeChronological, newKeyLists, rewriteMetadata);
    // 7. Write commits
    commitsToMergeChronological.stream().map(CommitLogEntry::getHash).forEach(branchCommits);
    writeMultipleCommits(ctx, commitsToMergeChronological);
    return toHead;
}
Also used : Spliterators(java.util.Spliterators) BiFunction(java.util.function.BiFunction) DatabaseAdapterUtil.referenceNotFound(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.referenceNotFound) KeyListEntity(org.projectnessie.versioned.persist.adapter.KeyListEntity) DatabaseAdapterUtil.takeUntilExcludeLast(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.takeUntilExcludeLast) GetNamedRefsParams(org.projectnessie.versioned.GetNamedRefsParams) DatabaseAdapterMetrics.tryLoopFinished(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterMetrics.tryLoopFinished) CommitAttempt(org.projectnessie.versioned.persist.adapter.CommitAttempt) Map(java.util.Map) ContentVariant(org.projectnessie.versioned.persist.adapter.ContentVariant) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) DatabaseAdapterUtil.hashKey(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.hashKey) NamedRef(org.projectnessie.versioned.NamedRef) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) DatabaseAdapterUtil.takeUntilIncludeLast(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.takeUntilIncludeLast) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) DatabaseAdapterUtil.newHasher(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.newHasher) ByteString(com.google.protobuf.ByteString) Objects(java.util.Objects) BranchName(org.projectnessie.versioned.BranchName) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) List(java.util.List) KeyList(org.projectnessie.versioned.persist.adapter.KeyList) Stream(java.util.stream.Stream) Difference(org.projectnessie.versioned.persist.adapter.Difference) Entry(java.util.Map.Entry) DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig) Optional(java.util.Optional) Spliterator(java.util.Spliterator) IntStream(java.util.stream.IntStream) AbstractSpliterator(java.util.Spliterators.AbstractSpliterator) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) TagName(org.projectnessie.versioned.TagName) KeyListEntry(org.projectnessie.versioned.persist.adapter.KeyListEntry) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) Function(java.util.function.Function) UnsafeByteOperations(com.google.protobuf.UnsafeByteOperations) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) KeyFilterPredicate(org.projectnessie.versioned.persist.adapter.KeyFilterPredicate) RefLog(org.projectnessie.versioned.persist.adapter.RefLog) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) StreamSupport(java.util.stream.StreamSupport) Hasher(com.google.common.hash.Hasher) Nonnull(javax.annotation.Nonnull) ContentVariantSupplier(org.projectnessie.versioned.persist.adapter.ContentVariantSupplier) Iterator(java.util.Iterator) Hash(org.projectnessie.versioned.Hash) Traced.trace(org.projectnessie.versioned.persist.adapter.spi.Traced.trace) ALLOW_ALL(org.projectnessie.versioned.persist.adapter.KeyFilterPredicate.ALLOW_ALL) DatabaseAdapterUtil.hashNotFound(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.hashNotFound) Key(org.projectnessie.versioned.Key) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) KeyWithBytes(org.projectnessie.versioned.persist.adapter.KeyWithBytes) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) RetrieveOptions(org.projectnessie.versioned.GetNamedRefsParams.RetrieveOptions) CommitsAheadBehind(org.projectnessie.versioned.ReferenceInfo.CommitsAheadBehind) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ContentAndState(org.projectnessie.versioned.persist.adapter.ContentAndState) ImmutableCommitLogEntry(org.projectnessie.versioned.persist.adapter.ImmutableCommitLogEntry) ReferenceInfo(org.projectnessie.versioned.ReferenceInfo) Preconditions(com.google.common.base.Preconditions) Diff(org.projectnessie.versioned.Diff) ImmutableReferenceInfo(org.projectnessie.versioned.ImmutableReferenceInfo) Collections(java.util.Collections) ContentId(org.projectnessie.versioned.persist.adapter.ContentId) ImmutableKeyList(org.projectnessie.versioned.persist.adapter.ImmutableKeyList) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) ImmutableCommitLogEntry(org.projectnessie.versioned.persist.adapter.ImmutableCommitLogEntry) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) DatabaseAdapterUtil.hashKey(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.hashKey) Key(org.projectnessie.versioned.Key)

Aggregations

ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)27 ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)22 Hash (org.projectnessie.versioned.Hash)21 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)16 RefLogNotFoundException (org.projectnessie.versioned.RefLogNotFoundException)15 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)12 VersionStoreException (org.projectnessie.versioned.VersionStoreException)12 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)11 ArrayList (java.util.ArrayList)8 BranchName (org.projectnessie.versioned.BranchName)8 Key (org.projectnessie.versioned.Key)7 CommitLogEntry (org.projectnessie.versioned.persist.adapter.CommitLogEntry)7 GlobalStateLogEntry (org.projectnessie.versioned.persist.serialize.AdapterTypes.GlobalStateLogEntry)7 ByteString (com.google.protobuf.ByteString)6 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)6 Optional (java.util.Optional)6 TagName (org.projectnessie.versioned.TagName)6 RefLogEntry (org.projectnessie.versioned.persist.serialize.AdapterTypes.RefLogEntry)6 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5