Search in sources :

Example 31 with ReferenceNotFoundException

use of org.projectnessie.versioned.ReferenceNotFoundException 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 32 with ReferenceNotFoundException

use of org.projectnessie.versioned.ReferenceNotFoundException 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)

Example 33 with ReferenceNotFoundException

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

the class AbstractDatabaseAdapter method commitAttempt.

/**
 * Logic implementation of a commit-attempt.
 *
 * @param ctx technical operation-context
 * @param commitAttempt commit parameters
 * @param branchHead current HEAD of {@code branch}
 * @param newKeyLists consumer for optimistically written {@link KeyListEntity}s
 * @return optimistically written commit-log-entry
 */
protected CommitLogEntry commitAttempt(OP_CONTEXT ctx, long timeInMicros, Hash branchHead, CommitAttempt commitAttempt, Consumer<Hash> newKeyLists) throws ReferenceNotFoundException, ReferenceConflictException {
    List<String> mismatches = new ArrayList<>();
    Callable<Void> validator = commitAttempt.getValidator();
    if (validator != null) {
        try {
            validator.call();
        } catch (RuntimeException e) {
            // just propagate the RuntimeException up
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    // verify expected global-states
    checkExpectedGlobalStates(ctx, commitAttempt, mismatches::add);
    checkForModifiedKeysBetweenExpectedAndCurrentCommit(ctx, commitAttempt, branchHead, mismatches);
    if (!mismatches.isEmpty()) {
        throw new ReferenceConflictException(String.join("\n", mismatches));
    }
    CommitLogEntry currentBranchEntry = fetchFromCommitLog(ctx, branchHead);
    int parentsPerCommit = config.getParentsPerCommit();
    List<Hash> newParents = new ArrayList<>(parentsPerCommit);
    newParents.add(branchHead);
    long commitSeq;
    if (currentBranchEntry != null) {
        List<Hash> p = currentBranchEntry.getParents();
        newParents.addAll(p.subList(0, Math.min(p.size(), parentsPerCommit - 1)));
        commitSeq = currentBranchEntry.getCommitSeq() + 1;
    } else {
        commitSeq = 1;
    }
    CommitLogEntry newBranchCommit = buildIndividualCommit(ctx, timeInMicros, newParents, commitSeq, commitAttempt.getCommitMetaSerialized(), commitAttempt.getPuts(), commitAttempt.getDeletes(), currentBranchEntry != null ? currentBranchEntry.getKeyListDistance() : 0, newKeyLists, NO_IN_MEMORY_COMMITS);
    writeIndividualCommit(ctx, newBranchCommit);
    return newBranchCommit;
}
Also used : ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) Hash(org.projectnessie.versioned.Hash) DatabaseAdapterUtil.randomHash(org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) RefLogNotFoundException(org.projectnessie.versioned.RefLogNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) ImmutableCommitLogEntry(org.projectnessie.versioned.persist.adapter.ImmutableCommitLogEntry)

Aggregations

ReferenceNotFoundException (org.projectnessie.versioned.ReferenceNotFoundException)33 ReferenceConflictException (org.projectnessie.versioned.ReferenceConflictException)25 Hash (org.projectnessie.versioned.Hash)24 RefLogNotFoundException (org.projectnessie.versioned.RefLogNotFoundException)17 DatabaseAdapterUtil.randomHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.randomHash)17 ReferenceAlreadyExistsException (org.projectnessie.versioned.ReferenceAlreadyExistsException)16 VersionStoreException (org.projectnessie.versioned.VersionStoreException)14 DatabaseAdapterUtil.verifyExpectedHash (org.projectnessie.versioned.persist.adapter.spi.DatabaseAdapterUtil.verifyExpectedHash)13 NessieReferenceNotFoundException (org.projectnessie.error.NessieReferenceNotFoundException)12 BranchName (org.projectnessie.versioned.BranchName)11 Key (org.projectnessie.versioned.Key)10 NamedRef (org.projectnessie.versioned.NamedRef)9 TagName (org.projectnessie.versioned.TagName)9 List (java.util.List)8 Map (java.util.Map)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 CommitLogEntry (org.projectnessie.versioned.persist.adapter.CommitLogEntry)8 ByteString (com.google.protobuf.ByteString)7