Search in sources :

Example 11 with DatastoreLockTarget

use of com.b2international.snowowl.core.internal.locks.DatastoreLockTarget in project snow-owl by b2ihealthcare.

the class DatastoreLockTests method testUnlockAll.

@Test
public void testUnlockAll() {
    final DatastoreLockContext context = createContext(USER, DatastoreLockContextDescriptions.MAINTENANCE);
    final DatastoreLockTarget target1 = new DatastoreLockTarget("snomedStore", "MAIN");
    final DatastoreLockTarget target2 = new DatastoreLockTarget("loincStore", "MAIN");
    manager.lock(context, TIMEOUT, target1, target2);
    checkIfLockExists(context, true, target1, target2);
    manager.unlockAll();
    checkIfLockExists(context, false, target1, target2);
}
Also used : DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) Test(org.junit.Test)

Example 12 with DatastoreLockTarget

use of com.b2international.snowowl.core.internal.locks.DatastoreLockTarget in project snow-owl by b2ihealthcare.

the class RepositoryRestService method unlockGlobal.

@Operation(summary = "Unlock all repositories", description = "Releases a previously acquired global lock.")
@ApiResponses({ @ApiResponse(responseCode = "204", description = "Unlock successful"), @ApiResponse(responseCode = "400", description = "Unspecified unlock-related issue") })
@ResponseStatus(HttpStatus.NO_CONTENT)
@PostMapping("/unlock")
public void unlockGlobal() {
    final DatastoreLockContext context = new DatastoreLockContext(User.SYSTEM.getUsername(), DatastoreLockContextDescriptions.CREATE_BACKUP);
    final DatastoreLockTarget target = DatastoreLockTarget.ALL;
    doUnlock(context, target);
}
Also used : DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 13 with DatastoreLockTarget

use of com.b2international.snowowl.core.internal.locks.DatastoreLockTarget in project snow-owl by b2ihealthcare.

the class DefaultOperationLockManager method canContextLockTargets.

@OverridingMethodsMustInvokeSuper
protected void canContextLockTargets(final DatastoreLockContext context, final Iterable<DatastoreLockTarget> targets, final Map<DatastoreLockTarget, DatastoreLockContext> alreadyLockedTargets) throws LockedException {
    if (!isDisposed()) {
        for (final DatastoreLockTarget newTarget : targets) {
            for (final IOperationLock existingLock : getExistingLocks()) {
                if (existingLock.targetConflicts(newTarget) && !canContextLock(context, existingLock)) {
                    alreadyLockedTargets.put(newTarget, existingLock.getContext());
                }
            }
        }
    } else {
        final DatastoreLockContext disposedContext = createLockContext(User.SYSTEM.getUsername(), DatastoreLockContextDescriptions.DISPOSE_LOCK_MANAGER, null);
        for (final DatastoreLockTarget target : targets) {
            alreadyLockedTargets.put(target, disposedContext);
        }
        throwLockedException(ACQUIRE_FAILED_MESSAGE, context, alreadyLockedTargets);
    }
}
Also used : DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) OverridingMethodsMustInvokeSuper(javax.annotation.OverridingMethodsMustInvokeSuper)

Example 14 with DatastoreLockTarget

use of com.b2international.snowowl.core.internal.locks.DatastoreLockTarget in project snow-owl by b2ihealthcare.

the class DefaultOperationLockManager method lock.

