Commit 04fdc3c8 authored by Costin Grigoras's avatar Costin Grigoras
Browse files

Use ThreadLocalRandom everywhere + other SpotBugs suggestions

parent 211d677c
......@@ -10,8 +10,8 @@ import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -206,7 +206,7 @@ public class JBoxServer extends Thread {
private UIPrintWriter out = null;
private void setShellPrintWriter(final OutputStream os, final String shelltype) {
if (shelltype.equals("jaliensh"))
if ("jaliensh".equals(shelltype))
out = new JShPrintWriter(os);
else
out = new XMLPrintWriter(os);
......@@ -271,7 +271,7 @@ public class JBoxServer extends Thread {
logger.info("Command options = " + optionNode.getTextContent());
}
if (sCmdValue != null && sCmdValue.equals("password")) {
if (sCmdValue != null && "password".equals(sCmdValue)) {
if (cmdOptions.get(0).equals(password)) {
os.write(passACK.getBytes());
......@@ -452,9 +452,7 @@ public class JBoxServer extends Thread {
// Get port range from config
final boolean portAny = ConfigUtils.getConfig().getb("port.range.any", true);
final Random rnd = new Random(System.nanoTime());
final int randomStartingPort = rnd.nextInt(1000);
final int randomStartingPort = ThreadLocalRandom.current().nextInt(1000);
final int portMin = ConfigUtils.getConfig().geti("port.range.start", 10100 + randomStartingPort);
final int portMax = ConfigUtils.getConfig().geti("port.range.end", portAny ? portMin + 1 : 200);
......
......@@ -4,8 +4,8 @@ import java.io.File;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -235,9 +235,7 @@ public class TomcatServer {
final boolean portAny = ConfigUtils.getConfig().getb("port.range.any", true);
final Random rnd = new Random(System.nanoTime());
final int randomStartingPort = rnd.nextInt(1000);
final int randomStartingPort = ThreadLocalRandom.current().nextInt(1000);
final int portMin = ConfigUtils.getConfig().geti("port.range.start", 13150 + randomStartingPort);
final int portMax = ConfigUtils.getConfig().geti("port.range.end", portMin + (portAny ? 1 : 200));
......
......@@ -62,6 +62,8 @@ public class MirrorLFN extends Request {
throw new SecurityException("You do not own this file: " + c + ", requester: " + getEffectiveRequester());
this.results = LFNUtils.mirrorLFN(this.path, this.ses, this.exses, this.qos, this.useGUID, this.attempts);
this.success = this.results != null ? this.results.size() : -1;
}
/**
......
......@@ -14,7 +14,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Vector;
......@@ -35,6 +34,7 @@ import alien.se.SEUtils;
import alien.user.AliEnPrincipal;
import alien.user.UserFactory;
import apmon.ApMon;
import io.netty.util.internal.ThreadLocalRandom;
import lazyj.DBFunctions;
import lazyj.DBFunctions.DBConnection;
import lazyj.Format;
......@@ -127,8 +127,6 @@ public class TransferBroker {
private long lastTimeNoWork = 0;
private final Random rnd = new Random(System.currentTimeMillis());
private DBFunctions dbCached = ConfigUtils.getDB("transfers");
private final ExpirationCache<String, Integer> maxTransfersCache = new ExpirationCache<>(1024);
......@@ -204,7 +202,7 @@ public class TransferBroker {
if (!dbCached.moveNext()) {
logger.log(Level.FINE, "There is no waiting transfer in the queue");
lastTimeNoWork = System.currentTimeMillis() + 30 * 1000 + rnd.nextInt(30 * 1000);
lastTimeNoWork = System.currentTimeMillis() + 30 * 1000 + ThreadLocalRandom.current().nextInt(30 * 1000);
return null;
}
......@@ -621,11 +619,10 @@ public class TransferBroker {
if (db.getUpdateCount() <= 0)
return;
if (!db.query("SELECT 1 FROM " + archiveTableName + " LIMIT 1;", true))
if (!db.query("CREATE TABLE " + archiveTableName + " LIKE TRANSFERS_DIRECT;")) {
logger.log(Level.SEVERE, "Exception creating the archive table " + archiveTableName);
return;
}
if (!db.query("SELECT 1 FROM " + archiveTableName + " LIMIT 1;", true) && !db.query("CREATE TABLE " + archiveTableName + " LIKE TRANSFERS_DIRECT;")) {
logger.log(Level.SEVERE, "Exception creating the archive table " + archiveTableName);
return;
}
}
try (DBFunctions db = ConfigUtils.getDB("transfers")) {
......@@ -743,7 +740,7 @@ public class TransferBroker {
final String prevStatus = db.gets(1);
if (db.moveNext() && prevStatus.equalsIgnoreCase("TRANSFERRING")) {
if (db.moveNext() && "TRANSFERRING".equalsIgnoreCase(prevStatus)) {
db.query("UPDATE TRANSFERS_DIRECT SET status='TRANSFERRING', reason='', finished=null WHERE transferId=" + t.getTransferId() + " AND status!='TRANSFERRING';");
if (db.getUpdateCount() > 0)
......
......@@ -13,8 +13,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
......@@ -77,14 +77,12 @@ public class Xrd3cpGW extends Xrootd {
}
}
private static Random rand = new Random(System.currentTimeMillis());
private static synchronized String getTransferServerInstance() {
updateServerList();
serverIdx = (serverIdx + 1) % transferServers.size();
return rand.nextInt(100000000) + "@" + transferServers.get(serverIdx);
return ThreadLocalRandom.current().nextInt(100000000) + "@" + transferServers.get(serverIdx);
}
/*
......
......@@ -4,13 +4,13 @@ import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
......@@ -45,19 +45,13 @@ public final class MonitorFactory {
private static Monitor systemMonitor = null;
private static final Random random = new Random(System.currentTimeMillis());
private static final ThreadFactory threadFactory = r -> {
final Thread t = new Thread(r);
private static final ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(final Runnable r) {
final Thread t = new Thread(r);
t.setName("alien.monitor.MonitorFactory - " + aiFactoryIndex.incrementAndGet());
t.setDaemon(true);
t.setName("alien.monitor.MonitorFactory - " + aiFactoryIndex.incrementAndGet());
t.setDaemon(true);
return t;
}
return t;
};
private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, threadFactory);
......@@ -121,7 +115,7 @@ public final class MonitorFactory {
final int interval = getConfigInt(component, "period", isJob() ? 120 : 60);
final ScheduledFuture<?> future = executor.scheduleAtFixedRate(m, random.nextInt(interval), interval, TimeUnit.SECONDS);
final ScheduledFuture<?> future = executor.scheduleAtFixedRate(m, ThreadLocalRandom.current().nextInt(interval), interval, TimeUnit.SECONDS);
m.future = future;
m.interval = interval;
......@@ -422,7 +416,7 @@ public final class MonitorFactory {
*
* @return the process id, if it can be determined, or <code>-1</code> if not
*/
public static final int getSelfProcessID() {
public static int getSelfProcessID() {
if (selfProcessID != 0)
return selfProcessID;
......
package alien.taskQueue;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -69,7 +69,7 @@ public class JobToken implements Comparable<JobToken> {
/**
* Create a new JobToken object
*
*
* @param queueId
* @param username
*/
......@@ -83,8 +83,6 @@ public class JobToken implements Comparable<JobToken> {
this.legacyToken = generateToken();
}
private static final Random ran = new Random(System.nanoTime());
/**
* @return a new, hopefully unique, job token
*/
......@@ -92,7 +90,7 @@ public class JobToken implements Comparable<JobToken> {
final char[] tok = new char[32];
for (int i = 0; i < 32; i++)
tok[i] = tokenStreet[ran.nextInt(tokenStreet.length)];
tok[i] = tokenStreet[ThreadLocalRandom.current().nextInt(tokenStreet.length)];
return new String(tok);
}
......@@ -166,7 +164,7 @@ public class JobToken implements Comparable<JobToken> {
logger.log(Level.INFO, "Replace JobToken for: " + queueId + " and exists: " + exists);
int resubmission_queue = TaskQueueUtils.getResubmission(Long.valueOf(queueId));
final int resubmission_queue = TaskQueueUtils.getResubmission(Long.valueOf(queueId));
if (resubmission_queue < 0) { // problem getting resubmission from QUEUE
logger.info("JobToken updateOrInsert: cannot retrieve resubmision");
......
......@@ -30,10 +30,10 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -109,8 +109,6 @@ public class JAKeyStore {
private static final String charString = "!0123456789abcdefghijklmnopqrstuvwxyz@#$%^&*()-+=_{}[]:;|?/>.,<";
private static final Random ran = new Random(System.nanoTime());
/**
*
*/
......@@ -489,16 +487,7 @@ public class JAKeyStore {
ks.store(fo, password);
f.deleteOnExit();
}
catch (final KeyStoreException e) {
e.printStackTrace();
}
catch (final NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (final CertificateException e) {
e.printStackTrace();
}
catch (final IOException e) {
catch (final KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
e.printStackTrace();
}
......@@ -647,7 +636,7 @@ public class JAKeyStore {
public static char[] getRandomString() {
final StringBuffer s = new StringBuffer(passLength);
for (int i = 0; i < passLength; i++) {
final int pos = ran.nextInt(charString.length());
final int pos = ThreadLocalRandom.current().nextInt(charString.length());
s.append(charString.charAt(pos));
}
......@@ -690,10 +679,8 @@ public class JAKeyStore {
if (!keystore_loaded)
loadKeyStore();
if (System.getenv("JALIEN_TOKEN_CERT") != null || tokenCertString != null) {
if (JAKeyStore.tokenCert != null) {
return JAKeyStore.tokenCert;
}
if ((System.getenv("JALIEN_TOKEN_CERT") != null || tokenCertString != null) && (JAKeyStore.tokenCert != null)) {
return JAKeyStore.tokenCert;
}
if (JAKeyStore.clientCert != null) {
......@@ -812,7 +799,7 @@ public class JAKeyStore {
pwritercert.write(tokReply.getCertificateAsString());
pwriterkey.write(tokReply.getPrivateKeyAsString());
}
catch (IOException e) {
catch (final IOException e) {
logger.log(Level.SEVERE, "Exception writing token content to files", e);
return false;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment