Skip to content

Commit e1df39d

Browse files
committed
Fix some Sonar smells
1 parent 914c7d7 commit e1df39d

4 files changed

Lines changed: 30 additions & 31 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ private static Set<String> findResources(final Path path, final Logger logger) {
6767

6868
private static Set<String> findResourcesInJar(final Path path, final Logger logger) {
6969
final Set<String> jarResources = new HashSet<>();
70-
try {
71-
final JarFile jarFile = new JarFile(path.toFile());
70+
try (final JarFile jarFile = new JarFile(path.toFile())) {
7271
final Enumeration<JarEntry> entries = jarFile.entries();
7372
while (entries.hasMoreElements()) {
7473
final JarEntry jarEntry = entries.nextElement();

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ public class MicroJitterSampler {
3939
private final int[] count = new int[DELAY.length];
4040
private long totalTime = 0;
4141

42-
private static void pause() throws InterruptedException
43-
{
44-
if(BUSYWAIT) {
42+
private static void pause() throws InterruptedException {
43+
if (BUSYWAIT) {
4544
long now = System.nanoTime();
46-
while(System.nanoTime() - now < 1_000_000);
45+
while (System.nanoTime() - now < 1_000_000) ;
4746
} else {
4847
Thread.sleep(1);
4948
}
5049

5150
}
51+
5252
public static void main(String... ignored) throws InterruptedException {
5353
MicroJitterSampler sampler = new MicroJitterSampler();
5454

55-
Thread t = new Thread( sampler::run );
55+
Thread t = new Thread(sampler::run);
5656
t.start();
5757
t.join();
5858
}
@@ -71,7 +71,7 @@ private void once() throws InterruptedException {
7171
}
7272

7373
public void run() {
74-
if(CPU != -1)
74+
if (CPU != -1)
7575
Affinity.setAffinity(CPU);
7676

7777
try {
@@ -80,7 +80,7 @@ public void run() {
8080
while (!Thread.currentThread().isInterrupted()) {
8181
once();
8282

83-
if(first) {
83+
if (first) {
8484
reset();
8585
first = false;
8686
System.out.println("Warmup complete. Running jitter tests...");
@@ -89,7 +89,8 @@ public void run() {
8989

9090
print(System.out);
9191
}
92-
} catch(InterruptedException e) {
92+
} catch (InterruptedException e) {
93+
Thread.currentThread().interrupt();
9394
}
9495
}
9596

@@ -101,7 +102,7 @@ private static String asString(long timeNS) {
101102
}
102103

103104
void reset() {
104-
for(int i=0; i<DELAY.length; ++i)
105+
for (int i = 0; i < DELAY.length; ++i)
105106
count[i] = 0;
106107
totalTime = 0;
107108
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class FileLockBasedLockChecker implements LockChecker {
2727

2828
private static final int MAX_LOCK_RETRIES = 5;
29-
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
29+
private static final ThreadLocal<SimpleDateFormat> dfTL = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z"));
3030
private static final FileAttribute<Set<PosixFilePermission>> LOCK_FILE_ATTRIBUTES = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-rw-rw-"));
3131
private static final Set<OpenOption> LOCK_FILE_OPEN_OPTIONS = new HashSet<>(Arrays.asList(READ, WRITE, CREATE, SYNC));
3232
private static final Logger LOGGER = LoggerFactory.getLogger(FileLockBasedLockChecker.class);
@@ -54,7 +54,9 @@ public synchronized boolean isLockFree(int id) {
5454
// if we can acquire a shared lock, nobody has an exclusive lock
5555
try (final FileLock fileLock = channel.tryLock(0, Long.MAX_VALUE, true)) {
5656
if (fileLock != null && fileLock.isValid()) {
57-
lockFile.delete(); // clean up the orphaned lock file
57+
if (!lockFile.delete()) { // try and clean up the orphaned lock file
58+
LOGGER.debug("Couldn't delete orphaned lock file " + lockFile);
59+
}
5860
return true;
5961
} else {
6062
// another process has an exclusive lock
@@ -102,7 +104,7 @@ public synchronized boolean obtainLock(int id, String metaInfo) throws IOExcepti
102104
*/
103105
private LockReference tryAcquireLockOnFile(int id, String metaInfo) throws IOException, ConcurrentLockFileDeletionException {
104106
final File lockFile = toFile(id);
105-
final FileChannel fileChannel = FileChannel.open(lockFile.toPath(), LOCK_FILE_OPEN_OPTIONS, LOCK_FILE_ATTRIBUTES);
107+
final FileChannel fileChannel = FileChannel.open(lockFile.toPath(), LOCK_FILE_OPEN_OPTIONS, LOCK_FILE_ATTRIBUTES); // NOSONAR
106108
final FileLock fileLock = fileChannel.tryLock(0, Long.MAX_VALUE, false);
107109
if (fileLock == null) {
108110
// someone else has a lock (exclusive or shared), fail to acquire
@@ -122,7 +124,7 @@ private LockReference tryAcquireLockOnFile(int id, String metaInfo) throws IOExc
122124
}
123125

124126
private void writeMetaInfoToFile(FileChannel fc, String metaInfo) throws IOException {
125-
byte[] content = String.format("%s%n%s", metaInfo, df.format(new Date())).getBytes();
127+
byte[] content = String.format("%s%n%s", metaInfo, dfTL.get().format(new Date())).getBytes();
126128
ByteBuffer buffer = ByteBuffer.wrap(content);
127129
while (buffer.hasRemaining()) {
128130
fc.write(buffer);

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
* @author Tom Shercliff
1111
*/
1212
public class AffinityTestMain {
13-
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
1413

1514
public static void main(String[] args) {
1615

@@ -28,21 +27,19 @@ public static void main(String[] args) {
2827

2928
private static void acquireAndDoWork() {
3029

31-
Thread t = new Thread(new Runnable() {
32-
@Override
33-
public void run() {
34-
try (AffinityLock al = Affinity.acquireLock()) {
35-
String threadName = Thread.currentThread().getName();
36-
System.out.println("Thread (" + threadName + ") locked onto cpu " + al.cpuId());
37-
38-
while (true) {
39-
System.out.println(df.format(new Date()) + " - Thread (" + threadName + ") doing work on cpu " + al.cpuId() + ". IsAllocated = " + al.isAllocated() + ", isBound = " + al.isBound() + ". " + al.toString());
40-
41-
try {
42-
Thread.sleep(10000L);
43-
} catch (InterruptedException e) {
44-
//nothing
45-
}
30+
Thread t = new Thread(() -> {
31+
final SimpleDateFormat df = new SimpleDateFormat("yyyy.MM" + ".dd 'at' HH:mm:ss z");
32+
try (AffinityLock al = Affinity.acquireLock()) {
33+
String threadName = Thread.currentThread().getName();
34+
System.out.println("Thread (" + threadName + ") locked onto cpu " + al.cpuId());
35+
36+
while (true) {
37+
System.out.println(df.format(new Date()) + " - Thread (" + threadName + ") doing work on cpu " + al.cpuId() + ". IsAllocated = " + al.isAllocated() + ", isBound = " + al.isBound() + ". " + al.toString());
38+
39+
try {
40+
Thread.sleep(10000L);
41+
} catch (InterruptedException e) {
42+
//nothing
4643
}
4744
}
4845
}

0 commit comments

Comments
 (0)