Skip to content

Commit

Permalink
Refactor Remote Store Metadata Lock Manager Utils
Browse files Browse the repository at this point in the history
Signed-off-by: Harish Bhakuni <hbhakuni@amazon.com>
  • Loading branch information
Harish Bhakuni committed Oct 5, 2023
1 parent 2965e69 commit 56ff047
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.opensearch.index.store.lockmanager;

import java.nio.file.NoSuchFileException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -51,11 +50,11 @@ String getLockPrefix() {
return fileToLock + RemoteStoreLockManagerUtils.SEPARATOR;
}

String getLockForAcquirer(String[] lockFiles) throws NoSuchFileException {
String getLockForAcquirer(List<String> lockFiles) throws NoSuchFileException {
if (acquirerId == null || acquirerId.isBlank()) {
throw new IllegalArgumentException("Acquirer ID should be provided");
}
List<String> locksForAcquirer = Arrays.stream(lockFiles)
List<String> locksForAcquirer = lockFiles.stream()
.filter(lockFile -> acquirerId.equals(LockFileUtils.getAcquirerIdFromLock(lockFile)))
.collect(Collectors.toList());

Expand Down Expand Up @@ -88,21 +87,18 @@ static String generateLockName(String fileToLock, String acquirerId) {
}

public static String getFileToLockNameFromLock(String lockName) {
String[] lockNameTokens = lockName.split(RemoteStoreLockManagerUtils.SEPARATOR);

if (lockNameTokens.length != 2) {
if (!lockName.endsWith(".lock")) {
throw new IllegalArgumentException("Provided Lock Name " + lockName + " is not Valid.");
}
return lockNameTokens[0];
return lockName.replace(getAcquirerIdFromLock(lockName) + RemoteStoreLockManagerUtils.LOCK_FILE_EXTENSION, "");
}

public static String getAcquirerIdFromLock(String lockName) {
String[] lockNameTokens = lockName.split(RemoteStoreLockManagerUtils.SEPARATOR);

if (lockNameTokens.length != 2) {
if (!lockName.endsWith(".lock")) {
throw new IllegalArgumentException("Provided Lock Name " + lockName + " is not Valid.");
}
return lockNameTokens[1].replace(RemoteStoreLockManagerUtils.LOCK_FILE_EXTENSION, "");
String[] lockNameTokens = lockName.split(RemoteStoreLockManagerUtils.SEPARATOR);
return lockNameTokens[lockNameTokens.length - 1].replace(RemoteStoreLockManagerUtils.LOCK_FILE_EXTENSION, "");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class RemoteStoreLockManagerUtils {
static final String FILE_TO_LOCK_NAME = "file_to_lock";
static final String SEPARATOR = "___";
static final String SEPARATOR = "____";
static final String LOCK_FILE_EXTENSION = ".lock";
static final String ACQUIRER_ID = "acquirer_id";
public static final String NO_TTL = "-1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* A Class that implements Remote Store Lock Manager by creating lock files for the remote store files that needs to
Expand Down Expand Up @@ -60,7 +63,7 @@ public void acquire(LockInfo lockInfo) throws IOException {
@Override
public void release(LockInfo lockInfo) throws IOException {
assert lockInfo instanceof FileLockInfo : "lockInfo should be instance of FileLockInfo";
String[] lockFiles = lockDirectory.listAll();
List<String> lockFiles = Arrays.stream(lockDirectory.listAll()).filter(file -> file.endsWith(".lock")).collect(Collectors.toList());
try {
String lockToRelease = ((FileLockInfo) lockInfo).getLockForAcquirer(lockFiles);
lockDirectory.deleteFile(lockToRelease);
Expand Down Expand Up @@ -98,7 +101,7 @@ public void cloneLock(LockInfo originalLockInfo, LockInfo clonedLockInfo) throws
String originalResourceId = Objects.requireNonNull(((FileLockInfo) originalLockInfo).getAcquirerId());
String clonedResourceId = Objects.requireNonNull(((FileLockInfo) clonedLockInfo).getAcquirerId());
assert originalResourceId != null && clonedResourceId != null : "provided resourceIds should not be null";
String[] lockFiles = lockDirectory.listAll();
List<String> lockFiles = Arrays.stream(lockDirectory.listAll()).filter(file -> file.endsWith(".lock")).collect(Collectors.toList());
String lockNameForAcquirer = ((FileLockInfo) originalLockInfo).getLockForAcquirer(lockFiles);
String fileToLockName = FileLockInfo.LockFileUtils.getFileToLockNameFromLock(lockNameForAcquirer);
acquire(FileLockInfo.getLockInfoBuilder().withFileToLock(fileToLockName).withAcquirerId(clonedResourceId).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.opensearch.test.OpenSearchTestCase;

import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.List;

public class FileLockInfoTests extends OpenSearchTestCase {
String testMetadata = "testMetadata";
Expand Down Expand Up @@ -42,11 +44,10 @@ public void testGetLockPrefixFailureCase() {
}

public void testGetLocksForAcquirer() throws NoSuchFileException {
String[] locks = new String[] {
FileLockInfo.LockFileUtils.generateLockName(testMetadata, testAcquirerId),
FileLockInfo.LockFileUtils.generateLockName(testMetadata, "acquirerId2") };
List<String> locks = new ArrayList<>();
locks.add(FileLockInfo.LockFileUtils.generateLockName(testMetadata, testAcquirerId));
locks.add(FileLockInfo.LockFileUtils.generateLockName(testMetadata, "acquirerId2"));
FileLockInfo fileLockInfo = FileLockInfo.getLockInfoBuilder().withAcquirerId(testAcquirerId).build();

assertEquals(fileLockInfo.getLockForAcquirer(locks), FileLockInfo.LockFileUtils.generateLockName(testMetadata, testAcquirerId));
}

Expand Down

0 comments on commit 56ff047

Please sign in to comment.