Skip to content

Commit

Permalink
Merge pull request #107 from upserve/write_lock_contents
Browse files Browse the repository at this point in the history
Testing write lock contents
  • Loading branch information
jeffrey-a-meunier authored Sep 18, 2019
2 parents 7d8e6fa + 75877e8 commit a5cbe7f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
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)
);
}
}
}

0 comments on commit a5cbe7f

Please sign in to comment.