Search in sources :

Example 16 with ReferenceConflictException

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

the class TreeApiImpl method transplantCommitsIntoBranch.

@Override
public void transplantCommitsIntoBranch(String branchName, String hash, String message, Transplant transplant) throws NessieNotFoundException, NessieConflictException {
    try {
        List<Hash> transplants;
        try (Stream<Hash> s = transplant.getHashesToTransplant().stream().map(Hash::of)) {
            transplants = s.collect(Collectors.toList());
        }
        getStore().transplant(BranchName.of(branchName), toHash(hash, true), transplants, commitMetaUpdate());
    } catch (ReferenceNotFoundException e) {
        throw new NessieReferenceNotFoundException(e.getMessage(), e);
    } catch (ReferenceConflictException e) {
        throw new NessieReferenceConflictException(e.getMessage(), e);
    }
}
Also used : NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceNotFoundException(org.projectnessie.versioned.ReferenceNotFoundException) NessieReferenceNotFoundException(org.projectnessie.error.NessieReferenceNotFoundException) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) NessieReferenceConflictException(org.projectnessie.error.NessieReferenceConflictException) NessieReferenceConflictException(org.projectnessie.error.NessieReferenceConflictException) WithHash(org.projectnessie.versioned.WithHash) Hash(org.projectnessie.versioned.Hash)

Example 17 with ReferenceConflictException

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

the class AbstractSingleBranch method singleBranchManyUsers.

/**
 * Use case simulation matrix: single branch, multiple users, each or all user updating a separate
 * or single table.
 */
@ParameterizedTest
@MethodSource("singleBranchManyUsersCases")
void singleBranchManyUsers(SingleBranchParam param) throws Exception {
    BranchName branch = BranchName.of(param.branchName);
    int numUsers = 5;
    int numCommits = 50;
    Hash[] hashesKnownByUser = new Hash[numUsers];
    Hash createHash = store().create(branch, Optional.empty());
    Arrays.fill(hashesKnownByUser, createHash);
    List<CommitMessage> expectedValues = new ArrayList<>();
    Map<Key, String> previousState = new HashMap<>();
    for (int commitNum = 0; commitNum < numCommits; commitNum++) {
        for (int user = 0; user < numUsers; user++) {
            Hash hashKnownByUser = hashesKnownByUser[user];
            CommitMessage msg = commitMessage(String.format("user %03d/commit %03d", user, commitNum));
            expectedValues.add(msg);
            Key key = Key.of(param.tableNameGen.apply(user));
            String contentId = param.contentIdGen.apply(user);
            Operation<BaseContent> put;
            if (param.globalState) {
                String state = String.format("%03d_%03d", user, commitNum);
                if (previousState.containsKey(key)) {
                    put = Put.of(key, withGlobal(state, "data_file", contentId), withGlobal(previousState.get(key), "foo", contentId));
                } else {
                    put = Put.of(key, withGlobal(state, "data_file", contentId));
                }
                previousState.put(key, state);
            } else {
                BaseContent value = newOnRef(String.format("data_file_%03d_%03d", user, commitNum));
                put = Put.of(key, value);
            }
            Hash commitHash;
            List<Operation<BaseContent>> ops = ImmutableList.of(put);
            try {
                commitHash = store().commit(branch, Optional.of(hashKnownByUser), msg, ops);
            } catch (ReferenceConflictException inconsistentValueException) {
                if (param.allowInconsistentValueException) {
                    hashKnownByUser = store().hashOnReference(branch, Optional.empty());
                    commitHash = store().commit(branch, Optional.of(hashKnownByUser), msg, ops);
                } else {
                    throw inconsistentValueException;
                }
            }
            assertNotEquals(hashKnownByUser, commitHash);
            hashesKnownByUser[user] = commitHash;
        }
    }
    // Verify that all commits are there and that the order of the commits is correct
    List<CommitMessage> committedValues = commitsList(branch, s -> s.map(Commit::getCommitMeta), false);
    Collections.reverse(expectedValues);
    assertEquals(expectedValues, committedValues);
}
Also used : CommitMessage(org.projectnessie.versioned.testworker.CommitMessage) HashMap(java.util.HashMap) BaseContent(org.projectnessie.versioned.testworker.BaseContent) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) ArrayList(java.util.ArrayList) Operation(org.projectnessie.versioned.Operation) Hash(org.projectnessie.versioned.Hash) BranchName(org.projectnessie.versioned.BranchName) Key(org.projectnessie.versioned.Key) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 18 with ReferenceConflictException

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

