Skip to content

Commit b9248a1

Browse files
committed
Small enhancements
1 parent 83bd27b commit b9248a1

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

affinity/src/main/java/net/openhft/affinity/AffinityLock.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ private static BitSet getReservedAffinity0() {
121121
reserverable.andNot(BASE_AFFINITY);
122122
if (reserverable.isEmpty() && PROCESSORS > 1) {
123123
LoggerFactory.getLogger(AffinityLock.class).info("No isolated CPUs found, so assuming CPUs 1 to {} available.", (PROCESSORS - 1));
124-
reserverable = new BitSet(PROCESSORS);
125-
// make the first CPU unavailable
126-
reserverable.set(1, PROCESSORS, true);
127-
reserverable.set(0, false);
124+
// make all but first CPUs available
125+
reserverable.set(1, PROCESSORS);
128126
return reserverable;
129127
}
130128
return reserverable;

affinity/src/main/java/net/openhft/affinity/lockchecker/FileBasedLockChecker.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
import java.text.SimpleDateFormat;
99
import java.util.Date;
1010

11-
/**
12-
* @author Tom Shercliff
13-
*/
11+
import static java.nio.charset.StandardCharsets.UTF_8;
12+
13+
@SuppressWarnings("ResultOfMethodCallIgnored")
1414
public class FileBasedLockChecker implements LockChecker {
1515

1616
private static final Logger LOGGER = LoggerFactory.getLogger(FileBasedLockChecker.class);
17-
protected static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
17+
static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
1818

1919
private static final LockChecker instance = new FileBasedLockChecker();
20+
2021
public static LockChecker getInstance() {
2122
return instance;
2223
}
@@ -36,7 +37,7 @@ public boolean obtainLock(int id, String metaInfo) throws IOException {
3637
file.delete();
3738

3839
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
39-
new FileOutputStream(file, false), "utf-8"))) {
40+
new FileOutputStream(file, false), UTF_8))) {
4041
writer.write(metaInfo + "\n" + df.format(new Date()));
4142
file.setWritable(true, false);
4243
file.setExecutable(false, false);
@@ -53,11 +54,11 @@ public boolean releaseLock(int id) {
5354
public String getMetaInfo(int id) throws IOException {
5455
File file = toFile(id);
5556

56-
if(!file.exists()) {
57+
if (!file.exists()) {
5758
return null;
5859
}
5960

60-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"))) {
61+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), UTF_8))) {
6162
final String firstLine = reader.readLine();
6263
if (firstLine == null) {
6364
LOGGER.error(String.format("Empty lock file %s%n", file.getAbsolutePath()));
@@ -72,7 +73,7 @@ protected File toFile(int id) {
7273
return new File(tmpDir(), "cpu-" + id + ".lock");
7374
}
7475

75-
static File tmpDir() {
76+
private static File tmpDir() {
7677
final File tempDir = new File(System.getProperty("java.io.tmpdir"));
7778

7879
if (!tempDir.exists())

affinity/src/main/java/net/openhft/affinity/lockchecker/FileLockBasedLockChecker.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7-
import java.io.*;
7+
import java.io.File;
8+
import java.io.IOException;
89
import java.nio.ByteBuffer;
910
import java.nio.channels.FileChannel;
1011
import java.nio.channels.FileLock;
1112
import java.nio.channels.OverlappingFileLockException;
1213
import java.nio.file.Files;
1314
import java.nio.file.StandardOpenOption;
15+
import java.nio.file.attribute.FileAttribute;
1416
import java.nio.file.attribute.PosixFilePermission;
1517
import java.nio.file.attribute.PosixFilePermissions;
1618
import java.util.Arrays;
1719
import java.util.Date;
1820
import java.util.HashSet;
21+
import java.util.Set;
1922

2023
import static java.nio.file.StandardOpenOption.*;
2124
import static net.openhft.affinity.impl.VanillaCpuLayout.MAX_CPUS_SUPPORTED;
2225

23-
/**
24-
* @author Tom Shercliff
25-
*/
2626
public class FileLockBasedLockChecker extends FileBasedLockChecker {
2727

2828
private static final Logger LOGGER = LoggerFactory.getLogger(FileLockBasedLockChecker.class);
2929
private static final String OS = System.getProperty("os.name").toLowerCase();
3030

3131
private static final LockChecker instance = new FileLockBasedLockChecker();
32+
private static final HashSet<StandardOpenOption> openOptions = new HashSet<>(Arrays.asList(CREATE_NEW, WRITE, READ, SYNC));
33+
private static final FileAttribute<Set<PosixFilePermission>> fileAttr = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-rw-rw-"));
34+
3235
public static LockChecker getInstance() {
3336
return instance;
3437
}
@@ -46,13 +49,13 @@ public boolean isLockFree(int id) {
4649

4750
private boolean isLockFree(File file, int id) {
4851
//if no file exists - nobody has the lock for sure
49-
if(!file.exists()) {
52+
if (!file.exists()) {
5053
return true;
5154
}
5255

5356
//do we have the lock already?
5457
LockReference existingLock = locks[id];
55-
if(existingLock != null) {
58+
if (existingLock != null) {
5659
return false;
5760
}
5861

@@ -77,16 +80,14 @@ private boolean isLockFree(File file, int id) {
7780
@Override
7881
public boolean obtainLock(int id, String metaInfo) throws IOException {
7982
final File file = toFile(id);
80-
if(!isLockFree(file, id)) {
83+
if (!isLockFree(file, id)) {
8184
return false;
8285
}
8386

84-
FileChannel fc = FileChannel.open(file.toPath(),
85-
new HashSet<>(Arrays.asList(CREATE_NEW, WRITE, READ, SYNC)),
86-
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-rw-rw-")));
87+
FileChannel fc = FileChannel.open(file.toPath(), openOptions, fileAttr);
8788
FileLock fl = fc.tryLock();
8889

89-
if(fl == null) {
90+
if (fl == null) {
9091
LOGGER.error(String.format("Could not obtain lock on file %s%n", file.getAbsolutePath()));
9192
return false;
9293
} else {
@@ -95,7 +96,7 @@ public boolean obtainLock(int id, String metaInfo) throws IOException {
9596

9697
byte[] content = String.format("%s%n%s", metaInfo, df.format(new Date())).getBytes();
9798
ByteBuffer buffer = ByteBuffer.wrap(content);
98-
while(buffer.hasRemaining()) {
99+
while (buffer.hasRemaining()) {
99100
fc.write(buffer);
100101
}
101102
return true;
@@ -105,7 +106,7 @@ public boolean obtainLock(int id, String metaInfo) throws IOException {
105106
@Override
106107
public boolean releaseLock(int id) {
107108
LockReference lock = locks[id];
108-
if(lock == null) {
109+
if (lock == null) {
109110
LOGGER.error(String.format("Cannot release lock for id %d as don't have it!", id));
110111
return false;
111112
}
@@ -125,20 +126,20 @@ public boolean releaseLock(int id) {
125126
@Override
126127
public String getMetaInfo(int id) throws IOException {
127128
final File file = toFile(id);
128-
if(isLockFree(file, id)) {
129+
if (isLockFree(file, id)) {
129130
LOGGER.warn("Cannot obtain lock on lock file {}", file.getAbsolutePath());
130131
return null;
131132
}
132133

133134
LockReference lr = locks[id];
134-
if(lr == null) {
135+
if (lr == null) {
135136
return null;
136137
}
137138
FileChannel fc = lr.channel;
138139
ByteBuffer buffer = ByteBuffer.allocate(64);
139140
int len = fc.read(buffer, 0);
140141
String content = new String(buffer.array(), 0, len);
141-
if (content == null || content.isEmpty()) {
142+
if (content.isEmpty()) {
142143
LOGGER.warn("Empty lock file {}", file.getAbsolutePath());
143144
return null;
144145
}
@@ -150,7 +151,7 @@ public String getMetaInfo(int id) throws IOException {
150151
protected File toFile(int id) {
151152
File file = super.toFile(id);
152153
try {
153-
if(file.exists() && OS.startsWith("linux")) {
154+
if (file.exists() && OS.startsWith("linux")) {
154155
Files.setPosixFilePermissions(file.toPath(), PosixFilePermissions.fromString("rwxrwxrwx"));
155156
}
156157
} catch (IOException e) {

0 commit comments

Comments
 (0)