@Override
public void lock(final DatastoreLockContext context, final long timeoutMillis, final Iterable<DatastoreLockTarget> targets) throws LockedException {
    final Map<DatastoreLockTarget, DatastoreLockContext> alreadyLockedTargets = Maps.newHashMap();
    final long startTimeMillis = getCurrentTimeMillis();
    try {
        synchronized (syncObject) {
            while (true) {
                alreadyLockedTargets.clear();
                canContextLockTargets(context, targets, alreadyLockedTargets);
                if (alreadyLockedTargets.isEmpty()) {
                    for (final DatastoreLockTarget newTarget : targets) {
                        final IOperationLock existingLock = getOrCreateLock(context, newTarget);
                        fireTargetAcquired(existingLock.getTarget(), context);
                    }
                    syncObject.notifyAll();
                    return;
                }
                if (NO_TIMEOUT == timeoutMillis) {
                    syncObject.wait();
                } else {
                    final long remainingTimeoutMillis = timeoutMillis - (getCurrentTimeMillis() - startTimeMillis);
                    if (remainingTimeoutMillis < 1L) {
                        throwLockedException(ACQUIRE_FAILED_MESSAGE, context, alreadyLockedTargets);
                    } else {
                        syncObject.wait(remainingTimeoutMillis);
                    }
                }
            }
        }
    } catch (InterruptedException e) {
        throw new SnowowlRuntimeException(e);
    }
}
Also used : DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) DatastoreLockContext(com.b2international.snowowl.core.internal.locks.DatastoreLockContext) SnowowlRuntimeException(com.b2international.snowowl.core.api.SnowowlRuntimeException)

Example 15 with DatastoreLockTarget

use of com.b2international.snowowl.core.internal.locks.DatastoreLockTarget in project snow-owl by b2ihealthcare.

the class LocksCommand method parseLockTarget.

private static DatastoreLockTarget parseLockTarget(final String lockTargetOrAll) {
    if (ALL.equalsIgnoreCase(lockTargetOrAll)) {
        return DatastoreLockTarget.ALL;
    }
    final String repositoryId;
    final String path;
    String[] parts = lockTargetOrAll.split(":");
    if (parts.length == 2) {
        repositoryId = parts[0];
        path = parts[1];
    } else if (parts.length == 1) {
        repositoryId = parts[0];
        path = null;
    } else {
        return null;
    }
    final RepositoryManager repositoryManager = ApplicationContext.getInstance().getService(RepositoryManager.class);
    final Repository repository = repositoryManager.get(repositoryId);
    if (null == repository) {
        return null;
    }
    if (Strings.isNullOrEmpty(path)) {
        return new DatastoreLockTarget(repositoryId, null);
    }
    final IBranchPath branchPath = BranchPathUtils.createPath(path);
    // assuming active connection manager service here
    RevisionBranch branch = repository.service(BaseRevisionBranching.class).getBranch(path);
    if (null == branch) {
        return null;
    }
    return new DatastoreLockTarget(repositoryId, branchPath.getPath());
}
Also used : Repository(com.b2international.snowowl.core.Repository) RevisionBranch(com.b2international.index.revision.RevisionBranch) RepositoryManager(com.b2international.snowowl.core.RepositoryManager) DatastoreLockTarget(com.b2international.snowowl.core.internal.locks.DatastoreLockTarget) IBranchPath(com.b2international.snowowl.core.api.IBranchPath) BaseRevisionBranching(com.b2international.index.revision.BaseRevisionBranching)

Aggregations

DatastoreLockTarget (com.b2international.snowowl.core.internal.locks.DatastoreLockTarget)16 DatastoreLockContext (com.b2international.snowowl.core.internal.locks.DatastoreLockContext)14 Test (org.junit.Test)5 Operation (io.swagger.v3.oas.annotations.Operation)4 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)4 SnowowlRuntimeException (com.b2international.snowowl.core.api.SnowowlRuntimeException)2 IOperationLockManager (com.b2international.snowowl.core.locks.IOperationLockManager)2 CycleDetectedException (com.b2international.commons.exceptions.CycleDetectedException)1 IndexException (com.b2international.index.IndexException)1 BaseRevisionBranching (com.b2international.index.revision.BaseRevisionBranching)1 RevisionBranch (com.b2international.index.revision.RevisionBranch)1 Repository (com.b2international.snowowl.core.Repository)1 RepositoryManager (com.b2international.snowowl.core.RepositoryManager)1 IBranchPath (com.b2international.snowowl.core.api.IBranchPath)1 Locks (com.b2international.snowowl.core.locks.Locks)1 OverridingMethodsMustInvokeSuper (javax.annotation.OverridingMethodsMustInvokeSuper)1