the class TxDatabaseAdapter method commit.

@Override
public Hash commit(CommitAttempt commitAttempt) throws ReferenceConflictException, ReferenceNotFoundException {
    try {
        return opLoop("commit", commitAttempt.getCommitToBranch(), false, (conn, branchHead) -> {
            long timeInMicros = commitTimeInMicros();
            CommitLogEntry newBranchCommit = commitAttempt(conn, timeInMicros, branchHead, commitAttempt, h -> {
            });
            upsertGlobalStates(commitAttempt, conn, newBranchCommit.getCreatedTime());
            Hash resultHash = tryMoveNamedReference(conn, commitAttempt.getCommitToBranch(), branchHead, newBranchCommit.getHash());
            commitRefLog(conn, timeInMicros, newBranchCommit.getHash(), commitAttempt.getCommitToBranch(), RefLogEntry.Operation.COMMIT, Collections.emptyList());
            return resultHash;
        }, () -> commitConflictMessage("Conflict", commitAttempt.getCommitToBranch(), commitAttempt.getExpectedHead()), () -> 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) ProtoSerialization.protoToCommitLogEntry(org.projectnessie.versioned.persist.serialize.ProtoSerialization.protoToCommitLogEntry) CommitLogEntry(org.projectnessie.versioned.persist.adapter.CommitLogEntry) 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 19 with ReferenceConflictException

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

the class TxDatabaseAdapter method transplant.

@SuppressWarnings("RedundantThrows")
@Override
public Hash transplant(BranchName targetBranch, Optional<Hash> expectedHead, List<Hash> commits, Function<ByteString, ByteString> updateCommitMetadata) throws ReferenceNotFoundException, ReferenceConflictException {
    try {
        return opLoop("transplant", targetBranch, false, (conn, currentHead) -> {
            long timeInMicros = commitTimeInMicros();
            Hash targetHead = transplantAttempt(conn, timeInMicros, targetBranch, expectedHead, currentHead, commits, h -> {
            }, h -> {
            }, updateCommitMetadata);
            Hash resultHash = tryMoveNamedReference(conn, targetBranch, currentHead, targetHead);
            commitRefLog(conn, timeInMicros, targetHead, targetBranch, RefLogEntry.Operation.TRANSPLANT, commits);
            return resultHash;
        }, () -> transplantConflictMessage("Conflict", targetBranch, expectedHead, commits), () -> transplantConflictMessage("Retry-failure", targetBranch, expectedHead, commits));
    } 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 20 with ReferenceConflictException

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

the class TxDatabaseAdapter method delete.

@Override
public void delete(NamedRef reference, Optional<Hash> expectedHead) throws ReferenceNotFoundException, ReferenceConflictException {
    try {
        opLoop("deleteRef", reference, false, (conn, pointer) -> {
            verifyExpectedHash(pointer, reference, expectedHead);
            Hash commitHash = fetchNamedRefHead(conn, reference);
            try (Traced ignore = trace("deleteRefInDb");
                PreparedStatement ps = conn.conn().prepareStatement(SqlStatements.DELETE_NAMED_REFERENCE)) {
                ps.setString(1, config.getRepositoryId());
                ps.setString(2, reference.getName());
                ps.setString(3, pointer.asString());
                if (ps.executeUpdate() != 1) {
                    return null;
                }
            }
            commitRefLog(conn, commitTimeInMicros(), commitHash, reference, RefLogEntry.Operation.DELETE_REFERENCE, Collections.emptyList());
            return pointer;
        }, () -> deleteConflictMessage("Conflict", reference, expectedHead), () -> 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) Traced(org.projectnessie.versioned.persist.adapter.spi.Traced) ReferenceConflictException(org.projectnessie.versioned.ReferenceConflictException) PreparedStatement(java.sql.PreparedStatement) 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)

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