Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing write lock contents #107

Merged
merged 2 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/main/java/com/upserve/uppend/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.*;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
Expand Down Expand Up @@ -80,10 +81,21 @@ abstract class FileStore<T extends Partition> implements AutoCloseable, Register

this.readOnly = readOnly;
lockPath = readOnly ? dir.resolve("readLock") : dir.resolve("writeLock");

try {
lockChan = FileChannel.open(lockPath, StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
lock = readOnly ? lockChan.lock(0L, Long.MAX_VALUE, true) : lockChan.lock(); // Write lock is exclusive
if(readOnly) {
// this is a readLock
lock = lockChan.lock(0L, Long.MAX_VALUE, true);
} else {
// this is an exclusive writeLock
lock = lockChan.lock();
String writeLockContentString = builder.getWriteLockContentString();
if(writeLockContentString != null) {
ByteBuffer byteBuf = ByteBuffer.wrap(writeLockContentString.getBytes());
lockChan.write(byteBuf);
lockChan.force(false);
}
}
} catch (IOException e) {
throw new UncheckedIOException("unable to open lock: " + lockPath, e);
} catch (OverlappingFileLockException e) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/upserve/uppend/FileStoreBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class FileStoreBuilder<T extends FileStoreBuilder<T>> {

private int targetBufferSize = TARGET_PRODUCTION_BUFFER_SIZE;

private String writeLockContentString = null;

// Store Options
public static final int DEFAULT_FLUSH_DELAY_SECONDS = 30;
public static final int DEFAULT_FLUSH_THRESHOLD = 1000;
Expand Down Expand Up @@ -76,6 +78,12 @@ public T withTargetBufferSize(int targetBufferSize) {
return (T) this;
}

@SuppressWarnings("unchecked")
public T withWriteLockContentString(String writeLockContentString) {
this.writeLockContentString = writeLockContentString;
return (T) this;
}

// Append Store Options
@SuppressWarnings("unchecked")
public T withStoreName(String storeName) {
Expand Down Expand Up @@ -179,6 +187,8 @@ public String getStoreName() {

public String getMetricsRootName(){ return metricsRootName; }

public String getWriteLockContentString() { return writeLockContentString; }

public LookupDataMetrics.Adders getLookupDataMetricsAdders(){ return lookupDataMetricsAdders; }

public MutableBlobStoreMetrics.Adders getMutableBlobStoreMetricsAdders() { return mutableBlobStoreMetricsAdders; }
Expand All @@ -203,6 +213,7 @@ public String toString() {
", storeMetrics=" + storeMetrics +
", cacheMetricsRegistry=" + cacheMetricsRegistry +
", cacheMetrics=" + cacheMetrics +
", writeLockContentString='" + writeLockContentString + "'" +
'}';
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/upserve/uppend/FileStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.nio.ByteBuffer;
import java.nio.file.*;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -197,6 +199,28 @@ public void testCreateDirectoriesWithParentSymlink() throws Exception {
SafeDeleting.removeDirectory(base);
}

@Test
public void testLockFileAndContent_readWrite() throws IOException {
String expectedWriteLockContent = "write lock message";
MyFileStore v = new MyFileStore(path, 1, expectedWriteLockContent);
Path writeLockFilePath = Paths.get(path.toString(), "writeLock");
try (FileReader fr = new FileReader(writeLockFilePath.toString());
BufferedReader br = new BufferedReader(fr)) {
String writeLockContent = br.readLine();
assertTrue(Pattern.matches(expectedWriteLockContent, writeLockContent));
}
SafeDeleting.removeDirectory(path);
}

@Test
public void testLockFileAndContent_readOnly() throws IOException {
MyFileStore v = new MyFileStore(path, 1, true);
Path readLockFilePath = Paths.get(path.toString(), "readLock");
File file = readLockFilePath.toFile();
assertEquals(0, file.length());
SafeDeleting.removeDirectory(path);
}

@Test
public void testReaderWriter() throws InterruptedException {

Expand Down Expand Up @@ -253,5 +277,14 @@ private class MyFileStore extends FileAppendOnlyStore {
.withMetadataTTL(30)
);
}

MyFileStore(Path dir, int numPartitions, String writeLockContentString) {
super(false, new AppendOnlyStoreBuilder()
.withDir(dir)
.withPartitionCount(numPartitions)
.withMetadataTTL(30)
.withWriteLockContentString(writeLockContentString)
);
}
}
}